matching.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. $this -> isContent = $this-> getIsContent();
  29. }
  30. /**
  31. * function which redifines Question::createAnswersForm
  32. * @param the formvalidator instance
  33. */
  34. function createAnswersForm ($form) {
  35. $defaults = array();
  36. $navigator_info = api_get_navigator();
  37. $nb_matches = $nb_options = 2;
  38. if($form -> isSubmitted())
  39. {
  40. $nb_matches = $form -> getSubmitValue('nb_matches');
  41. $nb_options = $form -> getSubmitValue('nb_options');
  42. if(isset($_POST['lessMatches']))
  43. $nb_matches--;
  44. if(isset($_POST['moreMatches']))
  45. $nb_matches++;
  46. if(isset($_POST['lessOptions']))
  47. $nb_options--;
  48. if(isset($_POST['moreOptions']))
  49. $nb_options++;
  50. } else if(!empty($this -> id)) {
  51. $answer = new Answer($this -> id);
  52. $answer -> read();
  53. if(count($answer->nbrAnswers)>0) {
  54. $a_matches = $a_options = array();
  55. $nb_matches = $nb_options = 0;
  56. for($i=1 ; $i<=$answer->nbrAnswers ; $i++){
  57. if ($answer -> isCorrect($i)) {
  58. $nb_matches++;
  59. $defaults['answer['.$nb_matches.']'] = $answer -> selectAnswer($i);
  60. $defaults['weighting['.$nb_matches.']'] = float_format($answer -> selectWeighting($i),1);
  61. $defaults['matches['.$nb_matches.']'] = $answer -> correct[$i];
  62. } else {
  63. $nb_options++;
  64. $defaults['option['.$nb_options.']'] = $answer -> selectAnswer($i);
  65. }
  66. }
  67. }
  68. }
  69. else {
  70. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  71. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  72. $defaults['matches[2]'] = '2';
  73. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  74. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  75. }
  76. $a_matches = array();
  77. for($i=1 ; $i<=$nb_options ; ++$i)
  78. {
  79. $a_matches[$i] = chr(64+$i); // fill the array with A, B, C.....
  80. }
  81. $form -> addElement('hidden', 'nb_matches', $nb_matches);
  82. $form -> addElement('hidden', 'nb_options', $nb_options);
  83. ////////////////////////
  84. // DISPLAY MATCHES ////
  85. //////////////////////
  86. $html='
  87. <div class="row">
  88. <div class="label">
  89. '.get_lang('Answers').' <br /> <img src="../img/fill_field.png">
  90. </div>
  91. <div class="formw">
  92. '.get_lang('MakeCorrespond').'
  93. <table class="data_table">
  94. <tr style="text-align: center">
  95. <th width="40px">
  96. '.get_lang('Number').'
  97. </th>
  98. <th>
  99. '.get_lang('Answer').'
  100. </th>
  101. <th>
  102. '.get_lang('MatchesTo').'
  103. </th>
  104. <th>
  105. '.get_lang('Weighting').'
  106. </th>
  107. </tr>';
  108. $form -> addElement ('html', $html);
  109. if ($nb_matches < 1) {
  110. $nb_matches = 1;
  111. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  112. }
  113. for($i = 1 ; $i <= $nb_matches ; ++$i) {
  114. $form -> addElement ('html', '<tr><td>');
  115. $group = array();
  116. $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"');
  117. $puce->freeze();
  118. $group[] = $puce;
  119. $group[] = FormValidator :: createElement ('text', 'answer['.$i.']',null, 'size="60" style="margin-left: 0em;"');
  120. $group[] = FormValidator :: createElement ('select', 'matches['.$i.']',null,$a_matches);
  121. $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  122. $form -> addGroup($group, null, null, '</td><td width="0">');
  123. $form -> addElement ('html', '</td></tr>');
  124. }
  125. $form -> addElement ('html', '</table></div></div>');
  126. $group = array();
  127. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  128. $group[] = FormValidator :: createElement ('submit', 'lessMatches', get_lang('DelElem'),'class="minus"');
  129. $group[] = FormValidator :: createElement ('submit', 'moreMatches', get_lang('AddElem'),'class="plus"');
  130. } else {
  131. $group[] = FormValidator :: createElement ('style_submit_button', 'moreMatches', get_lang('AddElem'),'class="plus"');
  132. $group[] = FormValidator :: createElement ('style_submit_button', 'lessMatches', get_lang('DelElem'),'class="minus"');
  133. }
  134. $form -> addGroup($group);
  135. // DISPLAY OPTIONS ////
  136. $html='
  137. <div class="row">
  138. <div class="label">
  139. </div>
  140. <div class="formw"><br /><br />
  141. <table class="data_table">
  142. <tr style="text-align: center;">
  143. <th width="40px">
  144. '.get_lang('Number').'
  145. </th>
  146. <th>
  147. '.get_lang('Answer').'
  148. </th>
  149. </tr>';
  150. $form -> addElement ('html', $html);
  151. if ($nb_options < 1) {
  152. $nb_options = 1;
  153. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  154. }
  155. for($i = 1 ; $i <= $nb_options ; ++$i) {
  156. $form -> addElement ('html', '<tr><td>');
  157. $group = array();
  158. $puce = FormValidator :: createElement ('text', null,null,'value="'.chr(64+$i).'"');
  159. $puce->freeze();
  160. $group[] = $puce;
  161. $group[] = FormValidator :: createElement ('text', 'option['.$i.']',null, 'size="60" style="margin-left: 0em;"');
  162. $form -> addGroup($group, null, null, '</td><td width="0">');
  163. $form -> addElement ('html', '</td></tr>');
  164. }
  165. $form -> addElement ('html', '</table></div></div>');
  166. $group = array();
  167. global $text, $class;
  168. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  169. // setting the save button here and not in the question class.php
  170. $group[] = FormValidator :: createElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  171. $group[] = FormValidator :: createElement ('submit', 'moreOptions',get_lang('AddElem'),'class="plus"');
  172. $group[] = FormValidator :: createElement ('submit', 'lessOptions', get_lang('DelElem'),'class="minus"');
  173. } else {
  174. // setting the save button here and not in the question class.php
  175. $group[] = FormValidator :: createElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  176. $group[] = FormValidator :: createElement ('style_submit_button', 'lessOptions', get_lang('DelElem'),'style="float:right;" class="minus"');
  177. $group[] = FormValidator :: createElement ('style_submit_button', 'moreOptions',get_lang('AddElem'),'style="float:right;" class="plus"');
  178. }
  179. $form -> addGroup($group);
  180. if (!empty($this -> id)) {
  181. $form -> setDefaults($defaults);
  182. } else {
  183. if ($this -> isContent == 1) {
  184. $form -> setDefaults($defaults);
  185. }
  186. }
  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. ?>