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 = 'Draggable'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class constructor. |
17
|
|
|
*/ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
parent::__construct(); |
21
|
|
|
$this->type = DRAGGABLE; |
22
|
|
|
$this->isContent = $this->getIsContent(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function createAnswersForm($form) |
29
|
|
|
{ |
30
|
|
|
$defaults = []; |
31
|
|
|
$nb_matches = $nb_options = 2; |
32
|
|
|
$matches = []; |
33
|
|
|
$answer = null; |
34
|
|
|
if ($form->isSubmitted()) { |
35
|
|
|
$nb_matches = $form->getSubmitValue('nb_matches'); |
36
|
|
|
$nb_options = $form->getSubmitValue('nb_options'); |
37
|
|
|
|
38
|
|
|
if (isset($_POST['lessMatches'])) { |
39
|
|
|
$nb_matches--; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (isset($_POST['moreMatches'])) { |
43
|
|
|
$nb_matches++; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (isset($_POST['lessOptions'])) { |
47
|
|
|
$nb_options--; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (isset($_POST['moreOptions'])) { |
51
|
|
|
$nb_options++; |
52
|
|
|
} |
53
|
|
|
} elseif (!empty($this->iid)) { |
54
|
|
|
$defaults['orientation'] = in_array($this->extra, ['h', 'v']) ? $this->extra : 'v'; |
55
|
|
|
|
56
|
|
|
$answer = new Answer($this->iid); |
57
|
|
|
$answer->read(); |
58
|
|
|
|
59
|
|
|
if ($answer->nbrAnswers > 0) { |
60
|
|
|
$nb_matches = $nb_options = 0; |
61
|
|
|
for ($i = 1; $i <= $answer->nbrAnswers; $i++) { |
62
|
|
|
if ($answer->isCorrect($i)) { |
63
|
|
|
$nb_matches++; |
64
|
|
|
$defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i); |
65
|
|
|
$defaults['weighting['.$nb_matches.']'] = float_format($answer->selectWeighting($i), 1); |
66
|
|
|
$answerInfo = $answer->getAnswerByAutoId($answer->correct[$i]); |
67
|
|
|
$defaults['matches['.$nb_matches.']'] = isset($answerInfo['answer']) ? $answerInfo['answer'] : ''; |
68
|
|
|
} else { |
69
|
|
|
$nb_options++; |
70
|
|
|
$defaults['option['.$nb_options.']'] = $answer->selectAnswer($i); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
$defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1'); |
76
|
|
|
$defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2'); |
77
|
|
|
$defaults['matches[2]'] = '2'; |
78
|
|
|
$defaults['option[1]'] = get_lang('DefaultMatchingOptA'); |
79
|
|
|
$defaults['option[2]'] = get_lang('DefaultMatchingOptB'); |
80
|
|
|
$defaults['orientation'] = 'v'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
for ($i = 1; $i <= $nb_matches; $i++) { |
84
|
|
|
$matches[$i] = $i; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$form->addElement('hidden', 'nb_matches', $nb_matches); |
88
|
|
|
$form->addElement('hidden', 'nb_options', $nb_options); |
89
|
|
|
|
90
|
|
|
$form->addRadio( |
91
|
|
|
'orientation', |
92
|
|
|
get_lang('ChooseOrientation'), |
93
|
|
|
['v' => get_lang('Vertical'), 'h' => get_lang('Horizontal')] |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
// DISPLAY MATCHES |
97
|
|
|
$html = '<table class="table table-striped table-hover"> |
98
|
|
|
<thead> |
99
|
|
|
<tr> |
100
|
|
|
<th width="85%">'.get_lang('Answer').'</th> |
101
|
|
|
<th width="15%">'.get_lang('MatchesTo').'</th> |
102
|
|
|
<th width="10">'.get_lang('Weighting').'</th> |
103
|
|
|
</tr> |
104
|
|
|
</thead> |
105
|
|
|
<tbody>'; |
106
|
|
|
|
107
|
|
|
$form->addHeader(get_lang('MakeCorrespond')); |
108
|
|
|
$form->addHtml($html); |
109
|
|
|
|
110
|
|
|
if ($nb_matches < 1) { |
111
|
|
|
$nb_matches = 1; |
112
|
|
|
echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'), 'normal'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
for ($i = 1; $i <= $nb_matches; $i++) { |
116
|
|
|
$renderer = &$form->defaultRenderer(); |
117
|
|
|
$renderer->setElementTemplate( |
118
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
119
|
|
|
"answer[$i]" |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$renderer->setElementTemplate( |
123
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
124
|
|
|
"matches[$i]" |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$renderer->setElementTemplate( |
128
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
129
|
|
|
"weighting[$i]" |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$form->addHtml('<tr>'); |
133
|
|
|
$form->addText("answer[$i]", null); |
134
|
|
|
$form->addSelect("matches[$i]", null, $matches); |
135
|
|
|
$form->addText("weighting[$i]", null, true, ['value' => 10, 'style' => 'width: 60px;']); |
136
|
|
|
$form->addHtml('</tr>'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$form->addHtml('</tbody></table>'); |
140
|
|
|
|
141
|
|
|
$renderer->setElementTemplate( |
|
|
|
|
142
|
|
|
'<div class="form-group"><div class="col-sm-offset-2">{element}', |
143
|
|
|
'lessMatches' |
144
|
|
|
); |
145
|
|
|
$renderer->setElementTemplate('{element}</div></div>', 'moreMatches'); |
146
|
|
|
|
147
|
|
|
global $text; |
148
|
|
|
|
149
|
|
|
$group = [ |
150
|
|
|
$form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true), |
151
|
|
|
$form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true), |
152
|
|
|
$form->addButtonSave($text, 'submitQuestion', true), |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
$form->addGroup($group); |
156
|
|
|
|
157
|
|
|
if (!empty($this->iid)) { |
158
|
|
|
$form->setDefaults($defaults); |
159
|
|
|
} else { |
160
|
|
|
$form->setDefaults(['orientation' => 'v']); |
161
|
|
|
|
162
|
|
|
if (1 == $this->isContent) { |
163
|
|
|
$form->setDefaults($defaults); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$form->setConstants( |
168
|
|
|
[ |
169
|
|
|
'nb_matches' => $nb_matches, |
170
|
|
|
'nb_options' => $nb_options, |
171
|
|
|
] |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function processAnswersCreation($form, $exercise) |
179
|
|
|
{ |
180
|
|
|
$this->extra = $form->exportValue('orientation'); |
181
|
|
|
$nb_matches = $form->getSubmitValue('nb_matches'); |
182
|
|
|
$this->weighting = 0; |
183
|
|
|
$position = 0; |
184
|
|
|
$objAnswer = new Answer($this->iid); |
185
|
|
|
// Insert the options |
186
|
|
|
for ($i = 1; $i <= $nb_matches; $i++) { |
187
|
|
|
$position++; |
188
|
|
|
$objAnswer->createAnswer($position, 0, '', 0, $position); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Insert the answers |
192
|
|
|
for ($i = 1; $i <= $nb_matches; $i++) { |
193
|
|
|
$position++; |
194
|
|
|
$answer = $form->getSubmitValue('answer['.$i.']'); |
195
|
|
|
$matches = $form->getSubmitValue('matches['.$i.']'); |
196
|
|
|
$weighting = $form->getSubmitValue('weighting['.$i.']'); |
197
|
|
|
$this->weighting += $weighting; |
198
|
|
|
$objAnswer->createAnswer( |
199
|
|
|
$answer, |
200
|
|
|
$matches, |
201
|
|
|
'', |
202
|
|
|
$weighting, |
203
|
|
|
$position |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$objAnswer->save(); |
208
|
|
|
$this->save($exercise); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function return_header(Exercise $exercise, $counter = null, $score = []) |
215
|
|
|
{ |
216
|
|
|
$header = parent::return_header($exercise, $counter, $score); |
217
|
|
|
$header .= '<table class="'.$this->question_table_class.'"><tr>'; |
218
|
|
|
|
219
|
|
|
if ($exercise->showExpectedChoice()) { |
220
|
|
|
$header .= '<th>'.get_lang('YourChoice').'</th>'; |
221
|
|
|
if ($exercise->showExpectedChoiceColumn()) { |
222
|
|
|
$header .= '<th>'.get_lang('ExpectedChoice').'</th>'; |
223
|
|
|
} |
224
|
|
|
} else { |
225
|
|
|
$header .= '<th>'.get_lang('ElementList').'</th>'; |
226
|
|
|
$header .= '<th>'.get_lang('YourChoice').'</th>'; |
227
|
|
|
$header .= '<th>'.get_lang('ExpectedChoice').'</th>'; |
228
|
|
|
} |
229
|
|
|
$header .= '<th>'.get_lang('Status').'</th>'; |
230
|
|
|
$header .= '</tr>'; |
231
|
|
|
|
232
|
|
|
return $header; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|