multiple_answer.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  15. */
  16. /**
  17. * File containing the MultipleAnswer class.
  18. * @package dokeos.exercise
  19. * @author Eric Marguin
  20. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  21. */
  22. if(!class_exists('MultipleAnswer')):
  23. /**
  24. CLASS MultipleAnswer
  25. *
  26. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  27. * extending the class question
  28. *
  29. * @author Eric Marguin
  30. * @package dokeos.exercise
  31. **/
  32. class MultipleAnswer extends Question {
  33. static $typePicture = 'mcma.gif';
  34. static $explanationLangVar = 'MultipleSelect';
  35. /**
  36. * Constructor
  37. */
  38. function MultipleAnswer(){
  39. parent::question();
  40. $this -> type = MULTIPLE_ANSWER;
  41. }
  42. /**
  43. * function which redifines Question::createAnswersForm
  44. * @param the formvalidator instance
  45. * @param the answers number to display
  46. */
  47. function createAnswersForm ($form) {
  48. global $fck_attribute;
  49. $fck_attribute = array();
  50. $fck_attribute['Width'] = '348px';
  51. $fck_attribute['Height'] = '100px';
  52. $fck_attribute['ToolbarSet'] = 'Test';
  53. $fck_attribute['Config']['IMUploadPath'] = 'upload/test/';
  54. $fck_attribute['Config']['FlashUploadPath'] = 'upload/test/';
  55. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2;
  56. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  57. $html='
  58. <div class="row">
  59. <div class="label">
  60. '.get_lang('Answers').'
  61. </div>
  62. <div class="formw">
  63. <table class="data_table">
  64. <tr style="text-align: center;">
  65. <th>
  66. '.get_lang('Number').'
  67. </th>
  68. <th>
  69. '.get_lang('True').'
  70. </th>
  71. <th>
  72. '.get_lang('Answer').'
  73. </th>
  74. <th>
  75. '.get_lang('Comment').'
  76. </th>
  77. <th>
  78. '.get_lang('Weighting').'
  79. </th>
  80. </tr>';
  81. $form -> addElement ('html', $html);
  82. $defaults = array();
  83. $correct = 0;
  84. if(!empty($this -> id))
  85. {
  86. $answer = new Answer($this -> id);
  87. $answer -> read();
  88. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted())
  89. {
  90. $nb_answers = $answer->nbrAnswers;
  91. }
  92. }
  93. $form -> addElement('hidden', 'nb_answers');
  94. $boxes_names = array();
  95. for($i = 1 ; $i <= $nb_answers ; ++$i)
  96. {
  97. if(is_object($answer))
  98. {
  99. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  100. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  101. $defaults['weighting['.$i.']'] = $answer -> weighting[$i];
  102. $defaults['correct['.$i.']'] = $answer -> correct[$i];
  103. }
  104. $renderer = & $form->defaultRenderer();
  105. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  106. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  107. $answer_number->freeze();
  108. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'style="margin-left: 0em;"');
  109. $boxes_names[] = 'correct['.$i.']';
  110. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"');
  111. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  112. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
  113. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="0"');
  114. $form -> addElement ('html', '</tr>');
  115. }
  116. $form -> addElement ('html', '</table>');
  117. $form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  118. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'));
  119. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'));
  120. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  121. $renderer->setElementTemplate('{element}','moreAnswers');
  122. $form -> addElement ('html', '</div></div>');
  123. $defaults['correct'] = $correct;
  124. $form -> setDefaults($defaults);
  125. $form->setConstants(array('nb_answers' => $nb_answers));
  126. }
  127. /**
  128. * abstract function which creates the form to create / edit the answers of the question
  129. * @param the formvalidator instance
  130. * @param the answers number to display
  131. */
  132. function processAnswersCreation($form) {
  133. $questionWeighting = $nbrGoodAnswers = 0;
  134. $objAnswer = new Answer($this->id);
  135. $nb_answers = $form -> getSubmitValue('nb_answers');
  136. for($i=1 ; $i <= $nb_answers ; $i++)
  137. {
  138. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  139. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  140. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  141. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  142. if($goodAnswer){
  143. $weighting = abs($weighting);
  144. }
  145. else{
  146. $weighting = abs($weighting);
  147. $weighting = -$weighting;
  148. }
  149. if($weighting > 0)
  150. {
  151. $questionWeighting += $weighting;
  152. }
  153. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  154. }
  155. // saves the answers into the data base
  156. $objAnswer -> save();
  157. // sets the total weighting of the question
  158. $this -> updateWeighting($questionWeighting);
  159. $this -> save();
  160. }
  161. }
  162. endif;
  163. ?>