multiple_answer_true_false.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. $form->addHtmlEditor(
  119. "answer[$i]",
  120. get_lang('ThisFieldIsRequired'),
  121. true,
  122. true,
  123. ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100']
  124. );
  125. // show comment when feedback is enable
  126. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  127. $form->addElement(
  128. 'html_editor',
  129. 'comment['.$i.']',
  130. null,
  131. array(),
  132. array(
  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'), array('class' => 'span1', 'value' => '1'));
  169. $form->addElement('text', 'option[2]', get_lang('Wrong'), array('class' => 'span1', 'value' => '-0.5'));
  170. $form->addElement('text', 'option[3]', get_lang('DoubtScore'), array('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. // setting the save button here and not in the question class.php
  187. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  188. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), '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(array('nb_answers' => $nb_answers));
  198. }
  199. /**
  200. * @inheritdoc
  201. */
  202. public function processAnswersCreation($form, $exercise)
  203. {
  204. $questionWeighting = $nbrGoodAnswers = 0;
  205. $objAnswer = new Answer($this->id);
  206. $nb_answers = $form->getSubmitValue('nb_answers');
  207. $course_id = api_get_course_int_id();
  208. $correct = array();
  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. $sorted_by_position = array();
  231. foreach ($new_options as $item) {
  232. $sorted_by_position[$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 = array();
  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 = $sorted_by_position[$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, $counter = null, $score = null)
  265. {
  266. $header = parent::return_header($exercise, $counter, $score);
  267. $header .= '<table class="'.$this->question_table_class.'">
  268. <tr>
  269. <th>'.get_lang("Choice").'</th>
  270. <th>'. get_lang("ExpectedChoice").'</th>
  271. <th>'. get_lang("Answer").'</th>';
  272. if ($exercise->feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  273. $header .= '<th>'.get_lang("Comment").'</th>';
  274. } else {
  275. $header .= '<th>&nbsp;</th>';
  276. }
  277. $header .= '</tr>';
  278. return $header;
  279. }
  280. }