global_multiple_answer.class.php 12 KB

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