multiple_answer_true_false.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class MultipleAnswerTrueFalse
  5. * This class allows to instantiate an object of type MULTIPLE_ANSWER
  6. * (MULTIPLE CHOICE, MULTIPLE ANSWER), extending the class question
  7. * @author Julio Montoya
  8. *
  9. * @package chamilo.exercise
  10. */
  11. class MultipleAnswerTrueFalse extends Question
  12. {
  13. static $typePicture = 'mcmao.png';
  14. static $explanationLangVar = 'MultipleAnswerTrueFalse';
  15. public $options;
  16. /**
  17. * Constructor
  18. */
  19. public function MultipleAnswerTrueFalse()
  20. {
  21. parent::question();
  22. $this->type = MULTIPLE_ANSWER_TRUE_FALSE;
  23. $this->isContent = $this-> getIsContent();
  24. $this->options = array(1 => 'True', 2 => 'False', 3 => 'DoubtScore');
  25. }
  26. /**
  27. * function which redefines Question::createAnswersForm
  28. * @param FormValidator $form
  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['objExercise'];
  37. $renderer = & $form->defaultRenderer();
  38. $defaults = array();
  39. $html = '<table class="data_table">
  40. <tr style="text-align: center;">
  41. <th>
  42. '.get_lang('Number').'
  43. </th>
  44. <th>
  45. '.get_lang('True').'
  46. </th>
  47. <th>
  48. '.get_lang('False').'
  49. </th>
  50. <th>
  51. '.get_lang('Answer').'
  52. </th>';
  53. // show column comment when feedback is enable
  54. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM ) {
  55. $html .='<th>'.get_lang('Comment').'</th>';
  56. }
  57. $html .= '</tr>';
  58. $form -> addElement('label', get_lang('Answers').'<br /> <img src="../img/fill_field.png">', $html);
  59. $correct = 0;
  60. $answer = null;
  61. if (!empty($this -> id)) {
  62. $answer = new Answer($this -> id);
  63. $answer->read();
  64. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  65. $nb_answers = $answer->nbrAnswers;
  66. }
  67. }
  68. $form -> addElement('hidden', 'nb_answers');
  69. $boxes_names = array();
  70. if ($nb_answers < 1) {
  71. $nb_answers = 1;
  72. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  73. }
  74. // Can be more options
  75. $option_data = Question::readQuestionOption($this->id, $course_id);
  76. for ($i = 1 ; $i <= $nb_answers ; ++$i) {
  77. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct['.$i.']');
  78. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  79. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  80. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  81. $answer_number=$form->addElement('text', 'counter['.$i.']',null,'value="'.$i.'"');
  82. $answer_number->freeze();
  83. if (is_object($answer)) {
  84. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  85. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  86. //$defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  87. $correct = $answer->correct[$i];
  88. $defaults['correct['.$i.']'] = $correct;
  89. $j = 1;
  90. if (!empty($option_data)) {
  91. foreach ($option_data as $id => $data) {
  92. $form->addElement('radio', 'correct['.$i.']', null, null, $id);
  93. $j++;
  94. if ($j == 3) {
  95. break;
  96. }
  97. }
  98. }
  99. } else {
  100. $form->addElement('radio', 'correct['.$i.']', null, null, 1);
  101. $form->addElement('radio', 'correct['.$i.']', null, null, 2);
  102. $defaults['answer['.$i.']'] = '';
  103. $defaults['comment['.$i.']'] = '';
  104. $defaults['correct['.$i.']'] = '';
  105. }
  106. $boxes_names[] = 'correct['.$i.']';
  107. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  108. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  109. // show comment when feedback is enable
  110. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  111. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  112. }
  113. $form->addElement ('html', '</tr>');
  114. }
  115. $form->addElement('html', '</table>');
  116. $form->addElement('html', '<br />');
  117. $form->addElement('html', '<table><tr><td></td><td>'.get_lang('Correct').'</td><td>'.get_lang('Wrong').'</td><td>'.get_lang('DoubtScore').'</td></tr>');
  118. $renderer->setElementTemplate('<tr><td><span class="form_required">*</span>'.get_lang('Score').'&nbsp;&nbsp;&nbsp;&nbsp;</td><td>{element} &nbsp;&nbsp; <br /><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --></td>', 'option[1]');
  119. $renderer->setElementTemplate('<td>{element} &nbsp;&nbsp;<br /><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --></td>', 'option[2]');
  120. $renderer->setElementTemplate('<td>{element} &nbsp;&nbsp;<br /><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --></td>', 'option[3]');
  121. // 3 scores
  122. $form->addElement('text', 'option[1]', get_lang('Correct'), array('class'=>'span1','value'=>'1'));
  123. $form->addElement('text', 'option[2]', get_lang('Wrong'), array('class'=>'span1','value'=>'-0.5'));
  124. $form->addElement('text', 'option[3]', get_lang('DoubtScore'),array('class'=>'span1','value'=>'0'));
  125. $form->addRule('option[1]', get_lang('ThisFieldIsRequired'), 'required');
  126. $form->addRule('option[2]', get_lang('ThisFieldIsRequired'), 'required');
  127. $form->addRule('option[3]', get_lang('ThisFieldIsRequired'), 'required');
  128. $form -> addElement ('html', '</tr><table>');
  129. $form -> addElement('hidden', 'options_count', 3);
  130. $form -> addElement ('html', '</table><br /><br />');
  131. //Extra values True, false, Dont known
  132. if (!empty($this->extra)) {
  133. $scores = explode(':',$this->extra);
  134. if (!empty($scores)) {
  135. for ($i = 1; $i <=3; $i++) {
  136. $defaults['option['.$i.']'] = $scores[$i-1];
  137. }
  138. }
  139. }
  140. $navigator_info = api_get_navigator();
  141. global $text, $class;
  142. if ($obj_ex->edit_exercise_in_lp == true) {
  143. //ie6 fix
  144. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  145. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  146. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  147. $form->addElement('submit', 'submitQuestion',$text, 'class="'.$class.'"');
  148. } else {
  149. // setting the save button here and not in the question class.php
  150. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  151. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  152. $form->addElement('style_submit_button', 'submitQuestion',$text, 'class="'.$class.'"');
  153. }
  154. }
  155. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  156. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  157. $renderer->setElementTemplate('{element}&nbsp;','moreAnswers');
  158. $form -> addElement ('html', '</div></div>');
  159. $defaults['correct'] = $correct;
  160. if (!empty($this -> id)) {
  161. $form -> setDefaults($defaults);
  162. } else {
  163. //if ($this -> isContent == 1) {
  164. $form -> setDefaults($defaults);
  165. //}
  166. }
  167. $form->setConstants(array('nb_answers' => $nb_answers));
  168. }
  169. /**
  170. * abstract function which creates the form to create / edit the answers of the question
  171. * @param FormValidator $form
  172. */
  173. public function processAnswersCreation($form)
  174. {
  175. $questionWeighting = $nbrGoodAnswers = 0;
  176. $objAnswer = new Answer($this->id);
  177. $nb_answers = $form->getSubmitValue('nb_answers');
  178. $options_count = $form->getSubmitValue('options_count');
  179. $course_id = api_get_course_int_id();
  180. $correct = array();
  181. $options = Question::readQuestionOption($this->id, $course_id);
  182. if (!empty($options)) {
  183. foreach ($options as $option_data) {
  184. $id = $option_data['id'];
  185. unset($option_data['id']);
  186. Question::updateQuestionOption($id, $option_data, $course_id);
  187. }
  188. } else {
  189. for ($i=1 ; $i <= 3 ; $i++) {
  190. $last_id = Question::saveQuestionOption($this->id, $this->options[$i], $course_id, $i);
  191. $correct[$i] = $last_id;
  192. }
  193. }
  194. /* Getting quiz_question_options (true, false, doubt) because
  195. it's possible that there are more options in the future */
  196. $new_options = Question::readQuestionOption($this->id, $course_id);
  197. $sorted_by_position = array();
  198. foreach($new_options as $item) {
  199. $sorted_by_position[$item['position']] = $item;
  200. }
  201. /* Saving quiz_question.extra values that has the correct scores of
  202. the true, false, doubt options registered in this format
  203. XX:YY:ZZZ where XX is a float score value.*/
  204. $extra_values = array();
  205. for ($i=1 ; $i <= 3 ; $i++) {
  206. $score = trim($form -> getSubmitValue('option['.$i.']'));
  207. $extra_values[]= $score;
  208. }
  209. $this->setExtra(implode(':',$extra_values));
  210. for ($i=1 ; $i <= $nb_answers ; $i++) {
  211. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  212. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  213. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  214. if (empty($options)) {
  215. //If this is the first time that the question is created when change the default values from the form 1 and 2 by the correct "option id" registered
  216. $goodAnswer = $sorted_by_position[$goodAnswer]['id'];
  217. }
  218. $questionWeighting += $extra_values[0]; //By default 0 has the correct answers
  219. $objAnswer->createAnswer($answer, $goodAnswer, $comment,'',$i);
  220. }
  221. // saves the answers into the data base
  222. $objAnswer->save();
  223. // sets the total weighting of the question
  224. $this->updateWeighting($questionWeighting);
  225. $this->save();
  226. }
  227. /**
  228. * @param int $feedback_type
  229. * @param int $counter
  230. * @param float $score
  231. * @return null|string
  232. */
  233. function return_header($feedback_type = null, $counter = null, $score = null)
  234. {
  235. $header = parent::return_header($feedback_type, $counter, $score);
  236. $header .= '<table class="'.$this->question_table_class .'">
  237. <tr>
  238. <th>'.get_lang("Choice").'</th>
  239. <th>'. get_lang("ExpectedChoice").'</th>
  240. <th>'. get_lang("Answer").'</th>';
  241. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  242. $header .= '<th>'.get_lang("Comment").'</th>';
  243. } else {
  244. $header .= '<th>&nbsp;</th>';
  245. }
  246. $header .= '</tr>';
  247. return $header;
  248. }
  249. }