MatchingDraggable.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. } else if (!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. * Process the creation of answers
  197. * @param FormValidator $form
  198. */
  199. public function processAnswersCreation($form)
  200. {
  201. $nb_matches = $form->getSubmitValue('nb_matches');
  202. $nb_options = $form->getSubmitValue('nb_options');
  203. $this->weighting = 0;
  204. $position = 0;
  205. $objAnswer = new Answer($this->id);
  206. // Insert the options
  207. for ($i = 1; $i <= $nb_options; ++$i) {
  208. $position++;
  209. $option = $form->getSubmitValue("option[$i]");
  210. $objAnswer->createAnswer($option, 0, '', 0, $position);
  211. }
  212. // Insert the answers
  213. for ($i = 1; $i <= $nb_matches; ++$i) {
  214. $position++;
  215. $answer = $form->getSubmitValue("answer[$i]");
  216. $matches = $form->getSubmitValue("matches[$i]");
  217. $weighting = $form->getSubmitValue("weighting[$i]");
  218. $this->weighting += $weighting;
  219. $objAnswer->createAnswer(
  220. $answer,
  221. $matches,
  222. '',
  223. $weighting,
  224. $position
  225. );
  226. }
  227. $objAnswer->save();
  228. $this->save();
  229. }
  230. /**
  231. * Shows question title an description
  232. * @param string $feedback_type
  233. * @param int $counter
  234. * @param float $score
  235. * @return string The header HTML code
  236. */
  237. public function return_header($feedback_type = null, $counter = null, $score = null)
  238. {
  239. $header = parent::return_header($feedback_type, $counter, $score);
  240. $header .= '<table class="'.$this->question_table_class.'">
  241. <tr>
  242. <th>' . get_lang('ElementList') . '</th>
  243. <th>' . get_lang('YourChoice') . '</th>
  244. <th>' . get_lang('ExpectedChoice') . '</th>
  245. <th>' . get_lang('Status') . '</th>
  246. </tr>';
  247. return $header;
  248. }
  249. }