Merge branch 'MDL-67886_master' of git://github.com/mdjnelson/moodle
[moodle.git] / h5p / classes / player.php
CommitLineData
35b62d00
SA
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 * H5P player class.
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
25namespace core_h5p;
26
27defined('MOODLE_INTERNAL') || die();
28
08fda3e0 29use core_h5p\local\library\autoloader;
8685c313 30use core_xapi\local\statement\item_activity;
e84ce938 31use core\lock\lock_config;
08fda3e0 32
35b62d00
SA
33/**
34 * H5P player class, for displaying any local H5P content.
35 *
36 * @package core_h5p
37 * @copyright 2019 Sara Arjona <sara@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class player {
41
42 /**
43 * @var string The local H5P URL containing the .h5p file to display.
44 */
45 private $url;
46
47 /**
9e67f5e3 48 * @var core The H5PCore object.
35b62d00
SA
49 */
50 private $core;
51
52 /**
53 * @var int H5P DB id.
54 */
55 private $h5pid;
56
57 /**
58 * @var array JavaScript requirements for this H5P.
59 */
60 private $jsrequires = [];
61
62 /**
63 * @var array CSS requirements for this H5P.
64 */
65 private $cssrequires = [];
66
67 /**
68 * @var array H5P content to display.
69 */
70 private $content;
71
8685c313
FR
72 /**
73 * @var string optional component name to send xAPI statements.
74 */
75 private $component;
76
35b62d00
SA
77 /**
78 * @var string Type of embed object, div or iframe.
79 */
80 private $embedtype;
81
82 /**
83 * @var context The context object where the .h5p belongs.
84 */
85 private $context;
86
df74cd4a 87 /**
c07f31ae 88 * @var factory The \core_h5p\factory object.
df74cd4a
MG
89 */
90 private $factory;
91
c07f31ae
SA
92 /**
93 * @var stdClass The error, exception and info messages, raised while preparing and running the player.
94 */
95 private $messages;
96
74346211
SA
97 /**
98 * @var bool Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions.
99 */
100 private $preventredirect;
101
35b62d00
SA
102 /**
103 * Inits the H5P player for rendering the content.
104 *
105 * @param string $url Local URL of the H5P file to display.
106 * @param stdClass $config Configuration for H5P buttons.
74346211 107 * @param bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
8685c313 108 * @param string $component optional moodle component to sent xAPI tracking
35b62d00 109 */
8685c313 110 public function __construct(string $url, \stdClass $config, bool $preventredirect = true, string $component = '') {
35b62d00
SA
111 if (empty($url)) {
112 throw new \moodle_exception('h5pinvalidurl', 'core_h5p');
113 }
114 $this->url = new \moodle_url($url);
74346211 115 $this->preventredirect = $preventredirect;
35b62d00 116
df74cd4a
MG
117 $this->factory = new \core_h5p\factory();
118
c07f31ae
SA
119 $this->messages = new \stdClass();
120
8685c313
FR
121 $this->component = $component;
122
df74cd4a
MG
123 // Create \core_h5p\core instance.
124 $this->core = $this->factory->get_core();
35b62d00
SA
125
126 // Get the H5P identifier linked to this URL.
127 if ($this->h5pid = $this->get_h5p_id($url, $config)) {
128 // Load the content of the H5P content associated to this $url.
129 $this->content = $this->core->loadContent($this->h5pid);
130
131 // Get the embedtype to use for displaying the H5P content.
9e67f5e3 132 $this->embedtype = core::determineEmbedType($this->content['embedType'], $this->content['library']['embedTypes']);
35b62d00
SA
133 }
134 }
135
f3c7e00f
FR
136 /**
137 * Get the encoded URL for embeding this H5P content.
138 *
139 * @param string $url Local URL of the H5P file to display.
140 * @param stdClass $config Configuration for H5P buttons.
141 * @param bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
8685c313 142 * @param string $component optional moodle component to sent xAPI tracking
f3c7e00f
FR
143 *
144 * @return string The embedable code to display a H5P file.
145 */
8685c313
FR
146 public static function display(string $url, \stdClass $config, bool $preventredirect = true,
147 string $component = ''): string {
f3c7e00f
FR
148 global $OUTPUT;
149 $params = [
150 'url' => $url,
151 'preventredirect' => $preventredirect,
8685c313 152 'component' => $component,
f3c7e00f
FR
153 ];
154
155 $optparams = ['frame', 'export', 'embed', 'copyright'];
156 foreach ($optparams as $optparam) {
157 if (!empty($config->$optparam)) {
158 $params[$optparam] = $config->$optparam;
159 }
160 }
161 $fileurl = new \moodle_url('/h5p/embed.php', $params);
162
163 $template = new \stdClass();
164 $template->embedurl = $fileurl->out(false);
165
166 $result = $OUTPUT->render_from_template('core_h5p/h5pembed', $template);
167 $result .= self::get_resize_code();
168 return $result;
169 }
170
35b62d00
SA
171 /**
172 * Get the error messages stored in our H5P framework.
173 *
174 * @return stdClass with framework error messages.
175 */
284af93b 176 public function get_messages(): \stdClass {
c07f31ae
SA
177 // Check if there are some errors and store them in $messages.
178 if (empty($this->messages->error)) {
179 $this->messages->error = $this->core->h5pF->getMessages('error') ?: false;
180 } else {
181 $this->messages->error = array_merge($this->messages->error, $this->core->h5pF->getMessages('error'));
182 }
35b62d00 183
c07f31ae
SA
184 if (empty($this->messages->info)) {
185 $this->messages->info = $this->core->h5pF->getMessages('info') ?: false;
186 } else {
187 $this->messages->info = array_merge($this->messages->info, $this->core->h5pF->getMessages('info'));
35b62d00 188 }
c07f31ae
SA
189
190 return $this->messages;
35b62d00
SA
191 }
192
193 /**
194 * Create the H5PIntegration variable that will be included in the page. This variable is used as the
195 * main H5P config variable.
196 */
197 public function add_assets_to_page() {
198 global $PAGE;
199
200 $cid = $this->get_cid();
201 $systemcontext = \context_system::instance();
202
9e67f5e3 203 $disable = array_key_exists('disable', $this->content) ? $this->content['disable'] : core::DISABLE_NONE;
35b62d00
SA
204 $displayoptions = $this->core->getDisplayOptionsForView($disable, $this->h5pid);
205
206 $contenturl = \moodle_url::make_pluginfile_url($systemcontext->id, \core_h5p\file_storage::COMPONENT,
207 \core_h5p\file_storage::CONTENT_FILEAREA, $this->h5pid, null, null);
65ae2441 208 $exporturl = $this->get_export_settings($displayoptions[ core::DISPLAY_OPTION_DOWNLOAD ]);
8685c313 209 $xapiobject = item_activity::create_from_id($this->context->id);
35b62d00 210 $contentsettings = [
9e67f5e3 211 'library' => core::libraryToString($this->content['library']),
35b62d00 212 'fullScreen' => $this->content['library']['fullscreen'],
65ae2441 213 'exportUrl' => ($exporturl instanceof \moodle_url) ? $exporturl->out(false) : '',
35b62d00 214 'embedCode' => $this->get_embed_code($this->url->out(),
9e67f5e3 215 $displayoptions[ core::DISPLAY_OPTION_EMBED ]),
f3c7e00f 216 'resizeCode' => self::get_resize_code(),
35b62d00
SA
217 'title' => $this->content['slug'],
218 'displayOptions' => $displayoptions,
8685c313 219 'url' => $xapiobject->get_data()->id,
35b62d00
SA
220 'contentUrl' => $contenturl->out(),
221 'metadata' => $this->content['metadata'],
222 'contentUserData' => [0 => ['state' => '{}']]
223 ];
224 // Get the core H5P assets, needed by the H5P classes to render the H5P content.
225 $settings = $this->get_assets();
226 $settings['contents'][$cid] = array_merge($settings['contents'][$cid], $contentsettings);
227
35b62d00
SA
228 // Print JavaScript settings to page.
229 $PAGE->requires->data_for_js('H5PIntegration', $settings, true);
230 }
231
232 /**
233 * Outputs H5P wrapper HTML.
234 *
235 * @return string The HTML code to display this H5P content.
236 */
284af93b 237 public function output(): string {
fba5bba8 238 global $OUTPUT, $USER;
35b62d00
SA
239
240 $template = new \stdClass();
241 $template->h5pid = $this->h5pid;
242 if ($this->embedtype === 'div') {
fba5bba8 243 $h5phtml = $OUTPUT->render_from_template('core_h5p/h5pdiv', $template);
35b62d00 244 } else {
fba5bba8 245 $h5phtml = $OUTPUT->render_from_template('core_h5p/h5piframe', $template);
35b62d00 246 }
fba5bba8 247
248 // Trigger capability_assigned event.
249 \core_h5p\event\h5p_viewed::create([
250 'objectid' => $this->h5pid,
251 'userid' => $USER->id,
252 'context' => $this->context,
253 'other' => [
254 'url' => $this->url->out(),
255 'time' => time()
256 ]
257 ])->trigger();
258
259 return $h5phtml;
35b62d00
SA
260 }
261
262 /**
263 * Get the title of the H5P content to display.
264 *
265 * @return string the title
266 */
284af93b 267 public function get_title(): string {
35b62d00
SA
268 return $this->content['title'];
269 }
270
271 /**
272 * Get the context where the .h5p file belongs.
273 *
274 * @return context The context.
275 */
284af93b 276 public function get_context(): \context {
35b62d00
SA
277 return $this->context;
278 }
279
280 /**
281 * Get the H5P DB instance id for a H5P pluginfile URL. The H5P file will be saved if it doesn't exist previously or
282 * if its content has changed. Besides, the displayoptions in the $config will be also updated when they have changed and
283 * the user has the right permissions.
284 *
285 * @param string $url H5P pluginfile URL.
286 * @param stdClass $config Configuration for H5P buttons.
287 *
288 * @return int|false H5P DB identifier.
289 */
290 private function get_h5p_id(string $url, \stdClass $config) {
c07f31ae 291 global $DB, $USER;
35b62d00
SA
292
293 $fs = get_file_storage();
294
295 // Deconstruct the URL and get the pathname associated.
296 $pathnamehash = $this->get_pluginfile_hash($url);
297 if (!$pathnamehash) {
298 $this->core->h5pF->setErrorMessage(get_string('h5pfilenotfound', 'core_h5p'));
299 return false;
300 }
301
302 // Get the file.
303 $file = $fs->get_file_by_hash($pathnamehash);
304 if (!$file) {
305 $this->core->h5pF->setErrorMessage(get_string('h5pfilenotfound', 'core_h5p'));
306 return false;
307 }
308
309 $h5p = $DB->get_record('h5p', ['pathnamehash' => $pathnamehash]);
310 $contenthash = $file->get_contenthash();
311 if ($h5p && $h5p->contenthash != $contenthash) {
312 // The content exists and it is different from the one deployed previously. The existing one should be removed before
313 // deploying the new version.
314 $this->delete_h5p($h5p);
315 $h5p = false;
316 }
317
8fda136d 318 if ($h5p) {
35b62d00
SA
319 // The H5P content has been deployed previously.
320 $displayoptions = $this->get_display_options($config);
321 // Check if the user can set the displayoptions.
322 if ($displayoptions != $h5p->displayoptions && has_capability('moodle/h5p:setdisplayoptions', $this->context)) {
323 // If the displayoptions has changed and the user has permission to modify it, update this information in the DB.
324 $this->core->h5pF->updateContentFields($h5p->id, ['displayoptions' => $displayoptions]);
325 }
326 return $h5p->id;
8fda136d
SA
327 } else {
328 // The H5P content hasn't been deployed previously.
329
330 // Check if the user uploading the H5P content is "trustable". If the file hasn't been uploaded by a user with this
331 // capability, the content won't be deployed and an error message will be displayed.
c07f31ae 332 if (!helper::can_deploy_package($file)) {
8fda136d
SA
333 $this->core->h5pF->setErrorMessage(get_string('nopermissiontodeploy', 'core_h5p'));
334 return false;
335 }
336
c07f31ae
SA
337 // The H5P content can be only deployed if the author of the .h5p file can update libraries or if all the
338 // content-type libraries exist, to avoid users without the h5p:updatelibraries capability upload malicious content.
339 $onlyupdatelibs = !helper::can_update_library($file);
340
e84ce938 341 // Start lock to prevent synchronous access to save the same h5p.
342 $lockfactory = lock_config::get_lock_factory('core_h5p');
343 $lockkey = 'core_h5p_' . $pathnamehash;
344 if ($lock = $lockfactory->get_lock($lockkey, 10)) {
345 try {
346 // Validate and store the H5P content before displaying it.
347 $h5pid = helper::save_h5p($this->factory, $file, $config, $onlyupdatelibs, false);
348 } finally {
349 $lock->release();
350 }
351 } else {
352 $this->core->h5pF->setErrorMessage(get_string('lockh5pdeploy', 'core_h5p'));
353 return false;
354 };
c07f31ae
SA
355 if (!$h5pid && $file->get_userid() != $USER->id && has_capability('moodle/h5p:updatelibraries', $this->context)) {
356 // The user has permission to update libraries but the package has been uploaded by a different
357 // user without this permission. Check if there is some missing required library error.
358 $missingliberror = false;
359 $messages = $this->get_messages();
360 if (!empty($messages->error)) {
361 foreach ($messages->error as $error) {
362 if ($error->code == 'missing-required-library') {
363 $missingliberror = true;
364 break;
365 }
366 }
367 }
368 if ($missingliberror) {
369 // The message about the permissions to upload libraries should be removed.
370 $infomsg = "Note that the libraries may exist in the file you uploaded, but you're not allowed to upload " .
371 "new libraries. Contact the site administrator about this.";
372 if (($key = array_search($infomsg, $messages->info)) !== false) {
373 unset($messages->info[$key]);
374 }
375
376 // No library will be installed and an error will be displayed, because this content is not trustable.
377 $this->core->h5pF->setInfoMessage(get_string('notrustablefile', 'core_h5p'));
378 }
379 return false;
380
381 }
382 return $h5pid;
35b62d00
SA
383 }
384 }
385
386 /**
387 * Get the pathnamehash from an H5P internal URL.
388 *
389 * @param string $url H5P pluginfile URL poiting to an H5P file.
390 *
391 * @return string|false pathnamehash for the file in the internal URL.
392 */
393 private function get_pluginfile_hash(string $url) {
c0c489bb 394 global $USER, $CFG;
35b62d00
SA
395
396 // Decode the URL before start processing it.
397 $url = new \moodle_url(urldecode($url));
398
399 // Remove params from the URL (such as the 'forcedownload=1'), to avoid errors.
400 $url->remove_params(array_keys($url->params()));
401 $path = $url->out_as_local_url();
402
5d81efd1 403 // We only need the slasharguments.
404 $path = substr($path, strpos($path, '.php/') + 5);
35b62d00
SA
405 $parts = explode('/', $path);
406 $filename = array_pop($parts);
35b62d00 407
5d81efd1 408 // If the request is made by tokenpluginfile.php we need to avoid userprivateaccesskey.
409 if (strpos($this->url, '/tokenpluginfile.php')) {
410 array_shift($parts);
411 }
35b62d00
SA
412 // Get the contextid, component and filearea.
413 $contextid = array_shift($parts);
414 $component = array_shift($parts);
415 $filearea = array_shift($parts);
416
417 // Ignore draft files, because they are considered temporary files, so shouldn't be displayed.
418 if ($filearea == 'draft') {
419 return false;
420 }
421
422 // Get the context.
423 try {
424 list($this->context, $course, $cm) = get_context_info_array($contextid);
425 } catch (\moodle_exception $e) {
426 throw new \moodle_exception('invalidcontextid', 'core_h5p');
427 }
428
429 // For CONTEXT_USER, such as the private files, raise an exception if the owner of the file is not the current user.
430 if ($this->context->contextlevel == CONTEXT_USER && $USER->id !== $this->context->instanceid) {
431 throw new \moodle_exception('h5pprivatefile', 'core_h5p');
432 }
433
c0c489bb 434 // For CONTEXT_COURSECAT No login necessary - unless login forced everywhere.
435 if ($this->context->contextlevel == CONTEXT_COURSECAT) {
436 if ($CFG->forcelogin) {
437 require_login(null, true, null, false, true);
438 }
439 }
440
441 // For CONTEXT_BLOCK.
442 if ($this->context->contextlevel == CONTEXT_BLOCK) {
443 if ($this->context->get_course_context(false)) {
444 // If block is in course context, then check if user has capability to access course.
445 require_course_login($course, true, null, false, true);
446 } else if ($CFG->forcelogin) {
447 // No login necessary - unless login forced everywhere.
448 require_login(null, true, null, false, true);
449 } else {
450 // Get parent context and see if user have proper permission.
451 $parentcontext = $this->context->get_parent_context();
452 if ($parentcontext->contextlevel === CONTEXT_COURSECAT) {
453 // Check if category is visible and user can view this category.
454 if (!core_course_category::get($parentcontext->instanceid, IGNORE_MISSING)) {
455 send_file_not_found();
456 }
457 } else if ($parentcontext->contextlevel === CONTEXT_USER && $parentcontext->instanceid != $USER->id) {
458 // The block is in the context of a user, it is only visible to the user who it belongs to.
459 send_file_not_found();
460 }
461 if ($filearea !== 'content') {
462 send_file_not_found();
463 }
464 }
465 }
466
467 // For CONTEXT_MODULE and CONTEXT_COURSE check if the user is enrolled in the course.
468 // And for CONTEXT_MODULE has permissions view this .h5p file.
469 if ($this->context->contextlevel == CONTEXT_MODULE ||
470 $this->context->contextlevel == CONTEXT_COURSE) {
35b62d00 471 // Require login to the course first (without login to the module).
74346211 472 require_course_login($course, true, null, !$this->preventredirect, $this->preventredirect);
35b62d00
SA
473
474 // Now check if module is available OR it is restricted but the intro is shown on the course page.
c0c489bb 475 if ($this->context->contextlevel == CONTEXT_MODULE) {
476 $cminfo = \cm_info::create($cm);
477 if (!$cminfo->uservisible) {
478 if (!$cm->showdescription || !$cminfo->is_visible_on_course_page()) {
479 // Module intro is not visible on the course page and module is not available, show access error.
6f44f094 480 require_course_login($course, true, $cminfo, !$this->preventredirect, $this->preventredirect);
c0c489bb 481 }
35b62d00
SA
482 }
483 }
484 }
485
486 // Some components, such as mod_page or mod_resource, add the revision to the URL to prevent caching problems.
487 // So the URL contains this revision number as itemid but a 0 is always stored in the files table.
488 // In order to get the proper hash, a callback should be done (looking for those exceptions).
3f6c7446 489 $pathdata = null;
490 if ($this->context->contextlevel == CONTEXT_MODULE || $this->context->contextlevel == CONTEXT_BLOCK) {
491 $pathdata = component_callback($component, 'get_path_from_pluginfile', [$filearea, $parts], null);
492 }
35b62d00
SA
493 if (null === $pathdata) {
494 // Look for the components and fileareas which have empty itemid defined in xxx_pluginfile.
495 $hasnullitemid = false;
496 $hasnullitemid = $hasnullitemid || ($component === 'user' && ($filearea === 'private' || $filearea === 'profile'));
3f6c7446 497 $hasnullitemid = $hasnullitemid || (substr($component, 0, 4) === 'mod_' && $filearea === 'intro');
35b62d00
SA
498 $hasnullitemid = $hasnullitemid || ($component === 'course' &&
499 ($filearea === 'summary' || $filearea === 'overviewfiles'));
500 $hasnullitemid = $hasnullitemid || ($component === 'coursecat' && $filearea === 'description');
501 $hasnullitemid = $hasnullitemid || ($component === 'backup' &&
502 ($filearea === 'course' || $filearea === 'activity' || $filearea === 'automated'));
503 if ($hasnullitemid) {
504 $itemid = 0;
505 } else {
506 $itemid = array_shift($parts);
507 }
508
509 if (empty($parts)) {
510 $filepath = '/';
511 } else {
512 $filepath = '/' . implode('/', $parts) . '/';
513 }
514 } else {
515 // The itemid and filepath have been returned by the component callback.
516 [
517 'itemid' => $itemid,
518 'filepath' => $filepath,
519 ] = $pathdata;
520 }
521
522 $fs = get_file_storage();
523 return $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath, $filename);
524 }
525
35b62d00
SA
526 /**
527 * Get the representation of display options as int.
528 * @param stdClass $config Button options config.
529 *
530 * @return int The representation of display options as int.
531 */
284af93b 532 private function get_display_options(\stdClass $config): int {
35b62d00
SA
533 $export = isset($config->export) ? $config->export : 0;
534 $embed = isset($config->embed) ? $config->embed : 0;
535 $copyright = isset($config->copyright) ? $config->copyright : 0;
536 $frame = ($export || $embed || $copyright);
537 if (!$frame) {
538 $frame = isset($config->frame) ? $config->frame : 0;
539 }
540
541 $disableoptions = [
9e67f5e3
AN
542 core::DISPLAY_OPTION_FRAME => $frame,
543 core::DISPLAY_OPTION_DOWNLOAD => $export,
544 core::DISPLAY_OPTION_EMBED => $embed,
545 core::DISPLAY_OPTION_COPYRIGHT => $copyright,
35b62d00
SA
546 ];
547
548 return $this->core->getStorableDisplayOptions($disableoptions, 0);
549 }
550
551 /**
552 * Delete an H5P package.
553 *
554 * @param stdClass $content The H5P package to delete.
555 */
556 private function delete_h5p(\stdClass $content) {
df74cd4a 557 $h5pstorage = $this->factory->get_storage();
35b62d00
SA
558 // Add an empty slug to the content if it's not defined, because the H5P library requires this field exists.
559 // It's not used when deleting a package, so the real slug value is not required at this point.
560 $content->slug = $content->slug ?? '';
561 $h5pstorage->deletePackage( (array) $content);
562 }
563
564 /**
565 * Export path for settings
566 *
567 * @param bool $downloadenabled Whether the option to export the H5P content is enabled.
568 *
65ae2441 569 * @return \moodle_url|null The URL of the exported file.
35b62d00 570 */
284af93b 571 private function get_export_settings(bool $downloadenabled): ?\moodle_url {
35b62d00 572
5d81efd1 573 if (!$downloadenabled) {
65ae2441 574 return null;
35b62d00
SA
575 }
576
577 $systemcontext = \context_system::instance();
578 $slug = $this->content['slug'] ? $this->content['slug'] . '-' : '';
5d81efd1 579 // We have to build the right URL.
580 // Depending the request was made through webservice/pluginfile.php or pluginfile.php.
581 if (strpos($this->url, '/webservice/pluginfile.php')) {
582 $url = \moodle_url::make_webservice_pluginfile_url(
583 $systemcontext->id,
584 \core_h5p\file_storage::COMPONENT,
585 \core_h5p\file_storage::EXPORT_FILEAREA,
586 '',
587 '',
588 "{$slug}{$this->content['id']}.h5p"
589 );
590 } else {
591 // If the request is made by tokenpluginfile.php we need to indicates to generate a token for current user.
592 $includetoken = false;
593 if (strpos($this->url, '/tokenpluginfile.php')) {
594 $includetoken = true;
595 }
596 $url = \moodle_url::make_pluginfile_url(
597 $systemcontext->id,
598 \core_h5p\file_storage::COMPONENT,
599 \core_h5p\file_storage::EXPORT_FILEAREA,
600 '',
601 '',
602 "{$slug}{$this->content['id']}.h5p",
603 false,
604 $includetoken
605 );
606 }
35b62d00 607
65ae2441 608 return $url;
35b62d00
SA
609 }
610
35b62d00
SA
611 /**
612 * Get the identifier for the H5P content, to be used in the arrays as index.
613 *
614 * @return string The identifier.
615 */
284af93b 616 private function get_cid(): string {
35b62d00
SA
617 return 'cid-' . $this->h5pid;
618 }
619
620 /**
621 * Get the core H5P assets, including all core H5P JavaScript and CSS.
622 *
623 * @return Array core H5P assets.
624 */
284af93b 625 private function get_assets(): array {
a0ce8f87
VDF
626 // Get core assets.
627 $settings = helper::get_core_assets();
628 // Added here because in the helper we don't have the h5p content id.
629 $settings['moodleLibraryPaths'] = $this->core->get_dependency_roots($this->h5pid);
2ac1d9ae
SA
630 // Add also the Moodle component where the results will be tracked.
631 $settings['moodleComponent'] = $this->component;
35b62d00
SA
632
633 $cid = $this->get_cid();
634 // The filterParameters function should be called before getting the dependencyfiles because it rebuild content
635 // dependency cache and export file.
636 $settings['contents'][$cid]['jsonContent'] = $this->core->filterParameters($this->content);
637
638 $files = $this->get_dependency_files();
639 if ($this->embedtype === 'div') {
640 $systemcontext = \context_system::instance();
641 $h5ppath = "/pluginfile.php/{$systemcontext->id}/core_h5p";
642
643 // Schedule JavaScripts for loading through Moodle.
644 foreach ($files['scripts'] as $script) {
645 $url = $script->path . $script->version;
646
647 // Add URL prefix if not external.
648 $isexternal = strpos($script->path, '://');
649 if ($isexternal === false) {
650 $url = $h5ppath . $url;
651 }
652 $settings['loadedJs'][] = $url;
653 $this->jsrequires[] = new \moodle_url($isexternal ? $url : $CFG->wwwroot . $url);
654 }
655
656 // Schedule stylesheets for loading through Moodle.
657 foreach ($files['styles'] as $style) {
658 $url = $style->path . $style->version;
659
660 // Add URL prefix if not external.
661 $isexternal = strpos($style->path, '://');
662 if ($isexternal === false) {
663 $url = $h5ppath . $url;
664 }
665 $settings['loadedCss'][] = $url;
666 $this->cssrequires[] = new \moodle_url($isexternal ? $url : $CFG->wwwroot . $url);
667 }
668
669 } else {
670 // JavaScripts and stylesheets will be loaded through h5p.js.
671 $settings['contents'][$cid]['scripts'] = $this->core->getAssetsUrls($files['scripts']);
672 $settings['contents'][$cid]['styles'] = $this->core->getAssetsUrls($files['styles']);
673 }
674 return $settings;
675 }
676
35b62d00
SA
677 /**
678 * Finds library dependencies of view
679 *
680 * @return array Files that the view has dependencies to
681 */
284af93b 682 private function get_dependency_files(): array {
35b62d00
SA
683 $preloadeddeps = $this->core->loadContentDependencies($this->h5pid, 'preloaded');
684 $files = $this->core->getDependenciesFiles($preloadeddeps);
685
686 return $files;
687 }
688
689 /**
690 * Resizing script for settings
691 *
692 * @return string The HTML code with the resize script.
693 */
f3c7e00f 694 private static function get_resize_code(): string {
35b62d00
SA
695 global $OUTPUT;
696
697 $template = new \stdClass();
08fda3e0 698 $template->resizeurl = autoloader::get_h5p_core_library_url('js/h5p-resizer.js');
35b62d00
SA
699
700 return $OUTPUT->render_from_template('core_h5p/h5presize', $template);
701 }
702
703 /**
704 * Embed code for settings
705 *
706 * @param string $url The URL of the .h5p file.
707 * @param bool $embedenabled Whether the option to embed the H5P content is enabled.
708 *
709 * @return string The HTML code to reuse this H5P content in a different place.
710 */
284af93b 711 private function get_embed_code(string $url, bool $embedenabled): string {
35b62d00
SA
712 global $OUTPUT;
713
714 if ( ! $embedenabled) {
715 return '';
716 }
717
718 $template = new \stdClass();
719 $template->embedurl = self::get_embed_url($url)->out();
720
721 return $OUTPUT->render_from_template('core_h5p/h5pembed', $template);
722 }
723
724 /**
725 * Get the encoded URL for embeding this H5P content.
726 * @param string $url The URL of the .h5p file.
727 *
728 * @return \moodle_url The embed URL.
729 */
284af93b 730 public static function get_embed_url(string $url): \moodle_url {
35b62d00
SA
731 return new \moodle_url('/h5p/embed.php', ['url' => $url]);
732 }
65ae2441 733
734 /**
735 * Return the export file for Mobile App.
736 *
737 * @return array
738 */
284af93b 739 public function get_export_file(): array {
65ae2441 740 // Get the export url.
741 $exporturl = $this->get_export_settings(true);
742 // Get the filename of the export url.
743 $path = $exporturl->out_as_local_url();
744 $parts = explode('/', $path);
745 $filename = array_pop($parts);
746 // Get the the export file.
747 $systemcontext = \context_system::instance();
748 $fs = get_file_storage();
749 $fileh5p = $fs->get_file($systemcontext->id,
750 \core_h5p\file_storage::COMPONENT,
751 \core_h5p\file_storage::EXPORT_FILEAREA,
752 0,
753 '/',
754 $filename);
755 // Get the options that the Mobile App needs.
756 $file = [];
757 $file['filename'] = $fileh5p->get_filename();
758 $file['filepath'] = $fileh5p->get_filepath();
759 $file['mimetype'] = $fileh5p->get_mimetype();
760 $file['filesize'] = $fileh5p->get_filesize();
761 $file['timemodified'] = $fileh5p->get_timemodified();
762 $file['fileurl'] = $exporturl->out(false);
763
764 return $file;
765 }
35b62d00 766}