Merge branch 'MDL-73004-311_access_unit_tests' of https://github.com/brickfield/moodl...
[moodle.git] / h5p / embed.php
blob779b3256ce933ee471c6b2b6717742b7085da4af
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 $PAGE->set_context($h5pplayer->get_context());
55 $PAGE->set_title($h5pplayer->get_title());
56 $PAGE->set_heading($h5pplayer->get_title());
58 // Embed specific page setup.
59 $PAGE->add_body_class('h5p-embed');
60 $PAGE->set_pagelayout('embedded');
62 // Load the embed.js to allow communication with the parent window.
63 $PAGE->requires->js(new moodle_url('/h5p/js/embed.js'));
65 // Add H5P assets to the page.
66 $h5pplayer->add_assets_to_page();
68 // Print page HTML.
69 echo $OUTPUT->header();
71 // Check if some error has been raised when adding assets to the page. If that's the case, display them above the H5P content.
72 $messages = $h5pplayer->get_messages();
73 if (!empty($messages->exception) || !empty($messages->error)) {
74 $messages->h5picon = new \moodle_url('/h5p/pix/icon.svg');
75 echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages);
78 // Display the H5P content.
79 echo $h5pplayer->output();
80 } else {
81 // If there is any error or exception when creating the player, it should be displayed.
82 $PAGE->set_context(context_system::instance());
83 $title = get_string('h5p', 'core_h5p');
84 $PAGE->set_title($title);
85 $PAGE->set_heading($title);
87 $PAGE->add_body_class('h5p-embed');
88 $PAGE->set_pagelayout('embedded');
90 // Load the embed.js to allow communication with the parent window.
91 $PAGE->requires->js(new moodle_url('/h5p/js/embed.js'));
93 echo $OUTPUT->header();
95 // Print all the errors.
96 $messages->h5picon = new \moodle_url('/h5p/pix/icon.svg');
97 echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages);
100 echo $OUTPUT->footer();