-
Notifications
You must be signed in to change notification settings - Fork 1
/
questiontype.php
308 lines (259 loc) · 11 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
<?php // $Id: questiontype.php,v 1.1 2009/06/23 13:39:49 arborrow Exp $
/////////////////
/// FLASH ///
/////////////////
// Recoded from true/false question type
require_once($CFG->libdir . '/questionlib.php');
/**
* @package questionbank
* @subpackage questiontypes
*/
/// QUESTION TYPE CLASS //////////////////
class question_flash_qtype extends default_questiontype {
function name() {
return 'flash';
}
function save_question_options($question) {
if ($options = get_record('question_flash', 'question', $question->id)) {
//$options->question = $question->id;
$options->width = $question->flashwidth;
$options->height = $question->flashheight;
@$options->optionalfile = $question->optionalfile;
$options->optionaldata = $question->optionaldata;
if (!update_record('question_flash', $options)) {
$result->error = "Could not update quiz flash options! (id=$options->id)";
return $result;
}
} else {
unset($options);
$options->question = $question->id;
$options->width = $question->flashwidth;
$options->height = $question->flashheight;
@$options->optionalfile = $question->optionalfile;
$options->optionaldata = $question->optionaldata;
if (!insert_record('question_flash', $options)) {
$result->error = 'Could not insert quiz flash 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 (!$options = get_record('question_flash', 'question', $question->id)) {
return false;
}
$question->flashwidth = $options->width;
$question->flashheight = $options->height;
@$question->optionalfile = $options->optionalfile;
@$question->optionaldata = $options->optionaldata;
// Load the answers
$question->options->answers = get_records('question_answers', 'question', $question->id);
return true;
}
/**
* Deletes question from the question-type specific tables
*
* @return boolean Success/Failure
* @param object $question The question being deleted
// */
// function delete_question($questionid) {
// delete_records("question_flash", "question", $questionid);
// return true;
// }
function delete_states($stateslist) {
/// The default question type does not have any tables of its own
// therefore there is nothing to delete
delete_records_select('question_flash_states', "stateid IN ($stateslist)");
return true;
}
function get_correct_responses(&$question, &$state) {
// foreach ($question->options->answers as $answer) {
// if (((int) $answer->fraction) === 1) {
// return array('' => $answer->id);
// }
// }
//$state->options->fillcorrect = true;
//return array('' => '100%');
return null;
}
// for moodle v1.9 and higher (dlnsk)
function get_html_head_contributions(&$question, &$state) {
global $CFG;
// Load flash interface libraries
require_js($CFG->wwwroot.'/question/type/flash/flash_tag.js');
require_js($CFG->wwwroot.'/question/type/flash/interface.js');
$contributions = parent::get_html_head_contributions($question, $state);
return $contributions;
}
/**
* Prints the main content of the question including any interactions
*/
function print_question_formulation_and_controls(&$question, &$state,
$cmoptions, $options) {
global $CFG;
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->para = false;
$qName = 'q'.$question->id;
$readonly = $options->readonly ? '&qRO=1' : '';
$adaptive = ($cmoptions->optionflags & QUESTION_ADAPTIVE) ? '&qAM=1' : '';
$fillcorrect = $options->correct_responses ? '&qFC=1' : '';
if ($options->responses) {
$flashdata = !empty($state->options->flashdata) ? addslashes_js('&flData='.$state->options->flashdata) : '';
}
$description = !empty($state->options->answer) ? addslashes_js('&qDesc='.$state->options->answer) : '';
$grade = '&qGr='.$state->raw_grade;
$width = $question->flashwidth;
$height = $question->flashheight;
$optionalfile = !empty($question->optionalfile) ? '&optFile='.get_file_url("{$cmoptions->course}/FlashQuestions/{$question->optionalfile}") : '';
$optionaldata = !empty($question->optionaldata) ? addslashes_js('&optData='.$question->optionaldata) : '';
// Print question formulation
$questiontext = format_text($question->questiontext,
$question->questiontextformat,
$formatoptions, $cmoptions->course);
$image = get_question_image($question, $cmoptions->course);
include("$CFG->dirroot/question/type/flash/display.html");
}
function grade_responses(&$question, &$state, $cmoptions) {
// Only allow one attempt at the question
//$state->penalty = 0;
if (isset($state->responses['grade'])) {
$gr = (float)$state->responses['grade'];
$gr = $gr < 0 ? 0 : $gr;
$gr = $gr > 1 ? 1 : $gr;
$state->raw_grade = $gr * $question->maxgrade;
}
// mark the state as graded
$state->event = ($state->event == QUESTION_EVENTCLOSE) ? QUESTION_EVENTCLOSEANDGRADE : QUESTION_EVENTGRADE;
return true;
}
function response_summary($question, $state, $length=80) {
if (isset($question->options->answers[$state->answer])) {
$responses = $question->options->answers[$state->answer]->answer;
} else {
$responses = '';
}
return $responses;
}
function get_all_responses(&$question, &$state) {
$result = parent::get_all_responses($question, $state);
foreach ($result->responses as $res) {
if ($res->credit == 1) {
return $result;
}
}
$r = new stdClass;
$r->answer = '100%';
$r->credit = 1;
$result->responses[0] = $r;
return $result;
}
function get_actual_response($question, $state) {
if (!empty($state->responses)) {
$responses[] = $state->responses[''];
} else {
$responses[] = '';
}
return $responses;
}
function restore_session_and_responses(&$question, &$state) {
if (!$options = get_record('question_flash_states', 'stateid', $state->id)) {
return false;
}
$state->options->flashdata = $options->flashdata;
$state->options->grade = $options->grade;
$state->options->answer = $state->responses[''];
$state->responses['flashdata'] = $options->flashdata;
$state->responses['grade'] = $options->grade;
return true;
}
function save_session_and_responses(&$question, &$state) {
if (!set_field('question_states', 'answer', $state->responses[''], 'id', $state->id)) {
return false;
}
$state->responses[''] = stripslashes($state->responses['']);
if (!empty($state->responses['flashdata'])) {
$state->responses['flashdata'] = stripslashes($state->responses['flashdata']);
}
$options->stateid = $state->id;
$options->flashdata = isset($state->responses['flashdata']) ? $state->responses['flashdata'] : '';
$options->grade = isset($state->responses['grade']) ? $state->responses['grade'] : 0;
$state->options = clone($options);
// Only in this function we already know $state->id
if ($options->id = get_field('question_flash_states', 'id', 'stateid', $state->id)) {
if (!update_record('question_flash_states', $options)) {
return false;
}
} else {
if (!$options->id = insert_record('question_flash_states', $options)) {
return false;
}
}
if (!empty($state->responses[''])) {
if (!$answer = get_record('question_answers', 'question', $question->id, 'answer', $state->responses[''])) {
$answer->question = $question->id;
$answer->answer = $state->responses[''];
$answer->fraction = $options->grade;
insert_record('question_answers', $answer);
} else {
$answer->fraction = $options->grade;
update_record('question_answers', $answer);
}
}
return true;
}
/// BACKUP FUNCTIONS ////////////////////////////
/*
* Backup the data in a truefalse question
*
* This is used in question/backuplib.php
*/
function backup($bf,$preferences,$question,$level=6) {
$status = true;
// Output the flash question settings.
$flashoptions = get_record('question_flash', 'question', $question);
if ($flashoptions) {
$status = fwrite ($bf,start_tag('FLASHOPTIONS',6,true));
fwrite ($bf,full_tag('WIDTH',7,false,$flashoptions->width));
fwrite ($bf,full_tag('HEIGHT',7,false,$flashoptions->height));
fwrite ($bf,full_tag('OPTIONALFILE',7,false,$flashoptions->optionalfile));
fwrite ($bf,full_tag('OPTIONALDATA',7,false,$flashoptions->optionaldata));
$status = fwrite ($bf,end_tag('FLASHOPTIONS',6,true));
}
return $status;
}
/// RESTORE FUNCTIONS /////////////////
/*
* Restores the data in the question
*
* This is used in question/restorelib.php
*/
function restore($old_question_id,$new_question_id,$info,$restore) {
$status = true;
//We have created every match_sub, now create the match
$flash = new stdClass;
$flash->question = $new_question_id;
// Get options
$flash->width = backup_todb($info['#']['FLASHOPTIONS']['0']['#']['WIDTH']['0']['#']);
$flash->height = backup_todb($info['#']['FLASHOPTIONS']['0']['#']['HEIGHT']['0']['#']);
$flash->optionalfile = backup_todb($info['#']['FLASHOPTIONS']['0']['#']['OPTIONALFILE']['0']['#']);
$flash->optionaldata = backup_todb($info['#']['FLASHOPTIONS']['0']['#']['OPTIONALDATA']['0']['#']);
//The structure is equal to the db, so insert the question_match_sub
$newid = insert_record ('question_flash', $flash);
if (!$newid) {
$status = false;
}
return $status;
}
}
//// END OF CLASS ////
//////////////////////////////////////////////////////////////////////////
//// INITIATION - Without this line the question type is not in use... ///
//////////////////////////////////////////////////////////////////////////
question_register_questiontype(new question_flash_qtype());
?>