matching.class.php 7.3 KB

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