multiple_answer_true_false.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. $obj_ex = $_SESSION['objExercise'];
  41. $html.='<div class="row">
  42. <div class="label">
  43. '.get_lang('Answers').'<br /><img src="../img/fill_field.png">
  44. </div>
  45. <div class="formw">';
  46. $html2 ='<div class="row">
  47. <div class="label">
  48. </div>
  49. <div class="formw">';
  50. $form -> addElement ('html', $html2);
  51. $form -> addElement ('html', '<table><tr>');
  52. $renderer = & $form->defaultRenderer();
  53. $defaults = array();
  54. //Extra values True, false, Dont known
  55. if (!empty($this->extra)) {
  56. $scores = explode(':',$this->extra);
  57. if (!empty($scores)) {
  58. for ($i = 1; $i <=3; $i++) {
  59. $defaults['option['.$i.']'] = $scores[$i-1];
  60. }
  61. }
  62. }
  63. // 3 scores
  64. $form->addElement('text', 'option[1]',get_lang('True'), array('size'=>'5','value'=>'1'));
  65. $form->addElement('text', 'option[2]',get_lang('False'), array('size'=>'5','value'=>'-0.5'));
  66. $form->addElement('text', 'option[3]',get_lang('DoubtScore'),array('size'=>'5','value'=>'0'));
  67. $form -> addElement('hidden', 'options_count', 3);
  68. $form -> addElement ('html', '</tr></table>');
  69. $form -> addElement ('html', '</div></div>');
  70. $html.='<table class="data_table">
  71. <tr style="text-align: center;">
  72. <th>
  73. '.get_lang('Number').'
  74. </th>
  75. <th>
  76. '.get_lang('True').'
  77. </th>
  78. <th>
  79. '.get_lang('False').'
  80. </th>
  81. <th>
  82. '.get_lang('Answer').'
  83. </th>';
  84. // show column comment when feedback is enable
  85. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM ) {
  86. $html .='<th>'.get_lang('Comment').'</th>';
  87. }
  88. $html .= '</tr>';
  89. $form -> addElement ('html', $html);
  90. $correct = 0;
  91. if (!empty($this -> id)) {
  92. $answer = new Answer($this -> id);
  93. $answer->read();
  94. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  95. $nb_answers = $answer->nbrAnswers;
  96. }
  97. }
  98. $form -> addElement('hidden', 'nb_answers');
  99. $boxes_names = array();
  100. if ($nb_answers < 1) {
  101. $nb_answers = 1;
  102. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  103. }
  104. // Can be more options
  105. $option_data = Question::readQuestionOption($this->id);
  106. for ($i = 1 ; $i <= $nb_answers ; ++$i) {
  107. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{label} &nbsp;&nbsp;{element}</td>');
  108. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  109. $answer_number->freeze();
  110. if (is_object($answer)) {
  111. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  112. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  113. //$defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  114. $correct = $answer->correct[$i];
  115. $defaults['correct['.$i.']'] = $correct;
  116. $j = 1;
  117. if (!empty($option_data)) {
  118. foreach ($option_data as $id=>$data) {
  119. $form->addElement('radio', 'correct['.$i.']', null, null, $id);
  120. $j++;
  121. if ($j == 3) {
  122. break;
  123. }
  124. }
  125. }
  126. } else {
  127. $form->addElement('radio', 'correct['.$i.']', null, null, 1);
  128. $form->addElement('radio', 'correct['.$i.']', null, null, 2);
  129. $defaults['answer['.$i.']'] = '';
  130. $defaults['comment['.$i.']'] = '';
  131. $defaults['correct['.$i.']'] = '';
  132. }
  133. //$form->addElement('select', 'correct['.$i.']',null, $this->options, array('id'=>$i,'onchange'=>'multiple_answer_true_false_onchange(this)'));
  134. $boxes_names[] = 'correct['.$i.']';
  135. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  136. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  137. // show comment when feedback is enable
  138. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  139. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  140. }
  141. $form->addElement ('html', '</tr>');
  142. }
  143. $form -> addElement ('html', '</table>');
  144. $form -> addElement ('html', '<br />');
  145. //$form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  146. $navigator_info = api_get_navigator();
  147. global $text, $class, $show_quiz_edition;
  148. if ($show_quiz_edition) {
  149. //ie6 fix
  150. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  151. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  152. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  153. $form->addElement('submit', 'submitQuestion',$text, 'class="'.$class.'"');
  154. } else {
  155. // setting the save button here and not in the question class.php
  156. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  157. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  158. $form->addElement('style_submit_button', 'submitQuestion',$text, 'class="'.$class.'"');
  159. }
  160. }
  161. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  162. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  163. $renderer->setElementTemplate('{element}','moreAnswers');
  164. $form -> addElement ('html', '</div></div>');
  165. $defaults['correct'] = $correct;
  166. if (!empty($this -> id)) {
  167. $form -> setDefaults($defaults);
  168. } else {
  169. //if ($this -> isContent == 1) {
  170. $form -> setDefaults($defaults);
  171. //}
  172. }
  173. $form->setConstants(array('nb_answers' => $nb_answers));
  174. }
  175. /**
  176. * abstract function which creates the form to create / edit the answers of the question
  177. * @param the formvalidator instance
  178. * @param the answers number to display
  179. */
  180. function processAnswersCreation($form) {
  181. $questionWeighting = $nbrGoodAnswers = 0;
  182. $objAnswer = new Answer($this->id);
  183. $nb_answers = $form->getSubmitValue('nb_answers');
  184. $options_count = $form->getSubmitValue('options_count');
  185. $correct = array();
  186. $options = Question::readQuestionOption($this->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);
  192. }
  193. } else {
  194. for ($i=1 ; $i <= 3 ; $i++) {
  195. $last_id = Question::saveQuestionOption($this->id, $this->options[$i], $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);
  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, $counter = null) {
  230. parent::return_header($feedback_type, $counter);
  231. $header = '<table width="100%" border="0" cellspacing="3" cellpadding="3">
  232. <tr>
  233. <td>&nbsp;</td>
  234. </tr>
  235. <tr>
  236. <td><i>'.get_lang("Choice").'</i> </td>
  237. <td><i>'. get_lang("ExpectedChoice").'</i></td>
  238. <td><i>'. get_lang("Answer").'</i></td>';
  239. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  240. $header .= '<td><i>'.get_lang("Comment").'</i></td>';
  241. } else {
  242. $header .= '<td>&nbsp;</td>';
  243. }
  244. $header .= '
  245. </tr>
  246. <tr>
  247. <td>&nbsp;</td>
  248. </tr>';
  249. return $header;
  250. }
  251. }
  252. endif;