matching.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. CLASS Matching
  5. *
  6. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  7. * extending the class question
  8. *
  9. * @author Eric Marguin
  10. * @package chamilo.exercise
  11. **/
  12. /**
  13. * Code
  14. */
  15. if(!class_exists('Matching')):
  16. /**
  17. * Matching questions type class
  18. * @package chamilo.exercise
  19. */
  20. class Matching extends Question {
  21. static $typePicture = 'matching.gif';
  22. static $explanationLangVar = 'Matching';
  23. /**
  24. * Constructor
  25. */
  26. function Matching(){
  27. parent::question();
  28. $this -> type = MATCHING;
  29. $this -> isContent = $this-> getIsContent();
  30. }
  31. /**
  32. * function which redifines Question::createAnswersForm
  33. * @param the formvalidator instance
  34. */
  35. function createAnswersForm ($form) {
  36. $defaults = array();
  37. $navigator_info = api_get_navigator();
  38. $nb_matches = $nb_options = 2;
  39. if($form -> isSubmitted())
  40. {
  41. $nb_matches = $form -> getSubmitValue('nb_matches');
  42. $nb_options = $form -> getSubmitValue('nb_options');
  43. if(isset($_POST['lessMatches']))
  44. $nb_matches--;
  45. if(isset($_POST['moreMatches']))
  46. $nb_matches++;
  47. if(isset($_POST['lessOptions']))
  48. $nb_options--;
  49. if(isset($_POST['moreOptions']))
  50. $nb_options++;
  51. } else if(!empty($this -> id)) {
  52. $answer = new Answer($this -> id);
  53. $answer -> read();
  54. if(count($answer->nbrAnswers)>0) {
  55. $a_matches = $a_options = array();
  56. $nb_matches = $nb_options = 0;
  57. for($i=1 ; $i<=$answer->nbrAnswers ; $i++){
  58. if ($answer -> isCorrect($i)) {
  59. $nb_matches++;
  60. $defaults['answer['.$nb_matches.']'] = $answer -> selectAnswer($i);
  61. $defaults['weighting['.$nb_matches.']'] = float_format($answer -> selectWeighting($i),1);
  62. $defaults['matches['.$nb_matches.']'] = $answer -> correct[$i];
  63. } else {
  64. $nb_options++;
  65. $defaults['option['.$nb_options.']'] = $answer -> selectAnswer($i);
  66. }
  67. }
  68. }
  69. }
  70. else {
  71. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  72. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  73. $defaults['matches[2]'] = '2';
  74. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  75. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  76. }
  77. $a_matches = array();
  78. for($i=1 ; $i<=$nb_options ; ++$i)
  79. {
  80. $a_matches[$i] = chr(64+$i); // fill the array with A, B, C.....
  81. }
  82. $form -> addElement('hidden', 'nb_matches', $nb_matches);
  83. $form -> addElement('hidden', 'nb_options', $nb_options);
  84. // DISPLAY MATCHES
  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', 'moreMatches', get_lang('AddElem'),'class="plus"');
  131. $group[] = FormValidator :: createElement ('style_submit_button', 'lessMatches', get_lang('DelElem'),'class="minus"');
  132. }
  133. $form -> addGroup($group);
  134. // DISPLAY OPTIONS ////
  135. $html='
  136. <div class="row">
  137. <div class="label">
  138. </div>
  139. <div class="formw"><br /><br />
  140. <table class="data_table">
  141. <tr style="text-align: center;">
  142. <th width="40px">
  143. '.get_lang('Number').'
  144. </th>
  145. <th>
  146. '.get_lang('Answer').'
  147. </th>
  148. </tr>';
  149. $form -> addElement ('html', $html);
  150. if ($nb_options < 1) {
  151. $nb_options = 1;
  152. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  153. }
  154. for($i = 1 ; $i <= $nb_options ; ++$i) {
  155. $form -> addElement ('html', '<tr><td>');
  156. $group = array();
  157. $puce = FormValidator :: createElement ('text', null,null,'value="'.chr(64+$i).'"');
  158. $puce->freeze();
  159. $group[] = $puce;
  160. $group[] = FormValidator :: createElement ('text', 'option['.$i.']',null, 'size="60" style="margin-left: 0em;"');
  161. $form -> addGroup($group, null, null, '</td><td width="0">');
  162. $form -> addElement ('html', '</td></tr>');
  163. }
  164. $form -> addElement ('html', '</table></div></div>');
  165. $group = array();
  166. global $text, $class;
  167. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  168. // setting the save button here and not in the question class.php
  169. $group[] = FormValidator :: createElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  170. $group[] = FormValidator :: createElement ('submit', 'lessOptions', get_lang('DelElem'),'class="minus"');
  171. $group[] = FormValidator :: createElement ('submit', 'moreOptions',get_lang('AddElem'),'class="plus"');
  172. } else {
  173. // setting the save button here and not in the question class.php
  174. $group[] = FormValidator :: createElement ('style_submit_button', 'lessOptions', get_lang('DelElem'),'class="minus"');
  175. $group[] = FormValidator :: createElement ('style_submit_button', 'moreOptions',get_lang('AddElem'),' class="plus"');
  176. $group[] = FormValidator :: createElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  177. }
  178. $form -> addGroup($group);
  179. if (!empty($this -> id)) {
  180. $form -> setDefaults($defaults);
  181. } else {
  182. if ($this -> isContent == 1) {
  183. $form -> setDefaults($defaults);
  184. }
  185. }
  186. $form->setConstants(array('nb_matches' => $nb_matches,'nb_options' => $nb_options));
  187. }
  188. /**
  189. * abstract function which creates the form to create / edit the answers of the question
  190. * @param the formvalidator instance
  191. */
  192. function processAnswersCreation($form) {
  193. $nb_matches = $form -> getSubmitValue('nb_matches');
  194. $nb_options = $form -> getSubmitValue('nb_options');
  195. $this -> weighting = 0;
  196. $objAnswer = new Answer($this->id);
  197. $position = 0;
  198. // insert the options
  199. for($i=1 ; $i<=$nb_options ; ++$i)
  200. {
  201. $position++;
  202. $option = $form -> getSubmitValue('option['.$i.']');
  203. $objAnswer->createAnswer($option, 0, '', 0, $position);
  204. }
  205. // insert the answers
  206. for($i=1 ; $i<=$nb_matches ; ++$i) {
  207. $position++;
  208. $answer = $form -> getSubmitValue('answer['.$i.']');
  209. $matches = $form -> getSubmitValue('matches['.$i.']');
  210. $weighting = $form -> getSubmitValue('weighting['.$i.']');
  211. $this -> weighting += $weighting;
  212. $objAnswer->createAnswer($answer,$matches,'',$weighting,$position);
  213. }
  214. $objAnswer->save();
  215. $this->save();
  216. }
  217. function return_header($feedback_type, $counter = null) {
  218. parent::return_header($feedback_type, $counter);
  219. echo '<table width="100%" height="71" border="0" cellspacing="3" cellpadding="3" >';
  220. echo '<tr><td colspan="2">&nbsp;</td></tr>';
  221. echo '<tr>
  222. <td><span style="font-style: italic;">'.get_lang('ElementList').'</span> </td>
  223. <td><span style="font-style: italic;">'.get_lang('CorrespondsTo').'</span></td>
  224. </tr>';
  225. echo '<tr><td colspan="2">&nbsp;</td></tr>';
  226. }
  227. }
  228. endif;
  229. ?>