global_multiple_answer.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. class GlobalMultipleAnswer extends Question
  4. {
  5. static $typePicture = 'mcmagl.gif';
  6. static $explanationLangVar = 'GlobalMultipleAnswer';
  7. /**
  8. *
  9. */
  10. public function GlobalMultipleAnswer($course_code = null)
  11. {
  12. parent::question($course_code);
  13. $this->type = GLOBAL_MULTIPLE_ANSWER;
  14. $this->isContent = $this->getIsContent();
  15. }
  16. /**
  17. * function which redefines Question::createAnswersForm
  18. * @param FormValidator instance
  19. */
  20. public function createAnswersForm($form)
  21. {
  22. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4;
  23. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  24. $obj_ex = $this->exercise;
  25. /* Mise en variable de Affichage "Reponses" et son icone, "N�", "Vrai", "Reponse" */
  26. $html = '<table class="data_table">
  27. <tr>
  28. <th width="10px">
  29. '.get_lang('Number').'
  30. </th>
  31. <th width="10px">
  32. '.get_lang('True').'
  33. </th>
  34. <th width="50%">
  35. '.get_lang('Answer').'
  36. </th>';
  37. // Espace entre l'entete et les r�ponses
  38. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  39. $html .= '<th>'.get_lang('Comment').'</th>';
  40. }
  41. $html .= '</tr>';
  42. $form->addElement('label', get_lang('Answers').'<br />'.Display::return_icon('fill_field.png'), $html);
  43. /* Initialiation variable */
  44. $defaults = array();
  45. $correct = 0;
  46. /* Mise en variable du nombre de reponse */
  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 (isset($answer) && is_object($answer)) {
  70. $answer_id = $answer->getRealAnswerIdFromList($i);
  71. $defaults['answer['.$i.']'] = $answer->answer[$answer_id];
  72. $defaults['comment['.$i.']'] = $answer->comment[$answer_id];
  73. $defaults['correct['.$i.']'] = $answer->correct[$answer_id];
  74. //------------- D�but
  75. $scoreA = $answer->weighting[$answer_id];
  76. }
  77. if ($scoreA > 0) {
  78. $scoreG = $scoreG + $scoreA;
  79. }
  80. //------------- Fin
  81. //------------- Debut si un des scores par reponse est egal � 0 : la coche vaut 1 (coch�)
  82. if ($scoreA == 0) {
  83. $defaults['pts'] = 1;
  84. } else {
  85. $defaults['pts'] = 0;
  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. //$renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  105. $answer_number = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"');
  106. $answer_number->freeze();
  107. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox"');
  108. $boxes_names[] = 'correct['.$i.']';
  109. $form->addElement(
  110. 'html_editor',
  111. 'answer['.$i.']',
  112. null,
  113. 'style="vertical-align:middle"',
  114. array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100')
  115. );
  116. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  117. // show comment when feedback is enable
  118. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  119. $form->addElement(
  120. 'html_editor',
  121. 'comment['.$i.']',
  122. null,
  123. 'style="vertical-align:middle"',
  124. array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100')
  125. );
  126. }
  127. $form->addElement('html', '</tr>');
  128. }
  129. //--------- Mise en variable du score global lors d'une modification de la question/r�ponse
  130. $defaults['weighting[1]'] = (round($scoreG));
  131. $form->addElement('html', '</div></div></table>');
  132. //$form -> addElement ('html', '<br />');
  133. $form->add_multiple_required_rule($boxes_names, get_lang('ChooseAtLeastOneCheckbox'), 'multiple_required');
  134. //only 1 answer the all deal ...
  135. $form->addElement('text', 'weighting[1]', get_lang('Score'));
  136. global $pts;
  137. //--------- Creation coche pour ne pas prendre en compte les n�gatifs
  138. $form->addElement('checkbox', 'pts', '', get_lang('NoNegativeScore'));
  139. $form->addElement('html', '<br />');
  140. // Affiche un message si le score n'est pas renseign�
  141. $form->addRule('weighting[1]', get_lang('ThisFieldIsRequired'), 'required');
  142. $navigator_info = api_get_navigator();
  143. //ie6 fix
  144. if ($obj_ex->edit_exercise_in_lp == true) {
  145. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  146. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'), 'class="minus"');
  147. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'), 'class="plus"');
  148. $form->addElement('submit', 'submitQuestion', $this->submitText, 'class="'.$this->submitClass.'"');
  149. } else {
  150. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'), 'class="minus"');
  151. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'), 'class="plus"');
  152. $form->addElement(
  153. 'style_submit_button',
  154. 'submitQuestion',
  155. $this->submitText,
  156. 'class="'.$this->submitClass.'"'
  157. );
  158. // setting the save button here and not in the question class.php
  159. }
  160. }
  161. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  162. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  163. $renderer->setElementTemplate('{element}', 'moreAnswers');
  164. $form->addElement('html', '</div></div>');
  165. $defaults['correct'] = $correct;
  166. if (!empty($this->id)) {
  167. $form->setDefaults($defaults);
  168. } else {
  169. if ($this->isContent == 1) {
  170. $form->setDefaults($defaults);
  171. }
  172. }
  173. $form->setConstants(array('nb_answers' => $nb_answers));
  174. }
  175. /**
  176. * abstract function which creates the form to create / edit the answers of the question
  177. * @param the formvalidator instance
  178. * @param the answers number to display
  179. */
  180. function processAnswersCreation($form)
  181. {
  182. $questionWeighting = $nbrGoodAnswers = 0;
  183. $objAnswer = new Answer($this->id);
  184. $nb_answers = $form->getSubmitValue('nb_answers');
  185. // Score total
  186. $answer_score = trim($form->getSubmitValue('weighting[1]'));
  187. // Reponses correctes
  188. $nbr_corrects = 0;
  189. for ($i = 1; $i <= $nb_answers; $i++) {
  190. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  191. if ($goodAnswer) {
  192. $nbr_corrects++;
  193. }
  194. }
  195. // Set question weighting (score total)
  196. $questionWeighting = $answer_score;
  197. // Set score per answer
  198. $nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
  199. $answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
  200. $answer_score = ($answer_score / $nbr_corrects);
  201. //$answer_score �quivaut � la valeur d'une bonne r�ponse
  202. // cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
  203. $test = $form->getSubmitValue('pts');
  204. for ($i = 1; $i <= $nb_answers; $i++) {
  205. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  206. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  207. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  208. if ($goodAnswer) {
  209. $weighting = abs($answer_score);
  210. } else {
  211. if ($test == 1) {
  212. $weighting = 0;
  213. } else {
  214. $weighting = -abs($answer_score);
  215. }
  216. }
  217. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
  218. }
  219. // saves the answers into the data base
  220. $objAnswer->save();
  221. // sets the total weighting of the question --> sert � donner le score total pendant l'examen
  222. $this->updateWeighting($questionWeighting);
  223. $this->save();
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false, $hideTitle = 0)
  229. {
  230. $header = parent::return_header($feedback_type, $counter, $score, $show_media, $hideTitle);
  231. $header .= '<table class="'.$this->question_table_class.'">
  232. <tr>
  233. <th>'.get_lang("Choice").'</th>
  234. <th>'.get_lang("ExpectedChoice").'</th>
  235. <th>'.get_lang("Answer").'</th>';
  236. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  237. $header .= '<th>'.get_lang("Comment").'</th>';
  238. } else {
  239. $header .= '<th>&nbsp;</th>';
  240. }
  241. $header .= '</tr>';
  242. return $header;
  243. }
  244. }