matching.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File containing the Matching class.
  5. * @package chamilo.exercise
  6. * @author Eric Marguin
  7. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  8. */
  9. if(!class_exists('Matching')):
  10. /**
  11. CLASS Matching
  12. *
  13. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  14. * extending the class question
  15. *
  16. * @author Eric Marguin
  17. * @package dokeos.exercise
  18. **/
  19. class Matching extends Question {
  20. static $typePicture = 'matching.gif';
  21. static $explanationLangVar = 'Matching';
  22. /**
  23. * Constructor
  24. */
  25. function Matching(){
  26. parent::question();
  27. $this -> type = MATCHING;
  28. }
  29. /**
  30. * function which redifines Question::createAnswersForm
  31. * @param the formvalidator instance
  32. */
  33. function createAnswersForm ($form) {
  34. $defaults = array();
  35. $navigator_info = api_get_navigator();
  36. $nb_matches = $nb_options = 2;
  37. if($form -> isSubmitted())
  38. {
  39. $nb_matches = $form -> getSubmitValue('nb_matches');
  40. $nb_options = $form -> getSubmitValue('nb_options');
  41. if(isset($_POST['lessMatches']))
  42. $nb_matches--;
  43. if(isset($_POST['moreMatches']))
  44. $nb_matches++;
  45. if(isset($_POST['lessOptions']))
  46. $nb_options--;
  47. if(isset($_POST['moreOptions']))
  48. $nb_options++;
  49. } else if(!empty($this -> id)) {
  50. $answer = new Answer($this -> id);
  51. $answer -> read();
  52. if(count($answer->nbrAnswers)>0) {
  53. $a_matches = $a_options = array();
  54. $nb_matches = $nb_options = 0;
  55. for($i=1 ; $i<=$answer->nbrAnswers ; $i++){
  56. if ($answer -> isCorrect($i)) {
  57. $nb_matches++;
  58. $defaults['answer['.$nb_matches.']'] = $answer -> selectAnswer($i);
  59. $defaults['weighting['.$nb_matches.']'] = float_format($answer -> selectWeighting($i),1);
  60. $defaults['matches['.$nb_matches.']'] = $answer -> correct[$i];
  61. } else {
  62. $nb_options++;
  63. $defaults['option['.$nb_options.']'] = $answer -> selectAnswer($i);
  64. }
  65. }
  66. }
  67. }
  68. else {
  69. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  70. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  71. $defaults['matches[2]'] = '2';
  72. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  73. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  74. }
  75. $a_matches = array();
  76. for($i=1 ; $i<=$nb_options ; ++$i)
  77. {
  78. $a_matches[$i] = chr(64+$i); // fill the array with A, B, C.....
  79. }
  80. $form -> addElement('hidden', 'nb_matches', $nb_matches);
  81. $form -> addElement('hidden', 'nb_options', $nb_options);
  82. ////////////////////////
  83. // DISPLAY MATCHES ////
  84. //////////////////////
  85. $html='
  86. <div class="row">
  87. <div class="label">
  88. '.get_lang('Answers').' <br /> <img src="../img/fill_field.png">
  89. </div>
  90. <div class="formw">
  91. '.get_lang('MakeCorrespond').'
  92. <table class="data_table">
  93. <tr style="text-align: center">
  94. <th width="40px">
  95. '.get_lang('Number').'
  96. </th>
  97. <th>
  98. '.get_lang('Answer').'
  99. </th>
  100. <th>
  101. '.get_lang('MatchesTo').'
  102. </th>
  103. <th>
  104. '.get_lang('Weighting').'
  105. </th>
  106. </tr>';
  107. $form -> addElement ('html', $html);
  108. if ($nb_matches < 1) {
  109. $nb_matches = 1;
  110. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  111. }
  112. for($i = 1 ; $i <= $nb_matches ; ++$i) {
  113. $form -> addElement ('html', '<tr><td>');
  114. $group = array();
  115. $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"');
  116. $puce->freeze();
  117. $group[] = $puce;
  118. $group[] = FormValidator :: createElement ('text', 'answer['.$i.']',null, 'size="60" style="margin-left: 0em;"');
  119. $group[] = FormValidator :: createElement ('select', 'matches['.$i.']',null,$a_matches);
  120. $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  121. $form -> addGroup($group, null, null, '</td><td width="0">');
  122. $form -> addElement ('html', '</td></tr>');
  123. }
  124. $form -> addElement ('html', '</table></div></div>');
  125. $group = array();
  126. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  127. $group[] = FormValidator :: createElement ('submit', 'lessMatches', get_lang('DelElem'),'class="minus"');
  128. $group[] = FormValidator :: createElement ('submit', 'moreMatches', get_lang('AddElem'),'class="plus"');
  129. } else {
  130. $group[] = FormValidator :: createElement ('style_submit_button', 'lessMatches', get_lang('DelElem'),'class="minus"');
  131. $group[] = FormValidator :: createElement ('style_submit_button', 'moreMatches', get_lang('AddElem'),'class="plus"');
  132. }
  133. $form -> addGroup($group);
  134. ////////////////////////
  135. // DISPLAY OPTIONS ////
  136. //////////////////////
  137. $html='
  138. <div class="row">
  139. <div class="label">
  140. </div>
  141. <div class="formw"><br /><br />
  142. <table class="data_table">
  143. <tr style="text-align: center;">
  144. <th width="40px">
  145. '.get_lang('Number').'
  146. </th>
  147. <th>
  148. '.get_lang('Answer').'
  149. </th>
  150. </tr>';
  151. $form -> addElement ('html', $html);
  152. if ($nb_options < 1) {
  153. $nb_options = 1;
  154. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  155. }
  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="60" 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. global $text, $class;
  169. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  170. $group[] = FormValidator :: createElement ('submit', 'lessOptions', get_lang('DelElem'),'class="minus"');
  171. $group[] = FormValidator :: createElement ('submit', 'moreOptions',get_lang('AddElem'),'class="plus"');
  172. // setting the save button here and not in the question class.php
  173. $group[] = FormValidator :: createElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  174. } else {
  175. $group[] = FormValidator :: createElement ('style_submit_button', 'lessOptions', get_lang('DelElem'),'class="minus"');
  176. $group[] = FormValidator :: createElement ('style_submit_button', 'moreOptions',get_lang('AddElem'),'class="plus"');
  177. // setting the save button here and not in the question class.php
  178. $group[] = FormValidator :: createElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  179. }
  180. $form -> addGroup($group);
  181. $form -> setDefaults($defaults);
  182. $form->setConstants(array('nb_matches' => $nb_matches,'nb_options' => $nb_options));
  183. }
  184. /**
  185. * abstract function which creates the form to create / edit the answers of the question
  186. * @param the formvalidator instance
  187. */
  188. function processAnswersCreation($form) {
  189. $nb_matches = $form -> getSubmitValue('nb_matches');
  190. $nb_options = $form -> getSubmitValue('nb_options');
  191. $this -> weighting = 0;
  192. $objAnswer = new Answer($this->id);
  193. $position = 0;
  194. // insert the options
  195. for($i=1 ; $i<=$nb_options ; ++$i)
  196. {
  197. $position++;
  198. $option = $form -> getSubmitValue('option['.$i.']');
  199. $objAnswer->createAnswer($option, 0, '', 0, $position);
  200. }
  201. // insert the answers
  202. for($i=1 ; $i<=$nb_matches ; ++$i)
  203. {
  204. $position++;
  205. $answer = $form -> getSubmitValue('answer['.$i.']');
  206. $matches = $form -> getSubmitValue('matches['.$i.']');
  207. $weighting = $form -> getSubmitValue('weighting['.$i.']');
  208. $this -> weighting += $weighting;
  209. $objAnswer->createAnswer($answer,$matches,'',$weighting,$position);
  210. }
  211. $objAnswer->save();
  212. $this->save();
  213. }
  214. }
  215. endif;
  216. ?>