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