Merge branch 'MDL-65448-master' of git://github.com/lucaboesch/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;
08fda3e0 31
35b62d00
SA
32/**
33 * H5P player class, for displaying any local H5P content.
34 *
35 * @package core_h5p
36 * @copyright 2019 Sara Arjona <sara@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class player {
40
41 /**
42 * @var string The local H5P URL containing the .h5p file to display.
43 */
44 private $url;
45
46 /**
9e67f5e3 47 * @var core The H5PCore object.
35b62d00
SA
48 */
49 private $core;
50
51 /**
52 * @var int H5P DB id.
53 */
54 private $h5pid;
55
56 /**
57 * @var array JavaScript requirements for this H5P.
58 */
59 private $jsrequires = [];
60
61 /**
62 * @var array CSS requirements for this H5P.
63 */
64 private $cssrequires = [];
65
66 /**
67 * @var array H5P content to display.
68 */
69 private $content;
70
8685c313
FR
71 /**
72 * @var string optional component name to send xAPI statements.
73 */
74 private $component;
75
35b62d00
SA
76 /**
77 * @var string Type of embed object, div or iframe.
78 */
79 private $embedtype;
80
81 /**
82 * @var context The context object where the .h5p belongs.
83 */
84 private $context;
85
df74cd4a 86 /**
c07f31ae 87 * @var factory The \core_h5p\factory object.
df74cd4a
MG
88 */
89 private $factory;
90
c07f31ae
SA
91 /**
92 * @var stdClass The error, exception and info messages, raised while preparing and running the player.
93 */
94 private $messages;
95
74346211
SA
96 /**
97 * @var bool Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions.
98 */
99 private $preventredirect;
100
35b62d00
SA
101 /**
102 * Inits the H5P player for rendering the content.
103 *
104 * @param string $url Local URL of the H5P file to display.
105 * @param stdClass $config Configuration for H5P buttons.
74346211 106 * @param bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
8685c313 107 * @param string $component optional moodle component to sent xAPI tracking
35b62d00 108 */
8685c313 109 public function __construct(string $url, \stdClass $config, bool $preventredirect = true, string $component = '') {
35b62d00
SA
110 if (empty($url)) {
111 throw new \moodle_exception('h5pinvalidurl', 'core_h5p');
112 }
113 $this->url = new \moodle_url($url);
74346211 114 $this->preventredirect = $preventredirect;
35b62d00 115
df74cd4a
MG
116 $this->factory = new \core_h5p\factory();
117
c07f31ae
SA
118 $this->messages = new \stdClass();
119
8685c313
FR
120 $this->component = $component;
121
df74cd4a
MG
122 // Create \core_h5p\core instance.
123 $this->core = $this->factory->get_core();
35b62d00
SA
124
125 // Get the H5P identifier linked to this URL.
153c4562
SA
126 list($file, $this->h5pid) = api::create_content_from_pluginfile_url(
127 $url,
128 $config,
129 $this->factory,
130 $this->messages
131 );
132 if ($file) {
133 $this->context = \context::instance_by_id($file->get_contextid());
134 if ($this->h5pid) {
135 // Load the content of the H5P content associated to this $url.
136 $this->content = $this->core->loadContent($this->h5pid);
137
138 // Get the embedtype to use for displaying the H5P content.
139 $this->embedtype = core::determineEmbedType($this->content['embedType'], $this->content['library']['embedTypes']);
140 }
35b62d00
SA
141 }
142 }
143
f3c7e00f
FR
144 /**
145 * Get the encoded URL for embeding this H5P content.
146 *
147 * @param string $url Local URL of the H5P file to display.
148 * @param stdClass $config Configuration for H5P buttons.
149 * @param bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
8685c313 150 * @param string $component optional moodle component to sent xAPI tracking
f3c7e00f
FR
151 *
152 * @return string The embedable code to display a H5P file.
153 */
8685c313
FR
154 public static function display(string $url, \stdClass $config, bool $preventredirect = true,
155 string $component = ''): string {
f3c7e00f
FR
156 global $OUTPUT;
157 $params = [
158 'url' => $url,
159 'preventredirect' => $preventredirect,
8685c313 160 'component' => $component,
f3c7e00f
FR
161 ];
162
163 $optparams = ['frame', 'export', 'embed', 'copyright'];
164 foreach ($optparams as $optparam) {
165 if (!empty($config->$optparam)) {
166 $params[$optparam] = $config->$optparam;
167 }
168 }
169 $fileurl = new \moodle_url('/h5p/embed.php', $params);
170
171 $template = new \stdClass();
172 $template->embedurl = $fileurl->out(false);
173
174 $result = $OUTPUT->render_from_template('core_h5p/h5pembed', $template);
175 $result .= self::get_resize_code();
176 return $result;
177 }
178
35b62d00
SA
179 /**
180 * Get the error messages stored in our H5P framework.
181 *
182 * @return stdClass with framework error messages.
183 */
284af93b 184 public function get_messages(): \stdClass {
153c4562 185 return helper::get_messages($this->messages, $this->factory);
35b62d00
SA
186 }
187
188 /**
189 * Create the H5PIntegration variable that will be included in the page. This variable is used as the
190 * main H5P config variable.
191 */
192 public function add_assets_to_page() {
193 global $PAGE;
194
195 $cid = $this->get_cid();
196 $systemcontext = \context_system::instance();
197
9e67f5e3 198 $disable = array_key_exists('disable', $this->content) ? $this->content['disable'] : core::DISABLE_NONE;
35b62d00
SA
199 $displayoptions = $this->core->getDisplayOptionsForView($disable, $this->h5pid);
200
201 $contenturl = \moodle_url::make_pluginfile_url($systemcontext->id, \core_h5p\file_storage::COMPONENT,
202 \core_h5p\file_storage::CONTENT_FILEAREA, $this->h5pid, null, null);
65ae2441 203 $exporturl = $this->get_export_settings($displayoptions[ core::DISPLAY_OPTION_DOWNLOAD ]);
8685c313 204 $xapiobject = item_activity::create_from_id($this->context->id);
35b62d00 205 $contentsettings = [
9e67f5e3 206 'library' => core::libraryToString($this->content['library']),
35b62d00 207 'fullScreen' => $this->content['library']['fullscreen'],
65ae2441 208 'exportUrl' => ($exporturl instanceof \moodle_url) ? $exporturl->out(false) : '',
35b62d00 209 'embedCode' => $this->get_embed_code($this->url->out(),
9e67f5e3 210 $displayoptions[ core::DISPLAY_OPTION_EMBED ]),
f3c7e00f 211 'resizeCode' => self::get_resize_code(),
35b62d00
SA
212 'title' => $this->content['slug'],
213 'displayOptions' => $displayoptions,
8685c313 214 'url' => $xapiobject->get_data()->id,
35b62d00
SA
215 'contentUrl' => $contenturl->out(),
216 'metadata' => $this->content['metadata'],
217 'contentUserData' => [0 => ['state' => '{}']]
218 ];
219 // Get the core H5P assets, needed by the H5P classes to render the H5P content.
220 $settings = $this->get_assets();
221 $settings['contents'][$cid] = array_merge($settings['contents'][$cid], $contentsettings);
222
35b62d00
SA
223 // Print JavaScript settings to page.
224 $PAGE->requires->data_for_js('H5PIntegration', $settings, true);
225 }
226
227 /**
228 * Outputs H5P wrapper HTML.
229 *
230 * @return string The HTML code to display this H5P content.
231 */
284af93b 232 public function output(): string {
fba5bba8 233 global $OUTPUT, $USER;
35b62d00
SA
234
235 $template = new \stdClass();
236 $template->h5pid = $this->h5pid;
237 if ($this->embedtype === 'div') {
fba5bba8 238 $h5phtml = $OUTPUT->render_from_template('core_h5p/h5pdiv', $template);
35b62d00 239 } else {
fba5bba8 240 $h5phtml = $OUTPUT->render_from_template('core_h5p/h5piframe', $template);
35b62d00 241 }
fba5bba8 242
243 // Trigger capability_assigned event.
244 \core_h5p\event\h5p_viewed::create([
245 'objectid' => $this->h5pid,
246 'userid' => $USER->id,
153c4562 247 'context' => $this->get_context(),
fba5bba8 248 'other' => [
249 'url' => $this->url->out(),
250 'time' => time()
251 ]
252 ])->trigger();
253
254 return $h5phtml;
35b62d00
SA
255 }
256
257 /**
258 * Get the title of the H5P content to display.
259 *
260 * @return string the title
261 */
284af93b 262 public function get_title(): string {
35b62d00
SA
263 return $this->content['title'];
264 }
265
266 /**
267 * Get the context where the .h5p file belongs.
268 *
269 * @return context The context.
270 */
284af93b 271 public function get_context(): \context {
35b62d00
SA
272 return $this->context;
273 }
274
35b62d00
SA
275 /**
276 * Delete an H5P package.
277 *
278 * @param stdClass $content The H5P package to delete.
279 */
280 private function delete_h5p(\stdClass $content) {
df74cd4a 281 $h5pstorage = $this->factory->get_storage();
35b62d00
SA
282 // Add an empty slug to the content if it's not defined, because the H5P library requires this field exists.
283 // It's not used when deleting a package, so the real slug value is not required at this point.
284 $content->slug = $content->slug ?? '';
285 $h5pstorage->deletePackage( (array) $content);
286 }
287
288 /**
289 * Export path for settings
290 *
291 * @param bool $downloadenabled Whether the option to export the H5P content is enabled.
292 *
65ae2441 293 * @return \moodle_url|null The URL of the exported file.
35b62d00 294 */
284af93b 295 private function get_export_settings(bool $downloadenabled): ?\moodle_url {
35b62d00 296
5d81efd1 297 if (!$downloadenabled) {
65ae2441 298 return null;
35b62d00
SA
299 }
300
301 $systemcontext = \context_system::instance();
302 $slug = $this->content['slug'] ? $this->content['slug'] . '-' : '';
5d81efd1 303 // We have to build the right URL.
304 // Depending the request was made through webservice/pluginfile.php or pluginfile.php.
305 if (strpos($this->url, '/webservice/pluginfile.php')) {
306 $url = \moodle_url::make_webservice_pluginfile_url(
307 $systemcontext->id,
308 \core_h5p\file_storage::COMPONENT,
309 \core_h5p\file_storage::EXPORT_FILEAREA,
310 '',
311 '',
312 "{$slug}{$this->content['id']}.h5p"
313 );
314 } else {
315 // If the request is made by tokenpluginfile.php we need to indicates to generate a token for current user.
316 $includetoken = false;
317 if (strpos($this->url, '/tokenpluginfile.php')) {
318 $includetoken = true;
319 }
320 $url = \moodle_url::make_pluginfile_url(
321 $systemcontext->id,
322 \core_h5p\file_storage::COMPONENT,
323 \core_h5p\file_storage::EXPORT_FILEAREA,
324 '',
325 '',
326 "{$slug}{$this->content['id']}.h5p",
327 false,
328 $includetoken
329 );
330 }
35b62d00 331
65ae2441 332 return $url;
35b62d00
SA
333 }
334
35b62d00
SA
335 /**
336 * Get the identifier for the H5P content, to be used in the arrays as index.
337 *
338 * @return string The identifier.
339 */
284af93b 340 private function get_cid(): string {
35b62d00
SA
341 return 'cid-' . $this->h5pid;
342 }
343
344 /**
345 * Get the core H5P assets, including all core H5P JavaScript and CSS.
346 *
347 * @return Array core H5P assets.
348 */
284af93b 349 private function get_assets(): array {
a0ce8f87
VDF
350 // Get core assets.
351 $settings = helper::get_core_assets();
352 // Added here because in the helper we don't have the h5p content id.
353 $settings['moodleLibraryPaths'] = $this->core->get_dependency_roots($this->h5pid);
2ac1d9ae
SA
354 // Add also the Moodle component where the results will be tracked.
355 $settings['moodleComponent'] = $this->component;
dd4a089b
FR
356 if (!empty($settings['moodleComponent'])) {
357 $settings['reportingIsEnabled'] = true;
358 }
35b62d00
SA
359
360 $cid = $this->get_cid();
361 // The filterParameters function should be called before getting the dependencyfiles because it rebuild content
362 // dependency cache and export file.
c68897b7 363 $settings['contents'][$cid]['jsonContent'] = $this->get_filtered_parameters();
35b62d00
SA
364
365 $files = $this->get_dependency_files();
366 if ($this->embedtype === 'div') {
367 $systemcontext = \context_system::instance();
368 $h5ppath = "/pluginfile.php/{$systemcontext->id}/core_h5p";
369
370 // Schedule JavaScripts for loading through Moodle.
371 foreach ($files['scripts'] as $script) {
372 $url = $script->path . $script->version;
373
374 // Add URL prefix if not external.
375 $isexternal = strpos($script->path, '://');
376 if ($isexternal === false) {
377 $url = $h5ppath . $url;
378 }
379 $settings['loadedJs'][] = $url;
380 $this->jsrequires[] = new \moodle_url($isexternal ? $url : $CFG->wwwroot . $url);
381 }
382
383 // Schedule stylesheets for loading through Moodle.
384 foreach ($files['styles'] as $style) {
385 $url = $style->path . $style->version;
386
387 // Add URL prefix if not external.
388 $isexternal = strpos($style->path, '://');
389 if ($isexternal === false) {
390 $url = $h5ppath . $url;
391 }
392 $settings['loadedCss'][] = $url;
393 $this->cssrequires[] = new \moodle_url($isexternal ? $url : $CFG->wwwroot . $url);
394 }
395
396 } else {
397 // JavaScripts and stylesheets will be loaded through h5p.js.
398 $settings['contents'][$cid]['scripts'] = $this->core->getAssetsUrls($files['scripts']);
399 $settings['contents'][$cid]['styles'] = $this->core->getAssetsUrls($files['styles']);
400 }
401 return $settings;
402 }
403
c68897b7
SA
404 /**
405 * Get filtered parameters, modifying them by the renderer if the theme implements the h5p_alter_filtered_parameters function.
406 *
407 * @return string Filtered parameters.
408 */
409 private function get_filtered_parameters(): string {
410 global $PAGE;
411
412 $safeparams = $this->core->filterParameters($this->content);
413 $decodedparams = json_decode($safeparams);
414 $h5poutput = $PAGE->get_renderer('core_h5p');
415 $h5poutput->h5p_alter_filtered_parameters(
416 $decodedparams,
417 $this->content['library']['name'],
418 $this->content['library']['majorVersion'],
419 $this->content['library']['minorVersion']
420 );
421 $safeparams = json_encode($decodedparams);
422
423 return $safeparams;
424 }
425
35b62d00
SA
426 /**
427 * Finds library dependencies of view
428 *
429 * @return array Files that the view has dependencies to
430 */
284af93b 431 private function get_dependency_files(): array {
c68897b7
SA
432 global $PAGE;
433
35b62d00
SA
434 $preloadeddeps = $this->core->loadContentDependencies($this->h5pid, 'preloaded');
435 $files = $this->core->getDependenciesFiles($preloadeddeps);
436
c68897b7
SA
437 // Add additional asset files if required.
438 $h5poutput = $PAGE->get_renderer('core_h5p');
439 $h5poutput->h5p_alter_scripts($files['scripts'], $preloadeddeps, $this->embedtype);
440 $h5poutput->h5p_alter_styles($files['styles'], $preloadeddeps, $this->embedtype);
441
35b62d00
SA
442 return $files;
443 }
444
445 /**
446 * Resizing script for settings
447 *
448 * @return string The HTML code with the resize script.
449 */
f3c7e00f 450 private static function get_resize_code(): string {
35b62d00
SA
451 global $OUTPUT;
452
453 $template = new \stdClass();
08fda3e0 454 $template->resizeurl = autoloader::get_h5p_core_library_url('js/h5p-resizer.js');
35b62d00
SA
455
456 return $OUTPUT->render_from_template('core_h5p/h5presize', $template);
457 }
458
459 /**
460 * Embed code for settings
461 *
462 * @param string $url The URL of the .h5p file.
463 * @param bool $embedenabled Whether the option to embed the H5P content is enabled.
464 *
465 * @return string The HTML code to reuse this H5P content in a different place.
466 */
284af93b 467 private function get_embed_code(string $url, bool $embedenabled): string {
35b62d00
SA
468 global $OUTPUT;
469
470 if ( ! $embedenabled) {
471 return '';
472 }
473
474 $template = new \stdClass();
f5b4ac4d 475 $template->embedurl = self::get_embed_url($url, $this->component)->out(false);
35b62d00
SA
476
477 return $OUTPUT->render_from_template('core_h5p/h5pembed', $template);
478 }
479
480 /**
481 * Get the encoded URL for embeding this H5P content.
482 * @param string $url The URL of the .h5p file.
f5b4ac4d 483 * @param string $component optional Moodle component to send xAPI tracking
35b62d00
SA
484 *
485 * @return \moodle_url The embed URL.
486 */
f5b4ac4d
SA
487 public static function get_embed_url(string $url, string $component = ''): \moodle_url {
488 $params = ['url' => $url];
489 if (!empty($component)) {
490 // If component is not empty, it will be passed too, in order to allow tracking too.
491 $params['component'] = $component;
492 }
493
494 return new \moodle_url('/h5p/embed.php', $params);
35b62d00 495 }
65ae2441 496
497 /**
14b463c9 498 * Return the info export file for Mobile App.
65ae2441 499 *
500 * @return array
501 */
284af93b 502 public function get_export_file(): array {
65ae2441 503 // Get the export url.
504 $exporturl = $this->get_export_settings(true);
505 // Get the filename of the export url.
506 $path = $exporturl->out_as_local_url();
507 $parts = explode('/', $path);
508 $filename = array_pop($parts);
14b463c9 509 // Get the required info from the export file to be able to get the export file by third apps.
510 $file = helper::get_export_info($filename, $exporturl);
65ae2441 511
512 return $file;
513 }
35b62d00 514}