unique_answer.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File containing the UNIQUE_ANSWER class.
  5. * @package chamilo.exercise
  6. * @author Eric Marguin
  7. */
  8. /**
  9. * Code
  10. */
  11. if(!class_exists('UniqueAnswer')):
  12. /**
  13. CLASS UNIQUE_ANSWER
  14. *
  15. * This class allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  16. * extending the class question
  17. *
  18. * @author Eric Marguin
  19. * @author Julio Montoya
  20. * @package chamilo.exercise
  21. **/
  22. class UniqueAnswer extends Question {
  23. static $typePicture = 'mcua.gif';
  24. static $explanationLangVar = 'UniqueSelect';
  25. /**
  26. * Constructor
  27. */
  28. function UniqueAnswer() {
  29. //this is highly important
  30. parent::question();
  31. $this -> type = UNIQUE_ANSWER;
  32. $this -> isContent = $this-> getIsContent();
  33. }
  34. /**
  35. * function which redifines Question::createAnswersForm
  36. * @param the formvalidator instance
  37. * @param the answers number to display
  38. */
  39. function createAnswersForm ($form) {
  40. // Getting the exercise list
  41. $obj_ex = $_SESSION['objExercise'];
  42. $editor_config = array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '125');
  43. //this line define how many question by default appear when creating a choice question
  44. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; // The previous default value was 2. See task #1759.
  45. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  46. /*
  47. Types of Feedback
  48. $feedback_option[0]=get_lang('Feedback');
  49. $feedback_option[1]=get_lang('DirectFeedback');
  50. $feedback_option[2]=get_lang('NoFeedback');
  51. */
  52. $feedback_title='';
  53. $comment_title='';
  54. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  55. $comment_title = '<th>'.get_lang('Comment').'</th>';
  56. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  57. //Scenario
  58. $editor_config['Width'] = '250';
  59. $editor_config['Height'] = '110';
  60. $comment_title = '<th width="500px" >'.get_lang('Comment').'</th>';
  61. $feedback_title = '<th width="350px" >'.get_lang('Scenario').'</th>';
  62. }
  63. $html='<table class="data_table">
  64. <tr style="text-align: center;">
  65. <th width="10px">
  66. '.get_lang('Number').'
  67. </th>
  68. <th width="10px" >
  69. '.get_lang('True').'
  70. </th>
  71. <th width="50%">
  72. '.get_lang('Answer').'
  73. </th>
  74. '.$comment_title.'
  75. '.$feedback_title.'
  76. <th width="50px">
  77. '.get_lang('Weighting').'
  78. </th>
  79. </tr>';
  80. $form -> addElement ('label', get_lang('Answers').'<br /> <img src="../img/fill_field.png">', $html);
  81. $defaults = array();
  82. $correct = 0;
  83. if(!empty($this -> id)) {
  84. $answer = new Answer($this -> id);
  85. $answer -> read();
  86. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  87. $nb_answers = $answer->nbrAnswers;
  88. }
  89. }
  90. $form -> addElement('hidden', 'nb_answers');
  91. //Feedback SELECT
  92. $question_list = $obj_ex->selectQuestionList();
  93. $select_question=array();
  94. $select_question[0] = get_lang('SelectTargetQuestion');
  95. require_once '../newscorm/learnpathList.class.php';
  96. if (is_array($question_list)) {
  97. foreach ($question_list as $key=>$questionid) {
  98. //To avoid warning messages
  99. if (!is_numeric($questionid)) {
  100. continue;
  101. }
  102. $question = Question::read($questionid);
  103. $select_question[$questionid]='Q'.$key.' :'.cut($question->selectTitle(),20);
  104. }
  105. }
  106. $select_question[-1] = get_lang('ExitTest');
  107. $list = new LearnpathList(api_get_user_id());
  108. $flat_list = $list->get_flat_list();
  109. $select_lp_id=array();
  110. $select_lp_id[0]=get_lang('SelectTargetLP');
  111. foreach ($flat_list as $id => $details) {
  112. $select_lp_id[$id] = cut($details['lp_name'],20);
  113. }
  114. $temp_scenario = array();
  115. if ($nb_answers < 1) {
  116. $nb_answers = 1;
  117. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  118. }
  119. for ($i = 1 ; $i <= $nb_answers ; ++$i) {
  120. $form -> addElement ('html', '<tr>');
  121. if (isset($answer) && is_object($answer)) {
  122. if ($answer -> correct[$i]) {
  123. $correct = $i;
  124. }
  125. $defaults['answer['.$i.']'] = $answer->answer[$i];
  126. $defaults['comment['.$i.']'] = $answer->comment[$i];
  127. $defaults['weighting['.$i.']'] = float_format($answer->weighting[$i], 1);
  128. $item_list=explode('@@',$answer -> destination[$i]);
  129. $try = $item_list[0];
  130. $lp = $item_list[1];
  131. $list_dest = $item_list[2];
  132. $url = $item_list[3];
  133. if ($try==0) {
  134. $try_result = 0;
  135. } else {
  136. $try_result = 1;
  137. }
  138. if ($url==0) {
  139. $url_result='';
  140. } else {
  141. $url_result = $url;
  142. }
  143. $temp_scenario['url'.$i] = $url_result;
  144. $temp_scenario['try'.$i] = $try_result;
  145. $temp_scenario['lp'.$i] = $lp;
  146. $temp_scenario['destination'.$i]= $list_dest;
  147. } else {
  148. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  149. $defaults['weighting[1]'] = 10;
  150. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  151. $defaults['weighting[2]'] = 0;
  152. $temp_scenario['destination'.$i] = array('0');
  153. $temp_scenario['lp'.$i] = array('0');
  154. }
  155. $defaults['scenario'] = $temp_scenario;
  156. $renderer = $form->defaultRenderer();
  157. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct');
  158. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  159. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  160. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  161. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  162. $answer_number = $form->addElement('text', 'counter['.$i.']',null,' value = "'.$i.'"');
  163. $answer_number->freeze();
  164. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  165. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  166. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  167. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  168. // feedback
  169. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  170. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  171. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  172. // Direct feedback
  173. //Adding extra feedback fields
  174. $group = array();
  175. $group['try'.$i] = $form->createElement('checkbox', 'try'.$i, null , get_lang('TryAgain'));
  176. $group['lp'.$i] = $form->createElement('select', 'lp'.$i, get_lang('SeeTheory').': ', $select_lp_id);
  177. $group['destination'.$i]= $form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' , $select_question);
  178. $group['url'.$i] = $form->createElement('text', 'url'.$i, get_lang('Other').': ', array('class'=>'span2', 'placeholder' => get_lang('Other')));
  179. $form->addGroup($group, 'scenario');
  180. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}', 'scenario');
  181. }
  182. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0'));
  183. $form->addElement ('html', '</tr>');
  184. }
  185. $form -> addElement ('html', '</table>');
  186. $form -> addElement ('html', '<br />');
  187. $navigator_info = api_get_navigator();
  188. global $text, $class;
  189. //ie6 fix
  190. if ($obj_ex->edit_exercise_in_lp == true) {
  191. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  192. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  193. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  194. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  195. } else {
  196. //setting the save button here and not in the question class.php
  197. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  198. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  199. $form->addElement('style_submit_button', 'submitQuestion',$text, 'class="'.$class.'"');
  200. }
  201. }
  202. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  203. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  204. $renderer->setElementTemplate('{element}&nbsp;','moreAnswers');
  205. $form->addElement('html', '</div></div>');
  206. //We check the first radio button to be sure a radio button will be check
  207. if ($correct==0) {
  208. $correct=1;
  209. }
  210. $defaults['correct'] = $correct;
  211. if (!empty($this -> id)) {
  212. $form -> setDefaults($defaults);
  213. } else {
  214. if ($this -> isContent == 1) {
  215. $form -> setDefaults($defaults);
  216. }
  217. }
  218. $form->setConstants(array('nb_answers' => $nb_answers));
  219. }
  220. /**
  221. * abstract function which creates the form to create / edit the answers of the question
  222. * @param the formvalidator instance
  223. * @param the answers number to display
  224. */
  225. function processAnswersCreation($form) {
  226. $questionWeighting = $nbrGoodAnswers = 0;
  227. $correct = $form -> getSubmitValue('correct');
  228. $objAnswer = new Answer($this->id);
  229. $nb_answers = $form->getSubmitValue('nb_answers');
  230. for ($i=1 ; $i <= $nb_answers ; $i++) {
  231. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  232. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  233. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  234. $scenario = $form -> getSubmitValue('scenario');
  235. //$list_destination = $form -> getSubmitValue('destination'.$i);
  236. //$destination_str = $form -> getSubmitValue('destination'.$i);
  237. $try = $scenario['try'.$i];
  238. $lp = $scenario['lp'.$i];
  239. $destination = $scenario['destination'.$i];
  240. $url = trim($scenario['url'.$i]);
  241. /*
  242. How we are going to parse the destination value
  243. here we parse the destination value which is a string
  244. 1@@3@@2;4;4;@@http://www.chamilo.org
  245. where: try_again@@lp_id@@selected_questions@@url
  246. try_again = is 1 || 0
  247. lp_id = id of a learning path (0 if dont select)
  248. selected_questions= ids of questions
  249. url= an url
  250. $destination_str='';
  251. foreach ($list_destination as $destination_id)
  252. {
  253. $destination_str.=$destination_id.';';
  254. }*/
  255. $goodAnswer = ($correct == $i) ? true : false;
  256. if ($goodAnswer) {
  257. $nbrGoodAnswers++;
  258. $weighting = abs($weighting);
  259. if($weighting > 0) {
  260. $questionWeighting += $weighting;
  261. }
  262. }
  263. if (empty($try)) {
  264. $try = 0;
  265. }
  266. if (empty($lp)) {
  267. $lp = 0;
  268. }
  269. if (empty($destination)) {
  270. $destination=0;
  271. }
  272. if ($url=='') {
  273. $url=0;
  274. }
  275. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  276. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  277. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i, null, null, $dest);
  278. }
  279. // saves the answers into the data base
  280. $objAnswer->save();
  281. // sets the total weighting of the question
  282. $this->updateWeighting($questionWeighting);
  283. $this->save();
  284. }
  285. function return_header($feedback_type = null, $counter = null, $score = null) {
  286. $header = parent::return_header($feedback_type, $counter, $score);
  287. $header .= '<table class="'.$this->question_table_class .'">
  288. <tr>
  289. <th>'.get_lang("Choice").'</th>
  290. <th>'. get_lang("ExpectedChoice").'</th>
  291. <th>'. get_lang("Answer").'</th>';
  292. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  293. $header .= '<th>'.get_lang("Comment").'</th>';
  294. } else {
  295. $header .= '<th>&nbsp;</th>';
  296. }
  297. $header .= '</tr>';
  298. return $header;
  299. }
  300. function create_answer($id, $question_id, $answer_title, $comment, $score = 0, $correct = 0) {
  301. $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  302. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  303. $course_id = api_get_course_int_id();
  304. $position = 1;
  305. $question_id = filter_var($question_id,FILTER_SANITIZE_NUMBER_INT);
  306. $score = filter_var($score,FILTER_SANITIZE_NUMBER_FLOAT);
  307. $correct = filter_var($correct,FILTER_SANITIZE_NUMBER_INT);
  308. // Get the max position
  309. $sql = "SELECT max(position) as max_position FROM $tbl_quiz_answer "
  310. ." WHERE c_id = $course_id AND question_id = $question_id";
  311. $rs_max = Database::query($sql);
  312. $row_max = Database::fetch_object($rs_max);
  313. $position = $row_max->max_position + 1;
  314. // Insert a new answer
  315. $sql = "INSERT INTO $tbl_quiz_answer "
  316. ."(c_id, id, question_id,answer,correct,comment,ponderation,position,destination)"
  317. ."VALUES ($course_id, $id,$question_id,'".Database::escape_string($answer_title)."',"
  318. ."$correct,'".Database::escape_string($comment)."',$score,$position, "
  319. ." '0@@0@@0@@0')";
  320. $rs = Database::query($sql);
  321. if ($correct) {
  322. $sql = "UPDATE $tbl_quiz_question "
  323. ." SET ponderation = (ponderation + $score) WHERE c_id = $course_id AND id = ".$question_id;
  324. $rs = Database::query($sql);
  325. }
  326. }
  327. }
  328. endif;