mirrored from git://git.moodle.org/moodle.git
/
questiontype.php
216 lines (188 loc) · 8.25 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
<?php // $Id$
/////////////////
/// TRUEFALSE ///
/////////////////
/// QUESTION TYPE CLASS //////////////////
class quiz_truefalse_qtype extends quiz_default_questiontype {
function name() {
return 'truefalse';
}
function save_question_options($question) {
// fetch old answer ids so that we can reuse them
if (!$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC")) {
$oldanswers = array();
}
// Save answer 'True'
if ($true = array_shift($oldanswers)) { // Existing answer, so reuse it
$true->answer = get_string("true", "quiz");
$true->fraction = $question->answer;
$true->feedback = $question->feedbacktrue;
if (!update_record("quiz_answers", $true)) {
$result->error = "Could not update quiz answer \"true\")!";
return $result;
}
} else {
unset($true);
$true->answer = get_string("true", "quiz");
$true->question = $question->id;
$true->fraction = $question->answer;
$true->feedback = $question->feedbacktrue;
if (!$true->id = insert_record("quiz_answers", $true)) {
$result->error = "Could not insert quiz answer \"true\")!";
return $result;
}
}
// Save answer 'False'
if ($false = array_shift($oldanswers)) { // Existing answer, so reuse it
$false->answer = get_string("false", "quiz");
$false->fraction = 1 - (int)$question->answer;
$false->feedback = $question->feedbackfalse;
if (!update_record("quiz_answers", $false)) {
$result->error = "Could not insert quiz answer \"false\")!";
return $result;
}
} else {
unset($false);
$false->answer = get_string("false", "quiz");
$false->question = $question->id;
$false->fraction = 1 - (int)$question->answer;
$false->feedback = $question->feedbackfalse;
if (!$false->id = insert_record("quiz_answers", $false)) {
$result->error = "Could not insert quiz answer \"false\")!";
return $result;
}
}
// delete any leftover old answer records (there couldn't really be any, but who knows)
if (!empty($oldanswers)) {
foreach($oldanswers as $oa) {
delete_records('quiz_answers', 'id', $oa->id);
}
}
// Save question options in quiz_truefalse table
if ($options = get_record("quiz_truefalse", "question", $question->id)) {
// No need to do anything, since the answer IDs won't have changed
// But we'll do it anyway, just for robustness
$options->trueanswer = $true->id;
$options->falseanswer = $false->id;
if (!update_record("quiz_truefalse", $options)) {
$result->error = "Could not update quiz truefalse options! (id=$options->id)";
return $result;
}
} else {
unset($options);
$options->question = $question->id;
$options->trueanswer = $true->id;
$options->falseanswer = $false->id;
if (!insert_record("quiz_truefalse", $options)) {
$result->error = "Could not insert quiz truefalse options!";
return $result;
}
}
return true;
}
/**
* Loads the question type specific options for the question.
*/
function get_question_options(&$question) {
// Get additional information from database
// and attach it to the question object
if (!$question->options = get_record('quiz_truefalse', 'question', $question->id)) {
notify('Error: Missing question options!');
return false;
}
// Load the answers
if (!$question->options->answers = get_records('quiz_answers', 'question', $question->id)) {
notify('Error: Missing question answers!');
return false;
}
return true;
}
/**
* Deletes question from the question-type specific tables
*
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
function delete_question($question) {
delete_records("quiz_truefalse", "question", $question->id);
return true;
}
function get_correct_responses(&$question, &$state) {
// The correct answer is the one which gives full marks
foreach ($question->options->answers as $answer) {
if (((int) $answer->fraction) === 1) {
return array('' => $answer->id);
}
}
return null;
}
/**
* Prints the main content of the question including any interactions
*/
function print_question_formulation_and_controls(&$question, &$state,
$cmoptions, $options) {
global $CFG;
$readonly = $options->readonly ? ' readonly="readonly"' : '';
// Print question formulation
$questiontext = format_text($question->questiontext,
$question->questiontextformat,
NULL, $cmoptions->course);
$image = quiz_get_image($question, $cmoptions->course);
$answers = &$question->options->answers;
$trueanswer = &$answers[$question->options->trueanswer];
$falseanswer = &$answers[$question->options->falseanswer];
$correctanswer = ($trueanswer->fraction == 1) ? $trueanswer : $falseanswer;
// Work out which radio button to select (if any)
$truechecked = ($state->responses[''] == $trueanswer->id) ? ' checked="checked"' : '';
$falsechecked = ($state->responses[''] == $falseanswer->id) ? ' checked="checked"' : '';
// Work out which answer is correct if we need to highlight it
if ($options->correct_responses) {
$trueclass = ($trueanswer->fraction) ? ' class="highlight"' : '';
$falseclass = ($falseanswer->fraction) ? ' class="highlight"' : '';
} else {
$trueclass = '';
$falseclass = '';
}
$inputname = ' name="'.$question->name_prefix.'" ';
$trueid = $question->name_prefix.'true';
$falseid = $question->name_prefix.'false';
$radiotrue = '<input type="radio"' . $truechecked . $readonly . $inputname
. 'id="'.$trueid . '" value="' . $trueanswer->id . '" alt="'
. s($trueanswer->answer) . '" /><label for="'.$trueid . '">'
. s($trueanswer->answer) . '</label>';
$radiofalse = '<input type="radio"' . $falsechecked . $readonly . $inputname
. 'id="'.$falseid . '" value="' . $falseanswer->id . '" alt="'
. s($falseanswer->answer) . '" /><label for="'.$falseid . '">'
. s($falseanswer->answer) . '</label>';
$feedback = '';
if ($options->feedback and isset($answers[$state->responses['']])) {
$chosenanswer = $answers[$state->responses['']];
$feedback = format_text($chosenanswer->feedback, true, false);
}
include("$CFG->dirroot/mod/quiz/questiontypes/truefalse/display.html");
}
function grade_responses(&$question, &$state, $cmoptions) {
if (isset($question->options->answers[$state->responses['']])) {
$state->raw_grade = $question->options->answers[$state->responses['']]->fraction * $question->maxgrade;
} else {
$state->raw_grade = 0;
}
// Only allow one attempt at the question
$state->penalty = 1;
return true;
}
function get_actual_response($question, $state) {
if (isset($question->options->answers[$state->responses['']])) {
$responses[] = $question->options->answers[$state->responses['']]->answer;
} else {
$responses[] = '';
}
return $responses;
}
}
//// END OF CLASS ////
//////////////////////////////////////////////////////////////////////////
//// INITIATION - Without this line the question type is not in use... ///
//////////////////////////////////////////////////////////////////////////
$QUIZ_QTYPES[TRUEFALSE]= new quiz_truefalse_qtype();
?>