multiple_answer_true_false.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * function which redefines Question::createAnswersForm
  29. * @param FormValidator $form
  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 = array();
  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->selectFeedbackType() != 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. $correct = 0;
  57. $answer = null;
  58. if (!empty($this->id)) {
  59. $answer = new Answer($this->id);
  60. $answer->read();
  61. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  62. $nb_answers = $answer->nbrAnswers;
  63. }
  64. }
  65. $form->addElement('hidden', 'nb_answers');
  66. $boxes_names = array();
  67. if ($nb_answers < 1) {
  68. $nb_answers = 1;
  69. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  70. }
  71. // Can be more options
  72. $optionData = Question::readQuestionOption($this->id, $course_id);
  73. for ($i = 1; $i <= $nb_answers; ++$i) {
  74. $form->addHtml('<tr>');
  75. $renderer->setElementTemplate(
  76. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  77. 'correct[' . $i . ']'
  78. );
  79. $renderer->setElementTemplate(
  80. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  81. 'counter[' . $i . ']'
  82. );
  83. $renderer->setElementTemplate(
  84. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  85. 'answer[' . $i . ']'
  86. );
  87. $renderer->setElementTemplate(
  88. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  89. 'comment[' . $i . ']'
  90. );
  91. $answer_number = $form->addElement(
  92. 'text',
  93. 'counter['.$i.']',
  94. null,
  95. 'value="'.$i.'"'
  96. );
  97. $answer_number->freeze();
  98. if (is_object($answer)) {
  99. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  100. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  101. $correct = $answer->correct[$i];
  102. $defaults['correct[' . $i . ']'] = $correct;
  103. $j = 1;
  104. if (!empty($optionData)) {
  105. foreach ($optionData as $id => $data) {
  106. $form->addElement('radio', 'correct[' . $i . ']', null, null, $id);
  107. $j++;
  108. if ($j == 3) {
  109. break;
  110. }
  111. }
  112. }
  113. } else {
  114. $form->addElement('radio', 'correct[' . $i . ']', null, null, 1);
  115. $form->addElement('radio', 'correct[' . $i . ']', null, null, 2);
  116. $defaults['answer[' . $i . ']'] = '';
  117. $defaults['comment[' . $i . ']'] = '';
  118. $defaults['correct[' . $i . ']'] = '';
  119. }
  120. $boxes_names[] = 'correct[' . $i . ']';
  121. $form->addHtmlEditor(
  122. "answer[$i]",
  123. get_lang('ThisFieldIsRequired'),
  124. true,
  125. true,
  126. ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100']
  127. );
  128. // show comment when feedback is enable
  129. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  130. $form->addHtmlEditor(
  131. 'comment[' . $i . ']',
  132. null,
  133. array(),
  134. array(
  135. 'ToolbarSet' => 'TestProposedAnswer',
  136. 'Width' => '100%',
  137. 'Height' => '100',
  138. )
  139. );
  140. }
  141. $form->addHtml('</tr>');
  142. }
  143. $form->addHtml('</tbody></table>');
  144. $correctInputTemplate = '<div class="form-group">';
  145. $correctInputTemplate .= '<label class="col-sm-2 control-label">';
  146. $correctInputTemplate .= '<span class="form_required">*</span>' . get_lang('Score');
  147. $correctInputTemplate .= '</label>';
  148. $correctInputTemplate .= '<div class="col-sm-8">';
  149. $correctInputTemplate .= '<table>';
  150. $correctInputTemplate .= '<tr>';
  151. $correctInputTemplate .= '<td>';
  152. $correctInputTemplate .= get_lang('Correct') . '{element}';
  153. $correctInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->';
  154. $correctInputTemplate .= '</td>';
  155. $wrongInputTemplate = '<td>';
  156. $wrongInputTemplate .= get_lang('Wrong') . '{element}';
  157. $wrongInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->';
  158. $wrongInputTemplate .= '</td>';
  159. $doubtScoreInputTemplate = '<td>' . get_lang('DoubtScore') . '<br>{element}';
  160. $doubtScoreInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->';
  161. $doubtScoreInputTemplate .= '</td>';
  162. $doubtScoreInputTemplate .= '</tr>';
  163. $doubtScoreInputTemplate .= '</table>';
  164. $doubtScoreInputTemplate .= '</div>';
  165. $doubtScoreInputTemplate .= '</div>';
  166. $renderer->setElementTemplate($correctInputTemplate, 'option[1]');
  167. $renderer->setElementTemplate($wrongInputTemplate, 'option[2]');
  168. $renderer->setElementTemplate($doubtScoreInputTemplate, 'option[3]');
  169. // 3 scores
  170. $form->addElement('text', 'option[1]', get_lang('Correct'), array('class' => 'span1', 'value' => '1'));
  171. $form->addElement('text', 'option[2]', get_lang('Wrong'), array('class' => 'span1', 'value' => '-0.5'));
  172. $form->addElement('text', 'option[3]', get_lang('DoubtScore'), array('class' => 'span1', 'value' => '0'));
  173. $form->addRule('option[1]', get_lang('ThisFieldIsRequired'), 'required');
  174. $form->addRule('option[2]', get_lang('ThisFieldIsRequired'), 'required');
  175. $form->addRule('option[3]', get_lang('ThisFieldIsRequired'), 'required');
  176. $form->addElement('hidden', 'options_count', 3);
  177. // Extra values True, false, Dont known
  178. if (!empty($this->extra)) {
  179. $scores = explode(':', $this->extra);
  180. if (!empty($scores)) {
  181. for ($i = 1; $i <= 3; $i++) {
  182. $defaults['option[' . $i . ']'] = $scores[$i - 1];
  183. }
  184. }
  185. }
  186. global $text;
  187. if ($obj_ex->edit_exercise_in_lp == true) {
  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. //if ($this -> isContent == 1) {
  198. $form->setDefaults($defaults);
  199. //}
  200. }
  201. $form->setConstants(array('nb_answers' => $nb_answers));
  202. }
  203. /**
  204. * abstract function which creates the form to create / edit the answers of the question
  205. * @param FormValidator $form
  206. */
  207. public function processAnswersCreation($form)
  208. {
  209. $questionWeighting = $nbrGoodAnswers = 0;
  210. $objAnswer = new Answer($this->id);
  211. $nb_answers = $form->getSubmitValue('nb_answers');
  212. //$options_count = $form->getSubmitValue('options_count');
  213. $course_id = api_get_course_int_id();
  214. $correct = array();
  215. $options = Question::readQuestionOption($this->id, $course_id);
  216. if (!empty($options)) {
  217. foreach ($options as $optionData) {
  218. $id = $optionData['id'];
  219. unset($optionData['id']);
  220. Question::updateQuestionOption($id, $optionData, $course_id);
  221. }
  222. } else {
  223. for ($i=1 ; $i <= 3 ; $i++) {
  224. $last_id = Question::saveQuestionOption(
  225. $this->id,
  226. $this->options[$i],
  227. $course_id,
  228. $i
  229. );
  230. $correct[$i] = $last_id;
  231. }
  232. }
  233. /* Getting quiz_question_options (true, false, doubt) because
  234. it's possible that there are more options in the future */
  235. $new_options = Question::readQuestionOption($this->id, $course_id);
  236. $sorted_by_position = array();
  237. foreach ($new_options as $item) {
  238. $sorted_by_position[$item['position']] = $item;
  239. }
  240. /* Saving quiz_question.extra values that has the correct scores of
  241. the true, false, doubt options registered in this format
  242. XX:YY:ZZZ where XX is a float score value.*/
  243. $extra_values = array();
  244. for ($i=1 ; $i <= 3 ; $i++) {
  245. $score = trim($form -> getSubmitValue('option['.$i.']'));
  246. $extra_values[]= $score;
  247. }
  248. $this->setExtra(implode(':', $extra_values));
  249. for ($i = 1; $i <= $nb_answers; $i++) {
  250. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  251. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  252. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  253. if (empty($options)) {
  254. //If this is the first time that the question is created when
  255. // change the default values from the form 1 and 2 by the correct "option id" registered
  256. $goodAnswer = $sorted_by_position[$goodAnswer]['id'];
  257. }
  258. $questionWeighting += $extra_values[0]; //By default 0 has the correct answers
  259. $objAnswer->createAnswer($answer, $goodAnswer, $comment,'',$i);
  260. }
  261. // saves the answers into the data base
  262. $objAnswer->save();
  263. // sets the total weighting of the question
  264. $this->updateWeighting($questionWeighting);
  265. $this->save();
  266. }
  267. /**
  268. * @param int $feedback_type
  269. * @param int $counter
  270. * @param float $score
  271. * @return null|string
  272. */
  273. function return_header($feedback_type = null, $counter = null, $score = null)
  274. {
  275. $header = parent::return_header($feedback_type, $counter, $score);
  276. $header .= '<table class="'.$this->question_table_class .'">
  277. <tr>
  278. <th>'.get_lang("Choice").'</th>
  279. <th>'. get_lang("ExpectedChoice").'</th>
  280. <th>'. get_lang("Answer").'</th>';
  281. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  282. $header .= '<th>'.get_lang("Comment").'</th>';
  283. } else {
  284. $header .= '<th>&nbsp;</th>';
  285. }
  286. $header .= '</tr>';
  287. return $header;
  288. }
  289. }