global_multiple_answer.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * Constructor
  13. */
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->type = GLOBAL_MULTIPLE_ANSWER;
  18. $this->isContent = $this->getIsContent();
  19. }
  20. /**
  21. * function which redefines Question::createAnswersForm
  22. * @param FormValidator $form
  23. */
  24. public function createAnswersForm($form)
  25. {
  26. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4;
  27. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  28. $obj_ex = Session::read('objExercise');
  29. /* Mise en variable de Affichage "Reponses" et son icone, "N�", "Vrai", "Reponse" */
  30. $html = '<table class="data_table">
  31. <tr>
  32. <th width="10px">
  33. ' . get_lang('Number') . '
  34. </th>
  35. <th width="10px">
  36. ' . get_lang('True') . '
  37. </th>
  38. <th width="50%">
  39. ' . get_lang('Answer') . '
  40. </th>';
  41. $html .='<th>' . get_lang('Comment') . '</th>';
  42. $html .='</tr>';
  43. $form->addElement('label', get_lang('Answers') . '<br /> '.Display::returnIconPath('fill_field.png'), $html);
  44. $defaults = array();
  45. $correct = 0;
  46. $answer = false;
  47. if (!empty($this->id)) {
  48. $answer = new Answer($this->id);
  49. $answer->read();
  50. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  51. $nb_answers = $answer->nbrAnswers;
  52. }
  53. }
  54. #le nombre de r�ponses est bien enregistr� sous la forme int(nb)
  55. /* Ajout mise en forme nb reponse */
  56. $form->addElement('hidden', 'nb_answers');
  57. $boxes_names = array();
  58. /* V�rification : Cr�action d'au moins une r�ponse */
  59. if ($nb_answers < 1) {
  60. $nb_answers = 1;
  61. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  62. }
  63. //D�but affichage score global dans la modification d'une question
  64. $scoreA = "0"; //par reponse
  65. $scoreG = "0"; //Global
  66. /* boucle pour sauvegarder les donn�es dans le tableau defaults */
  67. for ($i = 1; $i <= $nb_answers; ++$i) {
  68. /* si la reponse est de type objet */
  69. if (is_object($answer)) {
  70. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  71. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  72. $defaults['correct[' . $i . ']'] = $answer->correct[$i];
  73. // start
  74. $scoreA = $answer->weighting[$i];
  75. }
  76. if ($scoreA > 0) {
  77. $scoreG = $scoreG + $scoreA;
  78. }
  79. //------------- Fin
  80. //------------- Debut si un des scores par reponse est egal � 0 : la coche vaut 1 (coch�)
  81. if ($scoreA == 0) {
  82. $defaults['pts'] = 1;
  83. }
  84. $renderer = & $form->defaultRenderer();
  85. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct['.$i.']');
  86. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  87. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  88. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  89. //$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  90. $answer_number = $form->addElement('text', 'counter[' . $i . ']', null, 'value="' . $i . '"');
  91. $answer_number->freeze();
  92. $form->addElement('checkbox', 'correct[' . $i . ']', null, null, 'class="checkbox"');
  93. $boxes_names[] = 'correct[' . $i . ']';
  94. $form->addHtmlEditor('answer[' . $i . ']', null, null, array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  95. $form->addRule('answer[' . $i . ']', get_lang('ThisFieldIsRequired'), 'required');
  96. $form->addHtmlEditor('comment[' . $i . ']', null, null, array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  97. $form->addElement('html', '</tr>');
  98. }
  99. //--------- Mise en variable du score global lors d'une modification de la question/r�ponse
  100. $defaults['weighting[1]'] = (round($scoreG));
  101. $form->addElement('html', '</div></div></table>');
  102. //$form -> addElement ('html', '<br />');
  103. $form->add_multiple_required_rule($boxes_names, get_lang('ChooseAtLeastOneCheckbox'), 'multiple_required');
  104. //only 1 answer the all deal ...
  105. $form->addElement('text', 'weighting[1]', get_lang('Score'));
  106. //--------- Creation coche pour ne pas prendre en compte les n�gatifs
  107. $form->addElement('checkbox', 'pts', '', get_lang('NoNegativeScore'));
  108. $form->addElement('html', '<br />');
  109. // Affiche un message si le score n'est pas renseign�
  110. $form->addRule('weighting[1]', get_lang('ThisFieldIsRequired'), 'required');
  111. global $text, $class;
  112. if ($obj_ex->edit_exercise_in_lp == true) {
  113. $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers');
  114. $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers');
  115. $form->addButtonSave($text, 'submitQuestion');
  116. // setting the save button here and not in the question class.php
  117. }
  118. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  119. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  120. $renderer->setElementTemplate('{element}', 'moreAnswers');
  121. $form->addElement('html', '</div></div>');
  122. $defaults['correct'] = $correct;
  123. if (!empty($this->id)) {
  124. $form->setDefaults($defaults);
  125. } else {
  126. if ($this->isContent == 1) {
  127. $form->setDefaults($defaults);
  128. }
  129. }
  130. $form->setConstants(array('nb_answers' => $nb_answers));
  131. }
  132. /**
  133. * abstract function which creates the form to create / edit the answers of the question
  134. * @param FormValidator $form
  135. */
  136. function processAnswersCreation($form)
  137. {
  138. $questionWeighting = $nbrGoodAnswers = 0;
  139. $objAnswer = new Answer($this->id);
  140. $nb_answers = $form->getSubmitValue('nb_answers');
  141. // Score total
  142. $answer_score = trim($form->getSubmitValue('weighting[1]'));
  143. // Reponses correctes
  144. $nbr_corrects = 0;
  145. for ($i = 1; $i <= $nb_answers; $i++) {
  146. $goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
  147. if ($goodAnswer) {
  148. $nbr_corrects++;
  149. }
  150. }
  151. // Set question weighting (score total)
  152. $questionWeighting = $answer_score;
  153. // Set score per answer
  154. $nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
  155. $answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
  156. $answer_score = ($answer_score / $nbr_corrects);
  157. //$answer_score �quivaut � la valeur d'une bonne r�ponse
  158. // cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
  159. $test = $form->getSubmitValue('pts');
  160. for ($i = 1; $i <= $nb_answers; $i++) {
  161. $answer = trim($form->getSubmitValue('answer[' . $i . ']'));
  162. $comment = trim($form->getSubmitValue('comment[' . $i . ']'));
  163. $goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
  164. if ($goodAnswer) {
  165. $weighting = abs($answer_score);
  166. } else {
  167. if ($test == 1) {
  168. $weighting = 0;
  169. } else {
  170. $weighting = -abs($answer_score);
  171. }
  172. }
  173. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
  174. }
  175. // saves the answers into the data base
  176. $objAnswer->save();
  177. // sets the total weighting of the question --> sert � donner le score total pendant l'examen
  178. $this->updateWeighting($questionWeighting);
  179. $this->save();
  180. }
  181. public function return_header(
  182. $feedback_type = null,
  183. $counter = null,
  184. $score = null
  185. ) {
  186. $header = parent::return_header($feedback_type, $counter, $score);
  187. $header .= '<table class="'.$this->question_table_class .'">
  188. <tr>
  189. <th>' . get_lang("Choice") . '</th>
  190. <th>' . get_lang("ExpectedChoice") . '</th>
  191. <th>' . get_lang("Answer") . '</th>';
  192. $header .= '<th>' . get_lang("Comment") . '</th>';
  193. $header .= '</tr>';
  194. return $header;
  195. }
  196. }