MatchingDraggable.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 = array();
  27. $nb_matches = $nb_options = 2;
  28. $matches = array();
  29. $answer = null;
  30. $counter = 1;
  31. if (isset($this->id)) {
  32. $answer = new Answer($this->id);
  33. $answer->read();
  34. if (count($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 (count($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 = array(
  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->addText("option[$i]", null);
  164. $form->addHtmlEditor(
  165. "option[$i]",
  166. null,
  167. null,
  168. false,
  169. $editorConfig
  170. );
  171. $form->addHtml('</tr>');
  172. }
  173. $form->addHtml('</table>');
  174. global $text;
  175. $group = array();
  176. // setting the save button here and not in the question class.php
  177. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessOptions', true);
  178. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreOptions', true);
  179. $group[] = $form->addButtonSave($text, 'submitQuestion', true);
  180. $form->addGroup($group);
  181. if (!empty($this->id)) {
  182. $form->setDefaults($defaults);
  183. } else {
  184. if ($this->isContent == 1) {
  185. $form->setDefaults($defaults);
  186. }
  187. }
  188. $form->setConstants(
  189. array(
  190. 'nb_matches' => $nb_matches,
  191. 'nb_options' => $nb_options
  192. )
  193. );
  194. }
  195. /**
  196. * @inheritdoc
  197. */
  198. public function processAnswersCreation($form, $exercise)
  199. {
  200. $nb_matches = $form->getSubmitValue('nb_matches');
  201. $nb_options = $form->getSubmitValue('nb_options');
  202. $this->weighting = 0;
  203. $position = 0;
  204. $objAnswer = new Answer($this->id);
  205. // Insert the options
  206. for ($i = 1; $i <= $nb_options; ++$i) {
  207. $position++;
  208. $option = $form->getSubmitValue("option[$i]");
  209. $objAnswer->createAnswer($option, 0, '', 0, $position);
  210. }
  211. // Insert the answers
  212. for ($i = 1; $i <= $nb_matches; ++$i) {
  213. $position++;
  214. $answer = $form->getSubmitValue("answer[$i]");
  215. $matches = $form->getSubmitValue("matches[$i]");
  216. $weighting = $form->getSubmitValue("weighting[$i]");
  217. $this->weighting += $weighting;
  218. $objAnswer->createAnswer(
  219. $answer,
  220. $matches,
  221. '',
  222. $weighting,
  223. $position
  224. );
  225. }
  226. $objAnswer->save();
  227. $this->save($exercise);
  228. }
  229. /**
  230. * @inheritdoc
  231. */
  232. public function return_header($exercise, $counter = null, $score = null)
  233. {
  234. $header = parent::return_header($exercise, $counter, $score);
  235. $header .= '<table class="'.$this->question_table_class.'">
  236. <tr>
  237. <th>' . get_lang('ElementList') . '</th>
  238. <th>' . get_lang('YourChoice') . '</th>
  239. <th>' . get_lang('ExpectedChoice') . '</th>
  240. <th>' . get_lang('Status') . '</th>
  241. </tr>';
  242. return $header;
  243. }
  244. }