multiple_answer_true_false.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * abstract function which creates the form to create / edit the answers of the question
  202. * @param FormValidator $form
  203. */
  204. public function processAnswersCreation($form)
  205. {
  206. $questionWeighting = $nbrGoodAnswers = 0;
  207. $objAnswer = new Answer($this->id);
  208. $nb_answers = $form->getSubmitValue('nb_answers');
  209. //$options_count = $form->getSubmitValue('options_count');
  210. $course_id = api_get_course_int_id();
  211. $correct = array();
  212. $options = Question::readQuestionOption($this->id, $course_id);
  213. if (!empty($options)) {
  214. foreach ($options as $optionData) {
  215. $id = $optionData['id'];
  216. unset($optionData['id']);
  217. Question::updateQuestionOption($id, $optionData, $course_id);
  218. }
  219. } else {
  220. for ($i = 1; $i <= 3; $i++) {
  221. $last_id = Question::saveQuestionOption(
  222. $this->id,
  223. $this->options[$i],
  224. $course_id,
  225. $i
  226. );
  227. $correct[$i] = $last_id;
  228. }
  229. }
  230. /* Getting quiz_question_options (true, false, doubt) because
  231. it's possible that there are more options in the future */
  232. $new_options = Question::readQuestionOption($this->id, $course_id);
  233. $sorted_by_position = array();
  234. foreach ($new_options as $item) {
  235. $sorted_by_position[$item['position']] = $item;
  236. }
  237. /* Saving quiz_question.extra values that has the correct scores of
  238. the true, false, doubt options registered in this format
  239. XX:YY:ZZZ where XX is a float score value.*/
  240. $extra_values = array();
  241. for ($i = 1; $i <= 3; $i++) {
  242. $score = trim($form -> getSubmitValue('option['.$i.']'));
  243. $extra_values[] = $score;
  244. }
  245. $this->setExtra(implode(':', $extra_values));
  246. for ($i = 1; $i <= $nb_answers; $i++) {
  247. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  248. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  249. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  250. if (empty($options)) {
  251. //If this is the first time that the question is created when
  252. // change the default values from the form 1 and 2 by the correct "option id" registered
  253. $goodAnswer = $sorted_by_position[$goodAnswer]['id'];
  254. }
  255. $questionWeighting += $extra_values[0]; //By default 0 has the correct answers
  256. $objAnswer->createAnswer($answer, $goodAnswer, $comment, '', $i);
  257. }
  258. // saves the answers into the database
  259. $objAnswer->save();
  260. // sets the total weighting of the question
  261. $this->updateWeighting($questionWeighting);
  262. $this->save();
  263. }
  264. /**
  265. * @param int $feedback_type
  266. * @param int $counter
  267. * @param float $score
  268. * @return null|string
  269. */
  270. function return_header($feedback_type = null, $counter = null, $score = null)
  271. {
  272. $header = parent::return_header($feedback_type, $counter, $score);
  273. $header .= '<table class="'.$this->question_table_class .'">
  274. <tr>
  275. <th>'.get_lang("Choice").'</th>
  276. <th>'. get_lang("ExpectedChoice").'</th>
  277. <th>'. get_lang("Answer").'</th>';
  278. $header .= '<th>'.get_lang('Status').'</th>';
  279. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  280. $header .= '<th>'.get_lang("Comment").'</th>';
  281. } else {
  282. $header .= '<th>&nbsp;</th>';
  283. }
  284. $header .= '</tr>';
  285. return $header;
  286. }
  287. }