multiple_answer.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php // $Id: document.php 16494 2008-10-10 22:07:36Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * File containing the MultipleAnswer class.
  22. * @package dokeos.exercise
  23. * @author Eric Marguin
  24. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  25. */
  26. if(!class_exists('MultipleAnswer')):
  27. /**
  28. CLASS MultipleAnswer
  29. *
  30. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  31. * extending the class question
  32. *
  33. * @author Eric Marguin
  34. * @package dokeos.exercise
  35. **/
  36. class MultipleAnswer extends Question {
  37. static $typePicture = 'mcma.gif';
  38. static $explanationLangVar = 'MultipleSelect';
  39. /**
  40. * Constructor
  41. */
  42. function MultipleAnswer(){
  43. parent::question();
  44. $this -> type = MULTIPLE_ANSWER;
  45. }
  46. /**
  47. * function which redifines Question::createAnswersForm
  48. * @param the formvalidator instance
  49. * @param the answers number to display
  50. */
  51. function createAnswersForm ($form) {
  52. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2;
  53. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  54. $html='
  55. <div class="row">
  56. <div class="label">
  57. '.get_lang('Answers').'<br /><img src="../img/fill_field.png">
  58. </div>
  59. <div class="formw">
  60. <table class="data_table">
  61. <tr style="text-align: center;">
  62. <th>
  63. '.get_lang('Number').'
  64. </th>
  65. <th>
  66. '.get_lang('True').'
  67. </th>
  68. <th>
  69. '.get_lang('Answer').'
  70. </th>
  71. <th>
  72. '.get_lang('Comment').'
  73. </th>
  74. <th>
  75. '.get_lang('Weighting').'
  76. </th>
  77. </tr>';
  78. $form -> addElement ('html', $html);
  79. $defaults = array();
  80. $correct = 0;
  81. if(!empty($this -> id)) {
  82. $answer = new Answer($this -> id);
  83. $answer -> read();
  84. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  85. $nb_answers = $answer->nbrAnswers;
  86. }
  87. }
  88. $form -> addElement('hidden', 'nb_answers');
  89. $boxes_names = array();
  90. for($i = 1 ; $i <= $nb_answers ; ++$i) {
  91. if(is_object($answer)) {
  92. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  93. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  94. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  95. $defaults['correct['.$i.']'] = $answer -> correct[$i];
  96. } else {
  97. $defaults['answer[1]'] = get_lang('langDefaultMultipleAnswer2');
  98. $defaults['comment[1]'] = get_lang('langDefaultMultipleComment2');
  99. $defaults['correct[1]'] = true;
  100. $defaults['weighting[1]'] = 10;
  101. $defaults['answer[2]'] = get_lang('langDefaultMultipleAnswer1');
  102. $defaults['comment[2]'] = get_lang('langDefaultMultipleComment1');
  103. $defaults['correct[2]'] = false;
  104. $defaults['weighting[2]'] = -5;
  105. }
  106. $renderer = & $form->defaultRenderer();
  107. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  108. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  109. $answer_number->freeze();
  110. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox" style="margin-left: 0em;"');
  111. $boxes_names[] = 'correct['.$i.']';
  112. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  113. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  114. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  115. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  116. $form -> addElement ('html', '</tr>');
  117. }
  118. $form -> addElement ('html', '</table>');
  119. $form -> addElement ('html', '<br />');
  120. $form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  121. $navigator_info = api_get_navigator();
  122. global $text, $class;
  123. //ie6 fix
  124. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  125. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  126. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  127. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  128. } else {
  129. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  130. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  131. // setting the save button here and not in the question class.php
  132. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  133. }
  134. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  135. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  136. $renderer->setElementTemplate('{element}','moreAnswers');
  137. $form -> addElement ('html', '</div></div>');
  138. $defaults['correct'] = $correct;
  139. $form -> setDefaults($defaults);
  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. for($i=1 ; $i <= $nb_answers ; $i++)
  152. {
  153. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  154. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  155. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  156. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  157. if($goodAnswer){
  158. $weighting = abs($weighting);
  159. } else {
  160. $weighting = abs($weighting);
  161. $weighting = -$weighting;
  162. }
  163. if($weighting > 0)
  164. {
  165. $questionWeighting += $weighting;
  166. }
  167. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  168. }
  169. // saves the answers into the data base
  170. $objAnswer -> save();
  171. // sets the total weighting of the question
  172. $this -> updateWeighting($questionWeighting);
  173. $this -> save();
  174. }
  175. }
  176. endif;
  177. ?>