matching.class.php 9.6 KB

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