multiple_answer_true_false.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class MultipleAnswerTrueFalse
  6. * This class allows to instantiate an object of type MULTIPLE_ANSWER
  7. * (MULTIPLE CHOICE, MULTIPLE ANSWER), extending the class question.
  8. *
  9. * @author Julio Montoya
  10. */
  11. class MultipleAnswerTrueFalse extends Question
  12. {
  13. public $typePicture = 'mcmao.png';
  14. public $explanationLangVar = 'MultipleAnswerTrueFalse';
  15. public $options;
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->type = MULTIPLE_ANSWER_TRUE_FALSE;
  23. $this->isContent = $this->getIsContent();
  24. $this->options = [1 => 'True', 2 => 'False', 3 => 'DoubtScore'];
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function createAnswersForm($form)
  30. {
  31. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4;
  32. // The previous default value was 2. See task #1759.
  33. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  34. $course_id = api_get_course_int_id();
  35. $obj_ex = Session::read('objExercise');
  36. $renderer = &$form->defaultRenderer();
  37. $defaults = [];
  38. $html = '<table class="table table-striped table-hover">';
  39. $html .= '<thead>';
  40. $html .= '<tr>';
  41. $html .= '<th>'.get_lang('N°').'</th>';
  42. $html .= '<th>'.get_lang('True').'</th>';
  43. $html .= '<th>'.get_lang('False').'</th>';
  44. $html .= '<th>'.get_lang('Answer').'</th>';
  45. // show column comment when feedback is enable
  46. if ($obj_ex->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  47. $html .= '<th>'.get_lang('Comment').'</th>';
  48. }
  49. $html .= '</tr>';
  50. $html .= '</thead>';
  51. $html .= '<tbody>';
  52. $form->addHeader(get_lang('Answers'));
  53. $form->addHtml($html);
  54. $answer = null;
  55. if (!empty($this->id)) {
  56. $answer = new Answer($this->id);
  57. $answer->read();
  58. if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
  59. $nb_answers = $answer->nbrAnswers;
  60. }
  61. }
  62. $form->addElement('hidden', 'nb_answers');
  63. if ($nb_answers < 1) {
  64. $nb_answers = 1;
  65. echo Display::return_message(get_lang('You have to create at least one answer'));
  66. }
  67. // Can be more options
  68. $optionData = Question::readQuestionOption($this->id, $course_id);
  69. for ($i = 1; $i <= $nb_answers; $i++) {
  70. $form->addHtml('<tr>');
  71. $renderer->setElementTemplate(
  72. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  73. 'correct['.$i.']'
  74. );
  75. $renderer->setElementTemplate(
  76. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  77. 'counter['.$i.']'
  78. );
  79. $renderer->setElementTemplate(
  80. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  81. 'answer['.$i.']'
  82. );
  83. $renderer->setElementTemplate(
  84. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  85. 'comment['.$i.']'
  86. );
  87. $answer_number = $form->addElement(
  88. 'text',
  89. 'counter['.$i.']',
  90. null,
  91. 'value="'.$i.'"'
  92. );
  93. $answer_number->freeze();
  94. if (is_object($answer)) {
  95. $defaults['answer['.$i.']'] = $answer->answer[$i];
  96. $defaults['comment['.$i.']'] = $answer->comment[$i];
  97. $correct = $answer->correct[$i];
  98. $defaults['correct['.$i.']'] = $correct;
  99. $j = 1;
  100. if (!empty($optionData)) {
  101. foreach ($optionData as $id => $data) {
  102. $form->addElement('radio', 'correct['.$i.']', null, null, $id);
  103. $j++;
  104. if ($j == 3) {
  105. break;
  106. }
  107. }
  108. }
  109. } else {
  110. $form->addElement('radio', 'correct['.$i.']', null, null, 1);
  111. $form->addElement('radio', 'correct['.$i.']', null, null, 2);
  112. $defaults['answer['.$i.']'] = '';
  113. $defaults['comment['.$i.']'] = '';
  114. $defaults['correct['.$i.']'] = '';
  115. }
  116. $form->addHtmlEditor(
  117. "answer[$i]",
  118. get_lang('Required field'),
  119. true,
  120. false,
  121. ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100']
  122. );
  123. // show comment when feedback is enable
  124. if ($obj_ex->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  125. $form->addElement(
  126. 'html_editor',
  127. 'comment['.$i.']',
  128. null,
  129. [],
  130. [
  131. 'ToolbarSet' => 'TestProposedAnswer',
  132. 'Width' => '100%',
  133. 'Height' => '100',
  134. ]
  135. );
  136. }
  137. $form->addHtml('</tr>');
  138. }
  139. $form->addHtml('</tbody></table>');
  140. $correctInputTemplate = '<div class="form-group">';
  141. $correctInputTemplate .= '<label class="col-sm-2 control-label">';
  142. $correctInputTemplate .= '<span class="form_required">*</span>'.get_lang('Score');
  143. $correctInputTemplate .= '</label>';
  144. $correctInputTemplate .= '<div class="col-sm-8">';
  145. $correctInputTemplate .= '<table>';
  146. $correctInputTemplate .= '<tr>';
  147. $correctInputTemplate .= '<td>';
  148. $correctInputTemplate .= get_lang('Correct').'{element}';
  149. $correctInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->';
  150. $correctInputTemplate .= '</td>';
  151. $wrongInputTemplate = '<td>';
  152. $wrongInputTemplate .= get_lang('Wrong').'{element}';
  153. $wrongInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->';
  154. $wrongInputTemplate .= '</td>';
  155. $doubtScoreInputTemplate = '<td>'.get_lang('Don\'t know').'<br>{element}';
  156. $doubtScoreInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->';
  157. $doubtScoreInputTemplate .= '</td>';
  158. $doubtScoreInputTemplate .= '</tr>';
  159. $doubtScoreInputTemplate .= '</table>';
  160. $doubtScoreInputTemplate .= '</div>';
  161. $doubtScoreInputTemplate .= '</div>';
  162. $renderer->setElementTemplate($correctInputTemplate, 'option[1]');
  163. $renderer->setElementTemplate($wrongInputTemplate, 'option[2]');
  164. $renderer->setElementTemplate($doubtScoreInputTemplate, 'option[3]');
  165. // 3 scores
  166. $form->addElement('text', 'option[1]', get_lang('Correct'), ['class' => 'span1', 'value' => '1']);
  167. $form->addElement('text', 'option[2]', get_lang('Wrong'), ['class' => 'span1', 'value' => '-0.5']);
  168. $form->addElement('text', 'option[3]', get_lang('Don\'t know'), ['class' => 'span1', 'value' => '0']);
  169. $form->addRule('option[1]', get_lang('Required field'), 'required');
  170. $form->addRule('option[2]', get_lang('Required field'), 'required');
  171. $form->addRule('option[3]', get_lang('Required field'), 'required');
  172. $form->addElement('hidden', 'options_count', 3);
  173. // Extra values True, false, Dont known
  174. if (!empty($this->extra)) {
  175. $scores = explode(':', $this->extra);
  176. if (!empty($scores)) {
  177. for ($i = 1; $i <= 3; $i++) {
  178. $defaults['option['.$i.']'] = $scores[$i - 1];
  179. }
  180. }
  181. }
  182. global $text;
  183. if ($obj_ex->edit_exercise_in_lp == true ||
  184. (empty($this->exerciseList) && empty($obj_ex->id))
  185. ) {
  186. // setting the save button here and not in the question class.php
  187. $buttonGroup[] = $form->addButtonDelete(get_lang('Remove answer option'), 'lessAnswers', true);
  188. $buttonGroup[] = $form->addButtonCreate(get_lang('Add answer option'), 'moreAnswers', true);
  189. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  190. $form->addGroup($buttonGroup);
  191. }
  192. if (!empty($this->id)) {
  193. $form->setDefaults($defaults);
  194. } else {
  195. $form->setDefaults($defaults);
  196. }
  197. $form->setConstants(['nb_answers' => $nb_answers]);
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function processAnswersCreation($form, $exercise)
  203. {
  204. $questionWeighting = 0;
  205. $objAnswer = new Answer($this->id);
  206. $nb_answers = $form->getSubmitValue('nb_answers');
  207. $course_id = api_get_course_int_id();
  208. $correct = [];
  209. $options = Question::readQuestionOption($this->id, $course_id);
  210. if (!empty($options)) {
  211. foreach ($options as $optionData) {
  212. $id = $optionData['id'];
  213. unset($optionData['id']);
  214. Question::updateQuestionOption($id, $optionData, $course_id);
  215. }
  216. } else {
  217. for ($i = 1; $i <= 3; $i++) {
  218. $last_id = Question::saveQuestionOption(
  219. $this->id,
  220. $this->options[$i],
  221. $course_id,
  222. $i
  223. );
  224. $correct[$i] = $last_id;
  225. }
  226. }
  227. /* Getting quiz_question_options (true, false, doubt) because
  228. it's possible that there are more options in the future */
  229. $new_options = Question::readQuestionOption($this->id, $course_id);
  230. $sortedByPosition = [];
  231. foreach ($new_options as $item) {
  232. $sortedByPosition[$item['position']] = $item;
  233. }
  234. /* Saving quiz_question.extra values that has the correct scores of
  235. the true, false, doubt options registered in this format
  236. XX:YY:ZZZ where XX is a float score value.*/
  237. $extra_values = [];
  238. for ($i = 1; $i <= 3; $i++) {
  239. $score = trim($form->getSubmitValue('option['.$i.']'));
  240. $extra_values[] = $score;
  241. }
  242. $this->setExtra(implode(':', $extra_values));
  243. for ($i = 1; $i <= $nb_answers; $i++) {
  244. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  245. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  246. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  247. if (empty($options)) {
  248. //If this is the first time that the question is created when
  249. // change the default values from the form 1 and 2 by the correct "option id" registered
  250. $goodAnswer = isset($sortedByPosition[$goodAnswer]) ? $sortedByPosition[$goodAnswer]['id'] : '';
  251. }
  252. $questionWeighting += $extra_values[0]; //By default 0 has the correct answers
  253. $objAnswer->createAnswer($answer, $goodAnswer, $comment, '', $i);
  254. }
  255. // saves the answers into the database
  256. $objAnswer->save();
  257. // sets the total weighting of the question
  258. $this->updateWeighting($questionWeighting);
  259. $this->save($exercise);
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function return_header(Exercise $exercise, $counter = null, $score = [])
  265. {
  266. $header = parent::return_header($exercise, $counter, $score);
  267. $header .= '<table class="'.$this->question_table_class.'"><tr>';
  268. if (!in_array($exercise->results_disabled, [
  269. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  270. ])
  271. ) {
  272. $header .= '<th>'.get_lang('Your choice').'</th>';
  273. if ($exercise->showExpectedChoiceColumn()) {
  274. $header .= '<th>'.get_lang('ExpectedYour choice').'</th>';
  275. }
  276. }
  277. $header .= '<th>'.get_lang('Answer').'</th>';
  278. if ($exercise->showExpectedChoice()) {
  279. $header .= '<th>'.get_lang('Status').'</th>';
  280. }
  281. if ($exercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM ||
  282. in_array(
  283. $exercise->results_disabled,
  284. [
  285. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  286. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  287. ]
  288. )
  289. ) {
  290. $header .= '<th>'.get_lang('Comment').'</th>';
  291. }
  292. $header .= '</tr>';
  293. return $header;
  294. }
  295. }