MatchingDraggable.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 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. * function which redefines Question::createAnswersForm
  23. * @param FormValidator $form
  24. */
  25. public function createAnswersForm($form)
  26. {
  27. $defaults = array();
  28. $nb_matches = $nb_options = 2;
  29. $matches = array();
  30. $answer = null;
  31. $counter = 1;
  32. if (isset($this->id)) {
  33. $answer = new Answer($this->id);
  34. $answer->read();
  35. if (count($answer->nbrAnswers) > 0) {
  36. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  37. $correct = $answer->isCorrect($i);
  38. if (empty($correct)) {
  39. $matches[$answer->selectAutoId($i)] = chr(64 + $counter);
  40. $counter++;
  41. }
  42. }
  43. }
  44. }
  45. if ($form->isSubmitted()) {
  46. $nb_matches = $form->getSubmitValue('nb_matches');
  47. $nb_options = $form->getSubmitValue('nb_options');
  48. if (isset($_POST['lessOptions'])) {
  49. $nb_matches--;
  50. $nb_options--;
  51. }
  52. if (isset($_POST['moreOptions'])) {
  53. $nb_matches++;
  54. $nb_options++;
  55. }
  56. } else if (!empty($this->id)) {
  57. if (count($answer->nbrAnswers) > 0) {
  58. $nb_matches = $nb_options = 0;
  59. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  60. if ($answer->isCorrect($i)) {
  61. $nb_matches++;
  62. $defaults['answer[' . $nb_matches . ']'] = $answer->selectAnswer($i);
  63. $defaults['weighting[' . $nb_matches . ']'] = float_format($answer->selectWeighting($i), 1);
  64. $defaults['matches[' . $nb_matches . ']'] = $answer->correct[$i];
  65. } else {
  66. $nb_options++;
  67. $defaults['option[' . $nb_options . ']'] = $answer->selectAnswer($i);
  68. }
  69. }
  70. }
  71. } else {
  72. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  73. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  74. $defaults['matches[2]'] = '2';
  75. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  76. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  77. }
  78. if (empty($matches)) {
  79. for ($i = 1; $i <= $nb_options; ++$i) {
  80. // fill the array with A, B, C.....
  81. $matches[$i] = chr(64 + $i);
  82. }
  83. } else {
  84. for ($i = $counter; $i <= $nb_options; ++$i) {
  85. // fill the array with A, B, C.....
  86. $matches[$i] = chr(64 + $i);
  87. }
  88. }
  89. $form->addElement('hidden', 'nb_matches', $nb_matches);
  90. $form->addElement('hidden', 'nb_options', $nb_options);
  91. // DISPLAY MATCHES
  92. $html = '<table class="table table-striped table-hover">
  93. <thead>
  94. <tr>
  95. <th width="10">' . get_lang('Number') . '</th>
  96. <th width="85%">' . get_lang('Answer') . '</th>
  97. <th width="15%">' . get_lang('MatchesTo') . '</th>
  98. <th width="10">' . get_lang('Weighting') . '</th>
  99. </tr>
  100. </thead>
  101. <tbody>';
  102. $form->addHeader(get_lang('MakeCorrespond'));
  103. $form->addHtml($html);
  104. if ($nb_matches < 1) {
  105. $nb_matches = 1;
  106. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  107. }
  108. for ($i = 1; $i <= $nb_matches; ++$i) {
  109. $renderer = &$form->defaultRenderer();
  110. $renderer->setElementTemplate(
  111. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  112. "answer[$i]"
  113. );
  114. $renderer->setElementTemplate(
  115. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  116. "matches[$i]"
  117. );
  118. $renderer->setElementTemplate(
  119. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  120. "weighting[$i]"
  121. );
  122. $form->addHtml('<tr>');
  123. $form->addHtml("<td>$i</td>");
  124. $form->addText("answer[$i]", null);
  125. $form->addSelect("matches[$i]", null, $matches);
  126. $form->addText("weighting[$i]", null, true, ['style' => 'width: 60px;', 'value' => 10]);
  127. $form->addHtml('</tr>');
  128. }
  129. $form->addHtml('</tbody></table>');
  130. $group = array();
  131. $form->addGroup($group);
  132. // DISPLAY OPTIONS
  133. $html = '<table class="table table-striped table-hover">
  134. <thead>
  135. <tr>
  136. <th width="15%">' . get_lang('Number') . '</th>
  137. <th width="85%">' . get_lang('Answer') . '</th>
  138. </tr>
  139. </thead>
  140. <tbody>';
  141. $form->addHtml($html);
  142. if ($nb_options < 1) {
  143. $nb_options = 1;
  144. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  145. }
  146. for ($i = 1; $i <= $nb_options; ++$i) {
  147. $renderer = &$form->defaultRenderer();
  148. $renderer->setElementTemplate(
  149. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  150. "option[$i]"
  151. );
  152. $form->addHtml('<tr>');
  153. $form->addHtml('<td>' . chr(64 + $i) . '</td>');
  154. $form->addText("option[$i]", null);
  155. $form->addHtml('</tr>');
  156. }
  157. $form->addHtml('</table>');
  158. $group = array();
  159. global $text;
  160. // setting the save button here and not in the question class.php
  161. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessOptions', true);
  162. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreOptions', true);
  163. $group[] = $form->addButtonSave($text, 'submitQuestion', true);
  164. $form->addGroup($group);
  165. if (!empty($this->id)) {
  166. $form->setDefaults($defaults);
  167. } else {
  168. if ($this->isContent == 1) {
  169. $form->setDefaults($defaults);
  170. }
  171. }
  172. $form->setConstants(
  173. array(
  174. 'nb_matches' => $nb_matches,
  175. 'nb_options' => $nb_options
  176. )
  177. );
  178. }
  179. /**
  180. * Process the creation of answers
  181. * @param FormValidator $form
  182. */
  183. public function processAnswersCreation($form)
  184. {
  185. $nb_matches = $form->getSubmitValue('nb_matches');
  186. $nb_options = $form->getSubmitValue('nb_options');
  187. $this->weighting = 0;
  188. $position = 0;
  189. $objAnswer = new Answer($this->id);
  190. // Insert the options
  191. for ($i = 1; $i <= $nb_options; ++$i) {
  192. $position++;
  193. $option = $form->getSubmitValue("option[$i]");
  194. $objAnswer->createAnswer($option, 0, '', 0, $position);
  195. }
  196. // Insert the answers
  197. for ($i = 1; $i <= $nb_matches; ++$i) {
  198. $position++;
  199. $answer = $form->getSubmitValue("answer[$i]");
  200. $matches = $form->getSubmitValue("matches[$i]");
  201. $weighting = $form->getSubmitValue("weighting[$i]");
  202. $this->weighting += $weighting;
  203. $objAnswer->createAnswer(
  204. $answer,
  205. $matches,
  206. '',
  207. $weighting,
  208. $position
  209. );
  210. }
  211. $objAnswer->save();
  212. $this->save();
  213. }
  214. /**
  215. * Shows question title an description
  216. * @param string $feedback_type
  217. * @param int $counter
  218. * @param float $score
  219. * @return string The header HTML code
  220. */
  221. public function return_header($feedback_type = null, $counter = null, $score = null)
  222. {
  223. $header = parent::return_header($feedback_type, $counter, $score);
  224. $header .= '<table class="' . $this->question_table_class . '">
  225. <tr>
  226. <th>' . get_lang('ElementList') . '</th>
  227. <th>' . get_lang('CorrespondsTo') . '</th>
  228. </tr>';
  229. return $header;
  230. }
  231. }