multiple_answer_combination.class.php 8.3 KB

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