unique_answer.class.php 5.8 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 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'] = '348px';
  50. $fck_attribute['Height'] = '100px';
  51. $fck_attribute['ToolbarSet'] = 'Test';
  52. $fck_attribute['Config']['IMUploadPath'] = 'upload/test/';
  53. $fck_attribute['Config']['FlashUploadPath'] = 'upload/test/';
  54. $fck_attribute['Config']['InDocument'] = false;
  55. $fck_attribute['Config']['CreateDocumentDir'] = '../../courses/'.api_get_course_path().'/document/';
  56. //this line define how many question by default appear when creating a choice question
  57. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  58. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  59. $html='
  60. <div class="row">
  61. <div class="label">
  62. '.get_lang('Answers').'
  63. </div>
  64. <div class="formw">
  65. <table class="data_table">
  66. <tr style="text-align: center;">
  67. <th>
  68. '.get_lang('Number').'
  69. </th>
  70. <th>
  71. '.get_lang('True').'
  72. </th>
  73. <th>
  74. '.get_lang('Answer').'
  75. </th>
  76. <th>
  77. '.get_lang('Comment').'
  78. </th>
  79. <th>
  80. '.get_lang('Weighting').'
  81. </th>
  82. </tr>';
  83. $form -> addElement ('html', $html);
  84. $defaults = array();
  85. $correct = 0;
  86. if(!empty($this -> id))
  87. {
  88. $answer = new Answer($this -> id);
  89. $answer -> read();
  90. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted())
  91. {
  92. $nb_answers = $answer->nbrAnswers;
  93. }
  94. }
  95. $form -> addElement('hidden', 'nb_answers');
  96. for($i = 1 ; $i <= $nb_answers ; ++$i)
  97. {
  98. $form -> addElement ('html', '<tr>');
  99. if(is_object($answer))
  100. {
  101. if($answer -> correct[$i])
  102. {
  103. $correct = $i;
  104. }
  105. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  106. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  107. $defaults['weighting['.$i.']'] = $answer -> weighting[$i];
  108. }
  109. $renderer = & $form->defaultRenderer();
  110. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  111. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  112. $answer_number->freeze();
  113. $form->addElement('radio', 'correct', null, null, $i, 'style="margin-left: 0em;"');
  114. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"');
  115. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  116. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
  117. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="0"');
  118. $form -> addElement ('html', '</tr>');
  119. }
  120. $form -> addElement ('html', '</table>');
  121. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'));
  122. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'));
  123. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  124. $renderer->setElementTemplate('{element}','moreAnswers');
  125. $form -> addElement ('html', '</div></div>');
  126. //We check the first radio button to be sure a radio button will be check
  127. if($correct==0){
  128. $correct=1;
  129. }
  130. $defaults['correct'] = $correct;
  131. $form -> setDefaults($defaults);
  132. $form->setConstants(array('nb_answers' => $nb_answers));
  133. }
  134. /**
  135. * abstract function which creates the form to create / edit the answers of the question
  136. * @param the formvalidator instance
  137. * @param the answers number to display
  138. */
  139. function processAnswersCreation($form) {
  140. $questionWeighting = $nbrGoodAnswers = 0;
  141. $correct = $form -> getSubmitValue('correct');
  142. $objAnswer = new Answer($this->id);
  143. $nb_answers = $form -> getSubmitValue('nb_answers');
  144. for($i=1 ; $i <= $nb_answers ; $i++)
  145. {
  146. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  147. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  148. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  149. $goodAnswer= ($correct == $i) ? true : false;
  150. if($goodAnswer)
  151. {
  152. $nbrGoodAnswers++;
  153. $weighting = abs($weighting);
  154. if($weighting > 0)
  155. {
  156. $questionWeighting += $weighting;
  157. }
  158. }
  159. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  160. }
  161. // saves the answers into the data base
  162. $objAnswer -> save();
  163. // sets the total weighting of the question
  164. $this -> updateWeighting($questionWeighting);
  165. $this -> save();
  166. }
  167. }
  168. endif;
  169. ?>