mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-66609 core_h5p: Player and embed.php implementation
- Loading branch information
1 parent
9ea303e
commit 35b62d0
Showing
11 changed files
with
1,205 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Render H5P content from an H5P file. | ||
* | ||
* @package core_h5p | ||
* @copyright 2019 Sara Arjona <sara@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
require_once(__DIR__ . '/../config.php'); | ||
|
||
// The login check is done inside the player when getting the file from the url param. | ||
|
||
$url = required_param('url', PARAM_LOCALURL); | ||
|
||
$config = new stdClass(); | ||
$config->frame = optional_param('frame', 0, PARAM_INT); | ||
$config->export = optional_param('export', 0, PARAM_INT); | ||
$config->embed = optional_param('embed', 0, PARAM_INT); | ||
$config->copyright = optional_param('copyright', 0, PARAM_INT); | ||
|
||
$PAGE->set_url(new \moodle_url('/h5p/embed.php', array('url' => $url))); | ||
try { | ||
$h5pplayer = new \core_h5p\player($url, $config); | ||
$messages = $h5pplayer->get_messages(); | ||
|
||
} catch (\Exception $e) { | ||
$messages = (object) [ | ||
'exception' => $e->getMessage(), | ||
]; | ||
} | ||
|
||
if (empty($messages->error) && empty($messages->exception)) { | ||
// Configure page. | ||
$PAGE->set_context($h5pplayer->get_context()); | ||
$PAGE->set_title($h5pplayer->get_title()); | ||
$PAGE->set_heading($h5pplayer->get_title()); | ||
|
||
// Embed specific page setup. | ||
$PAGE->add_body_class('h5p-embed'); | ||
$PAGE->set_pagelayout('embedded'); | ||
|
||
// Load the embed.js to allow communication with the parent window. | ||
$PAGE->requires->js(new moodle_url('/h5p/js/embed.js')); | ||
|
||
// Add H5P assets to the page. | ||
$h5pplayer->add_assets_to_page(); | ||
|
||
// Check if there is some error when adding assets to the page. | ||
$messages = $h5pplayer->get_messages(); | ||
if (empty($messages->error) && empty($messages->exception)) { | ||
|
||
// Print page HTML. | ||
echo $OUTPUT->header(); | ||
|
||
echo $h5pplayer->output(); | ||
} | ||
} else { | ||
// If there is any error or exception when creating the player, it should be displayed. | ||
$PAGE->set_context(context_system::instance()); | ||
$title = get_string('h5p', 'core_h5p'); | ||
$PAGE->set_title($title); | ||
$PAGE->set_heading($title); | ||
|
||
$PAGE->add_body_class('h5p-embed'); | ||
$PAGE->set_pagelayout('embedded'); | ||
|
||
// Errors can't be printed yet, because some more errors might been added while preparing the output | ||
} | ||
|
||
if (!empty($messages->error) || !empty($messages->exception)) { | ||
// Print all the errors. | ||
echo $OUTPUT->header(); | ||
$messages->h5picon = new \moodle_url('/h5p/pix/icon.svg'); | ||
echo $OUTPUT->render_from_template('core_h5p/h5perror', $messages); | ||
} | ||
|
||
echo $OUTPUT->footer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/* global H5PEmbedCommunicator:true */ | ||
/** | ||
* When embedded the communicator helps talk to the parent page. | ||
* This is a copy of the H5P.communicator, which we need to communicate in this context | ||
* | ||
* @type {H5PEmbedCommunicator} | ||
* @module core_h5p | ||
* @copyright 2019 Joubel AS <contact@joubel.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
H5PEmbedCommunicator = (function() { | ||
/** | ||
* @class | ||
* @private | ||
*/ | ||
function Communicator() { | ||
var self = this; | ||
|
||
// Maps actions to functions. | ||
var actionHandlers = {}; | ||
|
||
// Register message listener. | ||
window.addEventListener('message', function receiveMessage(event) { | ||
if (window.parent !== event.source || event.data.context !== 'h5p') { | ||
return; // Only handle messages from parent and in the correct context. | ||
} | ||
|
||
if (actionHandlers[event.data.action] !== undefined) { | ||
actionHandlers[event.data.action](event.data); | ||
} | ||
}, false); | ||
|
||
/** | ||
* Register action listener. | ||
* | ||
* @param {string} action What you are waiting for | ||
* @param {function} handler What you want done | ||
*/ | ||
self.on = function(action, handler) { | ||
actionHandlers[action] = handler; | ||
}; | ||
|
||
/** | ||
* Send a message to the all mighty father. | ||
* | ||
* @param {string} action | ||
* @param {Object} [data] payload | ||
*/ | ||
self.send = function(action, data) { | ||
if (data === undefined) { | ||
data = {}; | ||
} | ||
data.context = 'h5p'; | ||
data.action = action; | ||
|
||
// Parent origin can be anything. | ||
window.parent.postMessage(data, '*'); | ||
}; | ||
} | ||
|
||
return (window.postMessage && window.addEventListener ? new Communicator() : undefined); | ||
})(); | ||
|
||
document.onreadystatechange = function() { | ||
// Wait for instances to be initialize. | ||
if (document.readyState !== 'complete') { | ||
return; | ||
} | ||
|
||
// Check for H5P iFrame. | ||
var iFrame = document.querySelector('.h5p-iframe'); | ||
if (!iFrame || !iFrame.contentWindow) { | ||
return; | ||
} | ||
var H5P = iFrame.contentWindow.H5P; | ||
|
||
// Check for H5P instances. | ||
if (!H5P || !H5P.instances || !H5P.instances[0]) { | ||
return; | ||
} | ||
|
||
var resizeDelay; | ||
var instance = H5P.instances[0]; | ||
var parentIsFriendly = false; | ||
|
||
// Handle that the resizer is loaded after the iframe. | ||
H5PEmbedCommunicator.on('ready', function() { | ||
H5PEmbedCommunicator.send('hello'); | ||
}); | ||
|
||
// Handle hello message from our parent window. | ||
H5PEmbedCommunicator.on('hello', function() { | ||
// Initial setup/handshake is done. | ||
parentIsFriendly = true; | ||
|
||
// Hide scrollbars for correct size. | ||
iFrame.contentDocument.body.style.overflow = 'hidden'; | ||
|
||
document.body.classList.add('h5p-resizing'); | ||
|
||
// Content need to be resized to fit the new iframe size. | ||
H5P.trigger(instance, 'resize'); | ||
}); | ||
|
||
// When resize has been prepared tell parent window to resize. | ||
H5PEmbedCommunicator.on('resizePrepared', function() { | ||
H5PEmbedCommunicator.send('resize', { | ||
scrollHeight: iFrame.contentDocument.body.scrollHeight | ||
}); | ||
}); | ||
|
||
H5PEmbedCommunicator.on('resize', function() { | ||
H5P.trigger(instance, 'resize'); | ||
}); | ||
|
||
H5P.on(instance, 'resize', function() { | ||
if (H5P.isFullscreen) { | ||
return; // Skip iframe resize. | ||
} | ||
|
||
// Use a delay to make sure iframe is resized to the correct size. | ||
clearTimeout(resizeDelay); | ||
resizeDelay = setTimeout(function() { | ||
// Only resize if the iframe can be resized. | ||
if (parentIsFriendly) { | ||
H5PEmbedCommunicator.send('prepareResize', | ||
{ | ||
scrollHeight: iFrame.contentDocument.body.scrollHeight, | ||
clientHeight: iFrame.contentDocument.body.clientHeight | ||
} | ||
); | ||
} else { | ||
H5PEmbedCommunicator.send('hello'); | ||
} | ||
}, 0); | ||
}); | ||
|
||
// Trigger initial resize for instance. | ||
H5P.trigger(instance, 'resize'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Callbacks. | ||
* | ||
* @package core_h5p | ||
* @copyright 2019 Bas Brands <bas@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Serve the files from the core_h5p file areas. | ||
* | ||
* @package core_h5p | ||
* @category files | ||
* | ||
* @param stdClass $course the course object | ||
* @param stdClass $cm the course module object | ||
* @param stdClass $context the newmodule's context | ||
* @param string $filearea the name of the file area | ||
* @param array $args extra arguments (itemid, path) | ||
* @param bool $forcedownload whether or not force download | ||
* @param array $options additional options affecting the file serving | ||
* | ||
* @return bool Returns false if we don't find a file. | ||
*/ | ||
function core_h5p_pluginfile($course, $cm, $context, string $filearea, array $args, bool $forcedownload, | ||
array $options = []) : bool { | ||
global $DB; | ||
|
||
$filesettingsset = false; | ||
|
||
switch ($filearea) { | ||
default: | ||
return false; // Invalid file area. | ||
|
||
case \core_h5p\file_storage::LIBRARY_FILEAREA: | ||
if ($context->contextlevel != CONTEXT_SYSTEM) { | ||
return false; // Invalid context because the libraries are loaded always in the context system. | ||
} | ||
|
||
$itemid = null; | ||
|
||
// The files that can be delivered to this function are unfortunately out of our control. Some of the | ||
// references are embedded into the JavaScript of the files and we have no ability to inject an item id. | ||
// We also don't know the location of the item id when we do include it, so we look for the first numeric | ||
// value and try to serve that file. | ||
foreach ($args as $key => $value) { | ||
if (is_numeric($value)) { | ||
$itemid = $value; | ||
unset($args[$key]); | ||
break; | ||
} | ||
} | ||
|
||
if (!isset($itemid)) { | ||
// We didn't find an item id to use, so we fall back to retrieving the record using all the other | ||
// fields. The combination of component, filearea, filepath, and filename is enough for a unique | ||
// record. | ||
$filename = array_pop($args); | ||
$filepath = '/' . implode('/', $args) . '/'; | ||
$itemid = $DB->get_field('files', 'itemid', [ | ||
'component' => \core_h5p\file_storage::COMPONENT, | ||
'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA, | ||
'filepath' => $filepath, | ||
'filename' => $filename | ||
]); | ||
$filesettingsset = true; | ||
} | ||
break; | ||
case \core_h5p\file_storage::CONTENT_FILEAREA: | ||
if ($context->contextlevel != CONTEXT_SYSTEM) { | ||
return false; // Invalid context because the content files are loaded always in the context system. | ||
} | ||
$itemid = array_shift($args); | ||
break; | ||
case \core_h5p\file_storage::CACHED_ASSETS_FILEAREA: | ||
case \core_h5p\file_storage::EXPORT_FILEAREA: | ||
$itemid = 0; | ||
break; | ||
} | ||
|
||
if (!$filesettingsset) { | ||
$filename = array_pop($args); | ||
$filepath = (!$args ? '/' : '/' . implode('/', $args) . '/'); | ||
} | ||
|
||
$fs = get_file_storage(); | ||
$file = $fs->get_file($context->id, \core_h5p\file_storage::COMPONENT, $filearea, $itemid, $filepath, $filename); | ||
if (!$file) { | ||
return false; // No such file. | ||
} | ||
|
||
send_stored_file($file, null, 0, $forcedownload, $options); | ||
|
||
return true; | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.