forked from justinhunt/moodle-qtype_cloudpoodll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestiontype.php
246 lines (212 loc) · 9.86 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
<?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 cloudpoodll question type.
*
* @package qtype
* @subpackage cloudpoodll
* @copyright 2012 Justin Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use qtype_cloudpoodll\constants;
/**
* The cloudpoodll question type.
*
* @copyright 2012 Justin Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_cloudpoodll extends question_type {
public function is_manual_graded() {
return true;
}
public function response_file_areas() {
return array('answer');
}
public function get_question_options($question) {
global $DB;
$question->options = $DB->get_record(constants::M_TABLE,
array('questionid' => $question->id), '*', MUST_EXIST);
parent::get_question_options($question);
}
public function save_question_options($formdata) {
global $DB;
$context = $formdata->context;
$options = $DB->get_record(constants::M_TABLE, array('questionid' => $formdata->id));
if (!$options) {
$options = new stdClass();
$options->questionid = $formdata->id;
$options->id = $DB->insert_record(constants::M_TABLE, $options);
}
//"import_or_save_files" won't work, because it expects output from an editor which is an array with member itemid
//the filemanager doesn't produce this, so need to use file save draft area directly
if (isset($formdata->qresource)) {
file_save_draft_area_files($formdata->qresource, $context->id, constants::M_COMP,
\qtype_cloudpoodll\constants::FILEAREA_QRESOURCE, $formdata->id,
array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
//save the itemid of the qresource filearea
$options->qresource = $formdata->qresource;
} else {
$options->qresource = null;
}
//if we have a recording time limit
if (isset($formdata->timelimit)) {
$options->timelimit = $formdata->timelimit;
} else {
$options->timelimit = 0;
}
//other options
$options->language = $formdata->language;
$options->expiredays = $formdata->expiredays;
$options->transcriber = $formdata->transcriber;
$options->transcode = $formdata->transcode;
$options->audioskin = $formdata->audioskin;
$options->videoskin = $formdata->videoskin;
$options->safesave = $formdata->safesave;
$options->studentplayer = $formdata->studentplayer;
$options->teacherplayer = $formdata->teacherplayer;
$options->responseformat = $formdata->responseformat;
$options->graderinfo = $this->import_or_save_files($formdata->graderinfo,
$context, constants::M_COMP, 'graderinfo', $formdata->id);
$options->graderinfoformat = $formdata->graderinfo['format'];
$DB->update_record(constants::M_TABLE, $options);
}
protected function initialise_question_instance(question_definition $question, $questiondata) {
parent::initialise_question_instance($question, $questiondata);
foreach (constants::extra_fields as $field) {
$question->{$field} = $questiondata->options->{$field};
}
$question->responseformat = $questiondata->options->responseformat;
}
/**
* @return array the different response formats that the question type supports.
* internal name => human-readable name.
*/
public function response_formats() {
return array(
'audio' => get_string('formataudio', constants::M_COMP),
'video' => get_string('formatvideo', constants::M_COMP)
);
}
public function move_files($questionid, $oldcontextid, $newcontextid) {
parent::move_files($questionid, $oldcontextid, $newcontextid);
$fs = get_file_storage();
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, constants::M_COMP,
\qtype_cloudpoodll\constants::FILEAREA_GRADERINFO, $questionid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, constants::M_COMP,
\qtype_cloudpoodll\constants::FILEAREA_QRESOURCE, $questionid);
}
protected function delete_files($questionid, $contextid) {
parent::delete_files($questionid, $contextid);
$fs = get_file_storage();
$fs->delete_area_files($contextid, constants::M_COMP,
\qtype_cloudpoodll\constants::FILEAREA_GRADERINFO, $questionid);
$fs->delete_area_files($contextid, constants::M_COMP,
\qtype_cloudpoodll\constants::FILEAREA_QRESOURCE, $questionid);
}
/**
* If your question type has a table that extends the question table, and
* you want the base class to automatically save, backup and restore the extra fields,
* override this method to return an array wherer the first element is the table name,
* and the subsequent entries are the column names (apart from id and questionid).
*
* @return mixed array as above, or null to tell the base class to do nothing.
*/
public function extra_question_fields() {
$tableinfo = array(constants::M_TABLE);
foreach (constants::extra_fields as $field) {
$tableinfo[] = $field;
}
return $tableinfo;
}
/*
* Export question to the Moodle XML format
*
* Export question using information from extra_question_fields function
* We override this because we need to export file fields as base 64 strings, not ids
*/
public function export_to_xml($question, qformat_xml $format, $extra = null) {
//get file storage
$fs = get_file_storage();
$expout = "";
$expout .= " <responseformat>" . $question->options->responseformat .
"</responseformat>\n";
$expout .= " <graderinfo " .
$format->format($question->options->graderinfoformat) . ">\n";
$expout .= $format->writetext($question->options->graderinfo, 3);
$expout .= $format->write_files($fs->get_area_files($question->contextid, constants::M_COMP,
\qtype_cloudpoodll\constants::FILEAREA_GRADERINFO, $question->id));
$expout .= " </graderinfo>\n";
$expout .= " <qresource>" . $format->write_files($fs->get_area_files($question->contextid, constants::M_COMP,
\qtype_cloudpoodll\constants::FILEAREA_QRESOURCE, $question->id)) .
"</qresource>\n";
foreach (constants::extra_fields as $field) {
switch ($field) {
case 'graderinfoformat':
case 'graderinfo':
case 'qresource':
break;
default:
$expout .= " <$field>" . $question->options->{$field} .
"</$field>\n";
}
}
return $expout;
}
/*
* Imports question from the Moodle XML format
*
* Imports question using information from extra_question_fields function
* If some of you fields contains id's you'll need to reimplement this
*/
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) {
global $CFG;
$question_type = "cloudpoodll";
//omit table name
$qo = $format->import_headers($data);
$qo->qtype = $question_type;
$q = $data;
$qo->responseformat = $format->getpath($q,
array('#', 'responseformat', 0, '#'), \qtype_cloudpoodll\constants::RESPONSEFORMAT_AUDIO);
$qo->graderinfo = $format->import_text_with_files($q, array('#', \qtype_cloudpoodll\constants::FILEAREA_GRADERINFO, 0), '',
$qo->questiontextformat);
$qo->qresource = $format->import_files_as_draft($format->getpath($q,
array('#', \qtype_cloudpoodll\constants::FILEAREA_QRESOURCE, '0', '#', 'file'), array()));
$qo->language = $format->getpath($q,
array('#', 'language', 0, '#'), constants::LANG_ENUS);
$qo->expiredays = $format->getpath($q,
array('#', 'expiredays', 0, '#'), 365);
$qo->transcriber = $format->getpath($q,
array('#', 'transcriber', 0, '#'), constants::TRANSCRIBER_AMAZONTRANSCRIBE);
$qo->studentplayer = $format->getpath($q,
array('#', 'studentplayer', 0, '#'), constants::PLAYERTYPE_INTERACTIVETRANSCRIPT);
$qo->teacherplayer = $format->getpath($q,
array('#', 'teacherplayer', 0, '#'), constants::PLAYERTYPE_INTERACTIVETRANSCRIPT);
$qo->transcode = $format->getpath($q,
array('#', 'transcode', 0, '#'), 1);
$qo->audioskin = $format->getpath($q,
array('#', 'audioskin', 0, '#'), constants::SKIN_123);
$qo->videoskin = $format->getpath($q,
array('#', 'videoskin', 0, '#'), constants::SKIN_123);
$qo->timelimit = $format->getpath($q,
array('#', 'timelimit', 0, '#'), 0);
$qo->safesave = $format->getpath($q,
array('#', 'safesave', 0, '#'), 0);
return $qo;
}//end of import from xml
}