matching.class.php 7.8 KB

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