global_multiple_answer.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class GlobalMultipleAnswer.
  6. */
  7. class GlobalMultipleAnswer extends Question
  8. {
  9. public static $typePicture = 'mcmagl.png';
  10. public static $explanationLangVar = 'GlobalMultipleAnswer';
  11. /**
  12. * GlobalMultipleAnswer constructor.
  13. */
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->type = GLOBAL_MULTIPLE_ANSWER;
  18. $this->isContent = $this->getIsContent();
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function createAnswersForm($form)
  24. {
  25. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4;
  26. $nb_answers += isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0);
  27. $obj_ex = Session::read('objExercise');
  28. /* Mise en variable de Affichage "Reponses" et son icone, "N�", "Vrai", "Reponse" */
  29. $html = '<table class="data_table">
  30. <tr>
  31. <th width="10px">
  32. '.get_lang('Number').'
  33. </th>
  34. <th width="10px">
  35. '.get_lang('True').'
  36. </th>
  37. <th width="50%">
  38. '.get_lang('Answer').'
  39. </th>';
  40. $html .= '<th>'.get_lang('Comment').'</th>';
  41. $html .= '</tr>';
  42. $form->addElement(
  43. 'label',
  44. get_lang('Answers').
  45. '<br /> '.Display::return_icon('fill_field.png'),
  46. $html
  47. );
  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. // le nombre de r�ponses est bien enregistr� sous la forme int(nb)
  59. /* Ajout mise en forme nb reponse */
  60. $form->addElement('hidden', 'nb_answers');
  61. $boxes_names = [];
  62. if ($nb_answers < 1) {
  63. $nb_answers = 1;
  64. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'), 'normal');
  65. }
  66. //D�but affichage score global dans la modification d'une question
  67. $scoreA = 0; //par reponse
  68. $scoreG = 0; //Global
  69. /* boucle pour sauvegarder les donn�es dans le tableau defaults */
  70. for ($i = 1; $i <= $nb_answers; $i++) {
  71. /* si la reponse est de type objet */
  72. if (is_object($answer)) {
  73. $defaults['answer['.$i.']'] = $answer->answer[$i];
  74. $defaults['comment['.$i.']'] = $answer->comment[$i];
  75. $defaults['correct['.$i.']'] = $answer->correct[$i];
  76. // start
  77. $scoreA = $answer->weighting[$i];
  78. }
  79. if ($scoreA > 0) {
  80. $scoreG = $scoreG + $scoreA;
  81. }
  82. //------------- Fin
  83. //------------- Debut si un des scores par reponse est egal � 0 : la coche vaut 1 (coch�)
  84. if ($scoreA == 0) {
  85. $defaults['pts'] = 1;
  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. $answer_number = $form->addElement(
  105. 'text',
  106. 'counter['.$i.']',
  107. null,
  108. 'value="'.$i.'"'
  109. );
  110. $answer_number->freeze();
  111. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox"');
  112. $boxes_names[] = 'correct['.$i.']';
  113. $form->addElement(
  114. 'html_editor',
  115. 'answer['.$i.']',
  116. null,
  117. [],
  118. [
  119. 'ToolbarSet' => 'TestProposedAnswer',
  120. 'Width' => '100%',
  121. 'Height' => '100',
  122. ]
  123. );
  124. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  125. $form->addElement(
  126. 'html_editor',
  127. 'comment['.$i.']',
  128. null,
  129. [],
  130. [
  131. 'ToolbarSet' => 'TestProposedAnswer',
  132. 'Width' => '100%',
  133. 'Height' => '100',
  134. ]
  135. );
  136. $form->addElement('html', '</tr>');
  137. }
  138. //--------- Mise en variable du score global lors d'une modification de la question/r�ponse
  139. $defaults['weighting[1]'] = (round($scoreG));
  140. $form->addElement('html', '</div></div></table>');
  141. $form->add_multiple_required_rule(
  142. $boxes_names,
  143. get_lang('ChooseAtLeastOneCheckbox'),
  144. 'multiple_required'
  145. );
  146. //only 1 answer the all deal ...
  147. $form->addElement('text', 'weighting[1]', get_lang('Score'));
  148. //--------- Creation coche pour ne pas prendre en compte les n�gatifs
  149. $form->addElement('checkbox', 'pts', '', get_lang('NoNegativeScore'));
  150. $form->addElement('html', '<br />');
  151. // Affiche un message si le score n'est pas renseign�
  152. $form->addRule('weighting[1]', get_lang('ThisFieldIsRequired'), 'required');
  153. global $text;
  154. if ($obj_ex->edit_exercise_in_lp ||
  155. (empty($this->exerciseList) && empty($obj_ex->id))
  156. ) {
  157. // setting the save button here and not in the question class.php
  158. $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers');
  159. $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers');
  160. $form->addButtonSave($text, 'submitQuestion');
  161. }
  162. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  163. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  164. $renderer->setElementTemplate('{element}', 'moreAnswers');
  165. $form->addElement('html', '</div></div>');
  166. $defaults['correct'] = $correct;
  167. if (!empty($this->id)) {
  168. $form->setDefaults($defaults);
  169. } else {
  170. if ($this->isContent == 1) {
  171. $form->setDefaults($defaults);
  172. }
  173. }
  174. $form->setConstants(['nb_answers' => $nb_answers]);
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function processAnswersCreation($form, $exercise)
  180. {
  181. $objAnswer = new Answer($this->id);
  182. $nb_answers = $form->getSubmitValue('nb_answers');
  183. // Score total
  184. $answer_score = trim($form->getSubmitValue('weighting[1]'));
  185. // Reponses correctes
  186. $nbr_corrects = 0;
  187. for ($i = 1; $i <= $nb_answers; $i++) {
  188. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  189. if ($goodAnswer) {
  190. $nbr_corrects++;
  191. }
  192. }
  193. // Set question weighting (score total)
  194. $questionWeighting = $answer_score;
  195. // Set score per answer
  196. $nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
  197. $answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
  198. $answer_score = $answer_score / $nbr_corrects;
  199. //$answer_score �quivaut � la valeur d'une bonne r�ponse
  200. // cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
  201. $test = $form->getSubmitValue('pts');
  202. for ($i = 1; $i <= $nb_answers; $i++) {
  203. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  204. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  205. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  206. if ($goodAnswer) {
  207. $weighting = abs($answer_score);
  208. } else {
  209. if ($test == 1) {
  210. $weighting = 0;
  211. } else {
  212. $weighting = -abs($answer_score);
  213. }
  214. }
  215. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
  216. }
  217. // saves the answers into the data base
  218. $objAnswer->save();
  219. // sets the total weighting of the question --> sert � donner le score total pendant l'examen
  220. $this->updateWeighting($questionWeighting);
  221. $this->save($exercise);
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function return_header(
  227. $exercise,
  228. $counter = null,
  229. $score = null
  230. ) {
  231. $header = parent::return_header($exercise, $counter, $score);
  232. $header .= '<table class="'.$this->question_table_class.'"><tr>';
  233. if (!in_array($exercise->results_disabled, [
  234. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  235. //RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  236. ])
  237. ) {
  238. $header .= '<th>'.get_lang('Choice').'</th>';
  239. $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
  240. }
  241. $header .= '<th>'.get_lang('Answer').'</th>';
  242. if ($exercise->showExpectedChoice()) {
  243. $header .= '<th>'.get_lang('Status').'</th>';
  244. }
  245. $header .= '<th>'.get_lang('Comment').'</th>';
  246. $header .= '</tr>';
  247. return $header;
  248. }
  249. }