1 | <?php |
||
2 | |||
3 | /* For licensing terms, see /license.txt */ |
||
4 | |||
5 | /** |
||
6 | * Class Draggable. |
||
7 | * |
||
8 | * @author Angel Fernando Quiroz Campos <[email protected]> |
||
9 | */ |
||
10 | class Draggable extends Question |
||
11 | { |
||
12 | public $typePicture = 'ordering.png'; |
||
13 | public $explanationLangVar = 'Sequence ordering'; |
||
14 | |||
15 | public function __construct() |
||
16 | { |
||
17 | parent::__construct(); |
||
18 | $this->type = DRAGGABLE; |
||
19 | $this->isContent = $this->getIsContent(); |
||
20 | } |
||
21 | |||
22 | public function createAnswersForm($form) |
||
23 | { |
||
24 | $defaults = []; |
||
25 | $nb_matches = $nb_options = 2; |
||
26 | $matches = []; |
||
27 | $answer = null; |
||
28 | if ($form->isSubmitted()) { |
||
29 | $nb_matches = $form->getSubmitValue('nb_matches'); |
||
30 | $nb_options = $form->getSubmitValue('nb_options'); |
||
31 | |||
32 | if (isset($_POST['lessMatches'])) { |
||
33 | $nb_matches--; |
||
34 | } |
||
35 | |||
36 | if (isset($_POST['moreMatches'])) { |
||
37 | $nb_matches++; |
||
38 | } |
||
39 | |||
40 | if (isset($_POST['lessOptions'])) { |
||
41 | $nb_options--; |
||
42 | } |
||
43 | |||
44 | if (isset($_POST['moreOptions'])) { |
||
45 | $nb_options++; |
||
46 | } |
||
47 | } elseif (!empty($this->id)) { |
||
48 | $defaults['orientation'] = in_array($this->extra, ['h', 'v']) ? $this->extra : 'h'; |
||
49 | |||
50 | $answer = new Answer($this->id); |
||
51 | $answer->read(); |
||
52 | |||
53 | if ($answer->nbrAnswers > 0) { |
||
54 | $nb_matches = $nb_options = 0; |
||
55 | for ($i = 1; $i <= $answer->nbrAnswers; $i++) { |
||
56 | if ($answer->isCorrect($i)) { |
||
57 | $nb_matches++; |
||
58 | $defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i); |
||
59 | $defaults['weighting['.$nb_matches.']'] = float_format($answer->selectWeighting($i), 1); |
||
60 | $answerInfo = $answer->getAnswerByAutoId($answer->correct[$i]); |
||
61 | $defaults['matches['.$nb_matches.']'] = isset($answerInfo['answer']) ? $answerInfo['answer'] : ''; |
||
62 | } else { |
||
63 | $nb_options++; |
||
64 | $defaults['option['.$nb_options.']'] = $answer->selectAnswer($i); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | } else { |
||
69 | $defaults['answer[1]'] = get_lang('First step'); |
||
70 | $defaults['answer[2]'] = get_lang('Second step'); |
||
71 | $defaults['matches[2]'] = '2'; |
||
72 | $defaults['option[1]'] = get_lang('Note down the address'); |
||
73 | $defaults['option[2]'] = get_lang('Contact the emergency services'); |
||
74 | $defaults['orientation'] = 'h'; |
||
75 | } |
||
76 | |||
77 | for ($i = 1; $i <= $nb_matches; $i++) { |
||
78 | $matches[$i] = $i; |
||
79 | } |
||
80 | |||
81 | $form->addElement('hidden', 'nb_matches', $nb_matches); |
||
82 | $form->addElement('hidden', 'nb_options', $nb_options); |
||
83 | |||
84 | $form->addRadio( |
||
85 | 'orientation', |
||
86 | get_lang('Choose orientation'), |
||
87 | ['h' => get_lang('Horizontal'), 'v' => get_lang('Vertical')] |
||
88 | ); |
||
89 | |||
90 | // DISPLAY MATCHES |
||
91 | $html = '<table class="table table-striped table-hover"> |
||
92 | <thead> |
||
93 | <tr> |
||
94 | <th width="85%">'.get_lang('Answer').'</th> |
||
95 | <th width="15%">'.get_lang('Matches To').'</th> |
||
96 | <th width="10">'.get_lang('Score').'</th> |
||
97 | </tr> |
||
98 | </thead> |
||
99 | <tbody>'; |
||
100 | |||
101 | $form->addHeader(get_lang('Match them')); |
||
102 | $form->addHtml($html); |
||
103 | |||
104 | if ($nb_matches < 1) { |
||
105 | $nb_matches = 1; |
||
106 | echo Display::return_message(get_lang('You have to create at least one answer'), 'normal'); |
||
107 | } |
||
108 | |||
109 | for ($i = 1; $i <= $nb_matches; $i++) { |
||
110 | $renderer = &$form->defaultRenderer(); |
||
111 | $renderer->setElementTemplate( |
||
112 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
113 | "answer[$i]" |
||
114 | ); |
||
115 | |||
116 | $renderer->setElementTemplate( |
||
117 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
118 | "matches[$i]" |
||
119 | ); |
||
120 | |||
121 | $renderer->setElementTemplate( |
||
122 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
123 | "weighting[$i]" |
||
124 | ); |
||
125 | |||
126 | $form->addHtml('<tr>'); |
||
127 | $form->addText("answer[$i]", null); |
||
128 | $form->addSelect("matches[$i]", null, $matches); |
||
129 | $form->addText("weighting[$i]", null, true, ['value' => 10, 'style' => 'width: 60px;']); |
||
130 | $form->addHtml('</tr>'); |
||
131 | } |
||
132 | |||
133 | $form->addHtml('</tbody></table>'); |
||
134 | |||
135 | $renderer->setElementTemplate( |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
136 | '<div class="form-group"><div class="col-sm-offset-2">{element}', |
||
137 | 'lessMatches' |
||
138 | ); |
||
139 | $renderer->setElementTemplate('{element}</div></div>', 'moreMatches'); |
||
140 | |||
141 | global $text; |
||
142 | |||
143 | $group = [ |
||
144 | $form->addButtonDelete(get_lang('Remove element'), 'lessMatches', true), |
||
145 | $form->addButtonCreate(get_lang('Add element'), 'moreMatches', true), |
||
146 | $form->addButtonSave($text, 'submitQuestion', true), |
||
147 | ]; |
||
148 | |||
149 | $form->addGroup($group); |
||
150 | |||
151 | if (!empty($this->id)) { |
||
152 | $form->setDefaults($defaults); |
||
153 | } else { |
||
154 | $form->setDefaults(['orientation' => 'h']); |
||
155 | |||
156 | if (1 == $this->isContent) { |
||
157 | $form->setDefaults($defaults); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $form->setConstants( |
||
162 | [ |
||
163 | 'nb_matches' => $nb_matches, |
||
164 | 'nb_options' => $nb_options, |
||
165 | ] |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | public function processAnswersCreation($form, $exercise) |
||
170 | { |
||
171 | $this->extra = $form->exportValue('orientation'); |
||
172 | $nb_matches = $form->getSubmitValue('nb_matches'); |
||
173 | $this->weighting = 0; |
||
174 | $position = 0; |
||
175 | $objAnswer = new Answer($this->id); |
||
176 | // Insert the options |
||
177 | for ($i = 1; $i <= $nb_matches; $i++) { |
||
178 | $position++; |
||
179 | $objAnswer->createAnswer($position, 0, '', 0, $position); |
||
180 | } |
||
181 | |||
182 | // Insert the answers |
||
183 | for ($i = 1; $i <= $nb_matches; $i++) { |
||
184 | $position++; |
||
185 | $answer = $form->getSubmitValue('answer['.$i.']'); |
||
186 | $matches = $form->getSubmitValue('matches['.$i.']'); |
||
187 | $weighting = $form->getSubmitValue('weighting['.$i.']'); |
||
188 | $this->weighting += $weighting; |
||
189 | $objAnswer->createAnswer( |
||
190 | $answer, |
||
191 | $matches, |
||
192 | '', |
||
193 | $weighting, |
||
194 | $position |
||
195 | ); |
||
196 | } |
||
197 | |||
198 | $objAnswer->save(); |
||
199 | $this->save($exercise); |
||
200 | } |
||
201 | |||
202 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
203 | { |
||
204 | $header = parent::return_header($exercise, $counter, $score); |
||
205 | $header .= '<table class="'.$this->questionTableClass.'"><tr>'; |
||
206 | |||
207 | if ($exercise->showExpectedChoice()) { |
||
208 | $header .= '<th>'.get_lang('Your choice').'</th>'; |
||
209 | if ($exercise->showExpectedChoiceColumn()) { |
||
210 | $header .= '<th>'.get_lang('Expected choice').'</th>'; |
||
211 | } |
||
212 | } else { |
||
213 | $header .= '<th>'.get_lang('Elements list').'</th>'; |
||
214 | } |
||
215 | $header .= '<th class="text-center">'.get_lang('Status').'</th>'; |
||
216 | $header .= '</tr>'; |
||
217 | |||
218 | return $header; |
||
219 | } |
||
220 | } |
||
221 |