unique_answer.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php // $Id: uniquer_answer.class.php 10234 2006-12-26
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  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: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * File containing the UNIQUE_ANSWER class.
  22. *
  23. * @author Eric Marguin
  24. * @package dokeos.exercise
  25. ==============================================================================
  26. */
  27. if(!class_exists('UniqueAnswer')):
  28. /**
  29. CLASS UNIQUE_ANSWER
  30. *
  31. * This class allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  32. * extending the class question
  33. *
  34. * @author Eric Marguin
  35. * @package dokeos.exercise
  36. **/
  37. class UniqueAnswer extends Question {
  38. /**
  39. * Constructor
  40. */
  41. function UniqueAnswer(){
  42. parent::question();
  43. $this -> type = UNIQUE_ANSWER;
  44. }
  45. /**
  46. * function which redifines Question::createAnswersForm
  47. * @param the formvalidator instance
  48. * @param the answers number to display
  49. */
  50. function createAnswersForm ($form) {
  51. global $fck_attribute;
  52. $fck_attribute = array();
  53. $fck_attribute['Width'] = '300px';
  54. $fck_attribute['Height'] = '100px';
  55. $fck_attribute['ToolbarSet'] = 'Small';
  56. $fck_attribute['Config']['IMUploadPath'] = 'upload/test/';
  57. $fck_attribute['Config']['FlashUploadPath'] = 'upload/test/';
  58. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2;
  59. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  60. $html='
  61. <div class="row">
  62. <div class="label">
  63. '.get_lang('Answers').'
  64. </div>
  65. <div class="formw">
  66. <table cellpadding="0" cellspacing="5">
  67. <tr bgcolor="#e6e6e6">
  68. <td>
  69. '.get_lang('Number').'
  70. </td>
  71. <td>
  72. '.get_lang('True').'
  73. </td>
  74. <td>
  75. '.get_lang('Answer').'
  76. </td>
  77. <td>
  78. '.get_lang('Comment').'
  79. </td>
  80. <td>
  81. '.get_lang('Weighting').'
  82. </td>
  83. <td width="0"></td>
  84. </tr>';
  85. $form -> addElement ('html', $html);
  86. $defaults = array();
  87. $correct = 0;
  88. if(!empty($this -> id))
  89. {
  90. $answer = new Answer($this -> id);
  91. $answer -> read();
  92. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted())
  93. {
  94. $nb_answers = $answer->nbrAnswers;
  95. }
  96. }
  97. $form -> addElement('hidden', 'nb_answers');
  98. for($i = 1 ; $i <= $nb_answers ; ++$i)
  99. {
  100. if(is_object($answer))
  101. {
  102. if($answer -> correct[$i])
  103. {
  104. $correct = $i;
  105. }
  106. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  107. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  108. $defaults['weighting['.$i.']'] = $answer -> weighting[$i];
  109. }
  110. $form -> addElement ('html', '<tr><td>');
  111. $group = array();
  112. $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"');
  113. $puce->freeze();
  114. $group[] = $puce;
  115. $group[] = FormValidator :: createElement ('radio', 'correct', null, null, $i);
  116. $group[] = FormValidator :: createElement ('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"');
  117. $group[] = FormValidator :: createElement ('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
  118. $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle" size="5" value="0"');
  119. $form -> addGroup($group, null, null, '</td><td width="0">');
  120. $form -> addElement ('html', '</td></tr>');
  121. }
  122. $form -> addElement ('html', '</table></div></div>');
  123. $group = array();
  124. $group[] = FormValidator :: createElement ('submit', 'lessAnswers', '-answ');
  125. $group[] = FormValidator :: createElement ('submit', 'moreAnswers', '+answ');
  126. $form -> addGroup($group);
  127. $defaults['correct'] = $correct;
  128. $form -> setDefaults($defaults);
  129. $form->setConstants(array('nb_answers' => $nb_answers));
  130. }
  131. /**
  132. * abstract function which creates the form to create / edit the answers of the question
  133. * @param the formvalidator instance
  134. * @param the answers number to display
  135. */
  136. function processAnswersCreation($form) {
  137. $questionWeighting = $nbrGoodAnswers = 0;
  138. $correct = $form -> getSubmitValue('correct');
  139. $objAnswer = new Answer($this->id);
  140. $nb_answers = $form -> getSubmitValue('nb_answers');
  141. for($i=1 ; $i <= $nb_answers ; $i++)
  142. {
  143. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  144. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  145. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  146. $goodAnswer= ($correct == $i) ? true : false;
  147. if($goodAnswer)
  148. {
  149. $nbrGoodAnswers++;
  150. $weighting = abs($weighting);
  151. if($weighting > 0)
  152. {
  153. $questionWeighting += $weighting;
  154. }
  155. }
  156. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  157. }
  158. // saves the answers into the data base
  159. $objAnswer -> save();
  160. // sets the total weighting of the question
  161. $this -> updateWeighting($questionWeighting);
  162. $this -> save();
  163. echo '<script type="text/javascript">window.location.href="admin.php"</script>';
  164. }
  165. }
  166. endif;
  167. ?>