matching.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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
  8. * type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER)
  9. * extending the class question
  10. *
  11. * @author Eric Marguin
  12. * @package chamilo.exercise
  13. */
  14. class Matching extends Question
  15. {
  16. static $typePicture = 'matching.png';
  17. static $explanationLangVar = 'Matching';
  18. /**
  19. * Constructor
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->type = MATCHING;
  25. $this->isContent = $this->getIsContent();
  26. }
  27. /**
  28. * function which redefines Question::createAnswersForm
  29. * @param FormValidator $form
  30. */
  31. public function createAnswersForm($form)
  32. {
  33. $defaults = array();
  34. $nb_matches = $nb_options = 2;
  35. $matches = array();
  36. $answer = null;
  37. $counter = 1;
  38. if (isset($this->id)) {
  39. $answer = new Answer($this->id);
  40. $answer->read();
  41. if (count($answer->nbrAnswers) > 0) {
  42. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  43. $correct = $answer->isCorrect($i);
  44. if (empty($correct)) {
  45. $matches[$answer->selectAutoId($i)] = chr(64 + $counter);
  46. $counter++;
  47. }
  48. }
  49. }
  50. }
  51. if ($form->isSubmitted()) {
  52. $nb_matches = $form->getSubmitValue('nb_matches');
  53. $nb_options = $form->getSubmitValue('nb_options');
  54. if (isset($_POST['lessMatches'])) {
  55. $nb_matches--;
  56. }
  57. if (isset($_POST['moreMatches'])) {
  58. $nb_matches++;
  59. }
  60. if (isset($_POST['lessOptions'])) {
  61. $nb_options--;
  62. }
  63. if (isset($_POST['moreOptions'])) {
  64. $nb_options++;
  65. }
  66. } else if (!empty($this->id)) {
  67. if (count($answer->nbrAnswers) > 0) {
  68. $nb_matches = $nb_options = 0;
  69. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  70. if ($answer->isCorrect($i)) {
  71. $nb_matches++;
  72. $defaults['answer[' . $nb_matches . ']'] = $answer->selectAnswer($i);
  73. $defaults['weighting[' . $nb_matches . ']'] = float_format($answer->selectWeighting($i), 1);
  74. $defaults['matches[' . $nb_matches . ']'] = $answer->correct[$i];
  75. } else {
  76. $nb_options++;
  77. $defaults['option[' . $nb_options . ']'] = $answer->selectAnswer($i);
  78. }
  79. }
  80. }
  81. } else {
  82. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  83. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  84. $defaults['matches[2]'] = '2';
  85. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  86. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  87. }
  88. if (empty($matches)) {
  89. for ($i = 1; $i <= $nb_options; ++$i) {
  90. // fill the array with A, B, C.....
  91. $matches[$i] = chr(64 + $i);
  92. }
  93. } else {
  94. for ($i = $counter; $i <= $nb_options; ++$i) {
  95. // fill the array with A, B, C.....
  96. $matches[$i] = chr(64 + $i);
  97. }
  98. }
  99. $form->addElement('hidden', 'nb_matches', $nb_matches);
  100. $form->addElement('hidden', 'nb_options', $nb_options);
  101. // DISPLAY MATCHES
  102. $html = '<table class="table table-striped table-hover">
  103. <thead>
  104. <tr>
  105. <th width="5%">' . get_lang('Number') . '</th>
  106. <th width="70%">' . get_lang('Answer') . '</th>
  107. <th width="15%">' . get_lang('MatchesTo') . '</th>
  108. <th width="10%">' . get_lang('Weighting') . '</th>
  109. </tr>
  110. </thead>
  111. <tbody>';
  112. $form->addHeader(get_lang('MakeCorrespond'));
  113. $form->addHtml($html);
  114. if ($nb_matches < 1) {
  115. $nb_matches = 1;
  116. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  117. }
  118. for ($i = 1; $i <= $nb_matches; ++$i) {
  119. $renderer = &$form->defaultRenderer();
  120. $renderer->setElementTemplate(
  121. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  122. "answer[$i]"
  123. );
  124. $renderer->setElementTemplate(
  125. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  126. "matches[$i]"
  127. );
  128. $renderer->setElementTemplate(
  129. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  130. "weighting[$i]"
  131. );
  132. $form->addHtml('<tr>');
  133. $form->addHtml("<td>$i</td>");
  134. $form->addText("answer[$i]", null);
  135. $form->addSelect("matches[$i]", null, $matches);
  136. $form->addText("weighting[$i]", null, true, ['value' => 10]);
  137. $form->addHtml('</tr>');
  138. }
  139. $form->addHtml('</tbody></table>');
  140. $group = array();
  141. $renderer->setElementTemplate('<div class="form-group"><div class="col-sm-offset-2">{element}', 'lessMatches');
  142. $renderer->setElementTemplate('{element}</div></div>', 'moreMatches');
  143. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true);
  144. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true);
  145. $form->addGroup($group);
  146. // DISPLAY OPTIONS
  147. $html = '<table class="table table-striped table-hover">
  148. <thead>
  149. <tr>
  150. <th width="15%">' . get_lang('Number') . '</th>
  151. <th width="85%">' . get_lang('Answer') . '</th>
  152. </tr>
  153. </thead>
  154. <tbody>';
  155. $form->addHtml($html);
  156. if ($nb_options < 1) {
  157. $nb_options = 1;
  158. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  159. }
  160. for ($i = 1; $i <= $nb_options; ++$i) {
  161. $renderer = &$form->defaultRenderer();
  162. $renderer->setElementTemplate(
  163. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  164. "option[$i]"
  165. );
  166. $form->addHtml('<tr>');
  167. $form->addHtml('<td>' . chr(64 + $i) . '</td>');
  168. $form->addText("option[$i]", null);
  169. $form->addHtml('</tr>');
  170. }
  171. $form->addHtml('</table>');
  172. $group = array();
  173. global $text;
  174. // setting the save button here and not in the question class.php
  175. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessOptions', true);
  176. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreOptions', true);
  177. $group[] = $form->addButtonSave($text, 'submitQuestion', true);
  178. $form->addGroup($group);
  179. if (!empty($this->id)) {
  180. $form->setDefaults($defaults);
  181. } else {
  182. if ($this->isContent == 1) {
  183. $form->setDefaults($defaults);
  184. }
  185. }
  186. $form->setConstants(
  187. array(
  188. 'nb_matches' => $nb_matches,
  189. 'nb_options' => $nb_options
  190. )
  191. );
  192. }
  193. /**
  194. * abstract function which creates the form to create / edit the answers of the question
  195. * @param FormValidator $form
  196. */
  197. public function processAnswersCreation($form)
  198. {
  199. $nb_matches = $form->getSubmitValue('nb_matches');
  200. $nb_options = $form->getSubmitValue('nb_options');
  201. $this->weighting = 0;
  202. $position = 0;
  203. $objAnswer = new Answer($this->id);
  204. // Insert the options
  205. for ($i = 1; $i <= $nb_options; ++$i) {
  206. $position++;
  207. $option = $form->getSubmitValue('option['.$i.']');
  208. $objAnswer->createAnswer($option, 0, '', 0, $position);
  209. }
  210. // Insert the answers
  211. for ($i = 1; $i <= $nb_matches; ++$i) {
  212. $position++;
  213. $answer = $form->getSubmitValue('answer['.$i.']');
  214. $matches = $form->getSubmitValue('matches['.$i.']');
  215. $weighting = $form->getSubmitValue('weighting['.$i.']');
  216. $this->weighting += $weighting;
  217. $objAnswer->createAnswer(
  218. $answer,
  219. $matches,
  220. '',
  221. $weighting,
  222. $position
  223. );
  224. }
  225. $objAnswer->save();
  226. $this->save();
  227. }
  228. /**
  229. * @param null $feedback_type
  230. * @param null $counter
  231. * @param null $score
  232. * @return string
  233. */
  234. public function return_header($feedback_type = null, $counter = null, $score = null)
  235. {
  236. $header = parent::return_header($feedback_type, $counter, $score);
  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. return $header;
  243. }
  244. }