MatchingDraggable.php 8.4 KB

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