forked from h5p/moodle-mod_hvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
392 lines (333 loc) · 12.2 KB
/
lib.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?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/>.
/**
* Library of interface functions and constants for module hvp.
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
*
* All the hvp specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_hvp
* @copyright 2016 Joubel AS <contact@joubel.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once('autoloader.php');
/* Moodle core API */
/**
* Returns the information on whether the module supports a feature
*
* See {@link plugin_supports()} for more info.
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed true if the feature is supported, null if unknown
*/
function hvp_supports($feature) {
switch($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return false;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* Saves a new instance of the hvp into the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param stdClass $hvp Submitted data from the form in mod_form.php
* @return int The id of the newly inserted newmodule record
*/
function hvp_add_instance($hvp) {
// Save content.
$hvp->id = hvp_save_content($hvp);
// Set and create grade item.
hvp_grade_item_update($hvp);
return $hvp->id;
}
/**
* Updates an instance of the hvp in the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param stdClass $hvp An object from the form in mod_form.php
* @return boolean Success/Fail
*/
function hvp_update_instance($hvp) {
// Make ID available for core to save.
$hvp->id = $hvp->instance;
// Save content.
hvp_save_content($hvp);
hvp_grade_item_update($hvp);
return true;
}
/**
* Does the actual process of saving the H5P content that's submitted through
* the activity form
*
* @param stdClass $hvp
* @return int Content ID
*/
function hvp_save_content($hvp) {
// Determine if we're uploading or creating.
if ($hvp->h5paction === 'upload') {
// Save uploaded package.
$hvp->uploaded = true;
$h5pstorage = \mod_hvp\framework::instance('storage');
$h5pstorage->savePackage((array)$hvp);
$hvp->id = $h5pstorage->contentId;
} else {
// Save newly created or edited content.
$core = \mod_hvp\framework::instance();
$editor = \mod_hvp\framework::instance('editor');
if (!empty($hvp->id)) {
// Load existing content to get old parameters for comparison.
$content = $core->loadContent($hvp->id);
$oldlib = $content['library'];
$oldparams = json_decode($content['params']);
}
// Make params and library available for core to save.
$hvp->library = H5PCore::libraryFromString($hvp->h5plibrary);
$hvp->library['libraryId'] = $core->h5pF->getLibraryId($hvp->library['machineName'],
$hvp->library['majorVersion'],
$hvp->library['minorVersion']);
$hvp->id = $core->saveContent((array)$hvp);
// We need to process the parameters to move any images or files and
// to determine which dependencies the content has.
// Prepare current parameters.
$params = json_decode($hvp->params);
// Move any uploaded images or files. Determine content dependencies.
$editor->processParameters($hvp, $hvp->library, $params,
isset($oldlib) ? $oldlib : null,
isset($oldparams) ? $oldparams : null);
}
return $hvp->id;
}
/**
* Removes an instance of the hvp from the database
*
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
*/
function hvp_delete_instance($id) {
global $DB;
// Load content record.
if (! $hvp = $DB->get_record('hvp', array('id' => "$id"))) {
return false;
}
// Load CM.
$cm = \get_coursemodule_from_instance('hvp', $id);
// Delete content.
$h5pstorage = \mod_hvp\framework::instance('storage');
$h5pstorage->deletePackage(array('id' => $hvp->id, 'slug' => $hvp->slug, 'coursemodule' => $cm->id));
// Delete xAPI statements.
$DB->delete_records('hvp_xapi_results', array (
'content_id' => $hvp->id
));
// Get library details.
$library = $DB->get_record_sql(
"SELECT machine_name AS name, major_version, minor_version
FROM {hvp_libraries}
WHERE id = ?",
array($hvp->main_library_id)
);
// Log content delete.
new \mod_hvp\event(
'content', 'delete',
$hvp->id, $hvp->name,
$library->name, $library->major_version . '.' . $library->minor_version
);
return true;
}
/**
* Serves the files from the hvp file areas
*
* @package mod_hvp
* @category files
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the newmodule's context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
*
* @return true|false Success
*/
function hvp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = array()) {
switch ($filearea) {
default:
return false; // Invalid file area.
case 'libraries':
case 'cachedassets':
if ($context->contextlevel != CONTEXT_SYSTEM) {
return false; // Invalid context.
}
// Check permissions.
if (!has_capability('mod/hvp:getcachedassets', $context)) {
return false;
}
$itemid = 0;
break;
case 'content':
if ($context->contextlevel != CONTEXT_MODULE) {
return false; // Invalid context.
}
// Check permissions.
if (!has_capability('mod/hvp:view', $context)) {
return false;
}
$itemid = array_shift($args);
break;
case 'exports':
if ($context->contextlevel != CONTEXT_MODULE) {
return false; // Invalid context.
}
// Check permission.
if (!has_capability('mod/hvp:view', $context)) {
return false;
}
// Note that the getexport permission is checked after loading the content.
// Get core.
$h5pinterface = \mod_hvp\framework::instance('interface');
$h5pcore = \mod_hvp\framework::instance('core');
$matches = array();
// Get content id from filename.
if (!preg_match('/(\d*).h5p$/', $args[0], $matches)) {
// Did not find any content ID.
return false;
}
$contentid = $matches[1];
$content = $h5pinterface->loadContent($contentid);
$displayoptions = $h5pcore->getDisplayOptionsForView($content['disable'], $context->instanceid);
// Check permissions.
if (!$displayoptions['export']) {
return false;
}
$itemid = 0;
// Change context to course for retrieving file.
$cm = get_coursemodule_from_id('hvp', $context->instanceid);
$context = context_course::instance($cm->course);
break;
case 'editor':
$cap = ($context->contextlevel === CONTEXT_COURSE ? 'addinstance' : 'manage');
// Check permissions.
if (!has_capability("mod/hvp:$cap", $context)) {
return false;
}
$itemid = 0;
break;
}
$filename = array_pop($args);
$filepath = (!$args ? '/' : '/' .implode('/', $args) . '/');
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'mod_hvp', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false; // No such file.
}
// Totara: use allowxss option to prevent application/x-javascript mimetype
// from being converted to application/x-forcedownload.
$options['allowxss'] = '1';
send_stored_file($file, 86400, 0, $forcedownload, $options);
return true;
}
/**
* Create/update grade item for given hvp
*
* @category grade
* @param stdClass $hvp object with extra cmidnumber
* @param mixed $grades Optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int, 0 if ok, error code otherwise
*/
function hvp_grade_item_update($hvp, $grades=null) {
global $CFG;
if (!function_exists('grade_update')) { // Workaround for buggy PHP versions.
require_once($CFG->libdir . '/gradelib.php');
}
$params = array('itemname' => $hvp->name, 'idnumber' => $hvp->cmidnumber);
if (isset($hvp->maximumgrade)) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $hvp->maximumgrade;
}
// Recalculate rawgrade relative to grademax.
if (isset($hvp->rawgrade) && isset($hvp->rawgrademax) && $hvp->rawgrademax != 0) {
// Get max grade Obs: do not try to use grade_get_grades because it
// requires context which we don't have inside an ajax.
$gradeitem = grade_item::fetch(array(
'itemtype' => 'mod',
'itemmodule' => 'hvp',
'iteminstance' => $hvp->id,
'courseid' => $hvp->course
));
if (isset($gradeitem) && isset($gradeitem->grademax)) {
$grades->rawgrade = ($hvp->rawgrade / $hvp->rawgrademax) * $gradeitem->grademax;
}
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/hvp', $hvp->course, 'mod', 'hvp', $hvp->id, 0, $grades, $params);
}
/**
* Update activity grades
*
* @category grade
* @param stdClass $hvp null means all hvps (with extra cmidnumber property)
* @param int $userid specific user only, 0 means all
* @param bool $nullifnone If true and the user has no grade then a grade item with rawgrade == null will be inserted
*/
function hvp_update_grades($hvp=null, $userid=0, $nullifnone=true) {
if ($userid and $nullifnone) {
$grade = new stdClass();
$grade->userid = $userid;
$grade->rawgrade = null;
hvp_grade_item_update($hvp, $grade);
} else {
hvp_grade_item_update($hvp);
}
}