Draggable.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * 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. $answerInfo = $answer->getAnswerByAutoId($answer->correct[$i]);
  57. $defaults['matches[' . $nb_matches . ']'] = isset($answerInfo['answer']) ? $answerInfo['answer'] : '';
  58. } else {
  59. $nb_options++;
  60. $defaults['option[' . $nb_options . ']'] = $answer->selectAnswer($i);
  61. }
  62. }
  63. }
  64. } else {
  65. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  66. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  67. $defaults['matches[2]'] = '2';
  68. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  69. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  70. }
  71. for ($i = 1; $i <= $nb_matches; ++$i) {
  72. $matches[$i] = $i;
  73. }
  74. $form->addElement('hidden', 'nb_matches', $nb_matches);
  75. $form->addElement('hidden', 'nb_options', $nb_options);
  76. // DISPLAY MATCHES
  77. $html = '<table class="table table-striped table-hover">
  78. <thead>
  79. <tr>
  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->addText("answer[$i]", null);
  108. $form->addSelect("matches[$i]", null, $matches);
  109. $form->addText("weighting[$i]", null, true, ['value' => 10, 'style' => 'width: 60px;']);
  110. $form->addHtml('</tr>');
  111. }
  112. $form->addHtml('</tbody></table>');
  113. $renderer->setElementTemplate(
  114. '<div class="form-group"><div class="col-sm-offset-2">{element}',
  115. 'lessMatches'
  116. );
  117. $renderer->setElementTemplate('{element}</div></div>', 'moreMatches');
  118. global $text;
  119. $group = [
  120. $form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true),
  121. $form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true),
  122. $form->addButtonSave($text, 'submitQuestion', true)
  123. ];
  124. $form->addGroup($group);
  125. if (!empty($this->id)) {
  126. $form->setDefaults($defaults);
  127. } else {
  128. if ($this->isContent == 1) {
  129. $form->setDefaults($defaults);
  130. }
  131. }
  132. $form->setConstants(
  133. [
  134. 'nb_matches' => $nb_matches,
  135. 'nb_options' => $nb_options
  136. ]
  137. );
  138. }
  139. /**
  140. * Abstract function which creates the form to create / edit the answers of the question
  141. * @param FormValidator $form
  142. */
  143. public function processAnswersCreation($form)
  144. {
  145. $nb_matches = $form->getSubmitValue('nb_matches');
  146. $this->weighting = 0;
  147. $position = 0;
  148. $objAnswer = new Answer($this->id);
  149. // Insert the options
  150. for ($i = 1; $i <= $nb_matches; ++$i) {
  151. $position++;
  152. $objAnswer->createAnswer($position, 0, '', 0, $position);
  153. }
  154. // Insert the answers
  155. for ($i = 1; $i <= $nb_matches; ++$i) {
  156. $position++;
  157. $answer = $form->getSubmitValue('answer[' . $i . ']');
  158. $matches = $form->getSubmitValue('matches[' . $i . ']');
  159. $weighting = $form->getSubmitValue('weighting[' . $i . ']');
  160. $this->weighting += $weighting;
  161. $objAnswer->createAnswer(
  162. $answer,
  163. $matches,
  164. '',
  165. $weighting,
  166. $position
  167. );
  168. }
  169. $objAnswer->save();
  170. $this->save();
  171. }
  172. /**
  173. * Shows question title an description
  174. * @param string $feedback_type
  175. * @param int $counter
  176. * @param float $score
  177. * @return string
  178. */
  179. public function return_header($feedback_type = null, $counter = null, $score = null)
  180. {
  181. $header = parent::return_header($feedback_type, $counter, $score);
  182. $header .= '<table class="' . $this->question_table_class . '">
  183. <tr>
  184. <th>' . get_lang('ElementList') . '</th>
  185. <th>' . get_lang('Status') . '</th>
  186. </tr>';
  187. return $header;
  188. }
  189. }