1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
use Chamilo\CourseBundle\Entity\CQuizAnswer; |
6
|
|
|
use ChamiloSession as Session; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class UniqueAnswer. |
10
|
|
|
* |
11
|
|
|
* This class allows to instantiate an object of type UNIQUE_ANSWER |
12
|
|
|
* (MULTIPLE CHOICE, UNIQUE ANSWER), |
13
|
|
|
* extending the class question |
14
|
|
|
* |
15
|
|
|
* @author Eric Marguin |
16
|
|
|
* @author Julio Montoya |
17
|
|
|
*/ |
18
|
|
|
class UniqueAnswer extends Question |
19
|
|
|
{ |
20
|
|
|
public $typePicture = 'mcua.png'; |
21
|
|
|
public $explanationLangVar = 'UniqueSelect'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
parent::__construct(); |
29
|
|
|
$this->type = UNIQUE_ANSWER; |
30
|
|
|
$this->isContent = $this->getIsContent(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function createAnswersForm($form) |
37
|
|
|
{ |
38
|
|
|
// Getting the exercise list |
39
|
|
|
/** @var Exercise $obj_ex */ |
40
|
|
|
$obj_ex = Session::read('objExercise'); |
41
|
|
|
|
42
|
|
|
$editor_config = [ |
43
|
|
|
'ToolbarSet' => 'TestProposedAnswer', |
44
|
|
|
'Width' => '100%', |
45
|
|
|
'Height' => '125', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
//this line defines how many questions by default appear when creating a choice question |
49
|
|
|
// The previous default value was 2. See task #1759. |
50
|
|
|
$nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; |
51
|
|
|
$nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
52
|
|
|
|
53
|
|
|
$feedback_title = ''; |
54
|
|
|
switch ($obj_ex->getFeedbackType()) { |
55
|
|
|
case EXERCISE_FEEDBACK_TYPE_DIRECT: |
56
|
|
|
// Scenario |
57
|
|
|
$comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
58
|
|
|
$feedback_title = '<th width="20%">'.get_lang('Scenario').'</th>'; |
59
|
|
|
break; |
60
|
|
|
case EXERCISE_FEEDBACK_TYPE_POPUP: |
61
|
|
|
$comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
62
|
|
|
break; |
63
|
|
|
default: |
64
|
|
|
$comment_title = '<th width="40%">'.get_lang('Comment').'</th>'; |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$html = '<table class="table table-striped table-hover"> |
69
|
|
|
<thead> |
70
|
|
|
<tr style="text-align: center;"> |
71
|
|
|
<th width="5%">'.get_lang('Number').'</th> |
72
|
|
|
<th width="5%"> '.get_lang('True').'</th> |
73
|
|
|
<th width="40%">'.get_lang('Answer').'</th> |
74
|
|
|
'.$comment_title.' |
75
|
|
|
'.$feedback_title.' |
76
|
|
|
<th width="10%">'.get_lang('Weighting').'</th> |
77
|
|
|
</tr> |
78
|
|
|
</thead> |
79
|
|
|
<tbody>'; |
80
|
|
|
|
81
|
|
|
$form->addHeader(get_lang('Answers')); |
82
|
|
|
$form->addHtml($html); |
83
|
|
|
|
84
|
|
|
$defaults = []; |
85
|
|
|
|
86
|
|
|
$correct = 0; |
87
|
|
|
if (!empty($this->iid)) { |
88
|
|
|
$answer = new Answer($this->iid); |
89
|
|
|
$answer->read(); |
90
|
|
|
if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
91
|
|
|
$nb_answers = $answer->nbrAnswers; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
$form->addElement('hidden', 'nb_answers'); |
95
|
|
|
|
96
|
|
|
$obj_ex->setQuestionList(true); |
97
|
|
|
$question_list = $obj_ex->getQuestionList(); |
98
|
|
|
$select_question = []; |
99
|
|
|
$select_question[0] = get_lang('SelectTargetQuestion'); |
100
|
|
|
if (is_array($question_list)) { |
101
|
|
|
foreach ($question_list as $key => $questionid) { |
102
|
|
|
//To avoid warning messages |
103
|
|
|
if (!is_numeric($questionid)) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
$question = Question::read($questionid); |
107
|
|
|
$questionTitle = strip_tags($question->selectTitle()); |
108
|
|
|
$select_question[$questionid] = "Q$key: $questionTitle"; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
$select_question[-1] = get_lang('ExitTest'); |
112
|
|
|
|
113
|
|
|
$list = new LearnpathList(api_get_user_id()); |
114
|
|
|
$flat_list = $list->get_flat_list(); |
115
|
|
|
$select_lp_id = []; |
116
|
|
|
$select_lp_id[0] = get_lang('SelectTargetLP'); |
117
|
|
|
|
118
|
|
|
foreach ($flat_list as $id => $details) { |
119
|
|
|
$select_lp_id[$id] = cut($details['lp_name'], 20); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$temp_scenario = []; |
123
|
|
|
if ($nb_answers < 1) { |
124
|
|
|
$nb_answers = 1; |
125
|
|
|
echo Display::return_message( |
126
|
|
|
get_lang('YouHaveToCreateAtLeastOneAnswer') |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
for ($i = 1; $i <= $nb_answers; $i++) { |
131
|
|
|
$form->addHtml('<tr>'); |
132
|
|
|
if (isset($answer) && is_object($answer)) { |
133
|
|
|
if (isset($answer->correct[$i]) && $answer->correct[$i]) { |
134
|
|
|
$correct = $i; |
135
|
|
|
} |
136
|
|
|
$defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : ''; |
137
|
|
|
$defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : ''; |
138
|
|
|
$defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : 0; |
139
|
|
|
$item_list = []; |
140
|
|
|
if (isset($answer->destination[$i])) { |
141
|
|
|
$item_list = explode('@@', $answer->destination[$i]); |
142
|
|
|
} |
143
|
|
|
$try = isset($item_list[0]) ? $item_list[0] : ''; |
144
|
|
|
$lp = isset($item_list[1]) ? $item_list[1] : ''; |
145
|
|
|
$list_dest = isset($item_list[2]) ? $item_list[2] : ''; |
146
|
|
|
$url = isset($item_list[3]) ? $item_list[3] : ''; |
147
|
|
|
|
148
|
|
|
if ($try == 0) { |
149
|
|
|
$try_result = 0; |
150
|
|
|
} else { |
151
|
|
|
$try_result = 1; |
152
|
|
|
} |
153
|
|
|
if ($url == 0) { |
154
|
|
|
$url_result = ''; |
155
|
|
|
} else { |
156
|
|
|
$url_result = $url; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$temp_scenario['url'.$i] = $url_result; |
160
|
|
|
$temp_scenario['try'.$i] = $try_result; |
161
|
|
|
$temp_scenario['lp'.$i] = $lp; |
162
|
|
|
$temp_scenario['destination'.$i] = $list_dest; |
163
|
|
|
} else { |
164
|
|
|
$defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1'); |
165
|
|
|
$defaults['weighting[1]'] = 10; |
166
|
|
|
$defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2'); |
167
|
|
|
$defaults['weighting[2]'] = 0; |
168
|
|
|
$temp_scenario['destination'.$i] = ['0']; |
169
|
|
|
$temp_scenario['lp'.$i] = ['0']; |
170
|
|
|
} |
171
|
|
|
$defaults['scenario'] = $temp_scenario; |
172
|
|
|
|
173
|
|
|
$renderer = $form->defaultRenderer(); |
174
|
|
|
|
175
|
|
|
$renderer->setElementTemplate( |
176
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
177
|
|
|
'correct' |
178
|
|
|
); |
179
|
|
|
$renderer->setElementTemplate( |
180
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
181
|
|
|
'counter['.$i.']' |
182
|
|
|
); |
183
|
|
|
$renderer->setElementTemplate( |
184
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
185
|
|
|
'answer['.$i.']' |
186
|
|
|
); |
187
|
|
|
$renderer->setElementTemplate( |
188
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
189
|
|
|
'comment['.$i.']' |
190
|
|
|
); |
191
|
|
|
$renderer->setElementTemplate( |
192
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
193
|
|
|
'weighting['.$i.']' |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$answer_number = $form->addElement( |
197
|
|
|
'text', |
198
|
|
|
'counter['.$i.']', |
199
|
|
|
null, |
200
|
|
|
' value = "'.$i.'"' |
201
|
|
|
); |
202
|
|
|
$answer_number->freeze(); |
203
|
|
|
$form->addElement( |
204
|
|
|
'radio', |
205
|
|
|
'correct', |
206
|
|
|
null, |
207
|
|
|
null, |
208
|
|
|
$i, |
209
|
|
|
'class="checkbox"' |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config); |
213
|
|
|
|
214
|
|
|
$form->addRule( |
215
|
|
|
'answer['.$i.']', |
216
|
|
|
get_lang('ThisFieldIsRequired'), |
217
|
|
|
'required' |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
switch ($obj_ex->getFeedbackType()) { |
221
|
|
|
case EXERCISE_FEEDBACK_TYPE_DIRECT: |
222
|
|
|
$this->setDirectOptions($i, $form, $renderer, $select_lp_id, $select_question); |
223
|
|
|
break; |
224
|
|
|
case EXERCISE_FEEDBACK_TYPE_POPUP: |
225
|
|
|
default: |
226
|
|
|
$form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config); |
227
|
|
|
break; |
228
|
|
|
} |
229
|
|
|
$form->addText('weighting['.$i.']', null, null, ['value' => '0']); |
230
|
|
|
$form->addHtml('</tr>'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$form->addHtml('</tbody>'); |
234
|
|
|
$form->addHtml('</table>'); |
235
|
|
|
|
236
|
|
|
global $text; |
237
|
|
|
$buttonGroup = []; |
238
|
|
|
|
239
|
|
|
if (true === $obj_ex->edit_exercise_in_lp || (empty($this->exerciseList) && empty($obj_ex->iid))) { |
240
|
|
|
//setting the save button here and not in the question class.php |
241
|
|
|
$buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true); |
242
|
|
|
$buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true); |
243
|
|
|
$buttonGroup[] = $form->addButton( |
244
|
|
|
'submitQuestion', |
245
|
|
|
$text, |
246
|
|
|
'check', |
247
|
|
|
'primary', |
248
|
|
|
'default', |
249
|
|
|
null, |
250
|
|
|
['id' => 'submit-question'], |
251
|
|
|
true |
252
|
|
|
); |
253
|
|
|
$form->addGroup($buttonGroup); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// We check the first radio button to be sure a radio button will be check |
257
|
|
|
if ($correct == 0) { |
258
|
|
|
$correct = 1; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if (isset($_POST) && isset($_POST['correct'])) { |
262
|
|
|
$correct = (int) $_POST['correct']; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$defaults['correct'] = $correct; |
266
|
|
|
|
267
|
|
|
if (!empty($this->iid)) { |
268
|
|
|
$form->setDefaults($defaults); |
269
|
|
|
} else { |
270
|
|
|
if ($this->isContent == 1) { |
271
|
|
|
// Default sample content. |
272
|
|
|
$form->setDefaults($defaults); |
273
|
|
|
} else { |
274
|
|
|
$correct = 1; |
275
|
|
|
if (isset($_POST) && isset($_POST['correct'])) { |
276
|
|
|
$correct = (int) $_POST['correct']; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$form->setDefaults(['correct' => $correct]); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
$form->setConstants(['nb_answers' => $nb_answers]); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function setDirectOptions($i, FormValidator $form, $renderer, $select_lp_id, $select_question) |
286
|
|
|
{ |
287
|
|
|
$editor_config = [ |
288
|
|
|
'ToolbarSet' => 'TestProposedAnswer', |
289
|
|
|
'Width' => '100%', |
290
|
|
|
'Height' => '125', |
291
|
|
|
]; |
292
|
|
|
|
293
|
|
|
$form->addHtmlEditor( |
294
|
|
|
'comment['.$i.']', |
295
|
|
|
null, |
296
|
|
|
null, |
297
|
|
|
false, |
298
|
|
|
$editor_config |
299
|
|
|
); |
300
|
|
|
// Direct feedback |
301
|
|
|
//Adding extra feedback fields |
302
|
|
|
$group = []; |
303
|
|
|
$group['try'.$i] = $form->createElement( |
304
|
|
|
'checkbox', |
305
|
|
|
'try'.$i, |
306
|
|
|
null, |
307
|
|
|
get_lang('TryAgain') |
308
|
|
|
); |
309
|
|
|
$group['lp'.$i] = $form->createElement( |
310
|
|
|
'select', |
311
|
|
|
'lp'.$i, |
312
|
|
|
get_lang('SeeTheory').': ', |
313
|
|
|
$select_lp_id |
314
|
|
|
); |
315
|
|
|
$group['destination'.$i] = $form->createElement( |
316
|
|
|
'select', |
317
|
|
|
'destination'.$i, |
318
|
|
|
get_lang('GoToQuestion').': ', |
319
|
|
|
$select_question |
320
|
|
|
); |
321
|
|
|
$group['url'.$i] = $form->createElement( |
322
|
|
|
'text', |
323
|
|
|
'url'.$i, |
324
|
|
|
get_lang('Other').': ', |
325
|
|
|
[ |
326
|
|
|
'class' => 'col-md-2', |
327
|
|
|
'placeholder' => get_lang('Other'), |
328
|
|
|
] |
329
|
|
|
); |
330
|
|
|
$form->addGroup($group, 'scenario'); |
331
|
|
|
|
332
|
|
|
$renderer->setElementTemplate( |
333
|
|
|
'<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}', |
334
|
|
|
'scenario' |
335
|
|
|
); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Validate question answers before saving. |
340
|
|
|
* @return bool|string True if valid, error message if invalid. |
341
|
|
|
*/ |
342
|
|
|
public function validateAnswers($form) |
343
|
|
|
{ |
344
|
|
|
$correct = $form->getSubmitValue('correct'); |
345
|
|
|
$nb_answers = $form->getSubmitValue('nb_answers'); |
346
|
|
|
$hasCorrectAnswer = false; |
347
|
|
|
$hasPositiveScore = false; |
348
|
|
|
$errors = []; |
349
|
|
|
$error_fields = []; |
350
|
|
|
|
351
|
|
|
for ($i = 1; $i <= $nb_answers; $i++) { |
352
|
|
|
$answer = trim($form->getSubmitValue("answer[$i]") ?? ''); |
353
|
|
|
$weighting = trim($form->getSubmitValue("weighting[$i]") ?? ''); |
354
|
|
|
$isCorrect = ($correct == $i); |
355
|
|
|
if (empty($answer)) { |
356
|
|
|
$errors[] = sprintf(get_lang('NoAnswerCanBeEmpty'), $i); |
357
|
|
|
$error_fields[] = "answer[$i]"; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if ($weighting === '' || !is_numeric($weighting)) { |
361
|
|
|
$errors[] = sprintf(get_lang('ScoreMustBeNumeric'), $i); |
362
|
|
|
$error_fields[] = "weighting[$i]"; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
if ($isCorrect) { |
366
|
|
|
$hasCorrectAnswer = true; |
367
|
|
|
if (floatval($weighting) > 0) { |
368
|
|
|
$hasPositiveScore = true; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if (!$hasCorrectAnswer) { |
374
|
|
|
$errors[] = get_lang('ACorrectAnswerIsRequired'); |
375
|
|
|
$error_fields[] = "correct"; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
if (!$hasPositiveScore) { |
379
|
|
|
$errors[] = get_lang('TheCorrectAnswerMustHaveAPositiveScore'); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return empty($errors) ? true : ['errors' => $errors, 'error_fields' => $error_fields]; |
|
|
|
|
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* {@inheritdoc} |
387
|
|
|
*/ |
388
|
|
|
public function processAnswersCreation($form, $exercise) |
389
|
|
|
{ |
390
|
|
|
$validationResult = $this->validateAnswers($form); |
391
|
|
|
if ($validationResult !== true) { |
392
|
|
|
Display::addFlash(Display::return_message(implode("<br>", $validationResult['errors']), 'error')); |
393
|
|
|
return; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
$questionWeighting = $nbrGoodAnswers = 0; |
397
|
|
|
$correct = $form->getSubmitValue('correct'); |
398
|
|
|
$objAnswer = new Answer($this->iid); |
399
|
|
|
$nb_answers = $form->getSubmitValue('nb_answers'); |
400
|
|
|
|
401
|
|
|
for ($i = 1; $i <= $nb_answers; $i++) { |
402
|
|
|
$answer = trim($form->getSubmitValue('answer['.$i.']')); |
403
|
|
|
$comment = trim($form->getSubmitValue('comment['.$i.']')); |
404
|
|
|
$weighting = trim($form->getSubmitValue('weighting['.$i.']')); |
405
|
|
|
$scenario = $form->getSubmitValue('scenario'); |
406
|
|
|
|
407
|
|
|
$try = null; |
408
|
|
|
$lp = null; |
409
|
|
|
$destination = null; |
410
|
|
|
$url = null; |
411
|
|
|
if (isset($scenario['try'.$i])) { |
412
|
|
|
$try = !empty($scenario['try'.$i]); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if (isset($scenario['lp'.$i])) { |
416
|
|
|
$lp = $scenario['lp'.$i]; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
if (isset($scenario['destination'.$i])) { |
420
|
|
|
$destination = $scenario['destination'.$i]; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
if (isset($scenario['url'.$i])) { |
424
|
|
|
$url = trim($scenario['url'.$i]); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/* |
428
|
|
|
How we are going to parse the destination value |
429
|
|
|
|
430
|
|
|
here we parse the destination value which is a string |
431
|
|
|
1@@3@@2;4;4;@@http://www.chamilo.org |
432
|
|
|
|
433
|
|
|
where: try_again@@lp_id@@selected_questions@@url |
434
|
|
|
|
435
|
|
|
try_again = is 1 || 0 |
436
|
|
|
lp_id = id of a learning path (0 if dont select) |
437
|
|
|
selected_questions= ids of questions |
438
|
|
|
url= an url |
439
|
|
|
|
440
|
|
|
$destination_str=''; |
441
|
|
|
foreach ($list_destination as $destination_id) |
442
|
|
|
{ |
443
|
|
|
$destination_str.=$destination_id.';'; |
444
|
|
|
}*/ |
445
|
|
|
|
446
|
|
|
$goodAnswer = $correct == $i ? true : false; |
447
|
|
|
|
448
|
|
|
if ($goodAnswer) { |
449
|
|
|
$nbrGoodAnswers++; |
450
|
|
|
$weighting = abs($weighting); |
451
|
|
|
if ($weighting > 0) { |
452
|
|
|
$questionWeighting += $weighting; |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
if (empty($try)) { |
457
|
|
|
$try = 0; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
if (empty($lp)) { |
461
|
|
|
$lp = 0; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
if (empty($destination)) { |
465
|
|
|
$destination = 0; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
if ($url == '') { |
469
|
|
|
$url = 0; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
//1@@1;2;@@2;4;4;@@http://www.chamilo.org |
473
|
|
|
$dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url; |
474
|
|
|
$objAnswer->createAnswer( |
475
|
|
|
$answer, |
476
|
|
|
$goodAnswer, |
477
|
|
|
$comment, |
478
|
|
|
$weighting, |
479
|
|
|
$i, |
480
|
|
|
null, |
481
|
|
|
null, |
482
|
|
|
$dest |
483
|
|
|
); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
// saves the answers into the data base |
487
|
|
|
$objAnswer->save(); |
488
|
|
|
|
489
|
|
|
// sets the total weighting of the question |
490
|
|
|
$this->updateWeighting($questionWeighting); |
491
|
|
|
$this->save($exercise); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* {@inheritdoc} |
496
|
|
|
*/ |
497
|
|
|
public function return_header(Exercise $exercise, $counter = null, $score = []) |
498
|
|
|
{ |
499
|
|
|
$header = parent::return_header($exercise, $counter, $score); |
500
|
|
|
$header .= '<table class="'.$this->question_table_class.'"><tr>'; |
501
|
|
|
|
502
|
|
|
$header .= '<th>'.get_lang('Choice').'</th>'; |
503
|
|
|
if ($exercise->showExpectedChoiceColumn()) { |
504
|
|
|
$header .= '<th>'.get_lang('ExpectedChoice').'</th>'; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
$header .= '<th>'.get_lang('Answer').'</th>'; |
508
|
|
|
if ($exercise->showExpectedChoice()) { |
509
|
|
|
$header .= '<th>'.get_lang('Status').'</th>'; |
510
|
|
|
} |
511
|
|
|
if (false === $exercise->hideComment) { |
512
|
|
|
$header .= '<th>'.get_lang('Comment').'</th>'; |
513
|
|
|
} |
514
|
|
|
$header .= '</tr>'; |
515
|
|
|
|
516
|
|
|
return $header; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Saves one answer to the database. |
521
|
|
|
* |
522
|
|
|
* @param int $id The ID of the answer (has to be calculated for this course) |
523
|
|
|
* @param int $question_id The question ID (to which the answer is attached) |
524
|
|
|
* @param string $title The text of the answer |
525
|
|
|
* @param string $comment The feedback for the answer |
526
|
|
|
* @param float $score The score you get when picking this answer |
527
|
|
|
* @param int $correct Whether this answer is considered *the* correct one (this is the unique answer type) |
528
|
|
|
*/ |
529
|
|
|
public function addAnswer( |
530
|
|
|
$id, |
531
|
|
|
$question_id, |
532
|
|
|
$title, |
533
|
|
|
$comment, |
534
|
|
|
$score = 0.0, |
535
|
|
|
$correct = 0 |
536
|
|
|
) { |
537
|
|
|
$em = Database::getManager(); |
538
|
|
|
$tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER); |
539
|
|
|
$tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
540
|
|
|
$course_id = api_get_course_int_id(); |
541
|
|
|
$question_id = (int) $question_id; |
542
|
|
|
$score = floatval($score); |
543
|
|
|
$correct = intval($correct); |
544
|
|
|
$title = Database::escape_string($title); |
545
|
|
|
$comment = Database::escape_string($comment); |
546
|
|
|
// Get the max position. |
547
|
|
|
$sql = "SELECT max(position) as max_position |
548
|
|
|
FROM $tbl_quiz_answer |
549
|
|
|
WHERE |
550
|
|
|
question_id = $question_id"; |
551
|
|
|
$rs_max = Database::query($sql); |
552
|
|
|
$row_max = Database::fetch_object($rs_max); |
553
|
|
|
$position = $row_max->max_position + 1; |
554
|
|
|
|
555
|
|
|
// Insert a new answer |
556
|
|
|
$quizAnswer = new CQuizAnswer(); |
557
|
|
|
$quizAnswer |
558
|
|
|
->setCId($course_id) |
559
|
|
|
->setId($id) |
560
|
|
|
->setQuestionId($question_id) |
561
|
|
|
->setAnswer($title) |
562
|
|
|
->setCorrect($correct) |
563
|
|
|
->setComment($comment) |
564
|
|
|
->setPonderation($score) |
565
|
|
|
->setPosition($position) |
566
|
|
|
->setDestination(CQuizAnswer::DEFAULT_DESTINATION); |
567
|
|
|
|
568
|
|
|
$em->persist($quizAnswer); |
569
|
|
|
$em->flush(); |
570
|
|
|
|
571
|
|
|
$id = $quizAnswer->getId(); |
572
|
|
|
|
573
|
|
|
if ($id) { |
574
|
|
|
$quizAnswer |
575
|
|
|
->setId($id); |
576
|
|
|
|
577
|
|
|
$em->merge($quizAnswer); |
578
|
|
|
$em->flush(); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
if ($correct) { |
582
|
|
|
$sql = "UPDATE $tbl_quiz_question |
583
|
|
|
SET ponderation = (ponderation + $score) |
584
|
|
|
WHERE iid = $question_id"; |
585
|
|
|
Database::query($sql); |
586
|
|
|
} |
587
|
|
|
} |
588
|
|
|
} |
589
|
|
|
|