multiple_answer_true_false.class.php 11 KB

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