multiple_answer.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  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 Eric Marguin
  11. *
  12. * @package chamilo.exercise
  13. */
  14. class MultipleAnswer extends Question
  15. {
  16. public static $typePicture = 'mcma.png';
  17. public static $explanationLangVar = 'MultipleSelect';
  18. /**
  19. * Constructor.
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->type = MULTIPLE_ANSWER;
  25. $this->isContent = $this->getIsContent();
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function createAnswersForm($form)
  31. {
  32. $editorConfig = [
  33. 'ToolbarSet' => 'TestProposedAnswer',
  34. 'Width' => '100%',
  35. 'Height' => '125',
  36. ];
  37. // The previous default value was 2. See task #1759.
  38. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4;
  39. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  40. $obj_ex = Session::read('objExercise');
  41. $form->addHeader(get_lang('Answers'));
  42. $html = '<table class="table table-striped table-hover">
  43. <thead>
  44. <tr>
  45. <th width="10">'.get_lang('Number').'</th>
  46. <th width="10">'.get_lang('True').'</th>
  47. <th width="50%">'.get_lang('Answer').'</th>
  48. <th width="50%">'.get_lang('Comment').'</th>
  49. <th width="10">'.get_lang('Weighting').'</th>
  50. </tr>
  51. </thead>
  52. <tbody>';
  53. $form->addHtml($html);
  54. $defaults = [];
  55. $correct = 0;
  56. $answer = false;
  57. if (!empty($this->id)) {
  58. $answer = new Answer($this->id);
  59. $answer->read();
  60. if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
  61. $nb_answers = $answer->nbrAnswers;
  62. }
  63. }
  64. $form->addElement('hidden', 'nb_answers');
  65. $boxes_names = [];
  66. if ($nb_answers < 1) {
  67. $nb_answers = 1;
  68. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  69. }
  70. for ($i = 1; $i <= $nb_answers; $i++) {
  71. $form->addHtml('<tr>');
  72. if (is_object($answer)) {
  73. $defaults['answer['.$i.']'] = $answer->answer[$i];
  74. $defaults['comment['.$i.']'] = $answer->comment[$i];
  75. $defaults['weighting['.$i.']'] = float_format($answer->weighting[$i], 1);
  76. $defaults['correct['.$i.']'] = $answer->correct[$i];
  77. } else {
  78. $defaults['answer[1]'] = get_lang('DefaultMultipleAnswer2');
  79. $defaults['comment[1]'] = get_lang('DefaultMultipleComment2');
  80. $defaults['correct[1]'] = true;
  81. $defaults['weighting[1]'] = 10;
  82. $defaults['answer[2]'] = get_lang('DefaultMultipleAnswer1');
  83. $defaults['comment[2]'] = get_lang('DefaultMultipleComment1');
  84. $defaults['correct[2]'] = false;
  85. $defaults['weighting[2]'] = -5;
  86. }
  87. $renderer = &$form->defaultRenderer();
  88. $renderer->setElementTemplate(
  89. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  90. 'correct['.$i.']'
  91. );
  92. $renderer->setElementTemplate(
  93. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  94. 'counter['.$i.']'
  95. );
  96. $renderer->setElementTemplate(
  97. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  98. 'answer['.$i.']'
  99. );
  100. $renderer->setElementTemplate(
  101. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  102. 'comment['.$i.']'
  103. );
  104. $renderer->setElementTemplate(
  105. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  106. 'weighting['.$i.']'
  107. );
  108. $answer_number = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"');
  109. $answer_number->freeze();
  110. $form->addElement(
  111. 'checkbox',
  112. 'correct['.$i.']',
  113. null,
  114. null,
  115. 'class="checkbox" style="margin-left: 0em;"'
  116. );
  117. $boxes_names[] = 'correct['.$i.']';
  118. $form->addHtmlEditor("answer[$i]", null, null, false, $editorConfig);
  119. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  120. $form->addHtmlEditor("comment[$i]", null, null, false, $editorConfig);
  121. $form->addElement('text', 'weighting['.$i.']', null, ['style' => "width: 60px;", 'value' => '0']);
  122. $form->addHtml('</tr>');
  123. }
  124. $form->addHtml('</tbody>');
  125. $form->addHtml('</table>');
  126. $form->add_multiple_required_rule(
  127. $boxes_names,
  128. get_lang('ChooseAtLeastOneCheckbox'),
  129. 'multiple_required'
  130. );
  131. $buttonGroup = [];
  132. global $text;
  133. if ($obj_ex->edit_exercise_in_lp == true ||
  134. (empty($this->exerciseList) && empty($obj_ex->id))
  135. ) {
  136. // setting the save button here and not in the question class.php
  137. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  138. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  139. $buttonGroup[] = $form->addButton(
  140. 'submitQuestion',
  141. $text,
  142. 'check',
  143. 'primary',
  144. 'default',
  145. null,
  146. ['id' => 'submit-question'],
  147. true
  148. );
  149. }
  150. $form->addGroup($buttonGroup);
  151. $defaults['correct'] = $correct;
  152. if (!empty($this->id)) {
  153. $form->setDefaults($defaults);
  154. } else {
  155. if ($this->isContent == 1) {
  156. $form->setDefaults($defaults);
  157. }
  158. }
  159. $form->setConstants(['nb_answers' => $nb_answers]);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function processAnswersCreation($form, $exercise)
  165. {
  166. $questionWeighting = 0;
  167. $objAnswer = new Answer($this->id);
  168. $nb_answers = $form->getSubmitValue('nb_answers');
  169. for ($i = 1; $i <= $nb_answers; $i++) {
  170. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']')));
  171. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']')));
  172. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  173. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  174. if ($goodAnswer) {
  175. $weighting = abs($weighting);
  176. } else {
  177. $weighting = abs($weighting);
  178. $weighting = -$weighting;
  179. }
  180. if ($weighting > 0) {
  181. $questionWeighting += $weighting;
  182. }
  183. $objAnswer->createAnswer(
  184. $answer,
  185. $goodAnswer,
  186. $comment,
  187. $weighting,
  188. $i
  189. );
  190. }
  191. // saves the answers into the data base
  192. $objAnswer->save();
  193. // sets the total weighting of the question
  194. $this->updateWeighting($questionWeighting);
  195. $this->save($exercise);
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function return_header(Exercise $exercise, $counter = null, $score = [])
  201. {
  202. $header = parent::return_header($exercise, $counter, $score);
  203. $header .= '<table class="'.$this->question_table_class.'"><tr>';
  204. $header .= '<th>'.get_lang('Choice').'</th>';
  205. if ($exercise->showExpectedChoiceColumn()) {
  206. $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
  207. }
  208. $header .= '<th>'.get_lang('Answer').'</th>';
  209. if ($exercise->showExpectedChoice()) {
  210. $header .= '<th>'.get_lang('Status').'</th>';
  211. }
  212. $header .= '<th>'.get_lang('Comment').'</th>';
  213. $header .= '</tr>';
  214. return $header;
  215. }
  216. }