multiple_answer.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File containing the MultipleAnswer class.
  5. * @package chamilo.exercise
  6. * @author Eric Marguin
  7. */
  8. /**
  9. * Code
  10. */
  11. /**
  12. CLASS MultipleAnswer
  13. *
  14. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  15. * extending the class question
  16. *
  17. * @author Eric Marguin
  18. * @package chamilo.exercise
  19. **/
  20. class MultipleAnswer extends Question
  21. {
  22. static $typePicture = 'mcma.gif';
  23. static $explanationLangVar = 'MultipleSelect';
  24. /**
  25. * Constructor
  26. */
  27. public function MultipleAnswer($course_code = null)
  28. {
  29. parent::question($course_code);
  30. $this->type = MULTIPLE_ANSWER;
  31. $this->isContent = $this->getIsContent();
  32. }
  33. /**
  34. * function which redefines Question::createAnswersForm
  35. * @param FormValidator instance
  36. */
  37. public function createAnswersForm($form)
  38. {
  39. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4; // The previous default value was 2. See task #1759.
  40. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  41. $obj_ex = $this->exercise;
  42. $html = '<table class="data_table">
  43. <tr>
  44. <th width="10px">
  45. '.get_lang('Number').'
  46. </th>
  47. <th width="10px">
  48. '.get_lang('True').'
  49. </th>
  50. <th width="50%">
  51. '.get_lang('Answer').'
  52. </th>';
  53. // show column comment when feedback is enable
  54. if (!empty($obj_ex) && $obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  55. $html .= '<th>
  56. '.get_lang('Comment').'
  57. </th>';
  58. }
  59. $html .= '<th width="50px">
  60. '.get_lang('Weighting').'
  61. </th>
  62. </tr>';
  63. $form->addElement('label', get_lang('Answers').'<br />'.Display::return_icon('fill_field.png'), $html);
  64. $defaults = array();
  65. $correct = 0;
  66. if (!empty($this->id)) {
  67. $answer = new Answer($this->id);
  68. $answer->read();
  69. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  70. $nb_answers = $answer->nbrAnswers;
  71. }
  72. }
  73. $form->addElement('hidden', 'nb_answers');
  74. $boxes_names = array();
  75. if ($nb_answers < 1) {
  76. $nb_answers = 1;
  77. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  78. }
  79. for ($i = 1; $i <= $nb_answers; ++$i) {
  80. if (isset($answer) && is_object($answer)) {
  81. $answer_id = $answer->getRealAnswerIdFromList($i);
  82. $defaults['answer['.$i.']'] = isset($answer->answer[$answer_id]) ? $answer->answer[$answer_id] : null;
  83. $defaults['comment['.$i.']'] = isset($answer->comment[$answer_id]) ? $answer->comment[$answer_id] : null;
  84. $defaults['weighting['.$i.']'] = isset($answer->weighting[$answer_id]) ? Text::float_format(
  85. $answer->weighting[$answer_id],
  86. 1
  87. ) : null;
  88. $defaults['correct['.$i.']'] = isset($answer->correct[$answer_id]) ? $answer->correct[$answer_id] : null;
  89. } else {
  90. $defaults['answer[1]'] = get_lang('DefaultMultipleAnswer2');
  91. $defaults['comment[1]'] = get_lang('DefaultMultipleComment2');
  92. $defaults['correct[1]'] = true;
  93. $defaults['weighting[1]'] = 10;
  94. $defaults['answer[2]'] = get_lang('DefaultMultipleAnswer1');
  95. $defaults['comment[2]'] = get_lang('DefaultMultipleComment1');
  96. $defaults['correct[2]'] = false;
  97. $defaults['weighting[2]'] = -5;
  98. }
  99. $renderer = & $form->defaultRenderer();
  100. $renderer->setElementTemplate(
  101. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  102. 'correct['.$i.']'
  103. );
  104. $renderer->setElementTemplate(
  105. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  106. 'counter['.$i.']'
  107. );
  108. $renderer->setElementTemplate(
  109. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  110. 'answer['.$i.']'
  111. );
  112. $renderer->setElementTemplate(
  113. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  114. 'comment['.$i.']'
  115. );
  116. $renderer->setElementTemplate(
  117. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  118. 'weighting['.$i.']'
  119. );
  120. $answer_number = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"');
  121. $answer_number->freeze();
  122. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox" style="margin-left: 0em;"');
  123. $boxes_names[] = 'correct['.$i.']';
  124. if ($obj_ex->fastEdition) {
  125. $form->addElement(
  126. 'textarea',
  127. 'answer['.$i.']',
  128. null,
  129. $this->textareaSettings
  130. );
  131. } else {
  132. $form->addElement(
  133. 'html_editor',
  134. 'answer['.$i.']',
  135. null,
  136. 'style="vertical-align:middle"',
  137. array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100')
  138. );
  139. }
  140. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  141. // show comment when feedback is enable
  142. if (!empty($obj_ex) && $obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  143. if ($obj_ex->fastEdition) {
  144. $form->addElement(
  145. 'textarea',
  146. 'comment['.$i.']',
  147. null,
  148. $this->textareaSettings
  149. );
  150. } else {
  151. $form->addElement(
  152. 'html_editor',
  153. 'comment['.$i.']',
  154. null,
  155. 'style="vertical-align:middle"',
  156. array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100')
  157. );
  158. }
  159. }
  160. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0'));
  161. $form->addElement('html', '</tr>');
  162. }
  163. $form->addElement('html', '</table>');
  164. $form->addElement('html', '<br />');
  165. $form->add_multiple_required_rule($boxes_names, get_lang('ChooseAtLeastOneCheckbox'), 'multiple_required');
  166. $navigator_info = api_get_navigator();
  167. if ($form->isFrozen() == false) {
  168. if ($obj_ex->edit_exercise_in_lp == true) {
  169. //ie6 fix
  170. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  171. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'), 'class="btn minus"');
  172. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'), 'class="btn plus"');
  173. $form->addElement('submit', 'submitQuestion', $this->submitText, 'class="'.$this->submitClass.'"');
  174. } else {
  175. // setting the save button here and not in the question class.php
  176. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'), 'class="btn minus"');
  177. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'), 'class="btn plus"');
  178. $form->addElement(
  179. 'style_submit_button',
  180. 'submitQuestion',
  181. $this->submitText,
  182. 'class="'.$this->submitClass.'"'
  183. );
  184. }
  185. }
  186. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  187. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  188. $renderer->setElementTemplate('{element}&nbsp;', 'moreAnswers');
  189. }
  190. $form->addElement('html', '</div></div>');
  191. $defaults['correct'] = $correct;
  192. if (!empty($this->id)) {
  193. $form->setDefaults($defaults);
  194. } else {
  195. if ($this->isContent == 1) {
  196. $form->setDefaults($defaults);
  197. }
  198. }
  199. $form->setConstants(array('nb_answers' => $nb_answers));
  200. }
  201. /**
  202. * abstract function which creates the form to create / edit the answers of the question
  203. * @param FormValidator instance
  204. */
  205. public function processAnswersCreation($form)
  206. {
  207. $questionWeighting = $nbrGoodAnswers = 0;
  208. $objAnswer = new Answer($this->id);
  209. $nb_answers = $form->getSubmitValue('nb_answers');
  210. for ($i = 1; $i <= $nb_answers; $i++) {
  211. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  212. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  213. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  214. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  215. if ($goodAnswer) {
  216. $weighting = abs($weighting);
  217. } else {
  218. $weighting = abs($weighting);
  219. $weighting = -$weighting;
  220. }
  221. if ($weighting > 0) {
  222. $questionWeighting += $weighting;
  223. }
  224. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
  225. }
  226. // Saves the answers into the data base.
  227. $objAnswer->save();
  228. // Sets the total weighting of the question.
  229. $this->updateWeighting($questionWeighting);
  230. $this->save();
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false, $hideTitle = 0)
  236. {
  237. $header = parent::return_header($feedback_type, $counter, $score, $show_media, $hideTitle);
  238. $header .= '<table class="'.$this->question_table_class.'">
  239. <tr>
  240. <th>'.get_lang("Choice").'</th>
  241. <th>'.get_lang("ExpectedChoice").'</th>
  242. <th>'.get_lang("Answer").'</th>';
  243. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  244. $header .= '<th>'.get_lang("Comment").'</th>';
  245. } else {
  246. $header .= '<th>&nbsp;</th>';
  247. }
  248. $header .= '</tr>';
  249. return $header;
  250. }
  251. }