weekly release 4.5dev
[moodle.git] / h5p / embed.php
blobc2ff803d29210edc733e8dd6568cdc27c9cd76d9
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.
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/>.
17 /**
18 * Render H5P content from an H5P file.
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
25 require_once(__DIR__ . '/../config.php');
27 // The login check is done inside the player when getting the file from the url param.
29 $url = required_param('url', PARAM_LOCALURL);
31 $config = new stdClass();
32 $config->frame = optional_param('frame', 0, PARAM_INT);
33 $config->export = optional_param('export', 0, PARAM_INT);
34 $config->embed = optional_param('embed', 0, PARAM_INT);
35 $config->copyright = optional_param('copyright', 0, PARAM_INT);
37 $preventredirect = optional_param('preventredirect', true, PARAM_BOOL);
39 $component = optional_param('component', '', PARAM_COMPONENT);
41 $PAGE->set_url(new \moodle_url('/h5p/embed.php', array('url' => $url)));
42 try {
43 $h5pplayer = new \core_h5p\player($url, $config, $preventredirect, $component);
44 $messages = $h5pplayer->get_messages();
46 } catch (\Exception $e) {
47 $messages = (object) [
48 'exception' => $e->getMessage(),
52 if (empty($messages->error) && empty($messages->exception)) {
53 // Configure page.
54 $context = $h5pplayer->get_context();
55 if ($context instanceof context_module) {
56 [$course, $cm] = get_course_and_cm_from_cmid($context->instanceid);
57 $PAGE->set_cm($cm, $course);
58 $PAGE->activityheader->disable();
59 } else {
60 $PAGE->set_context($context);
63 $PAGE->set_title($h5pplayer->get_title());
64 $PAGE->set_heading($h5pplayer->get_title());
66 // Embed specific page setup.
67 $PAGE->add_body_class('h5p-embed');
68 $PAGE->set_pagelayout('embedded');
70 // Load the embed.js to allow communication with the parent window.
71 $PAGE->requires->js(new moodle_url('/h5p/js/embed.js'));
73 // Add H5P assets to the page.
74 $h5pplayer->add_assets_to_page();
76 // Print page HTML.
77 echo $OUTPUT->header();
79 // Check if some error has been raised when adding assets to the page. If that's the case, display them above the H5P content.
80 $messages = $h5pplayer->get_messages();
81 if (!empty($messages->exception) || !empty($messages->error)) {
82 $messages->h5picon = new \moodle_url('/h5p/pix/icon.svg');
83 echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages);
86 // Display the H5P content.
87 echo $h5pplayer->output();
88 } else {
89 // If there is any error or exception when creating the player, it should be displayed.
90 $PAGE->set_context(context_system::instance());
91 $title = get_string('h5p', 'core_h5p');
92 $PAGE->set_title($title);
93 $PAGE->set_heading($title);
95 $PAGE->add_body_class('h5p-embed');
96 $PAGE->set_pagelayout('embedded');
98 // Load the embed.js to allow communication with the parent window.
99 $PAGE->requires->js(new moodle_url('/h5p/js/embed.js'));
101 echo $OUTPUT->header();
103 // Print all the errors.
104 $messages->h5picon = new \moodle_url('/h5p/pix/icon.svg');
105 echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages);
108 echo $OUTPUT->footer();