matching.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $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. // DISPLAY MATCHES
  84. $html='
  85. <div class="row">
  86. <div class="label">
  87. '.get_lang('Answers').' <br /> <img src="../img/fill_field.png">
  88. </div>
  89. <div class="formw">
  90. '.get_lang('MakeCorrespond').'
  91. <table class="data_table">
  92. <tr>
  93. <th width="10px">
  94. '.get_lang('Number').'
  95. </th>
  96. <th width="40%">
  97. '.get_lang('Answer').'
  98. </th>
  99. <th width="40%">
  100. '.get_lang('MatchesTo').'
  101. </th>
  102. <th width="50px">
  103. '.get_lang('Weighting').'
  104. </th>
  105. </tr>';
  106. $form -> addElement ('html', $html);
  107. if ($nb_matches < 1) {
  108. $nb_matches = 1;
  109. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  110. }
  111. for($i = 1 ; $i <= $nb_matches ; ++$i) {
  112. $form -> addElement ('html', '<tr><td>');
  113. $group = array();
  114. $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"');
  115. $puce->freeze();
  116. $group[] = $puce;
  117. $group[] = FormValidator :: createElement ('text', 'answer['.$i.']',null, 'size="60" style="margin-left: 0em;"');
  118. $group[] = FormValidator :: createElement ('select', 'matches['.$i.']',null,$a_matches);
  119. $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, array('class' => 'span1', 'value' => 10));
  120. $form -> addGroup($group, null, null, '</td><td>');
  121. $form -> addElement ('html', '</td></tr>');
  122. }
  123. $form -> addElement ('html', '</table></div></div>');
  124. $group = array();
  125. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  126. $group[] = FormValidator :: createElement ('submit', 'lessMatches', get_lang('DelElem'),'class="minus"');
  127. $group[] = FormValidator :: createElement ('submit', 'moreMatches', get_lang('AddElem'),'class="plus"');
  128. } else {
  129. $group[] = FormValidator :: createElement ('style_submit_button', 'moreMatches', get_lang('AddElem'),'class="plus"');
  130. $group[] = FormValidator :: createElement ('style_submit_button', 'lessMatches', get_lang('DelElem'),'class="minus"');
  131. }
  132. $form -> addGroup($group);
  133. // DISPLAY OPTIONS ////
  134. $html='
  135. <div class="row">
  136. <div class="label">
  137. </div>
  138. <div class="formw"><br /><br />
  139. <table class="data_table">
  140. <tr style="text-align: center;">
  141. <th width="10px">
  142. '.get_lang('Number').'
  143. </th>
  144. <th width="90%"
  145. '.get_lang('Answer').'
  146. </th>
  147. </tr>';
  148. $form -> addElement ('html', $html);
  149. if ($nb_options < 1) {
  150. $nb_options = 1;
  151. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  152. }
  153. for($i = 1 ; $i <= $nb_options ; ++$i) {
  154. $form -> addElement ('html', '<tr><td>');
  155. $group = array();
  156. $puce = FormValidator :: createElement ('text', null,null,'value="'.chr(64+$i).'"');
  157. $puce->freeze();
  158. $group[] = $puce;
  159. $group[] = FormValidator :: createElement ('text', 'option['.$i.']',null, array('class' =>'span6'));
  160. $form -> addGroup($group, null, null, '</td><td>');
  161. $form -> addElement ('html', '</td></tr>');
  162. }
  163. $form -> addElement ('html', '</table></div></div>');
  164. $group = array();
  165. global $text, $class;
  166. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  167. // setting the save button here and not in the question class.php
  168. $group[] = FormValidator :: createElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  169. $group[] = FormValidator :: createElement ('submit', 'lessOptions', get_lang('DelElem'),'class="minus"');
  170. $group[] = FormValidator :: createElement ('submit', 'moreOptions',get_lang('AddElem'),'class="plus"');
  171. } else {
  172. // setting the save button here and not in the question class.php
  173. $group[] = FormValidator :: createElement ('style_submit_button', 'lessOptions', get_lang('DelElem'),'class="minus"');
  174. $group[] = FormValidator :: createElement ('style_submit_button', 'moreOptions',get_lang('AddElem'),' class="plus"');
  175. $group[] = FormValidator :: createElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  176. }
  177. $form -> addGroup($group);
  178. if (!empty($this -> id)) {
  179. $form -> setDefaults($defaults);
  180. } else {
  181. if ($this -> isContent == 1) {
  182. $form -> setDefaults($defaults);
  183. }
  184. }
  185. $form->setConstants(array('nb_matches' => $nb_matches,'nb_options' => $nb_options));
  186. }
  187. /**
  188. * abstract function which creates the form to create / edit the answers of the question
  189. * @param the formvalidator instance
  190. */
  191. function processAnswersCreation($form) {
  192. $nb_matches = $form -> getSubmitValue('nb_matches');
  193. $nb_options = $form -> getSubmitValue('nb_options');
  194. $this -> weighting = 0;
  195. $objAnswer = new Answer($this->id);
  196. $position = 0;
  197. // insert the options
  198. for($i=1 ; $i<=$nb_options ; ++$i)
  199. {
  200. $position++;
  201. $option = $form -> getSubmitValue('option['.$i.']');
  202. $objAnswer->createAnswer($option, 0, '', 0, $position);
  203. }
  204. // insert the answers
  205. for($i=1 ; $i<=$nb_matches ; ++$i) {
  206. $position++;
  207. $answer = $form -> getSubmitValue('answer['.$i.']');
  208. $matches = $form -> getSubmitValue('matches['.$i.']');
  209. $weighting = $form -> getSubmitValue('weighting['.$i.']');
  210. $this -> weighting += $weighting;
  211. $objAnswer->createAnswer($answer,$matches,'',$weighting,$position);
  212. }
  213. $objAnswer->save();
  214. $this->save();
  215. }
  216. function return_header($feedback_type, $counter = null) {
  217. parent::return_header($feedback_type, $counter);
  218. echo '<table width="100%" height="71" class="data_table_exercise_result_left">';
  219. echo '<tr>
  220. <td><span style="font-style: italic;">'.get_lang('ElementList').'</span> </td>
  221. <td><span style="font-style: italic;">'.get_lang('CorrespondsTo').'</span></td>
  222. </tr>';
  223. }
  224. }
  225. endif;
  226. ?>