-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformat.php
More file actions
339 lines (318 loc) · 12.2 KB
/
format.php
File metadata and controls
339 lines (318 loc) · 12.2 KB
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
<?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 Import for H5P Quiz content type
*
* @package qformat_h5p
* @copyright 2020 Daniel Thies <dethies@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use qformat_h5p\local;
/**
* Question Import for H5P Quiz content type
*
* @copyright 2020 Daniel Thies <dethies@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qformat_h5p extends qformat_default {
/** @var $tempdir Tempory directory path */
protected $tempdir = '';
/**
* Imoport functionality
*
* @return bool whether this plugin provides import functionality.
*/
public function provide_import() {
return true;
}
/**
* Export functionality
*
* @return bool whether this plugin provides export functionality.
*/
public function provide_export() {
return false;
}
/**
* File extension
*
* @return string the file extension (including .) that is normally used for
* files handled by this plugin.
*/
public function export_file_extension() {
return '.h5p';
}
/**
* The string mime-type of the files that this plugin reads or writes.
*
* @return string
*/
public function mime_type() {
// This is a hack to support version before h5p support.
if (mimeinfo('type', $this->export_file_extension()) == 'document/unknown') {
return mimeinfo('type', '.zip');
}
return mimeinfo('type', $this->export_file_extension());
}
/**
* Return the content of a file given by its path in the tempdir directory.
*
* @param string $path path to the file inside tempdir
* @return mixed contents array or false on failure
*/
public function get_filecontent($path) {
$fullpath = $this->tempdir . '/' . $path;
if (is_file($fullpath) && is_readable($fullpath)) {
return file_get_contents($fullpath);
}
return false;
}
/**
* Return content of all files containing questions,
* as an array one element for each file found,
* For each file, the corresponding element is an array of lines.
*
* @param string $filename name of file
* @return mixed contents array or false on failure
*/
public function readdata($filename) {
$this->tempdir = make_request_directory();
if (is_readable($filename)) {
if (!copy($filename, $this->tempdir . '/content.zip')) {
$this->error(get_string('cannotcopybackup', 'question'));
fulldelete($this->tempdir);
return false;
}
$packer = get_file_packer('application/zip');
if ($packer->extract_to_pathname($this->tempdir . '/content.zip', $this->tempdir)) {
$h5p = json_decode($this->get_filecontent('h5p.json'));
$content = json_decode($this->get_filecontent('content/content.json'));
return $this->read_content($h5p->mainLibrary, $content, $h5p->title);
} else {
$this->error(get_string('cannotunzip', 'question'));
fulldelete($this->temp_dir);
}
} else {
$this->error(get_string('cannotreaduploadfile', 'error'));
fulldelete($this->tempdir);
}
return false;
}
/**
* Parse the a content object
*
* @param string $library The main library for content.
* @param object $content The content object.
* @param string $title default title for question
* @return array (of objects) question objects.
*/
public function read_content($library, $content, $title) {
$questions = [];
switch ($library) {
case 'H5P.InteractiveBook':
$content->content = array_reduce(
$content->chapters,
function ($carry, $content) {
return array_merge($carry, $content->params->content);
},
[]
);
return array_column($content->content, 'content');
case 'H5P.BranchingScenario':
$questions = [];
foreach ($content->branchingScenario->content as $subcontent) {
$questions += $this->read_content(
preg_replace('/ .*/', '', $subcontent->type->library),
$subcontent->type->params,
$title
);
}
return $questions;
case 'H5P.Column':
return array_column($content->content, 'content');
case 'H5P.CoursePresentation':
$actions = [];
foreach ($content->presentation->slides as $slide) {
foreach (array_column($slide->elements, 'action') as $action) {
$actions = array_merge($actions, $this->read_subcontent($action));
}
}
return $actions;
case 'H5P.Crossword':
$dialogs = [];
foreach ($content->words as $word) {
$dialogs[] = (object) [
'params' => (object) [
'question' => $word->clue,
'answer' => $word->answer,
],
'library' => 'Dialogcards',
'metadata' => (object) [
'title' => $word->answer,
],
];
}
return $dialogs;
case 'H5P.Flashcards':
$content->dialogs = $content->cards;
// Handle like dialog cards.
case 'H5P.Dialogcards':
$dialogs = [];
foreach ($content->dialogs as $dialog) {
$dialogs[] = (object) [
'params' => (object) [
'question' => $dialog->text,
'answer' => $dialog->answer,
'audio' => $dialog->audio ?? null,
'media' => (object) [
'type' => (object) [
'params' => (object) [
'file' => $dialog->image,
'contentName' => 'Image',
],
],
],
],
'library' => 'Dialogcards',
'metadata' => (object) [
'title' => $dialog->text,
],
];
}
return $dialogs;
case 'H5P.InteractiveVideo':
return array_column($content->interactiveVideo->assets->interactions, 'action');
case 'H5P.QuestionSet':
return $content->questions;
case 'H5P.SingleChoiceSet':
return $this->read_choices($content);
default:
$question = new stdClass();
$question->params = $content;
$question->metadata = (object) [
'title' => $title,
];
$question->library = $library;
return [$question];
}
}
/**
* Parse the array of objects into an array of questions.
*
* @param array $lines array of json decoded h5p content objects for each input file.
* @return array (of objects) question objects.
*/
public function readquestions($lines) {
// Set up array to hold all our questions.
$questions = [];
// Each element of $lines is a h5p content type with data.
foreach ($lines as $content) {
if (($type = $this->create_content_type($content)) && $qo = $type->import_question()) {
$questions[] = $qo;
}
}
return $questions;
}
/**
* Extract questions from subcontent of course presentation data
*
* @param object $content
* @return array question data to be imported
*/
public function read_subcontent($content) {
switch (preg_replace('/ .*/', '', $content->library)) {
case 'H5P.QuestionSet':
return $content->questions;
case 'H5P.SingleChoiceSet':
return $this->read_choices($content->params);
default:
return [$content];
}
}
/**
* Reformat Single choice set subcontent as multiple choice struncture
*
* @param object $content
* @return array multichoice content to be imported
*/
public function read_choices($content) {
$questions = [];
foreach ($content->choices as $choice) {
$answers = [];
foreach ($choice->answers as $key => $answer) {
$answers[] = (object) [
'text' => $answer,
'correct' => empty($key),
'tipsAndFeedback' => (object) [
'chosenFeedback' => empty($key) ? ($content->l10n->correctText ?? '') : $content->l10n->incorrectText ?? '',
],
];
}
$questions[] = (object) [
'params' => (object) [
'question' => $choice->question,
'answers' => $answers,
],
'metadata' => (object) [
'title' => $choice->question,
],
'library' => 'H5P.MultiChoice',
];
};
return $questions;
}
/**
* Find read question type from content and provide appropriate converter
*
* @param object $content question data
* @return object import object
*/
public function create_content_type($content) {
if (empty($content->library)) {
return '';
}
switch (preg_replace('/ .*/', '', $content->library)) {
case 'H5P.AdvancedBlanks':
return new local\type_afib($content, $this->tempdir);
case 'H5P.Blanks':
return new local\type_fib($content, $this->tempdir);
case 'Dialogcards':
return new local\type_card($content, $this->tempdir);
case 'H5P.GuessTheAnswer':
return new local\type_guess($content, $this->tempdir);
case 'H5P.ImageMultipleHotspotQuestion':
case 'H5P.ImageHotspotQuestion':
return new local\type_hotspot($content, $this->tempdir);
case 'H5P.ImageSequencing':
return new local\type_imagesequence($content, $this->tempdir);
case 'H5P.MultiChoice':
return new local\type_mc($content, $this->tempdir);
case 'H5P.TrueFalse':
return new local\type_tf($content, $this->tempdir);
case 'H5P.DragQuestion':
return new local\type_dnd($content, $this->tempdir);
case 'H5P.DragText':
return new local\type_dtw($content, $this->tempdir);
case 'H5P.Essay':
return new local\type_essay($content, $this->tempdir);
case 'H5P.MarkTheWords':
return new local\type_mtw($content, $this->tempdir);
default:
return '';
return new local\type_desc($content, $this->tempdir); // This is more helpful for debugging.
}
}
}