global_multiple_answer.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class GlobalMultipleAnswer
  5. */
  6. class GlobalMultipleAnswer extends Question
  7. {
  8. static $typePicture = 'mcmagl.png';
  9. static $explanationLangVar = 'GlobalMultipleAnswer';
  10. /**
  11. *
  12. */
  13. public function GlobalMultipleAnswer()
  14. {
  15. parent::question();
  16. $this->type = GLOBAL_MULTIPLE_ANSWER;
  17. $this->isContent = $this->getIsContent();
  18. }
  19. /**
  20. * function which redefines Question::createAnswersForm
  21. * @param the FormValidator
  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['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('label', get_lang('Answers') . '<br /> <img src="../img/fill_field.png">', $html);
  43. $defaults = array();
  44. $correct = 0;
  45. $answer = false;
  46. if (!empty($this->id)) {
  47. $answer = new Answer($this->id);
  48. $answer->read();
  49. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  50. $nb_answers = $answer->nbrAnswers;
  51. }
  52. }
  53. #le nombre de r�ponses est bien enregistr� sous la forme int(nb)
  54. /* Ajout mise en forme nb reponse */
  55. $form->addElement('hidden', 'nb_answers');
  56. $boxes_names = array();
  57. /* V�rification : Cr�action d'au moins une r�ponse */
  58. if ($nb_answers < 1) {
  59. $nb_answers = 1;
  60. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  61. }
  62. //D�but affichage score global dans la modification d'une question
  63. $scoreA = "0"; //par reponse
  64. $scoreG = "0"; //Global
  65. /* boucle pour sauvegarder les donn�es dans le tableau defaults */
  66. for ($i = 1; $i <= $nb_answers; ++$i) {
  67. /* si la reponse est de type objet */
  68. if (is_object($answer)) {
  69. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  70. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  71. $defaults['correct[' . $i . ']'] = $answer->correct[$i];
  72. //------------- D�but
  73. $scoreA = $answer->weighting[$i];
  74. }
  75. if ($scoreA > 0) {
  76. $scoreG = $scoreG + $scoreA;
  77. }
  78. //------------- Fin
  79. //------------- Debut si un des scores par reponse est egal � 0 : la coche vaut 1 (coch�)
  80. if ($scoreA == 0) {
  81. $defaults['pts'] = 1;
  82. }
  83. $renderer = & $form->defaultRenderer();
  84. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct['.$i.']');
  85. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  86. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  87. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  88. //$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  89. $answer_number = $form->addElement('text', 'counter[' . $i . ']', null, 'value="' . $i . '"');
  90. $answer_number->freeze();
  91. $form->addElement('checkbox', 'correct[' . $i . ']', null, null, 'class="checkbox"');
  92. $boxes_names[] = 'correct[' . $i . ']';
  93. $form->addElement('html_editor', 'answer[' . $i . ']', null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  94. $form->addRule('answer[' . $i . ']', get_lang('ThisFieldIsRequired'), 'required');
  95. $form->addElement('html_editor', 'comment[' . $i . ']', null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  96. $form->addElement('html', '</tr>');
  97. }
  98. //--------- Mise en variable du score global lors d'une modification de la question/r�ponse
  99. $defaults['weighting[1]'] = (round($scoreG));
  100. $form->addElement('html', '</div></div></table>');
  101. //$form -> addElement ('html', '<br />');
  102. $form->add_multiple_required_rule($boxes_names, get_lang('ChooseAtLeastOneCheckbox'), 'multiple_required');
  103. //only 1 answer the all deal ...
  104. $form->addElement('text', 'weighting[1]', get_lang('Score'));
  105. global $pts;
  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. $navigator_info = api_get_navigator();
  112. global $text, $class;
  113. //ie6 fix
  114. if ($obj_ex->edit_exercise_in_lp == true) {
  115. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  116. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'), 'class="minus"');
  117. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'), 'class="plus"');
  118. $form->addElement('submit', 'submitQuestion', $text, 'class="' . $class . '"');
  119. } else {
  120. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'), 'class="minus"');
  121. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'), 'class="plus"');
  122. $form->addElement('style_submit_button', 'submitQuestion', $text, 'class="' . $class . '"');
  123. // setting the save button here and not in the question class.php
  124. }
  125. }
  126. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  127. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  128. $renderer->setElementTemplate('{element}', 'moreAnswers');
  129. $form->addElement('html', '</div></div>');
  130. $defaults['correct'] = $correct;
  131. if (!empty($this->id)) {
  132. $form->setDefaults($defaults);
  133. } else {
  134. if ($this->isContent == 1) {
  135. $form->setDefaults($defaults);
  136. }
  137. }
  138. $form->setConstants(array('nb_answers' => $nb_answers));
  139. }
  140. /**
  141. * abstract function which creates the form to create / edit the answers of the question
  142. * @param the FormValidator instance
  143. */
  144. function processAnswersCreation($form)
  145. {
  146. $questionWeighting = $nbrGoodAnswers = 0;
  147. $objAnswer = new Answer($this->id);
  148. $nb_answers = $form->getSubmitValue('nb_answers');
  149. // Score total
  150. $answer_score = trim($form->getSubmitValue('weighting[1]'));
  151. // Reponses correctes
  152. $nbr_corrects = 0;
  153. for ($i = 1; $i <= $nb_answers; $i++) {
  154. $goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
  155. if ($goodAnswer) {
  156. $nbr_corrects++;
  157. }
  158. }
  159. // Set question weighting (score total)
  160. $questionWeighting = $answer_score;
  161. // Set score per answer
  162. $nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
  163. $answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
  164. $answer_score = ($answer_score / $nbr_corrects);
  165. //$answer_score �quivaut � la valeur d'une bonne r�ponse
  166. // cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
  167. $test = $form->getSubmitValue('pts');
  168. for ($i = 1; $i <= $nb_answers; $i++) {
  169. $answer = trim($form->getSubmitValue('answer[' . $i . ']'));
  170. $comment = trim($form->getSubmitValue('comment[' . $i . ']'));
  171. $goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
  172. if ($goodAnswer) {
  173. $weighting = abs($answer_score);
  174. } else {
  175. if ($test == 1) {
  176. $weighting = 0;
  177. } else {
  178. $weighting = -abs($answer_score);
  179. }
  180. }
  181. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
  182. }
  183. // saves the answers into the data base
  184. $objAnswer->save();
  185. // sets the total weighting of the question --> sert � donner le score total pendant l'examen
  186. $this->updateWeighting($questionWeighting);
  187. $this->save();
  188. }
  189. public function return_header(
  190. $feedback_type = null,
  191. $counter = null,
  192. $score = null
  193. ) {
  194. $header = parent::return_header($feedback_type, $counter, $score);
  195. $header .= '<table class="'.$this->question_table_class .'">
  196. <tr>
  197. <th>' . get_lang("Choice") . '</th>
  198. <th>' . get_lang("ExpectedChoice") . '</th>
  199. <th>' . get_lang("Answer") . '</th>';
  200. $header .= '<th>' . get_lang("Comment") . '</th>';
  201. $header .= '</tr>';
  202. return $header;
  203. }
  204. }