matching.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Matching
  5. * Matching questions type class
  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. {
  15. static $typePicture = 'matching.png';
  16. static $explanationLangVar = 'Matching';
  17. /**
  18. * Constructor
  19. */
  20. public function Matching()
  21. {
  22. parent::question();
  23. $this->type = MATCHING;
  24. $this->isContent = $this-> getIsContent();
  25. }
  26. /**
  27. * function which redefines Question::createAnswersForm
  28. * @param FormValidator $form
  29. */
  30. public function createAnswersForm ($form)
  31. {
  32. $defaults = array();
  33. $navigator_info = api_get_navigator();
  34. $nb_matches = $nb_options = 2;
  35. if ($form -> isSubmitted()) {
  36. $nb_matches = $form -> getSubmitValue('nb_matches');
  37. $nb_options = $form -> getSubmitValue('nb_options');
  38. if(isset($_POST['lessMatches']))
  39. $nb_matches--;
  40. if(isset($_POST['moreMatches']))
  41. $nb_matches++;
  42. if(isset($_POST['lessOptions']))
  43. $nb_options--;
  44. if(isset($_POST['moreOptions']))
  45. $nb_options++;
  46. } else if(!empty($this -> id)) {
  47. $answer = new Answer($this -> id);
  48. $answer -> read();
  49. if(count($answer->nbrAnswers)>0) {
  50. $a_matches = $a_options = array();
  51. $nb_matches = $nb_options = 0;
  52. for($i=1 ; $i<=$answer->nbrAnswers ; $i++){
  53. if ($answer -> isCorrect($i)) {
  54. $nb_matches++;
  55. $defaults['answer['.$nb_matches.']'] = $answer -> selectAnswer($i);
  56. $defaults['weighting['.$nb_matches.']'] = float_format($answer -> selectWeighting($i),1);
  57. $defaults['matches['.$nb_matches.']'] = $answer -> correct[$i];
  58. } else {
  59. $nb_options++;
  60. $defaults['option['.$nb_options.']'] = $answer -> selectAnswer($i);
  61. }
  62. }
  63. }
  64. } else {
  65. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  66. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  67. $defaults['matches[2]'] = '2';
  68. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  69. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  70. }
  71. $a_matches = array();
  72. for($i=1 ; $i<=$nb_options ; ++$i) {
  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='<table class="data_table">
  79. <tr>
  80. <th width="10px">
  81. '.get_lang('Number').'
  82. </th>
  83. <th width="40%">
  84. '.get_lang('Answer').'
  85. </th>
  86. <th width="40%">
  87. '.get_lang('MatchesTo').'
  88. </th>
  89. <th width="50px">
  90. '.get_lang('Weighting').'
  91. </th>
  92. </tr>';
  93. $form -> addElement ('label', get_lang('MakeCorrespond').'<br /> <img src="../img/fill_field.png">', $html);
  94. if ($nb_matches < 1) {
  95. $nb_matches = 1;
  96. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  97. }
  98. for($i = 1 ; $i <= $nb_matches ; ++$i) {
  99. $form -> addElement ('html', '<tr><td>');
  100. $group = array();
  101. $puce = $form->createElement('text', null,null,'value="'.$i.'"');
  102. $puce->freeze();
  103. $group[] = $puce;
  104. $group[] = $form->createElement('text', 'answer['.$i.']',null, 'size="60" style="margin-left: 0em;"');
  105. $group[] = $form->createElement('select', 'matches['.$i.']',null,$a_matches);
  106. $group[] = $form->createElement('text', 'weighting['.$i.']',null, array('class' => 'span1', 'value' => 10));
  107. $form -> addGroup($group, null, null, '</td><td>');
  108. $form -> addElement ('html', '</td></tr>');
  109. }
  110. $form -> addElement ('html', '</table></div></div>');
  111. $group = array();
  112. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  113. $group[] = $form->createElement('submit', 'lessMatches', get_lang('DelElem'),'class="btn minus"');
  114. $group[] = $form->createElement('submit', 'moreMatches', get_lang('AddElem'),'class="btn plus"');
  115. } else {
  116. $group[] = $form->createElement('style_submit_button', 'moreMatches', get_lang('AddElem'),'class="btn plus"');
  117. $group[] = $form->createElement('style_submit_button', 'lessMatches', get_lang('DelElem'),'class="btn minus"');
  118. }
  119. $form -> addGroup($group);
  120. // DISPLAY OPTIONS
  121. $html='<table class="data_table">
  122. <tr style="text-align: center;">
  123. <th width="10px">
  124. '.get_lang('Number').'
  125. </th>
  126. <th width="90%"
  127. '.get_lang('Answer').'
  128. </th>
  129. </tr>';
  130. //$form -> addElement ('html', $html);
  131. $form -> addElement ('label', null, $html);
  132. if ($nb_options < 1) {
  133. $nb_options = 1;
  134. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  135. }
  136. for($i = 1 ; $i <= $nb_options ; ++$i) {
  137. $form -> addElement ('html', '<tr><td>');
  138. $group = array();
  139. $puce = $form->createElement('text', null,null,'value="'.chr(64+$i).'"');
  140. $puce->freeze();
  141. $group[] = $puce;
  142. $group[] = $form->createElement('text', 'option['.$i.']',null, array('class' =>'span6'));
  143. $form -> addGroup($group, null, null, '</td><td>');
  144. $form -> addElement ('html', '</td></tr>');
  145. }
  146. $form -> addElement ('html', '</table></div></div>');
  147. $group = array();
  148. global $text, $class;
  149. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  150. // setting the save button here and not in the question class.php
  151. $group[] = $form->createElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  152. $group[] = $form->createElement('submit', 'lessOptions', get_lang('DelElem'),'class="minus"');
  153. $group[] = $form->createElement('submit', 'moreOptions',get_lang('AddElem'),'class="plus"');
  154. } else {
  155. // setting the save button here and not in the question class.php
  156. $group[] = $form->createElement('style_submit_button', 'lessOptions', get_lang('DelElem'),'class="minus"');
  157. $group[] = $form->createElement('style_submit_button', 'moreOptions',get_lang('AddElem'),' class="plus"');
  158. $group[] = $form->createElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  159. }
  160. $form -> addGroup($group);
  161. if (!empty($this -> id)) {
  162. $form -> setDefaults($defaults);
  163. } else {
  164. if ($this -> isContent == 1) {
  165. $form -> setDefaults($defaults);
  166. }
  167. }
  168. $form->setConstants(array('nb_matches' => $nb_matches,'nb_options' => $nb_options));
  169. }
  170. /**
  171. * abstract function which creates the form to create / edit the answers of the question
  172. * @param FormValidator $form
  173. */
  174. public function processAnswersCreation($form)
  175. {
  176. $nb_matches = $form -> getSubmitValue('nb_matches');
  177. $nb_options = $form -> getSubmitValue('nb_options');
  178. $this -> weighting = 0;
  179. $objAnswer = new Answer($this->id);
  180. $position = 0;
  181. // insert the options
  182. for($i=1 ; $i<=$nb_options; ++$i) {
  183. $position++;
  184. $option = $form -> getSubmitValue('option['.$i.']');
  185. $objAnswer->createAnswer($option, 0, '', 0, $position);
  186. }
  187. // insert the answers
  188. for($i=1 ; $i<=$nb_matches ; ++$i) {
  189. $position++;
  190. $answer = $form -> getSubmitValue('answer['.$i.']');
  191. $matches = $form -> getSubmitValue('matches['.$i.']');
  192. $weighting = $form -> getSubmitValue('weighting['.$i.']');
  193. $this -> weighting += $weighting;
  194. $objAnswer->createAnswer($answer,$matches,'',$weighting,$position);
  195. }
  196. $objAnswer->save();
  197. $this->save();
  198. }
  199. /**
  200. * @param null $feedback_type
  201. * @param null $counter
  202. * @param null $score
  203. * @return string
  204. */
  205. public function return_header($feedback_type = null, $counter = null, $score = null)
  206. {
  207. $header = parent::return_header($feedback_type, $counter, $score);
  208. $header .= '<table class="'.$this->question_table_class .'">';
  209. $header .= '<tr>
  210. <th>'.get_lang('ElementList').'</th>
  211. <th>'.get_lang('CorrespondsTo').'</th>
  212. </tr>';
  213. return $header;
  214. }
  215. }