1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Contains helper class for the H5P area.
|
|
|
19 |
*
|
|
|
20 |
* @package core_h5p
|
|
|
21 |
* @copyright 2019 Sara Arjona <sara@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_h5p;
|
|
|
26 |
|
|
|
27 |
use context_system;
|
|
|
28 |
use core_h5p\local\library\autoloader;
|
11 |
efrain |
29 |
use core_user;
|
1 |
efrain |
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die();
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Helper class for the H5P area.
|
|
|
35 |
*
|
|
|
36 |
* @copyright 2019 Sara Arjona <sara@moodle.com>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class helper {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Store an H5P file.
|
|
|
43 |
*
|
|
|
44 |
* @param factory $factory The \core_h5p\factory object
|
|
|
45 |
* @param \stored_file $file Moodle file instance
|
|
|
46 |
* @param \stdClass $config Button options config
|
|
|
47 |
* @param bool $onlyupdatelibs Whether new libraries can be installed or only the existing ones can be updated
|
|
|
48 |
* @param bool $skipcontent Should the content be skipped (so only the libraries will be saved)?
|
|
|
49 |
*
|
|
|
50 |
* @return int|false|null The H5P identifier or null if there is an error when saving or false if it's not a valid H5P package
|
|
|
51 |
*/
|
|
|
52 |
public static function save_h5p(factory $factory, \stored_file $file, \stdClass $config, bool $onlyupdatelibs = false,
|
|
|
53 |
bool $skipcontent = false) {
|
|
|
54 |
|
|
|
55 |
if (api::is_valid_package($file, $onlyupdatelibs, $skipcontent, $factory, false)) {
|
|
|
56 |
$core = $factory->get_core();
|
|
|
57 |
$h5pvalidator = $factory->get_validator();
|
|
|
58 |
$h5pstorage = $factory->get_storage();
|
|
|
59 |
|
|
|
60 |
$content = [
|
|
|
61 |
'pathnamehash' => $file->get_pathnamehash(),
|
|
|
62 |
'contenthash' => $file->get_contenthash(),
|
|
|
63 |
];
|
|
|
64 |
$options = ['disable' => self::get_display_options($core, $config)];
|
|
|
65 |
|
|
|
66 |
// Add the 'title' if exists from 'h5p.json' data to keep it for the editor.
|
|
|
67 |
if (!empty($h5pvalidator->h5pC->mainJsonData['title'])) {
|
|
|
68 |
$content['title'] = $h5pvalidator->h5pC->mainJsonData['title'];
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// If exists, add the metadata from 'h5p.json' to avoid loosing this information.
|
|
|
72 |
$data = $h5pvalidator->h5pC->mainJsonData;
|
|
|
73 |
if (!empty($data)) {
|
|
|
74 |
// The metadata fields are defined in 'joubel/core/h5p-metadata.class.php'.
|
|
|
75 |
$metadatafields = [
|
|
|
76 |
'title',
|
|
|
77 |
'a11yTitle',
|
|
|
78 |
'changes',
|
|
|
79 |
'authors',
|
|
|
80 |
'source',
|
|
|
81 |
'license',
|
|
|
82 |
'licenseVersion',
|
|
|
83 |
'licenseExtras',
|
|
|
84 |
'authorComments',
|
|
|
85 |
'yearFrom',
|
|
|
86 |
'yearTo',
|
|
|
87 |
'defaultLanguage',
|
|
|
88 |
];
|
|
|
89 |
$content['metadata'] = array_reduce($metadatafields, function ($array, $field) use ($data) {
|
|
|
90 |
if (array_key_exists($field, $data)) {
|
|
|
91 |
$array[$field] = $data[$field];
|
|
|
92 |
}
|
|
|
93 |
return $array;
|
|
|
94 |
}, []);
|
|
|
95 |
}
|
|
|
96 |
$h5pstorage->savePackage($content, null, $skipcontent, $options);
|
|
|
97 |
|
|
|
98 |
return $h5pstorage->contentId;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return false;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Get the error messages stored in our H5P framework.
|
|
|
106 |
*
|
|
|
107 |
* @param \stdClass $messages The error, exception and info messages, raised while preparing and running an H5P content.
|
|
|
108 |
* @param factory $factory The \core_h5p\factory object
|
|
|
109 |
*
|
|
|
110 |
* @return \stdClass with framework error messages.
|
|
|
111 |
*/
|
|
|
112 |
public static function get_messages(\stdClass $messages, factory $factory): \stdClass {
|
|
|
113 |
$core = $factory->get_core();
|
|
|
114 |
|
|
|
115 |
// Check if there are some errors and store them in $messages.
|
|
|
116 |
if (empty($messages->error)) {
|
|
|
117 |
$messages->error = $core->h5pF->getMessages('error') ?: false;
|
|
|
118 |
} else {
|
|
|
119 |
$messages->error = array_merge($messages->error, $core->h5pF->getMessages('error'));
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
if (empty($messages->info)) {
|
|
|
123 |
$messages->info = $core->h5pF->getMessages('info') ?: false;
|
|
|
124 |
} else {
|
|
|
125 |
$messages->info = array_merge($messages->info, $core->h5pF->getMessages('info'));
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
return $messages;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Get the representation of display options as int.
|
|
|
133 |
*
|
|
|
134 |
* @param core $core The \core_h5p\core object
|
|
|
135 |
* @param stdClass $config Button options config
|
|
|
136 |
*
|
|
|
137 |
* @return int The representation of display options as int
|
|
|
138 |
*/
|
|
|
139 |
public static function get_display_options(core $core, \stdClass $config): int {
|
|
|
140 |
$export = isset($config->export) ? $config->export : 0;
|
|
|
141 |
$embed = isset($config->embed) ? $config->embed : 0;
|
|
|
142 |
$copyright = isset($config->copyright) ? $config->copyright : 0;
|
|
|
143 |
$frame = ($export || $embed || $copyright);
|
|
|
144 |
if (!$frame) {
|
|
|
145 |
$frame = isset($config->frame) ? $config->frame : 0;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
$disableoptions = [
|
|
|
149 |
core::DISPLAY_OPTION_FRAME => $frame,
|
|
|
150 |
core::DISPLAY_OPTION_DOWNLOAD => $export,
|
|
|
151 |
core::DISPLAY_OPTION_EMBED => $embed,
|
|
|
152 |
core::DISPLAY_OPTION_COPYRIGHT => $copyright,
|
|
|
153 |
];
|
|
|
154 |
|
|
|
155 |
return $core->getStorableDisplayOptions($disableoptions, 0);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Convert the int representation of display options into stdClass
|
|
|
160 |
*
|
|
|
161 |
* @param core $core The \core_h5p\core object
|
|
|
162 |
* @param int $displayint integer value representing display options
|
|
|
163 |
*
|
|
|
164 |
* @return int The representation of display options as int
|
|
|
165 |
*/
|
|
|
166 |
public static function decode_display_options(core $core, int $displayint = null): \stdClass {
|
|
|
167 |
$config = new \stdClass();
|
|
|
168 |
if ($displayint === null) {
|
|
|
169 |
$displayint = self::get_display_options($core, $config);
|
|
|
170 |
}
|
|
|
171 |
$displayarray = $core->getDisplayOptionsForEdit($displayint);
|
|
|
172 |
$config->export = $displayarray[core::DISPLAY_OPTION_DOWNLOAD] ?? 0;
|
|
|
173 |
$config->embed = $displayarray[core::DISPLAY_OPTION_EMBED] ?? 0;
|
|
|
174 |
$config->copyright = $displayarray[core::DISPLAY_OPTION_COPYRIGHT] ?? 0;
|
|
|
175 |
return $config;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Checks if the author of the .h5p file is "trustable". If the file hasn't been uploaded by a user with the
|
11 |
efrain |
180 |
* required capability, the content won't be deployed, unless the user has been deleted, in this
|
|
|
181 |
* case we check the capability against current user.
|
1 |
efrain |
182 |
*
|
|
|
183 |
* @param stored_file $file The .h5p file to be deployed
|
|
|
184 |
* @return bool Returns true if the file can be deployed, false otherwise.
|
|
|
185 |
*/
|
|
|
186 |
public static function can_deploy_package(\stored_file $file): bool {
|
11 |
efrain |
187 |
$userid = $file->get_userid();
|
|
|
188 |
if (null === $userid) {
|
1 |
efrain |
189 |
// If there is no userid, it is owned by the system.
|
|
|
190 |
return true;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
$context = \context::instance_by_id($file->get_contextid());
|
11 |
efrain |
194 |
$fileuser = core_user::get_user($userid);
|
|
|
195 |
if (empty($fileuser) || $fileuser->deleted) {
|
|
|
196 |
$userid = null;
|
1 |
efrain |
197 |
}
|
11 |
efrain |
198 |
return has_capability('moodle/h5p:deploy', $context, $userid);
|
1 |
efrain |
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Checks if the content-type libraries can be upgraded.
|
|
|
203 |
* The H5P content-type libraries can only be upgraded if the author of the .h5p file can manage content-types or if all the
|
11 |
efrain |
204 |
* content-types exist, to avoid users without the required capability to upload malicious content. If user has been deleted
|
|
|
205 |
* we check against current user.
|
1 |
efrain |
206 |
*
|
|
|
207 |
* @param stored_file $file The .h5p file to be deployed
|
|
|
208 |
* @return bool Returns true if the content-type libraries can be created/updated, false otherwise.
|
|
|
209 |
*/
|
|
|
210 |
public static function can_update_library(\stored_file $file): bool {
|
11 |
efrain |
211 |
$userid = $file->get_userid();
|
|
|
212 |
if (null === $userid) {
|
1 |
efrain |
213 |
// If there is no userid, it is owned by the system.
|
|
|
214 |
return true;
|
|
|
215 |
}
|
|
|
216 |
// Check if the owner of the .h5p file has the capability to manage content-types.
|
|
|
217 |
$context = \context::instance_by_id($file->get_contextid());
|
11 |
efrain |
218 |
$fileuser = core_user::get_user($userid);
|
|
|
219 |
if (empty($fileuser) || $fileuser->deleted) {
|
|
|
220 |
$userid = null;
|
1 |
efrain |
221 |
}
|
|
|
222 |
|
11 |
efrain |
223 |
return has_capability('moodle/h5p:updatelibraries', $context, $userid);
|
1 |
efrain |
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Convenience to take a fixture test file and create a stored_file.
|
|
|
228 |
*
|
|
|
229 |
* @param string $filepath The filepath of the file
|
|
|
230 |
* @param int $userid The author of the file
|
|
|
231 |
* @param \context $context The context where the file will be created
|
|
|
232 |
* @return \stored_file The file created
|
|
|
233 |
*/
|
|
|
234 |
public static function create_fake_stored_file_from_path(string $filepath, int $userid = 0,
|
|
|
235 |
\context $context = null): \stored_file {
|
|
|
236 |
if (is_null($context)) {
|
|
|
237 |
$context = context_system::instance();
|
|
|
238 |
}
|
|
|
239 |
$filerecord = [
|
|
|
240 |
'contextid' => $context->id,
|
|
|
241 |
'component' => 'core_h5p',
|
|
|
242 |
'filearea' => 'unittest',
|
|
|
243 |
'itemid' => rand(),
|
|
|
244 |
'filepath' => '/',
|
|
|
245 |
'filename' => basename($filepath),
|
|
|
246 |
];
|
|
|
247 |
if (!is_null($userid)) {
|
|
|
248 |
$filerecord['userid'] = $userid;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
$fs = get_file_storage();
|
|
|
252 |
return $fs->create_file_from_pathname($filerecord, $filepath);
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Get information about different H5P tools and their status.
|
|
|
257 |
*
|
|
|
258 |
* @return array Data to render by the template
|
|
|
259 |
*/
|
|
|
260 |
public static function get_h5p_tools_info(): array {
|
|
|
261 |
$tools = array();
|
|
|
262 |
|
|
|
263 |
// Getting information from available H5P tools one by one because their enabled/disabled options are totally different.
|
|
|
264 |
// Check the atto button status.
|
|
|
265 |
$link = \editor_atto\plugininfo\atto::get_manage_url();
|
|
|
266 |
$status = strpos(get_config('editor_atto', 'toolbar'), 'h5p') > -1;
|
|
|
267 |
$tools[] = self::convert_info_into_array('atto_h5p', $link, $status);
|
|
|
268 |
|
|
|
269 |
// Check the Display H5P filter status.
|
|
|
270 |
$link = \core\plugininfo\filter::get_manage_url();
|
|
|
271 |
$status = filter_get_active_state('displayh5p', context_system::instance()->id);
|
|
|
272 |
$tools[] = self::convert_info_into_array('filter_displayh5p', $link, $status);
|
|
|
273 |
|
|
|
274 |
// Check H5P scheduled task.
|
|
|
275 |
$link = '';
|
|
|
276 |
$status = 0;
|
|
|
277 |
$statusaction = '';
|
|
|
278 |
if ($task = \core\task\manager::get_scheduled_task('\core\task\h5p_get_content_types_task')) {
|
|
|
279 |
$status = !$task->get_disabled();
|
|
|
280 |
$link = new \moodle_url(
|
|
|
281 |
'/admin/tool/task/scheduledtasks.php',
|
|
|
282 |
array('action' => 'edit', 'task' => get_class($task))
|
|
|
283 |
);
|
|
|
284 |
if ($status && \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow')) {
|
|
|
285 |
$statusaction = \html_writer::link(
|
|
|
286 |
new \moodle_url('/admin/tool/task/schedule_task.php',
|
|
|
287 |
array('task' => get_class($task))),
|
|
|
288 |
get_string('runnow', 'tool_task'));
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
$tools[] = self::convert_info_into_array('task_h5p', $link, $status, $statusaction);
|
|
|
292 |
|
|
|
293 |
return $tools;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
/**
|
|
|
297 |
* Convert information into needed mustache template data array
|
|
|
298 |
* @param string $tool The name of the tool
|
|
|
299 |
* @param \moodle_url $link The URL to management page
|
|
|
300 |
* @param int $status The current status of the tool
|
|
|
301 |
* @param string $statusaction A link to 'Run now' option for the task
|
|
|
302 |
* @return array
|
|
|
303 |
*/
|
|
|
304 |
private static function convert_info_into_array(string $tool,
|
|
|
305 |
\moodle_url $link,
|
|
|
306 |
int $status,
|
|
|
307 |
string $statusaction = ''): array {
|
|
|
308 |
|
|
|
309 |
$statusclasses = array(
|
|
|
310 |
TEXTFILTER_DISABLED => 'badge bg-danger text-white',
|
|
|
311 |
TEXTFILTER_OFF => 'badge bg-warning text-dark',
|
|
|
312 |
|
|
|
313 |
TEXTFILTER_ON => 'badge bg-success text-white',
|
|
|
314 |
);
|
|
|
315 |
|
|
|
316 |
$statuschoices = array(
|
|
|
317 |
TEXTFILTER_DISABLED => get_string('disabled', 'admin'),
|
|
|
318 |
TEXTFILTER_OFF => get_string('offbutavailable', 'core_filters'),
|
|
|
319 |
|
|
|
320 |
1 => get_string('enabled', 'admin'),
|
|
|
321 |
);
|
|
|
322 |
|
|
|
323 |
return [
|
|
|
324 |
'tool' => get_string($tool, 'h5p'),
|
|
|
325 |
'tool_description' => get_string($tool . '_description', 'h5p'),
|
|
|
326 |
'link' => $link,
|
|
|
327 |
'status' => $statuschoices[$status],
|
|
|
328 |
'status_class' => $statusclasses[$status],
|
|
|
329 |
'status_action' => $statusaction,
|
|
|
330 |
];
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Get a query string with the theme revision number to include at the end
|
|
|
335 |
* of URLs. This is used to force the browser to reload the asset when the
|
|
|
336 |
* theme caches are cleared.
|
|
|
337 |
*
|
|
|
338 |
* @return string
|
|
|
339 |
*/
|
|
|
340 |
public static function get_cache_buster(): string {
|
|
|
341 |
global $CFG;
|
|
|
342 |
return '?ver=' . $CFG->themerev;
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
/**
|
|
|
346 |
* Get the settings needed by the H5P library.
|
|
|
347 |
*
|
|
|
348 |
* @param string|null $component
|
|
|
349 |
* @return array The settings.
|
|
|
350 |
*/
|
|
|
351 |
public static function get_core_settings(?string $component = null): array {
|
|
|
352 |
global $CFG, $USER;
|
|
|
353 |
|
|
|
354 |
$basepath = $CFG->wwwroot . '/';
|
|
|
355 |
$systemcontext = context_system::instance();
|
|
|
356 |
|
|
|
357 |
// H5P doesn't currently support xAPI State. It implements a mechanism in contentUserDataAjax() in h5p.js to update user
|
|
|
358 |
// data. However, in our case, we're overriding this method to call the xAPI State web services.
|
|
|
359 |
$ajaxpaths = [
|
|
|
360 |
'contentUserData' => '',
|
|
|
361 |
];
|
|
|
362 |
|
|
|
363 |
$factory = new factory();
|
|
|
364 |
$core = $factory->get_core();
|
|
|
365 |
|
|
|
366 |
// When there is a logged in user, her information will be passed to the player. It will be used for tracking.
|
|
|
367 |
$usersettings = [];
|
|
|
368 |
if (isloggedin()) {
|
|
|
369 |
$usersettings['name'] = fullname($USER, has_capability('moodle/site:viewfullnames', $systemcontext));
|
|
|
370 |
$usersettings['id'] = $USER->id;
|
|
|
371 |
}
|
|
|
372 |
$savefreq = false;
|
|
|
373 |
if ($component !== null && get_config($component, 'enablesavestate')) {
|
|
|
374 |
$savefreq = get_config($component, 'savestatefreq');
|
|
|
375 |
}
|
|
|
376 |
$settings = array(
|
|
|
377 |
'baseUrl' => $basepath,
|
|
|
378 |
'url' => "{$basepath}pluginfile.php/{$systemcontext->instanceid}/core_h5p",
|
|
|
379 |
'urlLibraries' => "{$basepath}pluginfile.php/{$systemcontext->id}/core_h5p/libraries",
|
|
|
380 |
'postUserStatistics' => false,
|
|
|
381 |
'ajax' => $ajaxpaths,
|
|
|
382 |
'saveFreq' => $savefreq,
|
|
|
383 |
'siteUrl' => $CFG->wwwroot,
|
|
|
384 |
'l10n' => array('H5P' => $core->getLocalization()),
|
|
|
385 |
'user' => $usersettings,
|
|
|
386 |
'hubIsEnabled' => false,
|
|
|
387 |
'reportingIsEnabled' => false,
|
|
|
388 |
'crossorigin' => !empty($CFG->h5pcrossorigin) ? $CFG->h5pcrossorigin : null,
|
|
|
389 |
'libraryConfig' => $core->h5pF->getLibraryConfig(),
|
|
|
390 |
'pluginCacheBuster' => self::get_cache_buster(),
|
|
|
391 |
'libraryUrl' => autoloader::get_h5p_core_library_url('js')->out(),
|
|
|
392 |
);
|
|
|
393 |
|
|
|
394 |
return $settings;
|
|
|
395 |
}
|
|
|
396 |
|
|
|
397 |
/**
|
|
|
398 |
* Get the core H5P assets, including all core H5P JavaScript and CSS.
|
|
|
399 |
*
|
|
|
400 |
* @param string|null $component
|
|
|
401 |
* @return Array core H5P assets.
|
|
|
402 |
*/
|
|
|
403 |
public static function get_core_assets(?string $component = null): array {
|
|
|
404 |
global $PAGE;
|
|
|
405 |
|
|
|
406 |
// Get core settings.
|
|
|
407 |
$settings = self::get_core_settings($component);
|
|
|
408 |
$settings['core'] = [
|
|
|
409 |
'styles' => [],
|
|
|
410 |
'scripts' => []
|
|
|
411 |
];
|
|
|
412 |
$settings['loadedJs'] = [];
|
|
|
413 |
$settings['loadedCss'] = [];
|
|
|
414 |
|
|
|
415 |
// Make sure files are reloaded for each plugin update.
|
|
|
416 |
$cachebuster = self::get_cache_buster();
|
|
|
417 |
|
|
|
418 |
// Use relative URL to support both http and https.
|
|
|
419 |
$liburl = autoloader::get_h5p_core_library_url()->out();
|
|
|
420 |
$relpath = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $liburl);
|
|
|
421 |
|
|
|
422 |
// Add core stylesheets.
|
|
|
423 |
foreach (core::$styles as $style) {
|
|
|
424 |
$settings['core']['styles'][] = $relpath . $style . $cachebuster;
|
|
|
425 |
$PAGE->requires->css(new \moodle_url($liburl . $style . $cachebuster));
|
|
|
426 |
}
|
|
|
427 |
// Add core JavaScript.
|
|
|
428 |
foreach (core::get_scripts() as $script) {
|
|
|
429 |
$settings['core']['scripts'][] = $script->out(false);
|
|
|
430 |
$PAGE->requires->js($script, true);
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
return $settings;
|
|
|
434 |
}
|
|
|
435 |
|
|
|
436 |
/**
|
|
|
437 |
* Prepare the library name to be used as a cache key (remove whitespaces and replace dots to underscores).
|
|
|
438 |
*
|
|
|
439 |
* @param string $library Library name.
|
|
|
440 |
* @return string Library name in a cache simple key format (a-zA-Z0-9_).
|
|
|
441 |
*/
|
|
|
442 |
public static function get_cache_librarykey(string $library): string {
|
|
|
443 |
// Remove whitespaces and replace '.' to '_'.
|
|
|
444 |
return str_replace('.', '_', str_replace(' ', '', $library));
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
/**
|
|
|
448 |
* Parse a JS array to a PHP array.
|
|
|
449 |
*
|
|
|
450 |
* @param string $jscontent The JS array to parse to PHP array.
|
|
|
451 |
* @return array The JS array converted to PHP array.
|
|
|
452 |
*/
|
|
|
453 |
public static function parse_js_array(string $jscontent): array {
|
|
|
454 |
// Convert all line-endings to UNIX format first.
|
|
|
455 |
$jscontent = str_replace(array("\r\n", "\r"), "\n", $jscontent);
|
|
|
456 |
$jsarray = preg_split('/,\n\s+/', substr($jscontent, 0, -1));
|
|
|
457 |
$jsarray = preg_replace('~{?\\n~', '', $jsarray);
|
|
|
458 |
|
|
|
459 |
$strings = [];
|
|
|
460 |
foreach ($jsarray as $key => $value) {
|
|
|
461 |
$splitted = explode(":", $value, 2);
|
|
|
462 |
$value = preg_replace("/^['|\"](.*)['|\"]$/", "$1", trim($splitted[1], ' ,'));
|
|
|
463 |
$strings[ trim($splitted[0]) ] = str_replace("\'", "'", $value);
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
return $strings;
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
/**
|
|
|
470 |
* Get the information related to the H5P export file.
|
|
|
471 |
* The information returned will be:
|
|
|
472 |
* - filename, filepath, mimetype, filesize, timemodified and fileurl.
|
|
|
473 |
*
|
|
|
474 |
* @param string $exportfilename The H5P export filename (with slug).
|
|
|
475 |
* @param \moodle_url $url The URL of the exported file.
|
|
|
476 |
* @param factory $factory The \core_h5p\factory object
|
|
|
477 |
* @return array|null The information export file otherwise null.
|
|
|
478 |
*/
|
|
|
479 |
public static function get_export_info(string $exportfilename, \moodle_url $url = null, ?factory $factory = null): ?array {
|
|
|
480 |
|
|
|
481 |
if (!$factory) {
|
|
|
482 |
$factory = new factory();
|
|
|
483 |
}
|
|
|
484 |
$core = $factory->get_core();
|
|
|
485 |
|
|
|
486 |
// Get export file.
|
|
|
487 |
if (!$fileh5p = $core->fs->get_export_file($exportfilename)) {
|
|
|
488 |
return null;
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
// Build the export info array.
|
|
|
492 |
$file = [];
|
|
|
493 |
$file['filename'] = $fileh5p->get_filename();
|
|
|
494 |
$file['filepath'] = $fileh5p->get_filepath();
|
|
|
495 |
$file['mimetype'] = $fileh5p->get_mimetype();
|
|
|
496 |
$file['filesize'] = $fileh5p->get_filesize();
|
|
|
497 |
$file['timemodified'] = $fileh5p->get_timemodified();
|
|
|
498 |
|
|
|
499 |
if (!$url) {
|
|
|
500 |
$url = \moodle_url::make_webservice_pluginfile_url(
|
|
|
501 |
$fileh5p->get_contextid(),
|
|
|
502 |
$fileh5p->get_component(),
|
|
|
503 |
$fileh5p->get_filearea(),
|
|
|
504 |
'',
|
|
|
505 |
'',
|
|
|
506 |
$fileh5p->get_filename()
|
|
|
507 |
);
|
|
|
508 |
}
|
|
|
509 |
|
|
|
510 |
$file['fileurl'] = $url->out(false);
|
|
|
511 |
|
|
|
512 |
return $file;
|
|
|
513 |
}
|
|
|
514 |
}
|