MatchingDraggable.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * MatchingDraggable.
  5. *
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. */
  8. class MatchingDraggable extends Question
  9. {
  10. public static $typePicture = 'matchingdrag.png';
  11. public static $explanationLangVar = 'MatchingDraggable';
  12. /**
  13. * Class constructor.
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->type = MATCHING_DRAGGABLE;
  19. $this->isContent = $this->getIsContent();
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function createAnswersForm($form)
  25. {
  26. $defaults = [];
  27. $nb_matches = $nb_options = 2;
  28. $matches = [];
  29. $answer = null;
  30. $counter = 1;
  31. if (isset($this->id)) {
  32. $answer = new Answer($this->id);
  33. $answer->read();
  34. if ($answer->nbrAnswers > 0) {
  35. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  36. $correct = $answer->isCorrect($i);
  37. if (empty($correct)) {
  38. $matches[$answer->selectAutoId($i)] = chr(64 + $counter);
  39. $counter++;
  40. }
  41. }
  42. }
  43. }
  44. if ($form->isSubmitted()) {
  45. $nb_matches = $form->getSubmitValue('nb_matches');
  46. $nb_options = $form->getSubmitValue('nb_options');
  47. if (isset($_POST['lessOptions'])) {
  48. $nb_matches--;
  49. $nb_options--;
  50. }
  51. if (isset($_POST['moreOptions'])) {
  52. $nb_matches++;
  53. $nb_options++;
  54. }
  55. } elseif (!empty($this->id)) {
  56. if ($answer->nbrAnswers > 0) {
  57. $nb_matches = $nb_options = 0;
  58. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  59. if ($answer->isCorrect($i)) {
  60. $nb_matches++;
  61. $defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i);
  62. $defaults['weighting['.$nb_matches.']'] = float_format($answer->selectWeighting($i), 1);
  63. $defaults['matches['.$nb_matches.']'] = $answer->correct[$i];
  64. } else {
  65. $nb_options++;
  66. $defaults['option['.$nb_options.']'] = $answer->selectAnswer($i);
  67. }
  68. }
  69. }
  70. } else {
  71. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  72. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  73. $defaults['matches[2]'] = '2';
  74. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  75. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  76. }
  77. if (empty($matches)) {
  78. for ($i = 1; $i <= $nb_options; $i++) {
  79. // fill the array with A, B, C.....
  80. $matches[$i] = chr(64 + $i);
  81. }
  82. } else {
  83. for ($i = $counter; $i <= $nb_options; $i++) {
  84. // fill the array with A, B, C.....
  85. $matches[$i] = chr(64 + $i);
  86. }
  87. }
  88. $form->addElement('hidden', 'nb_matches', $nb_matches);
  89. $form->addElement('hidden', 'nb_options', $nb_options);
  90. // DISPLAY MATCHES
  91. $html = '<table class="table table-striped table-hover">
  92. <thead>
  93. <tr>
  94. <th width="10">'.get_lang('Number').'</th>
  95. <th width="85%">'.get_lang('Answer').'</th>
  96. <th width="15%">'.get_lang('MatchesTo').'</th>
  97. <th width="10">'.get_lang('Weighting').'</th>
  98. </tr>
  99. </thead>
  100. <tbody>';
  101. $form->addHeader(get_lang('MakeCorrespond'));
  102. $form->addHtml($html);
  103. if ($nb_matches < 1) {
  104. $nb_matches = 1;
  105. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'), 'normal');
  106. }
  107. $editorConfig = [
  108. 'ToolbarSet' => 'TestMatching',
  109. 'Width' => '100%',
  110. 'Height' => '125',
  111. ];
  112. for ($i = 1; $i <= $nb_matches; $i++) {
  113. $renderer = &$form->defaultRenderer();
  114. $renderer->setElementTemplate(
  115. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  116. "answer[$i]"
  117. );
  118. $renderer->setElementTemplate(
  119. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  120. "matches[$i]"
  121. );
  122. $renderer->setElementTemplate(
  123. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  124. "weighting[$i]"
  125. );
  126. $form->addHtml('<tr>');
  127. $form->addHtml("<td>$i</td>");
  128. //$form->addText("answer[$i]", null);
  129. $form->addHtmlEditor(
  130. "answer[$i]",
  131. null,
  132. null,
  133. false,
  134. $editorConfig
  135. );
  136. $form->addSelect("matches[$i]", null, $matches);
  137. $form->addText("weighting[$i]", null, true, ['style' => 'width: 60px;', 'value' => 10]);
  138. $form->addHtml('</tr>');
  139. }
  140. $form->addHtml('</tbody></table>');
  141. // DISPLAY OPTIONS
  142. $html = '<table class="table table-striped table-hover">
  143. <thead>
  144. <tr>
  145. <th width="15%">'.get_lang('Number').'</th>
  146. <th width="85%">'.get_lang('Answer').'</th>
  147. </tr>
  148. </thead>
  149. <tbody>';
  150. $form->addHtml($html);
  151. if ($nb_options < 1) {
  152. $nb_options = 1;
  153. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'), 'normal');
  154. }
  155. for ($i = 1; $i <= $nb_options; $i++) {
  156. $renderer = &$form->defaultRenderer();
  157. $renderer->setElementTemplate(
  158. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  159. "option[$i]"
  160. );
  161. $form->addHtml('<tr>');
  162. $form->addHtml('<td>'.chr(64 + $i).'</td>');
  163. $form->addHtmlEditor(
  164. "option[$i]",
  165. null,
  166. null,
  167. false,
  168. $editorConfig
  169. );
  170. $form->addHtml('</tr>');
  171. }
  172. $form->addHtml('</table>');
  173. global $text;
  174. $group = [];
  175. // setting the save button here and not in the question class.php
  176. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessOptions', true);
  177. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreOptions', true);
  178. $group[] = $form->addButtonSave($text, 'submitQuestion', true);
  179. $form->addGroup($group);
  180. if (!empty($this->id)) {
  181. $form->setDefaults($defaults);
  182. } else {
  183. if ($this->isContent == 1) {
  184. $form->setDefaults($defaults);
  185. }
  186. }
  187. $form->setConstants(
  188. [
  189. 'nb_matches' => $nb_matches,
  190. 'nb_options' => $nb_options,
  191. ]
  192. );
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function processAnswersCreation($form, $exercise)
  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($exercise);
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function return_header($exercise, $counter = null, $score = null)
  232. {
  233. $header = parent::return_header($exercise, $counter, $score);
  234. $header .= '<table class="matching '.$this->question_table_class.'"><tr>';
  235. $header .= '<th>'.get_lang('ElementList').'</th>';
  236. if (!in_array($exercise->results_disabled, [
  237. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  238. //RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  239. ])
  240. ) {
  241. $header .= '<th>'.get_lang('YourChoice').'</th>';
  242. }
  243. if ($exercise->showExpectedChoice()) {
  244. $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
  245. $header .= '<th>'.get_lang('Status').'</th>';
  246. } else {
  247. $header .= '<th>'.get_lang('CorrespondsTo').'</th>';
  248. }
  249. $header .= '</tr>';
  250. return $header;
  251. }
  252. }