unique_answer.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 UNIQUE_ANSWER 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('UniqueAnswer')):
  23. /**
  24. CLASS UNIQUE_ANSWER
  25. *
  26. * This class allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  27. * extending the class question
  28. *
  29. * @author Eric Marguin
  30. * @package dokeos.exercise
  31. **/
  32. class UniqueAnswer extends Question {
  33. static $typePicture = 'mcua.gif';
  34. static $explanationLangVar = 'UniqueSelect';
  35. /**
  36. * Constructor
  37. */
  38. function UniqueAnswer(){
  39. $this -> type = UNIQUE_ANSWER;
  40. }
  41. /**
  42. * function which redifines Question::createAnswersForm
  43. * @param the formvalidator instance
  44. * @param the answers number to display
  45. */
  46. function createAnswersForm ($form) {
  47. global $fck_attribute;
  48. $fck_attribute = array();
  49. $fck_attribute['Width'] = '300px';
  50. $fck_attribute['Height'] = '100px';
  51. $fck_attribute['ToolbarSet'] = 'Small';
  52. $fck_attribute['Config']['IMUploadPath'] = 'upload/test/';
  53. $fck_attribute['Config']['FlashUploadPath'] = 'upload/test/';
  54. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2;
  55. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  56. $html='
  57. <div class="row">
  58. <div class="label">
  59. '.get_lang('Answers').'
  60. </div>
  61. <div class="formw">
  62. <table cellpadding="0" cellspacing="5">
  63. <tr bgcolor="#e6e6e6">
  64. <td>
  65. '.get_lang('Number').'
  66. </td>
  67. <td>
  68. '.get_lang('True').'
  69. </td>
  70. <td>
  71. '.get_lang('Answer').'
  72. </td>
  73. <td>
  74. '.get_lang('Comment').'
  75. </td>
  76. <td>
  77. '.get_lang('Weighting').'
  78. </td>
  79. <td width="0"></td>
  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. for($i = 1 ; $i <= $nb_answers ; ++$i)
  95. {
  96. if(is_object($answer))
  97. {
  98. if($answer -> correct[$i])
  99. {
  100. $correct = $i;
  101. }
  102. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  103. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  104. $defaults['weighting['.$i.']'] = $answer -> weighting[$i];
  105. }
  106. $form -> addElement ('html', '<tr><td>');
  107. $group = array();
  108. $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"');
  109. $puce->freeze();
  110. $group[] = $puce;
  111. $group[] = FormValidator :: createElement ('radio', 'correct', null, null, $i);
  112. $group[] = FormValidator :: createElement ('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"');
  113. $group[] = FormValidator :: createElement ('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
  114. $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle" size="5" value="0"');
  115. $form -> addGroup($group, null, null, '</td><td width="0">');
  116. $form -> addElement ('html', '</td></tr>');
  117. }
  118. $form -> addElement ('html', '</table></div></div>');
  119. $group = array();
  120. $group[] = FormValidator :: createElement ('submit', 'lessAnswers', get_lang('LessAnswer'));
  121. $group[] = FormValidator :: createElement ('submit', 'moreAnswers', get_lang('PlusAnswer'));
  122. $form -> addGroup($group);
  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. $correct = $form -> getSubmitValue('correct');
  135. $objAnswer = new Answer($this->id);
  136. $nb_answers = $form -> getSubmitValue('nb_answers');
  137. for($i=1 ; $i <= $nb_answers ; $i++)
  138. {
  139. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  140. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  141. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  142. $goodAnswer= ($correct == $i) ? true : false;
  143. if($goodAnswer)
  144. {
  145. $nbrGoodAnswers++;
  146. $weighting = abs($weighting);
  147. if($weighting > 0)
  148. {
  149. $questionWeighting += $weighting;
  150. }
  151. }
  152. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  153. }
  154. // saves the answers into the data base
  155. $objAnswer -> save();
  156. // sets the total weighting of the question
  157. $this -> updateWeighting($questionWeighting);
  158. $this -> save();
  159. }
  160. }
  161. endif;
  162. ?>