multiple_answer_true_false.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * CLASS MultipleAnswer
  5. *
  6. * This class allows to instantiate an object of type MULTIPLE_ANSWER
  7. * (MULTIPLE CHOICE, MULTIPLE ANSWER), extending the class question
  8. * @author Julio Montoya
  9. * @package chamilo.exercise
  10. **/
  11. /**
  12. * Code
  13. */
  14. /**
  15. * Class
  16. * @package chamilo.exercise
  17. */
  18. class MultipleAnswerTrueFalse extends Question
  19. {
  20. static $typePicture = 'mcmao.gif';
  21. static $explanationLangVar = 'MultipleAnswerTrueFalse';
  22. var $options;
  23. /**
  24. * Constructor
  25. */
  26. public function MultipleAnswerTrueFalse($course_code = null)
  27. {
  28. parent::question($course_code);
  29. $this->type = MULTIPLE_ANSWER_TRUE_FALSE;
  30. $this->isContent = $this->getIsContent();
  31. $this->options = array(1 => get_lang('True'), 2 => get_lang('False'), 3 => get_lang('DoubtScore'));
  32. }
  33. /**
  34. * function which redefines Question::createAnswersForm
  35. * @param FormValidator instance
  36. * @param the answers number to display
  37. */
  38. public function createAnswersForm($form)
  39. {
  40. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4; // The previous default value was 2. See task #1759.
  41. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  42. $course_id = api_get_course_int_id();
  43. $obj_ex = $this->exercise;
  44. $renderer = & $form->defaultRenderer();
  45. $defaults = array();
  46. $html = '<table class="data_table">
  47. <tr style="text-align: center;">
  48. <th>
  49. '.get_lang('Number').'
  50. </th>
  51. <th>
  52. '.get_lang('True').'
  53. </th>
  54. <th>
  55. '.get_lang('False').'
  56. </th>
  57. <th>
  58. '.get_lang('Answer').'
  59. </th>';
  60. // show column comment when feedback is enable
  61. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  62. $html .= '<th>'.get_lang('Comment').'</th>';
  63. }
  64. $html .= '</tr>';
  65. $form->addElement('label', get_lang('Answers').'<br />'.Display::return_icon('fill_field.png'), $html);
  66. $correct = 0;
  67. if (!empty($this->id)) {
  68. $answer = new Answer($this->id);
  69. $answer->read();
  70. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  71. $nb_answers = $answer->nbrAnswers;
  72. }
  73. }
  74. $form->addElement('hidden', 'nb_answers');
  75. $boxes_names = array();
  76. if ($nb_answers < 1) {
  77. $nb_answers = 1;
  78. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  79. }
  80. // Can be more options
  81. $option_data = Question::readQuestionOption($this->id, $course_id);
  82. for ($i = 1; $i <= $nb_answers; ++$i) {
  83. $renderer->setElementTemplate(
  84. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  85. 'correct['.$i.']'
  86. );
  87. $renderer->setElementTemplate(
  88. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  89. 'counter['.$i.']'
  90. );
  91. $renderer->setElementTemplate(
  92. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  93. 'answer['.$i.']'
  94. );
  95. $renderer->setElementTemplate(
  96. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  97. 'comment['.$i.']'
  98. );
  99. $answer_number = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"');
  100. $answer_number->freeze();
  101. if (isset($answer) && is_object($answer)) {
  102. $answer_id = $answer->getRealAnswerIdFromList($i);
  103. $defaults['answer['.$i.']'] = $answer->answer[$answer_id];
  104. $defaults['comment['.$i.']'] = $answer->comment[$answer_id];
  105. $correct = $answer->correct[$answer_id];
  106. $defaults['correct['.$i.']'] = $correct;
  107. $j = 1;
  108. if (!empty($option_data)) {
  109. foreach ($option_data as $id => $data) {
  110. $form->addElement('radio', 'correct['.$i.']', null, null, $id);
  111. $j++;
  112. if ($j == 3) {
  113. break;
  114. }
  115. }
  116. }
  117. } else {
  118. $form->addElement('radio', 'correct['.$i.']', null, null, 1);
  119. $form->addElement('radio', 'correct['.$i.']', null, null, 2);
  120. $defaults['answer['.$i.']'] = '';
  121. $defaults['comment['.$i.']'] = '';
  122. $defaults['correct['.$i.']'] = '';
  123. }
  124. $boxes_names[] = 'correct['.$i.']';
  125. if ($obj_ex->fastEdition) {
  126. $form->addElement(
  127. 'textarea',
  128. 'answer['.$i.']',
  129. null,
  130. $this->textareaSettings
  131. );
  132. } else {
  133. $form->addElement(
  134. 'html_editor',
  135. 'answer['.$i.']',
  136. null,
  137. 'style="vertical-align:middle"',
  138. array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100')
  139. );
  140. }
  141. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  142. // show comment when feedback is enable
  143. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  144. if ($obj_ex->fastEdition) {
  145. $form->addElement(
  146. 'textarea',
  147. 'comment['.$i.']',
  148. null,
  149. $this->textareaSettings
  150. );
  151. } else {
  152. $form->addElement(
  153. 'html_editor',
  154. 'comment['.$i.']',
  155. null,
  156. 'style="vertical-align:middle"',
  157. array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100')
  158. );
  159. }
  160. }
  161. $form->addElement('html', '</tr>');
  162. }
  163. $form->addElement('html', '</table>');
  164. $form->addElement('html', '<br />');
  165. $form->addElement(
  166. 'html',
  167. '<table><tr><td></td><td>'.get_lang('Correct').'</td><td>'.get_lang('Wrong').'</td><td>'.get_lang(
  168. 'DoubtScore'
  169. ).'</td></tr>'
  170. );
  171. $renderer->setElementTemplate(
  172. '<tr><td><span class="form_required">*</span>'.get_lang(
  173. 'Score'
  174. ).'&nbsp;&nbsp;&nbsp;&nbsp;</td><td>{element} &nbsp;&nbsp; <br /><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --></td>',
  175. 'option[1]'
  176. );
  177. $renderer->setElementTemplate(
  178. '<td>{element} &nbsp;&nbsp;<br /><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --></td>',
  179. 'option[2]'
  180. );
  181. $renderer->setElementTemplate(
  182. '<td>{element} &nbsp;&nbsp;<br /><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --></td>',
  183. 'option[3]'
  184. );
  185. // 3 scores
  186. $form->addElement('text', 'option[1]', get_lang('Correct'), array('class' => 'span1', 'value' => '1'));
  187. $form->addElement('text', 'option[2]', get_lang('Wrong'), array('class' => 'span1', 'value' => '-0.5'));
  188. $form->addElement('text', 'option[3]', get_lang('DoubtScore'), array('class' => 'span1', 'value' => '0'));
  189. $form->addRule('option[1]', get_lang('ThisFieldIsRequired'), 'required');
  190. $form->addRule('option[2]', get_lang('ThisFieldIsRequired'), 'required');
  191. $form->addRule('option[3]', get_lang('ThisFieldIsRequired'), 'required');
  192. $form->addElement('html', '</tr><table>');
  193. $form->addElement('hidden', 'options_count', 3);
  194. $form->addElement('html', '</table><br /><br />');
  195. //Extra values True, false, Dont known
  196. if (!empty($this->extra)) {
  197. $scores = explode(':', $this->extra);
  198. if (!empty($scores)) {
  199. for ($i = 1; $i <= 3; $i++) {
  200. $defaults['option['.$i.']'] = $scores[$i - 1];
  201. }
  202. }
  203. }
  204. //$form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  205. $navigator_info = api_get_navigator();
  206. if ($obj_ex->edit_exercise_in_lp == true) {
  207. //ie6 fix
  208. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  209. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'), 'class="btn minus"');
  210. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'), 'class="btn plus"');
  211. $form->addElement('submit', 'submitQuestion', $this->submitText, 'class="'.$this->submitClass.'"');
  212. } else {
  213. // setting the save button here and not in the question class.php
  214. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'), 'class="btn minus"');
  215. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'), 'class="btn plus"');
  216. $form->addElement(
  217. 'style_submit_button',
  218. 'submitQuestion',
  219. $this->submitText,
  220. 'class="'.$this->submitClass.'"'
  221. );
  222. }
  223. }
  224. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  225. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  226. $renderer->setElementTemplate('{element}&nbsp;', 'moreAnswers');
  227. $form->addElement('html', '</div></div>');
  228. $defaults['correct'] = $correct;
  229. if (!empty($this->id)) {
  230. $form->setDefaults($defaults);
  231. } else {
  232. //if ($this -> isContent == 1) {
  233. $form->setDefaults($defaults);
  234. //}
  235. }
  236. $form->setConstants(array('nb_answers' => $nb_answers));
  237. }
  238. /**
  239. * abstract function which creates the form to create / edit the answers of the question
  240. * @param the formvalidator instance
  241. * @param the answers number to display
  242. */
  243. function processAnswersCreation($form)
  244. {
  245. $questionWeighting = $nbrGoodAnswers = 0;
  246. $objAnswer = new Answer($this->id);
  247. $nb_answers = $form->getSubmitValue('nb_answers');
  248. $options_count = $form->getSubmitValue('options_count');
  249. $course_id = api_get_course_int_id();
  250. $correct = array();
  251. $options = Question::readQuestionOption($this->id, $course_id);
  252. if (!empty($options)) {
  253. foreach ($options as $option_data) {
  254. $id = $option_data['iid'];
  255. unset($option_data['iid']);
  256. Question::updateQuestionOption($id, $option_data, $course_id);
  257. }
  258. } else {
  259. for ($i = 1; $i <= 3; $i++) {
  260. $last_id = Question::saveQuestionOption($this->id, $this->options[$i], $course_id, $i);
  261. $correct[$i] = $last_id;
  262. }
  263. }
  264. //Getting quiz_question_options (true, false, doubt) because it's possible that there are more options in the future
  265. $new_options = Question::readQuestionOption($this->id, $course_id);
  266. $sorted_by_position = array();
  267. foreach ($new_options as $item) {
  268. $sorted_by_position[$item['position']] = $item;
  269. }
  270. //Saving quiz_question.extra values that has the correct scores of the true, false, doubt options registered in this format XX:YY:ZZZ where XX is a float score value
  271. $extra_values = array();
  272. for ($i = 1; $i <= 3; $i++) {
  273. $score = trim($form->getSubmitValue('option['.$i.']'));
  274. $extra_values[] = $score;
  275. }
  276. $this->setExtra(implode(':', $extra_values));
  277. for ($i = 1; $i <= $nb_answers; $i++) {
  278. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  279. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  280. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  281. if (empty($options)) {
  282. //If this is the first time that the question is created when change the default values from the form 1 and 2 by the correct "option id" registered
  283. $goodAnswer = $sorted_by_position[$goodAnswer]['iid'];
  284. }
  285. $questionWeighting += $extra_values[0]; //By default 0 has the correct answers
  286. $objAnswer->createAnswer($answer, $goodAnswer, $comment, '', $i);
  287. }
  288. // saves the answers into the data base
  289. $objAnswer->save();
  290. // sets the total weighting of the question
  291. $this->updateWeighting($questionWeighting);
  292. $this->save();
  293. }
  294. /**
  295. * {@inheritdoc}
  296. */
  297. function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false, $hideTitle = 0)
  298. {
  299. $header = parent::return_header($feedback_type, $counter, $score, $show_media, $hideTitle);
  300. $header .= '<table class="'.$this->question_table_class.'">
  301. <tr>
  302. <th>'.get_lang("Choice").'</th>
  303. <th>'.get_lang("ExpectedChoice").'</th>
  304. <th>'.get_lang("Answer").'</th>';
  305. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  306. $header .= '<th>'.get_lang("Comment").'</th>';
  307. } else {
  308. $header .= '<th>&nbsp;</th>';
  309. }
  310. $header .= '</tr>';
  311. return $header;
  312. }
  313. }