Draggable.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Draggable
  5. *
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. */
  8. class Draggable extends Question
  9. {
  10. static $typePicture = 'ordering.png';
  11. static $explanationLangVar = 'Draggable';
  12. /**
  13. * Class constructor
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->type = 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. if ($form->isSubmitted()) {
  32. $nb_matches = $form->getSubmitValue('nb_matches');
  33. $nb_options = $form->getSubmitValue('nb_options');
  34. if (isset($_POST['lessMatches'])) {
  35. $nb_matches--;
  36. }
  37. if (isset($_POST['moreMatches'])) {
  38. $nb_matches++;
  39. }
  40. if (isset($_POST['lessOptions'])) {
  41. $nb_options--;
  42. }
  43. if (isset($_POST['moreOptions'])) {
  44. $nb_options++;
  45. }
  46. } else if (!empty($this->id)) {
  47. $answer = new Answer($this->id);
  48. $answer->read();
  49. if (count($answer->nbrAnswers) > 0) {
  50. $nb_matches = $nb_options = 0;
  51. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  52. if ($answer->isCorrect($i)) {
  53. $nb_matches++;
  54. $defaults['answer[' . $nb_matches . ']'] = $answer->selectAnswer($i);
  55. $defaults['weighting[' . $nb_matches . ']'] = float_format($answer->selectWeighting($i), 1);
  56. $defaults['matches[' . $nb_matches . ']'] = $answer->correct[$i];
  57. } else {
  58. $nb_options++;
  59. $defaults['option[' . $nb_options . ']'] = $answer->selectAnswer($i);
  60. }
  61. }
  62. }
  63. } else {
  64. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  65. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  66. $defaults['matches[2]'] = '2';
  67. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  68. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  69. }
  70. for ($i = 1; $i <= $nb_matches; ++$i) {
  71. $matches[$i] = $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="85%">' . get_lang('Answer') . '</th>
  80. <th width="15%">' . get_lang('MatchesTo') . '</th>
  81. <th width="10">' . get_lang('Weighting') . '</th>
  82. </tr>
  83. </thead>
  84. <tbody>';
  85. $form->addHeader(get_lang('MakeCorrespond'));
  86. $form->addHtml($html);
  87. if ($nb_matches < 1) {
  88. $nb_matches = 1;
  89. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  90. }
  91. for ($i = 1; $i <= $nb_matches; ++$i) {
  92. $renderer = &$form->defaultRenderer();
  93. $renderer->setElementTemplate(
  94. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  95. "answer[$i]"
  96. );
  97. $renderer->setElementTemplate(
  98. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  99. "matches[$i]"
  100. );
  101. $renderer->setElementTemplate(
  102. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  103. "weighting[$i]"
  104. );
  105. $form->addHtml('<tr>');
  106. $form->addText("answer[$i]", null);
  107. $form->addSelect("matches[$i]", null, $matches);
  108. $form->addText("weighting[$i]", null, true, ['value' => 10]);
  109. $form->addHtml('</tr>');
  110. }
  111. $form->addHtml('</tbody></table>');
  112. $renderer->setElementTemplate(
  113. '<div class="form-group"><div class="col-sm-offset-2">{element}',
  114. 'lessMatches'
  115. );
  116. $renderer->setElementTemplate('{element}</div></div>', 'moreMatches');
  117. global $text;
  118. $group = [
  119. $form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true),
  120. $form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true),
  121. $form->addButtonSave($text, 'submitQuestion', true)
  122. ];
  123. $form->addGroup($group);
  124. if (!empty($this->id)) {
  125. $form->setDefaults($defaults);
  126. } else {
  127. if ($this->isContent == 1) {
  128. $form->setDefaults($defaults);
  129. }
  130. }
  131. $form->setConstants(
  132. [
  133. 'nb_matches' => $nb_matches,
  134. 'nb_options' => $nb_options
  135. ]
  136. );
  137. }
  138. /**
  139. * Abstract function which creates the form to create / edit the answers of the question
  140. * @param FormValidator $form
  141. */
  142. public function processAnswersCreation($form)
  143. {
  144. $nb_matches = $form->getSubmitValue('nb_matches');
  145. $this->weighting = 0;
  146. $position = 0;
  147. $objAnswer = new Answer($this->id);
  148. // Insert the options
  149. for ($i = 1; $i <= $nb_matches; ++$i) {
  150. $position++;
  151. $objAnswer->createAnswer($position, 0, '', 0, $position);
  152. }
  153. // Insert the answers
  154. for ($i = 1; $i <= $nb_matches; ++$i) {
  155. $position++;
  156. $answer = $form->getSubmitValue('answer[' . $i . ']');
  157. $matches = $form->getSubmitValue('matches[' . $i . ']');
  158. $weighting = $form->getSubmitValue('weighting[' . $i . ']');
  159. $this->weighting += $weighting;
  160. $objAnswer->createAnswer($answer, $matches, '', $weighting, $position);
  161. }
  162. $objAnswer->save();
  163. $this->save();
  164. }
  165. /**
  166. * Shows question title an description
  167. * @param string $feedback_type
  168. * @param int $counter
  169. * @param float $score
  170. * @return string
  171. */
  172. public function return_header($feedback_type = null, $counter = null, $score = null)
  173. {
  174. $header = parent::return_header($feedback_type, $counter, $score);
  175. $header .= '<table class="' . $this->question_table_class . '">
  176. <tr>
  177. <th>' . get_lang('ElementList') . '</th>
  178. <th>' . get_lang('Status') . '</th>
  179. </tr>';
  180. return $header;
  181. }
  182. }