MDL-67415 environment: 3.10 base information
[moodle.git] / h5p / embed.php
blobd29ffc1cfd84986fdac5cb6bae6312867692e354
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 $PAGE->set_url(new \moodle_url('/h5p/embed.php', array('url' => $url)));
40 try {
41 $h5pplayer = new \core_h5p\player($url, $config, $preventredirect);
42 $messages = $h5pplayer->get_messages();
44 } catch (\Exception $e) {
45 $messages = (object) [
46 'exception' => $e->getMessage(),
50 if (empty($messages->error) && empty($messages->exception)) {
51 // Configure page.
52 $PAGE->set_context($h5pplayer->get_context());
53 $PAGE->set_title($h5pplayer->get_title());
54 $PAGE->set_heading($h5pplayer->get_title());
56 // Embed specific page setup.
57 $PAGE->add_body_class('h5p-embed');
58 $PAGE->set_pagelayout('embedded');
60 // Load the embed.js to allow communication with the parent window.
61 $PAGE->requires->js(new moodle_url('/h5p/js/embed.js'));
63 // Add H5P assets to the page.
64 $h5pplayer->add_assets_to_page();
66 // Print page HTML.
67 echo $OUTPUT->header();
69 // Check if some error has been raised when adding assets to the page. If that's the case, display them above the H5P content.
70 $messages = $h5pplayer->get_messages();
71 if (!empty($messages->exception) || !empty($messages->error)) {
72 $messages->h5picon = new \moodle_url('/h5p/pix/icon.svg');
73 echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages);
76 // Display the H5P content.
77 echo $h5pplayer->output();
78 } else {
79 // If there is any error or exception when creating the player, it should be displayed.
80 $PAGE->set_context(context_system::instance());
81 $title = get_string('h5p', 'core_h5p');
82 $PAGE->set_title($title);
83 $PAGE->set_heading($title);
85 $PAGE->add_body_class('h5p-embed');
86 $PAGE->set_pagelayout('embedded');
88 echo $OUTPUT->header();
90 // Print all the errors.
91 $messages->h5picon = new \moodle_url('/h5p/pix/icon.svg');
92 echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages);
95 echo $OUTPUT->footer();