global_multiple_answer.class.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 = array();
  49. $correct = 0;
  50. $answer = false;
  51. if (!empty($this->id)) {
  52. $answer = new Answer($this->id);
  53. $answer->read();
  54. if (count($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 = array();
  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. array(),
  118. array(
  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. array(),
  130. array(
  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 == true) {
  155. $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers');
  156. $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers');
  157. $form->addButtonSave($text, 'submitQuestion');
  158. // setting the save button here and not in the question class.php
  159. }
  160. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  161. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  162. $renderer->setElementTemplate('{element}', 'moreAnswers');
  163. $form->addElement('html', '</div></div>');
  164. $defaults['correct'] = $correct;
  165. if (!empty($this->id)) {
  166. $form->setDefaults($defaults);
  167. } else {
  168. if ($this->isContent == 1) {
  169. $form->setDefaults($defaults);
  170. }
  171. }
  172. $form->setConstants(array('nb_answers' => $nb_answers));
  173. }
  174. /**
  175. * abstract function which creates the form to create / edit the answers of the question
  176. * @param FormValidator $form
  177. */
  178. function processAnswersCreation($form)
  179. {
  180. $objAnswer = new Answer($this->id);
  181. $nb_answers = $form->getSubmitValue('nb_answers');
  182. // Score total
  183. $answer_score = trim($form->getSubmitValue('weighting[1]'));
  184. // Reponses correctes
  185. $nbr_corrects = 0;
  186. for ($i = 1; $i <= $nb_answers; $i++) {
  187. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  188. if ($goodAnswer) {
  189. $nbr_corrects++;
  190. }
  191. }
  192. // Set question weighting (score total)
  193. $questionWeighting = $answer_score;
  194. // Set score per answer
  195. $nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
  196. $answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
  197. $answer_score = ($answer_score / $nbr_corrects);
  198. //$answer_score �quivaut � la valeur d'une bonne r�ponse
  199. // cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
  200. $test = $form->getSubmitValue('pts');
  201. for ($i = 1; $i <= $nb_answers; $i++) {
  202. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  203. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  204. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  205. if ($goodAnswer) {
  206. $weighting = abs($answer_score);
  207. } else {
  208. if ($test == 1) {
  209. $weighting = 0;
  210. } else {
  211. $weighting = -abs($answer_score);
  212. }
  213. }
  214. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
  215. }
  216. // saves the answers into the data base
  217. $objAnswer->save();
  218. // sets the total weighting of the question --> sert � donner le score total pendant l'examen
  219. $this->updateWeighting($questionWeighting);
  220. $this->save();
  221. }
  222. public function return_header(
  223. $feedback_type = null,
  224. $counter = null,
  225. $score = null
  226. ) {
  227. $header = parent::return_header($feedback_type, $counter, $score);
  228. $header .= '<table class="'.$this->question_table_class.'">
  229. <tr>
  230. <th>' . get_lang("Choice") . '</th>
  231. <th>' . get_lang("ExpectedChoice") . '</th>
  232. <th>' . get_lang("Answer") . '</th>';
  233. $header .= '<th>' . get_lang('Status') . '</th>';
  234. $header .= '<th>' . get_lang("Comment") . '</th>';
  235. $header .= '</tr>';
  236. return $header;
  237. }
  238. }