mirrored from git://git.moodle.org/moodle.git
/
questiontype.php
574 lines (524 loc) · 26 KB
/
questiontype.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Question type class for the multi-answer question type.
*
* @package qtype
* @subpackage multianswer
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/questiontypebase.php');
require_once($CFG->dirroot . '/question/type/multichoice/question.php');
require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
/**
* The multi-answer question type class.
*
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_multianswer extends question_type {
public function can_analyse_responses() {
return false;
}
public function get_question_options($question) {
global $DB, $OUTPUT;
// Get relevant data indexed by positionkey from the multianswers table.
$sequence = $DB->get_field('question_multianswer', 'sequence',
array('question' => $question->id), MUST_EXIST);
$wrappedquestions = $DB->get_records_list('question', 'id',
explode(',', $sequence), 'id ASC');
// We want an array with question ids as index and the positions as values.
$sequence = array_flip(explode(',', $sequence));
array_walk($sequence, function(&$val) {
$val++;
});
// If a question is lost, the corresponding index is null
// so this null convention is used to test $question->options->questions
// before using the values.
// First all possible questions from sequence are nulled
// then filled with the data if available in $wrappedquestions.
foreach ($sequence as $seq) {
$question->options->questions[$seq] = '';
}
foreach ($wrappedquestions as $wrapped) {
question_bank::get_qtype($wrapped->qtype)->get_question_options($wrapped);
// For wrapped questions the maxgrade is always equal to the defaultmark,
// there is no entry in the question_instances table for them.
$wrapped->maxmark = $wrapped->defaultmark;
$question->options->questions[$sequence[$wrapped->id]] = $wrapped;
}
$question->hints = $DB->get_records('question_hints',
array('questionid' => $question->id), 'id ASC');
return true;
}
public function save_question_options($question) {
global $DB;
$result = new stdClass();
// This function needs to be able to handle the case where the existing set of wrapped
// questions does not match the new set of wrapped questions so that some need to be
// created, some modified and some deleted.
// Unfortunately the code currently simply overwrites existing ones in sequence. This
// will make re-marking after a re-ordering of wrapped questions impossible and
// will also create difficulties if questiontype specific tables reference the id.
// First we get all the existing wrapped questions.
if (!$oldwrappedids = $DB->get_field('question_multianswer', 'sequence',
array('question' => $question->id))) {
$oldwrappedquestions = array();
} else {
$oldwrappedquestions = $DB->get_records_list('question', 'id',
explode(',', $oldwrappedids), 'id ASC');
}
$sequence = array();
foreach ($question->options->questions as $wrapped) {
if (!empty($wrapped)) {
// If we still have some old wrapped question ids, reuse the next of them.
if (is_array($oldwrappedquestions) &&
$oldwrappedquestion = array_shift($oldwrappedquestions)) {
$wrapped->id = $oldwrappedquestion->id;
if ($oldwrappedquestion->qtype != $wrapped->qtype) {
switch ($oldwrappedquestion->qtype) {
case 'multichoice':
$DB->delete_records('qtype_multichoice_options',
array('questionid' => $oldwrappedquestion->id));
break;
case 'shortanswer':
$DB->delete_records('qtype_shortanswer_options',
array('questionid' => $oldwrappedquestion->id));
break;
case 'numerical':
$DB->delete_records('question_numerical',
array('question' => $oldwrappedquestion->id));
break;
default:
throw new moodle_exception('qtypenotrecognized',
'qtype_multianswer', '', $oldwrappedquestion->qtype);
$wrapped->id = 0;
}
}
} else {
$wrapped->id = 0;
}
}
$wrapped->name = $question->name;
$wrapped->parent = $question->id;
$previousid = $wrapped->id;
// Save_question strips this extra bit off the category again.
$wrapped->category = $question->category . ',1';
$wrapped = question_bank::get_qtype($wrapped->qtype)->save_question(
$wrapped, clone($wrapped));
$sequence[] = $wrapped->id;
if ($previousid != 0 && $previousid != $wrapped->id) {
// For some reasons a new question has been created
// so delete the old one.
question_delete_question($previousid);
}
}
// Delete redundant wrapped questions.
if (is_array($oldwrappedquestions) && count($oldwrappedquestions)) {
foreach ($oldwrappedquestions as $oldwrappedquestion) {
question_delete_question($oldwrappedquestion->id);
}
}
if (!empty($sequence)) {
$multianswer = new stdClass();
$multianswer->question = $question->id;
$multianswer->sequence = implode(',', $sequence);
if ($oldid = $DB->get_field('question_multianswer', 'id',
array('question' => $question->id))) {
$multianswer->id = $oldid;
$DB->update_record('question_multianswer', $multianswer);
} else {
$DB->insert_record('question_multianswer', $multianswer);
}
}
$this->save_hints($question, true);
}
public function save_question($authorizedquestion, $form) {
$question = qtype_multianswer_extract_question($form->questiontext);
if (isset($authorizedquestion->id)) {
$question->id = $authorizedquestion->id;
}
$question->category = $authorizedquestion->category;
$form->defaultmark = $question->defaultmark;
$form->questiontext = $question->questiontext;
$form->questiontextformat = 0;
$form->options = clone($question->options);
unset($question->options);
return parent::save_question($question, $form);
}
protected function make_hint($hint) {
return question_hint_with_parts::load_from_record($hint);
}
public function delete_question($questionid, $contextid) {
global $DB;
$DB->delete_records('question_multianswer', array('question' => $questionid));
parent::delete_question($questionid, $contextid);
}
protected function initialise_question_instance(question_definition $question, $questiondata) {
parent::initialise_question_instance($question, $questiondata);
$bits = preg_split('/\{#(\d+)\}/', $question->questiontext,
null, PREG_SPLIT_DELIM_CAPTURE);
$question->textfragments[0] = array_shift($bits);
$i = 1;
while (!empty($bits)) {
$question->places[$i] = array_shift($bits);
$question->textfragments[$i] = array_shift($bits);
$i += 1;
}
foreach ($questiondata->options->questions as $key => $subqdata) {
$subqdata->contextid = $questiondata->contextid;
if ($subqdata->qtype == 'multichoice') {
$answerregs = array();
if ($subqdata->options->shuffleanswers == 1 && isset($questiondata->options->shuffleanswers)
&& $questiondata->options->shuffleanswers == 0 ) {
$subqdata->options->shuffleanswers = 0;
}
}
$question->subquestions[$key] = question_bank::make_question($subqdata);
$question->subquestions[$key]->maxmark = $subqdata->defaultmark;
if (isset($subqdata->options->layout)) {
$question->subquestions[$key]->layout = $subqdata->options->layout;
}
}
}
public function get_random_guess_score($questiondata) {
$fractionsum = 0;
$fractionmax = 0;
foreach ($questiondata->options->questions as $key => $subqdata) {
$fractionmax += $subqdata->defaultmark;
$fractionsum += question_bank::get_qtype(
$subqdata->qtype)->get_random_guess_score($subqdata);
}
return $fractionsum / $fractionmax;
}
public function move_files($questionid, $oldcontextid, $newcontextid) {
parent::move_files($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
}
protected function delete_files($questionid, $contextid) {
parent::delete_files($questionid, $contextid);
$this->delete_files_in_hints($questionid, $contextid);
}
}
// ANSWER_ALTERNATIVE regexes.
define('ANSWER_ALTERNATIVE_FRACTION_REGEX',
'=|%(-?[0-9]+)%');
// For the syntax '(?<!' see http://www.perl.com/doc/manual/html/pod/perlre.html#item_C.
define('ANSWER_ALTERNATIVE_ANSWER_REGEX',
'.+?(?<!\\\\|&|&)(?=[~#}]|$)');
define('ANSWER_ALTERNATIVE_FEEDBACK_REGEX',
'.*?(?<!\\\\)(?=[~}]|$)');
define('ANSWER_ALTERNATIVE_REGEX',
'(' . ANSWER_ALTERNATIVE_FRACTION_REGEX .')?' .
'(' . ANSWER_ALTERNATIVE_ANSWER_REGEX . ')' .
'(#(' . ANSWER_ALTERNATIVE_FEEDBACK_REGEX .'))?');
// Parenthesis positions for ANSWER_ALTERNATIVE_REGEX.
define('ANSWER_ALTERNATIVE_REGEX_PERCENTILE_FRACTION', 2);
define('ANSWER_ALTERNATIVE_REGEX_FRACTION', 1);
define('ANSWER_ALTERNATIVE_REGEX_ANSWER', 3);
define('ANSWER_ALTERNATIVE_REGEX_FEEDBACK', 5);
// NUMBER_FORMATED_ALTERNATIVE_ANSWER_REGEX is used
// for identifying numerical answers in ANSWER_ALTERNATIVE_REGEX_ANSWER.
define('NUMBER_REGEX',
'-?(([0-9]+[.,]?[0-9]*|[.,][0-9]+)([eE][-+]?[0-9]+)?)');
define('NUMERICAL_ALTERNATIVE_REGEX',
'^(' . NUMBER_REGEX . ')(:' . NUMBER_REGEX . ')?$');
// Parenthesis positions for NUMERICAL_FORMATED_ALTERNATIVE_ANSWER_REGEX.
define('NUMERICAL_CORRECT_ANSWER', 1);
define('NUMERICAL_ABS_ERROR_MARGIN', 6);
// Remaining ANSWER regexes.
define('ANSWER_TYPE_DEF_REGEX',
'(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|' .
'(SHORTANSWER|SA|MW)|(SHORTANSWER_C|SAC|MWC)|' .
'(MULTICHOICE_S|MCS)|(MULTICHOICE_VS|MCVS)|(MULTICHOICE_HS|MCHS)|'.
'(MULTIRESPONSE|MR)|(MULTIRESPONSE_H|MRH)|(MULTIRESPONSE_S|MRS)|(MULTIRESPONSE_HS|MRHS)');
define('ANSWER_START_REGEX',
'\{([0-9]*):(' . ANSWER_TYPE_DEF_REGEX . '):');
define('ANSWER_REGEX',
ANSWER_START_REGEX
. '(' . ANSWER_ALTERNATIVE_REGEX
. '(~'
. ANSWER_ALTERNATIVE_REGEX
. ')*)\}');
// Parenthesis positions for singulars in ANSWER_REGEX.
define('ANSWER_REGEX_NORM', 1);
define('ANSWER_REGEX_ANSWER_TYPE_NUMERICAL', 3);
define('ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE', 4);
define('ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_REGULAR', 5);
define('ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_HORIZONTAL', 6);
define('ANSWER_REGEX_ANSWER_TYPE_SHORTANSWER', 7);
define('ANSWER_REGEX_ANSWER_TYPE_SHORTANSWER_C', 8);
define('ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_SHUFFLED', 9);
define('ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_REGULAR_SHUFFLED', 10);
define('ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_HORIZONTAL_SHUFFLED', 11);
define('ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE', 12);
define('ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE_HORIZONTAL', 13);
define('ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE_SHUFFLED', 14);
define('ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE_HORIZONTAL_SHUFFLED', 15);
define('ANSWER_REGEX_ALTERNATIVES', 16);
/**
* Initialise subquestion fields that are constant across all MULTICHOICE
* types.
*
* @param objet $wrapped The subquestion to initialise
*
*/
function qtype_multianswer_initialise_multichoice_subquestion($wrapped) {
$wrapped->qtype = 'multichoice';
$wrapped->single = 1;
$wrapped->answernumbering = 0;
$wrapped->correctfeedback['text'] = '';
$wrapped->correctfeedback['format'] = FORMAT_HTML;
$wrapped->correctfeedback['itemid'] = '';
$wrapped->partiallycorrectfeedback['text'] = '';
$wrapped->partiallycorrectfeedback['format'] = FORMAT_HTML;
$wrapped->partiallycorrectfeedback['itemid'] = '';
$wrapped->incorrectfeedback['text'] = '';
$wrapped->incorrectfeedback['format'] = FORMAT_HTML;
$wrapped->incorrectfeedback['itemid'] = '';
}
function qtype_multianswer_extract_question($text) {
// Variable $text is an array [text][format][itemid].
$question = new stdClass();
$question->qtype = 'multianswer';
$question->questiontext = $text;
$question->generalfeedback['text'] = '';
$question->generalfeedback['format'] = FORMAT_HTML;
$question->generalfeedback['itemid'] = '';
$question->options = new stdClass();
$question->options->questions = array();
$question->defaultmark = 0; // Will be increased for each answer norm.
for ($positionkey = 1;
preg_match('/'.ANSWER_REGEX.'/s', $question->questiontext['text'], $answerregs);
++$positionkey) {
$wrapped = new stdClass();
$wrapped->generalfeedback['text'] = '';
$wrapped->generalfeedback['format'] = FORMAT_HTML;
$wrapped->generalfeedback['itemid'] = '';
if (isset($answerregs[ANSWER_REGEX_NORM]) && $answerregs[ANSWER_REGEX_NORM] !== '') {
$wrapped->defaultmark = $answerregs[ANSWER_REGEX_NORM];
} else {
$wrapped->defaultmark = '1';
}
if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_NUMERICAL])) {
$wrapped->qtype = 'numerical';
$wrapped->multiplier = array();
$wrapped->units = array();
$wrapped->instructions['text'] = '';
$wrapped->instructions['format'] = FORMAT_HTML;
$wrapped->instructions['itemid'] = '';
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_SHORTANSWER])) {
$wrapped->qtype = 'shortanswer';
$wrapped->usecase = 0;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_SHORTANSWER_C])) {
$wrapped->qtype = 'shortanswer';
$wrapped->usecase = 1;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->shuffleanswers = 0;
$wrapped->layout = qtype_multichoice_base::LAYOUT_DROPDOWN;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_SHUFFLED])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->shuffleanswers = 1;
$wrapped->layout = qtype_multichoice_base::LAYOUT_DROPDOWN;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_REGULAR])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->shuffleanswers = 0;
$wrapped->layout = qtype_multichoice_base::LAYOUT_VERTICAL;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_REGULAR_SHUFFLED])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->shuffleanswers = 1;
$wrapped->layout = qtype_multichoice_base::LAYOUT_VERTICAL;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_HORIZONTAL])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->shuffleanswers = 0;
$wrapped->layout = qtype_multichoice_base::LAYOUT_HORIZONTAL;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_HORIZONTAL_SHUFFLED])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->shuffleanswers = 1;
$wrapped->layout = qtype_multichoice_base::LAYOUT_HORIZONTAL;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->single = 0;
$wrapped->shuffleanswers = 0;
$wrapped->layout = qtype_multichoice_base::LAYOUT_VERTICAL;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE_HORIZONTAL])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->single = 0;
$wrapped->shuffleanswers = 0;
$wrapped->layout = qtype_multichoice_base::LAYOUT_HORIZONTAL;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE_SHUFFLED])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->single = 0;
$wrapped->shuffleanswers = 1;
$wrapped->layout = qtype_multichoice_base::LAYOUT_VERTICAL;
} else if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTIRESPONSE_HORIZONTAL_SHUFFLED])) {
qtype_multianswer_initialise_multichoice_subquestion($wrapped);
$wrapped->single = 0;
$wrapped->shuffleanswers = 1;
$wrapped->layout = qtype_multichoice_base::LAYOUT_HORIZONTAL;
} else {
print_error('unknownquestiontype', 'question', '', $answerregs[2]);
return false;
}
// Each $wrapped simulates a $form that can be processed by the
// respective save_question and save_question_options methods of the
// wrapped questiontypes.
$wrapped->answer = array();
$wrapped->fraction = array();
$wrapped->feedback = array();
$wrapped->questiontext['text'] = $answerregs[0];
$wrapped->questiontext['format'] = FORMAT_HTML;
$wrapped->questiontext['itemid'] = '';
$answerindex = 0;
$hasspecificfraction = false;
$remainingalts = $answerregs[ANSWER_REGEX_ALTERNATIVES];
while (preg_match('/~?'.ANSWER_ALTERNATIVE_REGEX.'/s', $remainingalts, $altregs)) {
if ('=' == $altregs[ANSWER_ALTERNATIVE_REGEX_FRACTION]) {
$wrapped->fraction["{$answerindex}"] = '1';
} else if ($percentile = $altregs[ANSWER_ALTERNATIVE_REGEX_PERCENTILE_FRACTION]) {
$wrapped->fraction["{$answerindex}"] = .01 * $percentile;
$hasspecificfraction = true;
} else {
$wrapped->fraction["{$answerindex}"] = '0';
}
if (isset($altregs[ANSWER_ALTERNATIVE_REGEX_FEEDBACK])) {
$feedback = html_entity_decode(
$altregs[ANSWER_ALTERNATIVE_REGEX_FEEDBACK], ENT_QUOTES, 'UTF-8');
$feedback = str_replace('\}', '}', $feedback);
$wrapped->feedback["{$answerindex}"]['text'] = str_replace('\#', '#', $feedback);
$wrapped->feedback["{$answerindex}"]['format'] = FORMAT_HTML;
$wrapped->feedback["{$answerindex}"]['itemid'] = '';
} else {
$wrapped->feedback["{$answerindex}"]['text'] = '';
$wrapped->feedback["{$answerindex}"]['format'] = FORMAT_HTML;
$wrapped->feedback["{$answerindex}"]['itemid'] = '';
}
if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_NUMERICAL])
&& preg_match('~'.NUMERICAL_ALTERNATIVE_REGEX.'~s',
$altregs[ANSWER_ALTERNATIVE_REGEX_ANSWER], $numregs)) {
$wrapped->answer[] = $numregs[NUMERICAL_CORRECT_ANSWER];
if (array_key_exists(NUMERICAL_ABS_ERROR_MARGIN, $numregs)) {
$wrapped->tolerance["{$answerindex}"] =
$numregs[NUMERICAL_ABS_ERROR_MARGIN];
} else {
$wrapped->tolerance["{$answerindex}"] = 0;
}
} else { // Tolerance can stay undefined for non numerical questions.
// Undo quoting done by the HTML editor.
$answer = html_entity_decode(
$altregs[ANSWER_ALTERNATIVE_REGEX_ANSWER], ENT_QUOTES, 'UTF-8');
$answer = str_replace('\}', '}', $answer);
$wrapped->answer["{$answerindex}"] = str_replace('\#', '#', $answer);
if ($wrapped->qtype == 'multichoice') {
$wrapped->answer["{$answerindex}"] = array(
'text' => $wrapped->answer["{$answerindex}"],
'format' => FORMAT_HTML,
'itemid' => '');
}
}
$tmp = explode($altregs[0], $remainingalts, 2);
$remainingalts = $tmp[1];
$answerindex++;
}
// Fix the score for multichoice_multi questions (as positive scores should add up to 1, not have a maximum of 1).
if (isset($wrapped->single) && $wrapped->single == 0) {
$total = 0;
foreach ($wrapped->fraction as $idx => $fraction) {
if ($fraction > 0) {
$total += $fraction;
}
}
if ($total) {
foreach ($wrapped->fraction as $idx => $fraction) {
if ($fraction > 0) {
$wrapped->fraction[$idx] = $fraction / $total;
} else if (!$hasspecificfraction) {
// If no specific fractions are given, set incorrect answers to each cancel out one correct answer.
$wrapped->fraction[$idx] = -(1.0 / $total);
}
}
}
}
$question->defaultmark += $wrapped->defaultmark;
$question->options->questions[$positionkey] = clone($wrapped);
$question->questiontext['text'] = implode("{#$positionkey}",
explode($answerregs[0], $question->questiontext['text'], 2));
}
return $question;
}
/**
* Validate a multianswer question.
*
* @param object $question The multianswer question to validate as returned by qtype_multianswer_extract_question
* @return array Array of error messages with questions field names as keys.
*/
function qtype_multianswer_validate_question($question) {
$errors = array();
if (!isset($question->options->questions)) {
$errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer');
} else {
$subquestions = fullclone($question->options->questions);
if (count($subquestions)) {
$sub = 1;
foreach ($subquestions as $subquestion) {
$prefix = 'sub_'.$sub.'_';
$answercount = 0;
$maxgrade = false;
$maxfraction = -1;
foreach ($subquestion->answer as $key => $answer) {
if (is_array($answer)) {
$answer = $answer['text'];
}
$trimmedanswer = trim($answer);
if ($trimmedanswer !== '') {
$answercount++;
if ($subquestion->qtype == 'numerical' &&
!(qtype_numerical::is_valid_number($trimmedanswer) || $trimmedanswer == '*')) {
$errors[$prefix.'answer['.$key.']'] =
get_string('answermustbenumberorstar', 'qtype_numerical');
}
if ($subquestion->fraction[$key] == 1) {
$maxgrade = true;
}
if ($subquestion->fraction[$key] > $maxfraction) {
$maxfraction = $subquestion->fraction[$key];
}
// For 'multiresponse' we are OK if there is at least one fraction > 0.
if ($subquestion->qtype == 'multichoice' && $subquestion->single == 0 &&
$subquestion->fraction[$key] > 0) {
$maxgrade = true;
}
}
}
if ($subquestion->qtype == 'multichoice' && $answercount < 2) {
$errors[$prefix.'answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
} else if ($answercount == 0) {
$errors[$prefix.'answer[0]'] = get_string('notenoughanswers', 'question', 1);
}
if ($maxgrade == false) {
$errors[$prefix.'fraction[0]'] = get_string('fractionsnomax', 'question');
}
$sub++;
}
} else {
$errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer');
}
}
return $errors;
}