MatchingDraggable.php 9.2 KB

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