matching.class.php 8.4 KB

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