matching.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. */
  13. class Matching extends Question
  14. {
  15. public $typePicture = 'matching.png';
  16. public $explanationLangVar = 'Matching';
  17. /**
  18. * Constructor.
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->type = MATCHING;
  24. $this->isContent = $this->getIsContent();
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function createAnswersForm($form)
  30. {
  31. $defaults = [];
  32. $nb_matches = $nb_options = 2;
  33. $matches = [];
  34. $answer = null;
  35. $counter = 1;
  36. if (isset($this->id)) {
  37. $answer = new Answer($this->id);
  38. $answer->read();
  39. if ($answer->nbrAnswers > 0) {
  40. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  41. $correct = $answer->isCorrect($i);
  42. if (empty($correct)) {
  43. $matches[$answer->selectAutoId($i)] = chr(64 + $counter);
  44. $counter++;
  45. }
  46. }
  47. }
  48. }
  49. if ($form->isSubmitted()) {
  50. $nb_matches = $form->getSubmitValue('nb_matches');
  51. $nb_options = $form->getSubmitValue('nb_options');
  52. if (isset($_POST['lessOptions'])) {
  53. $nb_matches--;
  54. $nb_options--;
  55. }
  56. if (isset($_POST['moreOptions'])) {
  57. $nb_matches++;
  58. $nb_options++;
  59. }
  60. } elseif (!empty($this->id)) {
  61. if ($answer->nbrAnswers > 0) {
  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.']'] = 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('First step');
  77. $defaults['answer[2]'] = get_lang('Second step');
  78. $defaults['matches[2]'] = '2';
  79. $defaults['option[1]'] = get_lang('Note down the address');
  80. $defaults['option[2]'] = get_lang('Contact the emergency services');
  81. }
  82. if (empty($matches)) {
  83. for ($i = 1; $i <= $nb_options; $i++) {
  84. // fill the array with A, B, C.....
  85. $matches[$i] = chr(64 + $i);
  86. }
  87. } else {
  88. for ($i = $counter; $i <= $nb_options; $i++) {
  89. // fill the array with A, B, C.....
  90. $matches[$i] = chr(64 + $i);
  91. }
  92. }
  93. $form->addElement('hidden', 'nb_matches', $nb_matches);
  94. $form->addElement('hidden', 'nb_options', $nb_options);
  95. // DISPLAY MATCHES
  96. $html = '<table class="table table-striped table-hover">
  97. <thead>
  98. <tr>
  99. <th width="5%">'.get_lang('N°').'</th>
  100. <th width="70%">'.get_lang('Answer').'</th>
  101. <th width="15%">'.get_lang('Matches To').'</th>
  102. <th width="10%">'.get_lang('Score').'</th>
  103. </tr>
  104. </thead>
  105. <tbody>';
  106. $form->addHeader(get_lang('Match them'));
  107. $form->addHtml($html);
  108. if ($nb_matches < 1) {
  109. $nb_matches = 1;
  110. Display::addFlash(
  111. Display::return_message(get_lang('You have to create at least one answer'))
  112. );
  113. }
  114. $editorConfig = [
  115. 'ToolbarSet' => 'TestMatching',
  116. 'Width' => '100%',
  117. 'Height' => '125',
  118. ];
  119. for ($i = 1; $i <= $nb_matches; $i++) {
  120. $renderer = &$form->defaultRenderer();
  121. $renderer->setElementTemplate(
  122. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  123. "answer[$i]"
  124. );
  125. $renderer->setElementTemplate(
  126. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  127. "matches[$i]"
  128. );
  129. $renderer->setElementTemplate(
  130. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  131. "weighting[$i]"
  132. );
  133. $form->addHtml('<tr>');
  134. $form->addHtml("<td>$i</td>");
  135. //$form->addText("answer[$i]", null);
  136. $form->addHtmlEditor(
  137. "answer[$i]",
  138. null,
  139. null,
  140. false,
  141. $editorConfig
  142. );
  143. $form->addSelect(
  144. "matches[$i]",
  145. null,
  146. $matches,
  147. ['id' => 'matches_'.$i]
  148. );
  149. $form->addText(
  150. "weighting[$i]",
  151. null,
  152. true,
  153. ['id' => 'weighting_'.$i, 'value' => 10]
  154. );
  155. $form->addHtml('</tr>');
  156. }
  157. $form->addHtml('</tbody></table>');
  158. // DISPLAY OPTIONS
  159. $html = '<table class="table table-striped table-hover">
  160. <thead>
  161. <tr>
  162. <th width="15%">'.get_lang('N°').'</th>
  163. <th width="85%">'.get_lang('Answer').'</th>
  164. </tr>
  165. </thead>
  166. <tbody>';
  167. $form->addHtml($html);
  168. if ($nb_options < 1) {
  169. $nb_options = 1;
  170. Display::addFlash(
  171. Display::return_message(get_lang('You have to create at least one answer'))
  172. );
  173. }
  174. for ($i = 1; $i <= $nb_options; $i++) {
  175. $renderer = &$form->defaultRenderer();
  176. $renderer->setElementTemplate(
  177. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  178. "option[$i]"
  179. );
  180. $form->addHtml('<tr>');
  181. $form->addHtml('<td>'.chr(64 + $i).'</td>');
  182. $form->addHtmlEditor(
  183. "option[$i]",
  184. null,
  185. null,
  186. false,
  187. $editorConfig
  188. );
  189. $form->addHtml('</tr>');
  190. }
  191. $form->addHtml('</table>');
  192. global $text;
  193. $group = [];
  194. // setting the save button here and not in the question class.php
  195. $group[] = $form->addButtonDelete(get_lang('Remove element'), 'lessOptions', true);
  196. $group[] = $form->addButtonCreate(get_lang('Add element'), 'moreOptions', true);
  197. $group[] = $form->addButtonSave($text, 'submitQuestion', true);
  198. $form->addGroup($group);
  199. if (!empty($this->id)) {
  200. $form->setDefaults($defaults);
  201. } else {
  202. if ($this->isContent == 1) {
  203. $form->setDefaults($defaults);
  204. }
  205. }
  206. $form->setConstants(
  207. [
  208. 'nb_matches' => $nb_matches,
  209. 'nb_options' => $nb_options,
  210. ]
  211. );
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function processAnswersCreation($form, $exercise)
  217. {
  218. $nb_matches = $form->getSubmitValue('nb_matches');
  219. $nb_options = $form->getSubmitValue('nb_options');
  220. $this->weighting = 0;
  221. $position = 0;
  222. $objAnswer = new Answer($this->id);
  223. // Insert the options
  224. for ($i = 1; $i <= $nb_options; $i++) {
  225. $position++;
  226. $option = $form->getSubmitValue('option['.$i.']');
  227. $objAnswer->createAnswer($option, 0, '', 0, $position);
  228. }
  229. // Insert the answers
  230. for ($i = 1; $i <= $nb_matches; $i++) {
  231. $position++;
  232. $answer = $form->getSubmitValue('answer['.$i.']');
  233. $matches = $form->getSubmitValue('matches['.$i.']');
  234. $weighting = $form->getSubmitValue('weighting['.$i.']');
  235. $this->weighting += $weighting;
  236. $objAnswer->createAnswer(
  237. $answer,
  238. $matches,
  239. '',
  240. $weighting,
  241. $position
  242. );
  243. }
  244. $objAnswer->save();
  245. $this->save($exercise);
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. public function return_header(Exercise $exercise, $counter = null, $score = [])
  251. {
  252. $header = parent::return_header($exercise, $counter, $score);
  253. $header .= '<table class="'.$this->question_table_class.'">';
  254. $header .= '<tr>';
  255. $header .= '<th>'.get_lang('Elements list').'</th>';
  256. if (!in_array($exercise->results_disabled, [
  257. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  258. ])
  259. ) {
  260. $header .= '<th>'.get_lang('Your choice').'</th>';
  261. //if ($exercise->showExpectedYour choiceColumn()) {
  262. //$header .= '<th>'.get_lang('ExpectedYour choice').'</th>';
  263. //}
  264. }
  265. if ($exercise->showExpectedChoice()) {
  266. $header .= '<th>'.get_lang('Status').'</th>';
  267. } else {
  268. if ($exercise->showExpectedChoiceColumn()) {
  269. $header .= '<th>'.get_lang('Corresponds to').'</th>';
  270. }
  271. }
  272. $header .= '</tr>';
  273. return $header;
  274. }
  275. /**
  276. * Check if a answer is correct.
  277. *
  278. * @param int $position
  279. * @param int $answer
  280. * @param int $questionId
  281. *
  282. * @return bool
  283. */
  284. public static function isCorrect($position, $answer, $questionId)
  285. {
  286. $em = Database::getManager();
  287. $count = $em
  288. ->createQuery('
  289. SELECT COUNT(a) From ChamiloCourseBundle:CQuizAnswer a
  290. WHERE a.iid = :position AND a.correct = :answer AND a.questionId = :question
  291. ')
  292. ->setParameters([
  293. 'position' => $position,
  294. 'answer' => $answer,
  295. 'question' => $questionId,
  296. ])
  297. ->getSingleScalarResult();
  298. return $count ? true : false;
  299. }
  300. }