matching.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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="table table-striped table-hover">
  79. <thead>
  80. <tr>
  81. <th width="10">' . get_lang('Number') . '</th>
  82. <th width="85%">' . get_lang('Answer') . '</th>
  83. <th width="15%">' . get_lang('MatchesTo') . '</th>
  84. <th width="10">' . get_lang('Weighting') . '</th>
  85. </tr>
  86. </thead>
  87. <tbody>';
  88. $form->addHeader(get_lang('MakeCorrespond'));
  89. $form->addHtml($html);
  90. if ($nb_matches < 1) {
  91. $nb_matches = 1;
  92. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  93. }
  94. for ($i = 1; $i <= $nb_matches; ++$i) {
  95. $renderer = &$form->defaultRenderer();
  96. $renderer->setElementTemplate(
  97. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  98. "answer[$i]"
  99. );
  100. $renderer->setElementTemplate(
  101. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  102. "matches[$i]"
  103. );
  104. $renderer->setElementTemplate(
  105. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  106. "weighting[$i]"
  107. );
  108. $form->addHtml('<tr>');
  109. $form->addHtml("<td>$i</td>");
  110. $form->addText("answer[$i]", null);
  111. $form->addSelect("matches[$i]", null, $a_matches);
  112. $form->addText("weighting[$i]", null, true, ['value' => 10]);
  113. $form->addHtml('</tr>');
  114. }
  115. $form->addHtml('</tbody></table>');
  116. $group = array();
  117. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  118. $group[] = $form->createElement('submit', 'lessMatches', get_lang('DelElem'), 'class="btn minus"');
  119. $group[] = $form->createElement('submit', 'moreMatches', get_lang('AddElem'), 'class="btn plus"');
  120. } else {
  121. $renderer->setElementTemplate('<div class="form-group"><div class="col-sm-offset-2">{element}', 'lessMatches');
  122. $renderer->setElementTemplate('{element}</div></div>', 'moreMatches');
  123. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true);
  124. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true);
  125. }
  126. $form->addGroup($group);
  127. // DISPLAY OPTIONS
  128. $html = '<table class="table table-striped table-hover">
  129. <thead>
  130. <tr>
  131. <th width="15%">' . get_lang('Number') . '</th>
  132. <th width="85%">' . get_lang('Answer') . '</th>
  133. </tr>
  134. </thead>
  135. <tbody>';
  136. $form->addHtml($html);
  137. if ($nb_options < 1) {
  138. $nb_options = 1;
  139. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  140. }
  141. for ($i = 1; $i <= $nb_options; ++$i) {
  142. $renderer = &$form->defaultRenderer();
  143. $renderer->setElementTemplate(
  144. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  145. "option[$i]"
  146. );
  147. $form->addHtml('<tr>');
  148. $form->addHtml('<td>' . chr(64 + $i) . '</td>');
  149. $form->addText("option[$i]", null);
  150. $form->addHtml('</tr>');
  151. }
  152. $form->addHtml('</table>');
  153. $group = array();
  154. global $text, $class;
  155. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  156. // setting the save button here and not in the question class.php
  157. $group[] = $form->createElement('submit', 'submitQuestion', $text, 'class="' . $class . '"');
  158. $group[] = $form->createElement('submit', 'lessOptions', get_lang('DelElem'), 'class="minus"');
  159. $group[] = $form->createElement('submit', 'moreOptions', get_lang('AddElem'), 'class="plus"');
  160. } else {
  161. // setting the save button here and not in the question class.php
  162. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessOptions', true);
  163. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreOptions', true);
  164. $group[] = $form->addButtonSave($text, 'submitQuestion', true);
  165. }
  166. $form->addGroup($group);
  167. if (!empty($this->id)) {
  168. $form->setDefaults($defaults);
  169. } else {
  170. if ($this->isContent == 1) {
  171. $form->setDefaults($defaults);
  172. }
  173. }
  174. $form->setConstants(array('nb_matches' => $nb_matches, 'nb_options' => $nb_options));
  175. }
  176. /**
  177. * abstract function which creates the form to create / edit the answers of the question
  178. * @param FormValidator $form
  179. */
  180. public function processAnswersCreation($form)
  181. {
  182. $nb_matches = $form -> getSubmitValue('nb_matches');
  183. $nb_options = $form -> getSubmitValue('nb_options');
  184. $this -> weighting = 0;
  185. $objAnswer = new Answer($this->id);
  186. $position = 0;
  187. // insert the options
  188. for($i=1 ; $i<=$nb_options; ++$i) {
  189. $position++;
  190. $option = $form -> getSubmitValue('option['.$i.']');
  191. $objAnswer->createAnswer($option, 0, '', 0, $position);
  192. }
  193. // insert the answers
  194. for($i=1 ; $i<=$nb_matches ; ++$i) {
  195. $position++;
  196. $answer = $form -> getSubmitValue('answer['.$i.']');
  197. $matches = $form -> getSubmitValue('matches['.$i.']');
  198. $weighting = $form -> getSubmitValue('weighting['.$i.']');
  199. $this -> weighting += $weighting;
  200. $objAnswer->createAnswer($answer,$matches,'',$weighting,$position);
  201. }
  202. $objAnswer->save();
  203. $this->save();
  204. }
  205. /**
  206. * @param null $feedback_type
  207. * @param null $counter
  208. * @param null $score
  209. * @return string
  210. */
  211. public function return_header($feedback_type = null, $counter = null, $score = null)
  212. {
  213. $header = parent::return_header($feedback_type, $counter, $score);
  214. $header .= '<table class="'.$this->question_table_class .'">';
  215. $header .= '<tr>
  216. <th>'.get_lang('ElementList').'</th>
  217. <th>'.get_lang('CorrespondsTo').'</th>
  218. </tr>';
  219. return $header;
  220. }
  221. }