|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
use ChamiloSession as Session; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class MultipleAnswer. |
|
9
|
|
|
* |
|
10
|
|
|
* This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER), |
|
11
|
|
|
* extending the class question |
|
12
|
|
|
* |
|
13
|
|
|
* @author Eric Marguin |
|
14
|
|
|
*/ |
|
15
|
|
|
class MultipleAnswer extends Question |
|
16
|
|
|
{ |
|
17
|
|
|
public $typePicture = 'mcma.png'; |
|
18
|
|
|
public $explanationLangVar = 'MultipleSelect'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct(); |
|
26
|
|
|
$this->type = MULTIPLE_ANSWER; |
|
27
|
|
|
$this->isContent = $this->getIsContent(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function createAnswersForm($form) |
|
34
|
|
|
{ |
|
35
|
|
|
$editorConfig = [ |
|
36
|
|
|
'ToolbarSet' => 'TestProposedAnswer', |
|
37
|
|
|
'Width' => '100%', |
|
38
|
|
|
'Height' => '125', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
// The previous default value was 2. See task #1759. |
|
42
|
|
|
$nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4; |
|
43
|
|
|
$nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
|
44
|
|
|
|
|
45
|
|
|
$obj_ex = Session::read('objExercise'); |
|
46
|
|
|
|
|
47
|
|
|
$form->addHeader(get_lang('Answers')); |
|
48
|
|
|
|
|
49
|
|
|
$html = '<table class="table table-striped table-hover"> |
|
50
|
|
|
<thead> |
|
51
|
|
|
<tr> |
|
52
|
|
|
<th width="10">'.get_lang('Number').'</th> |
|
53
|
|
|
<th width="10">'.get_lang('True').'</th> |
|
54
|
|
|
<th width="50%">'.get_lang('Answer').'</th> |
|
55
|
|
|
<th width="50%">'.get_lang('Comment').'</th> |
|
56
|
|
|
<th width="10">'.get_lang('Weighting').'</th> |
|
57
|
|
|
</tr> |
|
58
|
|
|
</thead> |
|
59
|
|
|
<tbody>'; |
|
60
|
|
|
|
|
61
|
|
|
$form->addHtml($html); |
|
62
|
|
|
|
|
63
|
|
|
$defaults = []; |
|
64
|
|
|
$correct = 0; |
|
65
|
|
|
$answer = false; |
|
66
|
|
|
if (!empty($this->iid)) { |
|
67
|
|
|
$answer = new Answer($this->iid); |
|
68
|
|
|
$answer->read(); |
|
69
|
|
|
if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
|
70
|
|
|
$nb_answers = $answer->nbrAnswers; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$form->addElement('hidden', 'nb_answers'); |
|
75
|
|
|
$boxes_names = []; |
|
76
|
|
|
|
|
77
|
|
|
if ($nb_answers < 1) { |
|
78
|
|
|
$nb_answers = 1; |
|
79
|
|
|
echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
for ($i = 1; $i <= $nb_answers; $i++) { |
|
83
|
|
|
$form->addHtml('<tr>'); |
|
84
|
|
|
if (is_object($answer)) { |
|
85
|
|
|
$defaults['answer['.$i.']'] = $answer->answer[$i]; |
|
86
|
|
|
$defaults['comment['.$i.']'] = $answer->comment[$i]; |
|
87
|
|
|
$defaults['weighting['.$i.']'] = float_format($answer->weighting[$i], 1); |
|
88
|
|
|
$defaults['correct['.$i.']'] = $answer->correct[$i]; |
|
89
|
|
|
} else { |
|
90
|
|
|
$defaults['answer[1]'] = get_lang('DefaultMultipleAnswer2'); |
|
91
|
|
|
$defaults['comment[1]'] = get_lang('DefaultMultipleComment2'); |
|
92
|
|
|
$defaults['correct[1]'] = true; |
|
93
|
|
|
$defaults['weighting[1]'] = 10; |
|
94
|
|
|
|
|
95
|
|
|
$defaults['answer[2]'] = get_lang('DefaultMultipleAnswer1'); |
|
96
|
|
|
$defaults['comment[2]'] = get_lang('DefaultMultipleComment1'); |
|
97
|
|
|
$defaults['correct[2]'] = false; |
|
98
|
|
|
$defaults['weighting[2]'] = -5; |
|
99
|
|
|
} |
|
100
|
|
|
$renderer = &$form->defaultRenderer(); |
|
101
|
|
|
|
|
102
|
|
|
$renderer->setElementTemplate( |
|
103
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
|
104
|
|
|
'correct['.$i.']' |
|
105
|
|
|
); |
|
106
|
|
|
$renderer->setElementTemplate( |
|
107
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
|
108
|
|
|
'counter['.$i.']' |
|
109
|
|
|
); |
|
110
|
|
|
$renderer->setElementTemplate( |
|
111
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
|
112
|
|
|
'answer['.$i.']' |
|
113
|
|
|
); |
|
114
|
|
|
$renderer->setElementTemplate( |
|
115
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
|
116
|
|
|
'comment['.$i.']' |
|
117
|
|
|
); |
|
118
|
|
|
$renderer->setElementTemplate( |
|
119
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
|
120
|
|
|
'weighting['.$i.']' |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
$answer_number = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"'); |
|
124
|
|
|
$answer_number->freeze(); |
|
125
|
|
|
|
|
126
|
|
|
$form->addElement( |
|
127
|
|
|
'checkbox', |
|
128
|
|
|
'correct['.$i.']', |
|
129
|
|
|
null, |
|
130
|
|
|
null, |
|
131
|
|
|
'class="checkbox" style="margin-left: 0em;"' |
|
132
|
|
|
); |
|
133
|
|
|
$boxes_names[] = 'correct['.$i.']'; |
|
134
|
|
|
|
|
135
|
|
|
$form->addHtmlEditor("answer[$i]", null, null, false, $editorConfig); |
|
136
|
|
|
$form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required'); |
|
137
|
|
|
|
|
138
|
|
|
$form->addHtmlEditor("comment[$i]", null, null, false, $editorConfig); |
|
139
|
|
|
|
|
140
|
|
|
$form->addElement('text', 'weighting['.$i.']', null, ['style' => "width: 60px;", 'value' => '0']); |
|
141
|
|
|
$form->addHtml('</tr>'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$form->addHtml('</tbody>'); |
|
145
|
|
|
$form->addHtml('</table>'); |
|
146
|
|
|
|
|
147
|
|
|
$form->add_multiple_required_rule( |
|
148
|
|
|
$boxes_names, |
|
149
|
|
|
get_lang('ChooseAtLeastOneCheckbox'), |
|
150
|
|
|
'multiple_required' |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
$buttonGroup = []; |
|
154
|
|
|
global $text; |
|
155
|
|
|
if ($obj_ex->edit_exercise_in_lp == true || |
|
156
|
|
|
(empty($this->exerciseList) && empty($obj_ex->iid)) |
|
157
|
|
|
) { |
|
158
|
|
|
// setting the save button here and not in the question class.php |
|
159
|
|
|
$buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true); |
|
160
|
|
|
$buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true); |
|
161
|
|
|
$buttonGroup[] = $form->addButton( |
|
162
|
|
|
'submitQuestion', |
|
163
|
|
|
$text, |
|
164
|
|
|
'check', |
|
165
|
|
|
'primary', |
|
166
|
|
|
'default', |
|
167
|
|
|
null, |
|
168
|
|
|
['id' => 'submit-question'], |
|
169
|
|
|
true |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$form->addGroup($buttonGroup); |
|
174
|
|
|
|
|
175
|
|
|
$defaults['correct'] = $correct; |
|
176
|
|
|
|
|
177
|
|
|
if (!empty($this->iid)) { |
|
178
|
|
|
$form->setDefaults($defaults); |
|
179
|
|
|
} else { |
|
180
|
|
|
if ($this->isContent == 1) { |
|
181
|
|
|
$form->setDefaults($defaults); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
$form->setConstants(['nb_answers' => $nb_answers]); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Validate question answers before saving. |
|
189
|
|
|
* |
|
190
|
|
|
* @param object $form The form object. |
|
191
|
|
|
* @return array|bool True if valid, or an array with errors if invalid. |
|
192
|
|
|
*/ |
|
193
|
|
|
public function validateAnswers($form) |
|
194
|
|
|
{ |
|
195
|
|
|
$nb_answers = $form->getSubmitValue('nb_answers'); |
|
196
|
|
|
$hasCorrectAnswer = false; |
|
197
|
|
|
$hasValidWeighting = false; |
|
198
|
|
|
$errors = []; |
|
199
|
|
|
$error_fields = []; |
|
200
|
|
|
|
|
201
|
|
|
for ($i = 1; $i <= $nb_answers; $i++) { |
|
202
|
|
|
$isCorrect = $form->getSubmitValue("correct[$i]"); |
|
203
|
|
|
$weighting = trim($form->getSubmitValue("weighting[$i]") ?? ''); |
|
204
|
|
|
|
|
205
|
|
|
if ($isCorrect) { |
|
206
|
|
|
$hasCorrectAnswer = true; |
|
207
|
|
|
|
|
208
|
|
|
if (is_numeric($weighting) && floatval($weighting) > 0) { |
|
209
|
|
|
$hasValidWeighting = true; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if (!$hasCorrectAnswer) { |
|
215
|
|
|
$errors[] = get_lang('AtLeastOneCorrectAnswerRequired'); |
|
216
|
|
|
$error_fields[] = "correct"; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if ($hasCorrectAnswer && !$hasValidWeighting) { |
|
220
|
|
|
// Only show if at least one correct answer exists but its weighting is not valid |
|
221
|
|
|
$errors[] = get_lang('AtLeastOneCorrectAnswerMustHaveAPositiveScore'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return empty($errors) ? true : ['errors' => $errors, 'error_fields' => $error_fields]; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* {@inheritdoc} |
|
229
|
|
|
*/ |
|
230
|
|
|
public function processAnswersCreation($form, $exercise) |
|
231
|
|
|
{ |
|
232
|
|
|
$validationResult = $this->validateAnswers($form); |
|
233
|
|
|
if ($validationResult !== true) { |
|
234
|
|
|
Display::addFlash(Display::return_message(implode("<br>", $validationResult['errors']), 'error')); |
|
235
|
|
|
|
|
236
|
|
|
return; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$questionWeighting = 0; |
|
240
|
|
|
$objAnswer = new Answer($this->iid); |
|
241
|
|
|
$nb_answers = $form->getSubmitValue('nb_answers'); |
|
242
|
|
|
|
|
243
|
|
|
for ($i = 1; $i <= $nb_answers; $i++) { |
|
244
|
|
|
$answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']'))); |
|
245
|
|
|
$comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']'))); |
|
246
|
|
|
$weighting = trim($form->getSubmitValue('weighting['.$i.']')); |
|
247
|
|
|
$goodAnswer = trim($form->getSubmitValue('correct['.$i.']')); |
|
248
|
|
|
|
|
249
|
|
|
if ($goodAnswer) { |
|
250
|
|
|
$weighting = abs($weighting); |
|
251
|
|
|
} else { |
|
252
|
|
|
$weighting = abs($weighting); |
|
253
|
|
|
$weighting = -$weighting; |
|
254
|
|
|
} |
|
255
|
|
|
if ($weighting > 0) { |
|
256
|
|
|
$questionWeighting += $weighting; |
|
257
|
|
|
} |
|
258
|
|
|
$objAnswer->createAnswer( |
|
259
|
|
|
$answer, |
|
260
|
|
|
$goodAnswer, |
|
261
|
|
|
$comment, |
|
262
|
|
|
$weighting, |
|
263
|
|
|
$i |
|
264
|
|
|
); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
// saves the answers into the data base |
|
268
|
|
|
$objAnswer->save(); |
|
269
|
|
|
|
|
270
|
|
|
// sets the total weighting of the question |
|
271
|
|
|
$this->updateWeighting($questionWeighting); |
|
272
|
|
|
$this->save($exercise); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* {@inheritdoc} |
|
277
|
|
|
*/ |
|
278
|
|
|
public function return_header(Exercise $exercise, $counter = null, $score = []) |
|
279
|
|
|
{ |
|
280
|
|
|
$header = parent::return_header($exercise, $counter, $score); |
|
281
|
|
|
$header .= '<table class="'.$this->question_table_class.'"><tr>'; |
|
282
|
|
|
|
|
283
|
|
|
$header .= '<th>'.get_lang('Choice').'</th>'; |
|
284
|
|
|
|
|
285
|
|
|
if ($exercise->showExpectedChoiceColumn()) { |
|
286
|
|
|
$header .= '<th>'.get_lang('ExpectedChoice').'</th>'; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$header .= '<th>'.get_lang('Answer').'</th>'; |
|
290
|
|
|
if ($exercise->showExpectedChoice()) { |
|
291
|
|
|
$header .= '<th>'.get_lang('Status').'</th>'; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
if (false === $exercise->hideComment) { |
|
295
|
|
|
$header .= '<th>'.get_lang('Comment').'</th>'; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
$header .= '</tr>'; |
|
299
|
|
|
|
|
300
|
|
|
return $header; |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|