matching.class.php 8.0 KB

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