matching.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Matching 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('Matching')):
  23. /**
  24. CLASS Matching
  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 Matching extends Question {
  33. static $typePicture = 'matching.gif';
  34. static $explanationLangVar = 'Matching';
  35. /**
  36. * Constructor
  37. */
  38. function Matching(){
  39. parent::question();
  40. $this -> type = MATCHING;
  41. }
  42. /**
  43. * function which redifines Question::createAnswersForm
  44. * @param the formvalidator instance
  45. */
  46. function createAnswersForm ($form) {
  47. $defaults = array();
  48. $nb_matches = $nb_options = 2;
  49. if($form -> isSubmitted())
  50. {
  51. $nb_matches = $form -> getSubmitValue('nb_matches');
  52. $nb_options = $form -> getSubmitValue('nb_options');
  53. if(isset($_POST['lessMatches']))
  54. $nb_matches--;
  55. if(isset($_POST['moreMatches']))
  56. $nb_matches++;
  57. if(isset($_POST['lessOptions']))
  58. $nb_options--;
  59. if(isset($_POST['moreOptions']))
  60. $nb_options++;
  61. }
  62. else if(!empty($this -> id))
  63. {
  64. $answer = new Answer($this -> id);
  65. $answer -> read();
  66. if(count($answer->nbrAnswers)>0)
  67. {
  68. $a_matches = $a_options = array();
  69. $nb_matches = $nb_options = 0;
  70. for($i=1 ; $i<=$answer->nbrAnswers ; $i++){
  71. if($answer -> isCorrect($i))
  72. {
  73. $nb_matches++;
  74. $defaults['answer['.$nb_matches.']'] = $answer -> selectAnswer($i);
  75. $defaults['weighting['.$nb_matches.']'] = $answer -> selectWeighting($i);
  76. $defaults['matches['.$nb_matches.']'] = $answer -> correct[$i];
  77. }
  78. else
  79. {
  80. $nb_options++;
  81. $defaults['option['.$nb_options.']'] = $answer -> selectAnswer($i);
  82. }
  83. }
  84. }
  85. }
  86. else {
  87. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  88. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  89. $defaults['matches[2]'] = '2';
  90. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  91. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  92. }
  93. $a_matches = array();
  94. for($i=1 ; $i<=$nb_options ; ++$i)
  95. {
  96. $a_matches[$i] = chr(64+$i); // fill the array with A, B, C.....
  97. }
  98. $form -> addElement('hidden', 'nb_matches', $nb_matches);
  99. $form -> addElement('hidden', 'nb_options', $nb_options);
  100. ////////////////////////
  101. // DISPLAY MATCHES ////
  102. //////////////////////
  103. $html='
  104. <div class="row">
  105. <div class="label">
  106. '.get_lang('Answers').'
  107. </div>
  108. <div class="formw">
  109. '.get_lang('MakeCorrespond').'
  110. <table class="data_table">
  111. <tr style="text-align: center">
  112. <th>
  113. '.get_lang('Number').'
  114. </th>
  115. <th>
  116. '.get_lang('Answer').'
  117. </th>
  118. <th>
  119. '.get_lang('MatchesTo').'
  120. </th>
  121. <th>
  122. '.get_lang('Weighting').'
  123. </th>
  124. </tr>';
  125. $form -> addElement ('html', $html);
  126. for($i = 1 ; $i <= $nb_matches ; ++$i)
  127. {
  128. $form -> addElement ('html', '<tr><td>');
  129. $group = array();
  130. $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"');
  131. $puce->freeze();
  132. $group[] = $puce;
  133. $group[] = FormValidator :: createElement ('text', 'answer['.$i.']',null, 'size="30" style="margin-left: 0em;"');
  134. $group[] = FormValidator :: createElement ('select', 'matches['.$i.']',null,$a_matches);
  135. $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="2" value="10"');
  136. $form -> addGroup($group, null, null, '</td><td width="0">');
  137. $form -> addElement ('html', '</td></tr>');
  138. }
  139. $form -> addElement ('html', '</table></div></div>');
  140. $group = array();
  141. $group[] = FormValidator :: createElement ('submit', 'lessMatches', get_lang('DelElem'));
  142. $group[] = FormValidator :: createElement ('submit', 'moreMatches', get_lang('AddElem'));
  143. $form -> addGroup($group);
  144. ////////////////////////
  145. // DISPLAY OPTIONS ////
  146. //////////////////////
  147. $html='
  148. <div class="row">
  149. <div class="label">
  150. </div>
  151. <div class="formw"><br /><br />
  152. <table class="data_table">
  153. <tr style="text-align: center;">
  154. <th>
  155. '.get_lang('Number').'
  156. </th>
  157. <th>
  158. '.get_lang('Answer').'
  159. </th>
  160. </tr>';
  161. $form -> addElement ('html', $html);
  162. for($i = 1 ; $i <= $nb_options ; ++$i)
  163. {
  164. $form -> addElement ('html', '<tr><td>');
  165. $group = array();
  166. $puce = FormValidator :: createElement ('text', null,null,'value="'.chr(64+$i).'"');
  167. $puce->freeze();
  168. $group[] = $puce;
  169. $group[] = FormValidator :: createElement ('text', 'option['.$i.']',null, 'size="30" style="margin-left: 0em;"');
  170. $form -> addGroup($group, null, null, '</td><td width="0">');
  171. $form -> addElement ('html', '</td></tr>');
  172. }
  173. $form -> addElement ('html', '</table></div></div>');
  174. $group = array();
  175. $group[] = FormValidator :: createElement ('submit', 'lessOptions', get_lang('DelElem'));
  176. $group[] = FormValidator :: createElement ('submit', 'moreOptions',get_lang('AddElem'));
  177. $form -> addGroup($group);
  178. $form -> setDefaults($defaults);
  179. $form->setConstants(array('nb_matches' => $nb_matches,'nb_options' => $nb_options));
  180. }
  181. /**
  182. * abstract function which creates the form to create / edit the answers of the question
  183. * @param the formvalidator instance
  184. */
  185. function processAnswersCreation($form) {
  186. $nb_matches = $form -> getSubmitValue('nb_matches');
  187. $nb_options = $form -> getSubmitValue('nb_options');
  188. $this -> weighting = 0;
  189. $objAnswer = new Answer($this->id);
  190. $position = 0;
  191. // insert the options
  192. for($i=1 ; $i<=$nb_options ; ++$i)
  193. {
  194. $position++;
  195. $option = $form -> getSubmitValue('option['.$i.']');
  196. $objAnswer->createAnswer($option, 0, '', 0, $position);
  197. }
  198. // insert the answers
  199. for($i=1 ; $i<=$nb_matches ; ++$i)
  200. {
  201. $position++;
  202. $answer = $form -> getSubmitValue('answer['.$i.']');
  203. $matches = $form -> getSubmitValue('matches['.$i.']');
  204. $weighting = $form -> getSubmitValue('weighting['.$i.']');
  205. $this -> weighting += $weighting;
  206. $objAnswer->createAnswer($answer,$matches,'',$weighting,$position);
  207. }
  208. $objAnswer->save();
  209. $this->save();
  210. }
  211. }
  212. endif;
  213. ?>