multiple_answer_true_false.class.php 13 KB

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