matching.class.php 9.6 KB

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