matching.class.php 10 KB

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