global_multiple_answer.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /* V�rification : Cr�action d'au moins une r�ponse */
  63. if ($nb_answers < 1) {
  64. $nb_answers = 1;
  65. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  66. }
  67. //D�but affichage score global dans la modification d'une question
  68. $scoreA = "0"; //par reponse
  69. $scoreG = "0"; //Global
  70. /* boucle pour sauvegarder les donn�es dans le tableau defaults */
  71. for ($i = 1; $i <= $nb_answers; ++$i) {
  72. /* si la reponse est de type objet */
  73. if (is_object($answer)) {
  74. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  75. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  76. $defaults['correct[' . $i . ']'] = $answer->correct[$i];
  77. // start
  78. $scoreA = $answer->weighting[$i];
  79. }
  80. if ($scoreA > 0) {
  81. $scoreG = $scoreG + $scoreA;
  82. }
  83. //------------- Fin
  84. //------------- Debut si un des scores par reponse est egal � 0 : la coche vaut 1 (coch�)
  85. if ($scoreA == 0) {
  86. $defaults['pts'] = 1;
  87. }
  88. $renderer = & $form->defaultRenderer();
  89. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct['.$i.']');
  90. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  91. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  92. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  93. //$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  94. $answer_number = $form->addElement(
  95. 'text',
  96. 'counter['.$i.']',
  97. null,
  98. 'value="'.$i.'"'
  99. );
  100. $answer_number->freeze();
  101. $form->addElement('checkbox', 'correct[' . $i . ']', null, null, 'class="checkbox"');
  102. $boxes_names[] = 'correct[' . $i . ']';
  103. $form->addElement(
  104. 'html_editor',
  105. 'answer['.$i.']',
  106. null,
  107. array(),
  108. array(
  109. 'ToolbarSet' => 'TestProposedAnswer',
  110. 'Width' => '100%',
  111. 'Height' => '100',
  112. )
  113. );
  114. $form->addRule('answer[' . $i . ']', get_lang('ThisFieldIsRequired'), 'required');
  115. $form->addElement(
  116. 'html_editor',
  117. 'comment['.$i.']',
  118. null,
  119. array(),
  120. array(
  121. 'ToolbarSet' => 'TestProposedAnswer',
  122. 'Width' => '100%',
  123. 'Height' => '100',
  124. )
  125. );
  126. $form->addElement('html', '</tr>');
  127. }
  128. //--------- Mise en variable du score global lors d'une modification de la question/r�ponse
  129. $defaults['weighting[1]'] = (round($scoreG));
  130. $form->addElement('html', '</div></div></table>');
  131. $form->add_multiple_required_rule(
  132. $boxes_names,
  133. get_lang('ChooseAtLeastOneCheckbox'),
  134. 'multiple_required'
  135. );
  136. //only 1 answer the all deal ...
  137. $form->addElement('text', 'weighting[1]', get_lang('Score'));
  138. //--------- Creation coche pour ne pas prendre en compte les n�gatifs
  139. $form->addElement('checkbox', 'pts', '', get_lang('NoNegativeScore'));
  140. $form->addElement('html', '<br />');
  141. // Affiche un message si le score n'est pas renseign�
  142. $form->addRule('weighting[1]', get_lang('ThisFieldIsRequired'), 'required');
  143. global $text;
  144. if ($obj_ex->edit_exercise_in_lp == true) {
  145. $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers');
  146. $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers');
  147. $form->addButtonSave($text, 'submitQuestion');
  148. // setting the save button here and not in the question class.php
  149. }
  150. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  151. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  152. $renderer->setElementTemplate('{element}', 'moreAnswers');
  153. $form->addElement('html', '</div></div>');
  154. $defaults['correct'] = $correct;
  155. if (!empty($this->id)) {
  156. $form->setDefaults($defaults);
  157. } else {
  158. if ($this->isContent == 1) {
  159. $form->setDefaults($defaults);
  160. }
  161. }
  162. $form->setConstants(array('nb_answers' => $nb_answers));
  163. }
  164. /**
  165. * abstract function which creates the form to create / edit the answers of the question
  166. * @param FormValidator $form
  167. */
  168. function processAnswersCreation($form)
  169. {
  170. $questionWeighting = $nbrGoodAnswers = 0;
  171. $objAnswer = new Answer($this->id);
  172. $nb_answers = $form->getSubmitValue('nb_answers');
  173. // Score total
  174. $answer_score = trim($form->getSubmitValue('weighting[1]'));
  175. // Reponses correctes
  176. $nbr_corrects = 0;
  177. for ($i = 1; $i <= $nb_answers; $i++) {
  178. $goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
  179. if ($goodAnswer) {
  180. $nbr_corrects++;
  181. }
  182. }
  183. // Set question weighting (score total)
  184. $questionWeighting = $answer_score;
  185. // Set score per answer
  186. $nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
  187. $answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
  188. $answer_score = ($answer_score / $nbr_corrects);
  189. //$answer_score �quivaut � la valeur d'une bonne r�ponse
  190. // cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
  191. $test = $form->getSubmitValue('pts');
  192. for ($i = 1; $i <= $nb_answers; $i++) {
  193. $answer = trim($form->getSubmitValue('answer[' . $i . ']'));
  194. $comment = trim($form->getSubmitValue('comment[' . $i . ']'));
  195. $goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
  196. if ($goodAnswer) {
  197. $weighting = abs($answer_score);
  198. } else {
  199. if ($test == 1) {
  200. $weighting = 0;
  201. } else {
  202. $weighting = -abs($answer_score);
  203. }
  204. }
  205. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
  206. }
  207. // saves the answers into the data base
  208. $objAnswer->save();
  209. // sets the total weighting of the question --> sert � donner le score total pendant l'examen
  210. $this->updateWeighting($questionWeighting);
  211. $this->save();
  212. }
  213. public function return_header(
  214. $feedback_type = null,
  215. $counter = null,
  216. $score = null
  217. ) {
  218. $header = parent::return_header($feedback_type, $counter, $score);
  219. $header .= '<table class="'.$this->question_table_class .'">
  220. <tr>
  221. <th>' . get_lang("Choice") . '</th>
  222. <th>' . get_lang("ExpectedChoice") . '</th>
  223. <th>' . get_lang("Answer") . '</th>';
  224. $header .= '<th>' . get_lang("Comment") . '</th>';
  225. $header .= '</tr>';
  226. return $header;
  227. }
  228. }