multiple_answer.class.php 8.4 KB

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