Commit | Line | Data |
---|---|---|
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 | * Render H5P content from an H5P file. | |
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 | ||
25 | require_once(__DIR__ . '/../config.php'); | |
26 | ||
27 | // The login check is done inside the player when getting the file from the url param. | |
28 | ||
29 | $url = required_param('url', PARAM_LOCALURL); | |
30 | ||
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); | |
36 | ||
74346211 SA |
37 | $preventredirect = optional_param('preventredirect', true, PARAM_BOOL); |
38 | ||
8685c313 FR |
39 | $component = optional_param('component', '', PARAM_COMPONENT); |
40 | ||
35b62d00 SA |
41 | $PAGE->set_url(new \moodle_url('/h5p/embed.php', array('url' => $url))); |
42 | try { | |
8685c313 | 43 | $h5pplayer = new \core_h5p\player($url, $config, $preventredirect, $component); |
35b62d00 SA |
44 | $messages = $h5pplayer->get_messages(); |
45 | ||
46 | } catch (\Exception $e) { | |
47 | $messages = (object) [ | |
48 | 'exception' => $e->getMessage(), | |
49 | ]; | |
50 | } | |
51 | ||
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()); | |
57 | ||
58 | // Embed specific page setup. | |
59 | $PAGE->add_body_class('h5p-embed'); | |
60 | $PAGE->set_pagelayout('embedded'); | |
61 | ||
62 | // Load the embed.js to allow communication with the parent window. | |
63 | $PAGE->requires->js(new moodle_url('/h5p/js/embed.js')); | |
64 | ||
65 | // Add H5P assets to the page. | |
66 | $h5pplayer->add_assets_to_page(); | |
67 | ||
285976f1 SA |
68 | // Print page HTML. |
69 | echo $OUTPUT->header(); | |
35b62d00 | 70 | |
285976f1 SA |
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); | |
35b62d00 | 76 | } |
285976f1 SA |
77 | |
78 | // Display the H5P content. | |
79 | echo $h5pplayer->output(); | |
35b62d00 SA |
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); | |
86 | ||
87 | $PAGE->add_body_class('h5p-embed'); | |
88 | $PAGE->set_pagelayout('embedded'); | |
89 | ||
285976f1 | 90 | echo $OUTPUT->header(); |
35b62d00 | 91 | |
35b62d00 | 92 | // Print all the errors. |
35b62d00 SA |
93 | $messages->h5picon = new \moodle_url('/h5p/pix/icon.svg'); |
94 | echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages); | |
95 | } | |
96 | ||
8685c313 | 97 | echo $OUTPUT->footer(); |