multiple_answer_true_false.class.php 12 KB

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