mirrored from git://git.moodle.org/moodle.git
/
edit_multianswer_form.php
71 lines (66 loc) · 2.96 KB
/
edit_multianswer_form.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
<?php
/**
* Defines the editing form for the multianswer question type.
*
* @copyright © 2007 Jamie Pratt
* @author Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questions
*/
/**
* multianswer editing form definition.
*/
class question_edit_multianswer_form extends question_edit_form {
// No question-type specific form fields.
function set_defaults($question) {
if ($question->questiontext and $question->id and $question->qtype) {
foreach ($question->options->questions as $key => $wrapped) {
// The old way of restoring the definitions is kept to gradually
// update all multianswer questions
if (empty($wrapped->questiontext)) {
$parsableanswerdef = '{' . $wrapped->defaultgrade . ':';
switch ($wrapped->qtype) {
case 'multichoice':
$parsableanswerdef .= 'MULTICHOICE:';
break;
case 'shortanswer':
$parsableanswerdef .= 'SHORTANSWER:';
break;
case 'numerical':
$parsableanswerdef .= 'NUMERICAL:';
break;
default:
error("questiontype $wrapped->qtype not recognized");
}
$separator= '';
foreach ($wrapped->options->answers as $subanswer) {
$parsableanswerdef .= $separator
. '%' . round(100*$subanswer->fraction) . '%';
$parsableanswerdef .= $subanswer->answer;
if (!empty($wrapped->options->tolerance)) {
// Special for numerical answers:
$parsableanswerdef .= ":{$wrapped->options->tolerance}";
// We only want tolerance for the first alternative, it will
// be applied to all of the alternatives.
unset($wrapped->options->tolerance);
}
if ($subanswer->feedback) {
$parsableanswerdef .= "#$subanswer->feedback";
}
$separator = '~';
}
$parsableanswerdef .= '}';
// Fix the questiontext fields of old questions
set_field('question', 'questiontext', addslashes($parsableanswerdef), 'id', $wrapped->id);
} else {
$parsableanswerdef = str_replace('&#', '&\#', $wrapped->questiontext);
}
$question->questiontext = str_replace("{#$key}", $parsableanswerdef, $question->questiontext);
}
}
}
function qtype() {
return 'multianswer';
}
}
?>