multiple_answer.class.php 7.1 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. global $fck_attribute;
  53. $fck_attribute = array();
  54. //$fck_attribute['Width'] = '348px';
  55. $fck_attribute['Width'] = '100%';
  56. $fck_attribute['Height'] = '100px';
  57. $fck_attribute['ToolbarSet'] = 'Answer';
  58. $fck_attribute['Config']['ToolbarStartExpanded']='false';
  59. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2;
  60. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  61. $html='
  62. <div class="row">
  63. <div class="label">
  64. '.get_lang('Answers').'<br /><img src="../img/fill_field.png">
  65. </div>
  66. <div class="formw">
  67. <table class="data_table">
  68. <tr style="text-align: center;">
  69. <th>
  70. '.get_lang('Number').'
  71. </th>
  72. <th>
  73. '.get_lang('True').'
  74. </th>
  75. <th>
  76. '.get_lang('Answer').'
  77. </th>
  78. <th>
  79. '.get_lang('Comment').'
  80. </th>
  81. <th>
  82. '.get_lang('Weighting').'
  83. </th>
  84. </tr>';
  85. $form -> addElement ('html', $html);
  86. $defaults = array();
  87. $correct = 0;
  88. if(!empty($this -> id)) {
  89. $answer = new Answer($this -> id);
  90. $answer -> read();
  91. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  92. $nb_answers = $answer->nbrAnswers;
  93. }
  94. }
  95. $form -> addElement('hidden', 'nb_answers');
  96. $boxes_names = array();
  97. for($i = 1 ; $i <= $nb_answers ; ++$i) {
  98. if(is_object($answer)) {
  99. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  100. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  101. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  102. $defaults['correct['.$i.']'] = $answer -> correct[$i];
  103. } else {
  104. $defaults['answer[1]'] = get_lang('langDefaultMultipleAnswer1');
  105. $defaults['comment[1]'] = get_lang('langDefaultMultipleComment1');
  106. $defaults['correct[1]'] = true;
  107. $defaults['weighting[1]'] = 10;
  108. $defaults['answer[2]'] = get_lang('langDefaultMultipleAnswer2');
  109. $defaults['comment[2]'] = get_lang('langDefaultMultipleComment2');
  110. $defaults['correct[2]'] = false;
  111. $defaults['weighting[2]'] = -5;
  112. }
  113. $renderer = & $form->defaultRenderer();
  114. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  115. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  116. $answer_number->freeze();
  117. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox" style="margin-left: 0em;"');
  118. $boxes_names[] = 'correct['.$i.']';
  119. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"');
  120. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  121. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
  122. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  123. $form -> addElement ('html', '</tr>');
  124. }
  125. $form -> addElement ('html', '</table>');
  126. $form -> addElement ('html', '<br />');
  127. $form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  128. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  129. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  130. global $text, $class;
  131. // setting the save button here and not in the question class.php
  132. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  133. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  134. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  135. $renderer->setElementTemplate('{element}','moreAnswers');
  136. $form -> addElement ('html', '</div></div>');
  137. $defaults['correct'] = $correct;
  138. $form -> setDefaults($defaults);
  139. $form->setConstants(array('nb_answers' => $nb_answers));
  140. }
  141. /**
  142. * abstract function which creates the form to create / edit the answers of the question
  143. * @param the formvalidator instance
  144. * @param the answers number to display
  145. */
  146. function processAnswersCreation($form) {
  147. $questionWeighting = $nbrGoodAnswers = 0;
  148. $objAnswer = new Answer($this->id);
  149. $nb_answers = $form -> getSubmitValue('nb_answers');
  150. for($i=1 ; $i <= $nb_answers ; $i++)
  151. {
  152. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  153. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  154. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  155. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  156. if($goodAnswer){
  157. $weighting = abs($weighting);
  158. } else {
  159. $weighting = abs($weighting);
  160. $weighting = -$weighting;
  161. }
  162. if($weighting > 0)
  163. {
  164. $questionWeighting += $weighting;
  165. }
  166. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  167. }
  168. // saves the answers into the data base
  169. $objAnswer -> save();
  170. // sets the total weighting of the question
  171. $this -> updateWeighting($questionWeighting);
  172. $this -> save();
  173. }
  174. }
  175. endif;
  176. ?>