multiple_answer_true_false.class.php 12 KB

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