Draggable.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Draggable
  5. *
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. */
  8. class Draggable extends Question
  9. {
  10. public static $typePicture = 'ordering.png';
  11. public 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. * @inheritdoc
  23. */
  24. public function createAnswersForm($form)
  25. {
  26. $defaults = array();
  27. $nb_matches = $nb_options = 2;
  28. $matches = array();
  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. $answerInfo = $answer->getAnswerByAutoId($answer->correct[$i]);
  56. $defaults['matches[' . $nb_matches . ']'] = isset($answerInfo['answer']) ? $answerInfo['answer'] : '';
  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, 'style' => 'width: 60px;']);
  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(
  161. $answer,
  162. $matches,
  163. '',
  164. $weighting,
  165. $position
  166. );
  167. }
  168. $objAnswer->save();
  169. $this->save();
  170. }
  171. /**
  172. * Shows question title an description
  173. * @param string $feedback_type
  174. * @param int $counter
  175. * @param float $score
  176. * @return string
  177. */
  178. public function return_header($feedback_type = null, $counter = null, $score = null)
  179. {
  180. $header = parent::return_header($feedback_type, $counter, $score);
  181. $header .= '<table class="' . $this->question_table_class . '">
  182. <tr>
  183. <th>' . get_lang('ElementList') . '</th>
  184. <th>' . get_lang('Status') . '</th>
  185. </tr>';
  186. return $header;
  187. }
  188. }