Draggable.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 = [];
  27. $nb_matches = $nb_options = 2;
  28. $matches = [];
  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. } elseif (!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 ($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('ChooseOrientation'),
  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. * {@inheritdoc}
  148. */
  149. public function processAnswersCreation($form, $exercise)
  150. {
  151. $this->extra = $form->exportValue('orientation');
  152. $nb_matches = $form->getSubmitValue('nb_matches');
  153. $this->weighting = 0;
  154. $position = 0;
  155. $objAnswer = new Answer($this->id);
  156. // Insert the options
  157. for ($i = 1; $i <= $nb_matches; $i++) {
  158. $position++;
  159. $objAnswer->createAnswer($position, 0, '', 0, $position);
  160. }
  161. // Insert the answers
  162. for ($i = 1; $i <= $nb_matches; $i++) {
  163. $position++;
  164. $answer = $form->getSubmitValue('answer['.$i.']');
  165. $matches = $form->getSubmitValue('matches['.$i.']');
  166. $weighting = $form->getSubmitValue('weighting['.$i.']');
  167. $this->weighting += $weighting;
  168. $objAnswer->createAnswer(
  169. $answer,
  170. $matches,
  171. '',
  172. $weighting,
  173. $position
  174. );
  175. }
  176. $objAnswer->save();
  177. $this->save($exercise);
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function return_header(Exercise $exercise, $counter = null, $score = [])
  183. {
  184. $header = parent::return_header($exercise, $counter, $score);
  185. $header .= '<table class="'.$this->question_table_class.'"><tr>';
  186. if ($exercise->showExpectedChoice()) {
  187. $header .= '<th>'.get_lang('YourChoice').'</th>';
  188. if ($exercise->showExpectedChoiceColumn()) {
  189. $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
  190. }
  191. } else {
  192. $header .= '<th>'.get_lang('ElementList').'</th>';
  193. }
  194. $header .= '<th>'.get_lang('Status').'</th>';
  195. $header .= '</tr>';
  196. return $header;
  197. }
  198. }