multiple_answer_combination.class.php 8.3 KB

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