Draggable.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $defaults['orientation'] = in_array($this->extra, ['h', 'v']) ? $this->extra : 'h';
  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. $defaults['orientation'] = 'h';
  71. }
  72. for ($i = 1; $i <= $nb_matches; ++$i) {
  73. $matches[$i] = $i;
  74. }
  75. $form->addElement('hidden', 'nb_matches', $nb_matches);
  76. $form->addElement('hidden', 'nb_options', $nb_options);
  77. $form->addRadio(
  78. 'orientation',
  79. get_lang('Orientation'),
  80. ['h' => get_lang('Horizontal'), 'v' => get_lang('Vertical')]
  81. );
  82. // DISPLAY MATCHES
  83. $html = '<table class="table table-striped table-hover">
  84. <thead>
  85. <tr>
  86. <th width="85%">' . get_lang('Answer').'</th>
  87. <th width="15%">' . get_lang('MatchesTo').'</th>
  88. <th width="10">' . get_lang('Weighting').'</th>
  89. </tr>
  90. </thead>
  91. <tbody>';
  92. $form->addHeader(get_lang('MakeCorrespond'));
  93. $form->addHtml($html);
  94. if ($nb_matches < 1) {
  95. $nb_matches = 1;
  96. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'), 'normal');
  97. }
  98. for ($i = 1; $i <= $nb_matches; ++$i) {
  99. $renderer = &$form->defaultRenderer();
  100. $renderer->setElementTemplate(
  101. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  102. "answer[$i]"
  103. );
  104. $renderer->setElementTemplate(
  105. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  106. "matches[$i]"
  107. );
  108. $renderer->setElementTemplate(
  109. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  110. "weighting[$i]"
  111. );
  112. $form->addHtml('<tr>');
  113. $form->addText("answer[$i]", null);
  114. $form->addSelect("matches[$i]", null, $matches);
  115. $form->addText("weighting[$i]", null, true, ['value' => 10, 'style' => 'width: 60px;']);
  116. $form->addHtml('</tr>');
  117. }
  118. $form->addHtml('</tbody></table>');
  119. $renderer->setElementTemplate(
  120. '<div class="form-group"><div class="col-sm-offset-2">{element}',
  121. 'lessMatches'
  122. );
  123. $renderer->setElementTemplate('{element}</div></div>', 'moreMatches');
  124. global $text;
  125. $group = [
  126. $form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true),
  127. $form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true),
  128. $form->addButtonSave($text, 'submitQuestion', true)
  129. ];
  130. $form->addGroup($group);
  131. if (!empty($this->id)) {
  132. $form->setDefaults($defaults);
  133. } else {
  134. $form->setDefaults(['orientation' => 'h']);
  135. if ($this->isContent == 1) {
  136. $form->setDefaults($defaults);
  137. }
  138. }
  139. $form->setConstants(
  140. [
  141. 'nb_matches' => $nb_matches,
  142. 'nb_options' => $nb_options
  143. ]
  144. );
  145. }
  146. /**
  147. * Abstract function which creates the form to create / edit the answers of the question
  148. * @param FormValidator $form
  149. */
  150. public function processAnswersCreation($form)
  151. {
  152. $this->extra = $form->exportValue('orientation');
  153. $nb_matches = $form->getSubmitValue('nb_matches');
  154. $this->weighting = 0;
  155. $position = 0;
  156. $objAnswer = new Answer($this->id);
  157. // Insert the options
  158. for ($i = 1; $i <= $nb_matches; ++$i) {
  159. $position++;
  160. $objAnswer->createAnswer($position, 0, '', 0, $position);
  161. }
  162. // Insert the answers
  163. for ($i = 1; $i <= $nb_matches; ++$i) {
  164. $position++;
  165. $answer = $form->getSubmitValue('answer['.$i.']');
  166. $matches = $form->getSubmitValue('matches['.$i.']');
  167. $weighting = $form->getSubmitValue('weighting['.$i.']');
  168. $this->weighting += $weighting;
  169. $objAnswer->createAnswer(
  170. $answer,
  171. $matches,
  172. '',
  173. $weighting,
  174. $position
  175. );
  176. }
  177. $objAnswer->save();
  178. $this->save();
  179. }
  180. /**
  181. * Shows question title an description
  182. * @param string $feedback_type
  183. * @param int $counter
  184. * @param float $score
  185. * @return string
  186. */
  187. public function return_header($feedback_type = null, $counter = null, $score = null)
  188. {
  189. $header = parent::return_header($feedback_type, $counter, $score);
  190. $header .= '<table class="' . $this->question_table_class . '">
  191. <tr>
  192. <th>' . get_lang('YourChoice') . '</th>
  193. <th>' . get_lang('ExpectedChoice') . '</th>
  194. <th>' . get_lang('Status') . '</th>
  195. </tr>
  196. ';
  197. return $header;
  198. }
  199. }