MDL-66609 core_h5p: Player and embed.php implementation

This commit is contained in:
Sara Arjona 2019-09-06 13:54:48 +02:00 committed by Andrew Nicols
parent 9ea303e773
commit 35b62d00b8
11 changed files with 1205 additions and 1 deletions

646
h5p/classes/player.php Normal file
View File

@ -0,0 +1,646 @@
<?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/>.
/**
* H5P player class.
*
* @package core_h5p
* @copyright 2019 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_h5p;
defined('MOODLE_INTERNAL') || die();
/**
* H5P player class, for displaying any local H5P content.
*
* @package core_h5p
* @copyright 2019 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class player {
/**
* @var string The local H5P URL containing the .h5p file to display.
*/
private $url;
/**
* @var \H5PCore The H5PCore object.
*/
private $core;
/**
* @var int H5P DB id.
*/
private $h5pid;
/**
* @var array JavaScript requirements for this H5P.
*/
private $jsrequires = [];
/**
* @var array CSS requirements for this H5P.
*/
private $cssrequires = [];
/**
* @var array H5P content to display.
*/
private $content;
/**
* @var string Type of embed object, div or iframe.
*/
private $embedtype;
/**
* @var context The context object where the .h5p belongs.
*/
private $context;
/**
* Inits the H5P player for rendering the content.
*
* @param string $url Local URL of the H5P file to display.
* @param stdClass $config Configuration for H5P buttons.
*/
public function __construct(string $url, \stdClass $config) {
if (empty($url)) {
throw new \moodle_exception('h5pinvalidurl', 'core_h5p');
}
$this->url = new \moodle_url($url);
// Create H5PFramework instance.
$this->core = \core_h5p\framework::instance();
// Get the H5P identifier linked to this URL.
if ($this->h5pid = $this->get_h5p_id($url, $config)) {
// Load the content of the H5P content associated to this $url.
$this->content = $this->core->loadContent($this->h5pid);
// Get the embedtype to use for displaying the H5P content.
$this->embedtype = \H5PCore::determineEmbedType($this->content['embedType'], $this->content['library']['embedTypes']);
}
}
/**
* Get the error messages stored in our H5P framework.
*
* @return stdClass with framework error messages.
*/
public function get_messages() : \stdClass {
$messages = new \stdClass();
$messages->error = $this->core->h5pF->getMessages('error');
if (empty($messages->error)) {
$messages->error = false;
}
return $messages;
}
/**
* Create the H5PIntegration variable that will be included in the page. This variable is used as the
* main H5P config variable.
*/
public function add_assets_to_page() {
global $PAGE;
$cid = $this->get_cid();
$systemcontext = \context_system::instance();
$disable = array_key_exists('disable', $this->content) ? $this->content['disable'] : \H5PCore::DISABLE_NONE;
$displayoptions = $this->core->getDisplayOptionsForView($disable, $this->h5pid);
$contenturl = \moodle_url::make_pluginfile_url($systemcontext->id, \core_h5p\file_storage::COMPONENT,
\core_h5p\file_storage::CONTENT_FILEAREA, $this->h5pid, null, null);
$contentsettings = [
'library' => \H5PCore::libraryToString($this->content['library']),
'fullScreen' => $this->content['library']['fullscreen'],
'exportUrl' => $this->get_export_settings($displayoptions[ \H5PCore::DISPLAY_OPTION_DOWNLOAD ]),
'embedCode' => $this->get_embed_code($this->url->out(),
$displayoptions[ \H5PCore::DISPLAY_OPTION_EMBED ]),
'resizeCode' => $this->get_resize_code(),
'title' => $this->content['slug'],
'displayOptions' => $displayoptions,
'url' => self::get_embed_url($this->url->out())->out(),
'contentUrl' => $contenturl->out(),
'metadata' => $this->content['metadata'],
'contentUserData' => [0 => ['state' => '{}']]
];
// Get the core H5P assets, needed by the H5P classes to render the H5P content.
$settings = $this->get_assets();
$settings['contents'][$cid] = array_merge($settings['contents'][$cid], $contentsettings);
foreach ($this->jsrequires as $script) {
$PAGE->requires->js($script, true);
}
foreach ($this->cssrequires as $css) {
$PAGE->requires->css($css);
}
// Print JavaScript settings to page.
$PAGE->requires->data_for_js('H5PIntegration', $settings, true);
}
/**
* Outputs H5P wrapper HTML.
*
* @return string The HTML code to display this H5P content.
*/
public function output() : string {
global $OUTPUT;
$template = new \stdClass();
$template->h5pid = $this->h5pid;
if ($this->embedtype === 'div') {
return $OUTPUT->render_from_template('core_h5p/h5pdiv', $template);
} else {
return $OUTPUT->render_from_template('core_h5p/h5piframe', $template);
}
}
/**
* Get the title of the H5P content to display.
*
* @return string the title
*/
public function get_title() : string {
return $this->content['title'];
}
/**
* Get the context where the .h5p file belongs.
*
* @return context The context.
*/
public function get_context() : \context {
return $this->context;
}
/**
* Get the H5P DB instance id for a H5P pluginfile URL. The H5P file will be saved if it doesn't exist previously or
* if its content has changed. Besides, the displayoptions in the $config will be also updated when they have changed and
* the user has the right permissions.
*
* @param string $url H5P pluginfile URL.
* @param stdClass $config Configuration for H5P buttons.
*
* @return int|false H5P DB identifier.
*/
private function get_h5p_id(string $url, \stdClass $config) {
global $DB;
$fs = get_file_storage();
// Deconstruct the URL and get the pathname associated.
$pathnamehash = $this->get_pluginfile_hash($url);
if (!$pathnamehash) {
$this->core->h5pF->setErrorMessage(get_string('h5pfilenotfound', 'core_h5p'));
return false;
}
// Get the file.
$file = $fs->get_file_by_hash($pathnamehash);
if (!$file) {
$this->core->h5pF->setErrorMessage(get_string('h5pfilenotfound', 'core_h5p'));
return false;
}
$h5p = $DB->get_record('h5p', ['pathnamehash' => $pathnamehash]);
$contenthash = $file->get_contenthash();
if ($h5p && $h5p->contenthash != $contenthash) {
// The content exists and it is different from the one deployed previously. The existing one should be removed before
// deploying the new version.
$this->delete_h5p($h5p);
$h5p = false;
}
if (!$h5p) {
// The H5P content hasn't been deployed previously. It has to be validated and stored before displaying it.
return $this->save_h5p($file, $config);
} else {
// The H5P content has been deployed previously.
$displayoptions = $this->get_display_options($config);
// Check if the user can set the displayoptions.
if ($displayoptions != $h5p->displayoptions && has_capability('moodle/h5p:setdisplayoptions', $this->context)) {
// If the displayoptions has changed and the user has permission to modify it, update this information in the DB.
$this->core->h5pF->updateContentFields($h5p->id, ['displayoptions' => $displayoptions]);
}
return $h5p->id;
}
}
/**
* Get the pathnamehash from an H5P internal URL.
*
* @param string $url H5P pluginfile URL poiting to an H5P file.
*
* @return string|false pathnamehash for the file in the internal URL.
*/
private function get_pluginfile_hash(string $url) {
global $USER;
// Decode the URL before start processing it.
$url = new \moodle_url(urldecode($url));
// Remove params from the URL (such as the 'forcedownload=1'), to avoid errors.
$url->remove_params(array_keys($url->params()));
$path = $url->out_as_local_url();
$parts = explode('/', $path);
$filename = array_pop($parts);
// First is an empty row and then the pluginfile.php part. Both can be ignored.
array_shift($parts);
array_shift($parts);
// Get the contextid, component and filearea.
$contextid = array_shift($parts);
$component = array_shift($parts);
$filearea = array_shift($parts);
// Ignore draft files, because they are considered temporary files, so shouldn't be displayed.
if ($filearea == 'draft') {
return false;
}
// Get the context.
try {
list($this->context, $course, $cm) = get_context_info_array($contextid);
} catch (\moodle_exception $e) {
throw new \moodle_exception('invalidcontextid', 'core_h5p');
}
// For CONTEXT_USER, such as the private files, raise an exception if the owner of the file is not the current user.
if ($this->context->contextlevel == CONTEXT_USER && $USER->id !== $this->context->instanceid) {
throw new \moodle_exception('h5pprivatefile', 'core_h5p');
}
// For CONTEXT_MODULE, check if the user is enrolled in the course and has permissions view this .h5p file.
if ($this->context->contextlevel == CONTEXT_MODULE) {
// Require login to the course first (without login to the module).
require_course_login($course, true, null, false, true);
// Now check if module is available OR it is restricted but the intro is shown on the course page.
$cminfo = \cm_info::create($cm);
if (!$cminfo->uservisible) {
if (!$cm->showdescription || !$cminfo->is_visible_on_course_page()) {
// Module intro is not visible on the course page and module is not available, show access error.
require_course_login($course, true, $cminfo, false, true);
}
}
}
// Some components, such as mod_page or mod_resource, add the revision to the URL to prevent caching problems.
// So the URL contains this revision number as itemid but a 0 is always stored in the files table.
// In order to get the proper hash, a callback should be done (looking for those exceptions).
$pathdata = component_callback($component, 'get_path_from_pluginfile', [$filearea, $parts], null);
if (null === $pathdata) {
// Look for the components and fileareas which have empty itemid defined in xxx_pluginfile.
$hasnullitemid = false;
$hasnullitemid = $hasnullitemid || ($component === 'user' && ($filearea === 'private' || $filearea === 'profile'));
$hasnullitemid = $hasnullitemid || ($component === 'mod' && $filearea === 'intro');
$hasnullitemid = $hasnullitemid || ($component === 'course' &&
($filearea === 'summary' || $filearea === 'overviewfiles'));
$hasnullitemid = $hasnullitemid || ($component === 'coursecat' && $filearea === 'description');
$hasnullitemid = $hasnullitemid || ($component === 'backup' &&
($filearea === 'course' || $filearea === 'activity' || $filearea === 'automated'));
if ($hasnullitemid) {
$itemid = 0;
} else {
$itemid = array_shift($parts);
}
if (empty($parts)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $parts) . '/';
}
} else {
// The itemid and filepath have been returned by the component callback.
[
'itemid' => $itemid,
'filepath' => $filepath,
] = $pathdata;
}
$fs = get_file_storage();
return $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath, $filename);
}
/**
* Store an H5P file
*
* @param stored_file $file Moodle file instance
* @param stdClass $config Button options config.
*
* @return int|false The H5P identifier or false if it's not a valid H5P package.
*/
private function save_h5p($file, \stdClass $config) : int {
// This may take a long time.
\core_php_time_limit::raise();
$path = $this->core->fs->getTmpPath();
$this->core->h5pF->getUploadedH5pFolderPath($path);
// Add manually the extension to the file to avoid the validation fails.
$path .= '.h5p';
$this->core->h5pF->getUploadedH5pPath($path);
// Copy the .h5p file to the temporary folder.
$file->copy_content_to($path);
// Check if the h5p file is valid before saving it.
$h5pvalidator = \core_h5p\framework::instance('validator');
if ($h5pvalidator->isValidPackage(false, false)) {
$h5pstorage = \core_h5p\framework::instance('storage');
$options = ['disable' => $this->get_display_options($config)];
$content = [
'pathnamehash' => $file->get_pathnamehash(),
'contenthash' => $file->get_contenthash(),
];
$h5pstorage->savePackage($content, null, false, $options);
return $h5pstorage->contentId;
}
return false;
}
/**
* Get the representation of display options as int.
* @param stdClass $config Button options config.
*
* @return int The representation of display options as int.
*/
private function get_display_options(\stdClass $config) : int {
$export = isset($config->export) ? $config->export : 0;
$embed = isset($config->embed) ? $config->embed : 0;
$copyright = isset($config->copyright) ? $config->copyright : 0;
$frame = ($export || $embed || $copyright);
if (!$frame) {
$frame = isset($config->frame) ? $config->frame : 0;
}
$disableoptions = [
\H5PCore::DISPLAY_OPTION_FRAME => $frame,
\H5PCore::DISPLAY_OPTION_DOWNLOAD => $export,
\H5PCore::DISPLAY_OPTION_EMBED => $embed,
\H5PCore::DISPLAY_OPTION_COPYRIGHT => $copyright,
];
return $this->core->getStorableDisplayOptions($disableoptions, 0);
}
/**
* Delete an H5P package.
*
* @param stdClass $content The H5P package to delete.
*/
private function delete_h5p(\stdClass $content) {
$h5pstorage = \core_h5p\framework::instance('storage');
// Add an empty slug to the content if it's not defined, because the H5P library requires this field exists.
// It's not used when deleting a package, so the real slug value is not required at this point.
$content->slug = $content->slug ?? '';
$h5pstorage->deletePackage( (array) $content);
}
/**
* Export path for settings
*
* @param bool $downloadenabled Whether the option to export the H5P content is enabled.
*
* @return string The URL of the exported file.
*/
private function get_export_settings(bool $downloadenabled) : string {
if ( ! $downloadenabled) {
return '';
}
$systemcontext = \context_system::instance();
$slug = $this->content['slug'] ? $this->content['slug'] . '-' : '';
$url = \moodle_url::make_pluginfile_url(
$systemcontext->id,
\core_h5p\file_storage::COMPONENT,
\core_h5p\file_storage::EXPORT_FILEAREA,
'',
'',
"{$slug}{$this->content['id']}.h5p"
);
return $url->out();
}
/**
* Get a query string with the theme revision number to include at the end
* of URLs. This is used to force the browser to reload the asset when the
* theme caches are cleared.
*
* @return string
*/
private function get_cache_buster() : string {
global $CFG;
return '?ver=' . $CFG->themerev;
}
/**
* Get the identifier for the H5P content, to be used in the arrays as index.
*
* @return string The identifier.
*/
private function get_cid() : string {
return 'cid-' . $this->h5pid;
}
/**
* Get the core H5P assets, including all core H5P JavaScript and CSS.
*
* @return Array core H5P assets.
*/
private function get_assets() : array {
global $CFG;
// Get core settings.
$settings = $this->get_core_settings();
$settings['core'] = [
'styles' => [],
'scripts' => []
];
$settings['loadedJs'] = [];
$settings['loadedCss'] = [];
// Make sure files are reloaded for each plugin update.
$cachebuster = $this->get_cache_buster();
// Use relative URL to support both http and https.
$liburl = $CFG->wwwroot . '/lib/h5p/';
$relpath = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $liburl);
// Add core stylesheets.
foreach (\H5PCore::$styles as $style) {
$settings['core']['styles'][] = $relpath . $style . $cachebuster;
$this->cssrequires[] = new \moodle_url($liburl . $style . $cachebuster);
}
// Add core JavaScript.
foreach (\H5PCore::$scripts as $script) {
$settings['core']['scripts'][] = $relpath . $script . $cachebuster;
$this->jsrequires[] = new \moodle_url($liburl . $script . $cachebuster);
}
$cid = $this->get_cid();
// The filterParameters function should be called before getting the dependencyfiles because it rebuild content
// dependency cache and export file.
$settings['contents'][$cid]['jsonContent'] = $this->core->filterParameters($this->content);
$files = $this->get_dependency_files();
if ($this->embedtype === 'div') {
$systemcontext = \context_system::instance();
$h5ppath = "/pluginfile.php/{$systemcontext->id}/core_h5p";
// Schedule JavaScripts for loading through Moodle.
foreach ($files['scripts'] as $script) {
$url = $script->path . $script->version;
// Add URL prefix if not external.
$isexternal = strpos($script->path, '://');
if ($isexternal === false) {
$url = $h5ppath . $url;
}
$settings['loadedJs'][] = $url;
$this->jsrequires[] = new \moodle_url($isexternal ? $url : $CFG->wwwroot . $url);
}
// Schedule stylesheets for loading through Moodle.
foreach ($files['styles'] as $style) {
$url = $style->path . $style->version;
// Add URL prefix if not external.
$isexternal = strpos($style->path, '://');
if ($isexternal === false) {
$url = $h5ppath . $url;
}
$settings['loadedCss'][] = $url;
$this->cssrequires[] = new \moodle_url($isexternal ? $url : $CFG->wwwroot . $url);
}
} else {
// JavaScripts and stylesheets will be loaded through h5p.js.
$settings['contents'][$cid]['scripts'] = $this->core->getAssetsUrls($files['scripts']);
$settings['contents'][$cid]['styles'] = $this->core->getAssetsUrls($files['styles']);
}
return $settings;
}
/**
* Get the settings needed by the H5P library.
*
* @return array The settings.
*/
private function get_core_settings() : array {
global $CFG;
$basepath = $CFG->wwwroot . '/';
$systemcontext = \context_system::instance();
// Generate AJAX paths.
$ajaxpaths = [];
$ajaxpaths['xAPIResult'] = '';
$ajaxpaths['contentUserData'] = '';
$settings = array(
'baseUrl' => $basepath,
'url' => "{$basepath}pluginfile.php/{$systemcontext->instanceid}/core_h5p",
'urlLibraries' => "{$basepath}pluginfile.php/{$systemcontext->id}/core_h5p/libraries",
'postUserStatistics' => false,
'ajax' => $ajaxpaths,
'saveFreq' => false,
'siteUrl' => $CFG->wwwroot,
'l10n' => array('H5P' => $this->core->getLocalization()),
'user' => [],
'hubIsEnabled' => false,
'reportingIsEnabled' => false,
'crossorigin' => null,
'libraryConfig' => $this->core->h5pF->getLibraryConfig(),
'pluginCacheBuster' => $this->get_cache_buster(),
'libraryUrl' => $basepath . 'lib/h5p/js',
);
return $settings;
}
/**
* Finds library dependencies of view
*
* @return array Files that the view has dependencies to
*/
private function get_dependency_files() : array {
$preloadeddeps = $this->core->loadContentDependencies($this->h5pid, 'preloaded');
$files = $this->core->getDependenciesFiles($preloadeddeps);
return $files;
}
/**
* Resizing script for settings
*
* @return string The HTML code with the resize script.
*/
private function get_resize_code() : string {
global $OUTPUT;
$template = new \stdClass();
$template->resizeurl = new \moodle_url('/lib/h5p/js/h5p-resizer.js');
return $OUTPUT->render_from_template('core_h5p/h5presize', $template);
}
/**
* Embed code for settings
*
* @param string $url The URL of the .h5p file.
* @param bool $embedenabled Whether the option to embed the H5P content is enabled.
*
* @return string The HTML code to reuse this H5P content in a different place.
*/
private function get_embed_code(string $url, bool $embedenabled) : string {
global $OUTPUT;
if ( ! $embedenabled) {
return '';
}
$template = new \stdClass();
$template->embedurl = self::get_embed_url($url)->out();
return $OUTPUT->render_from_template('core_h5p/h5pembed', $template);
}
/**
* Get the encoded URL for embeding this H5P content.
* @param string $url The URL of the .h5p file.
*
* @return \moodle_url The embed URL.
*/
public static function get_embed_url(string $url) : \moodle_url {
return new \moodle_url('/h5p/embed.php', ['url' => $url]);
}
}

93
h5p/embed.php Normal file
View File

@ -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();

155
h5p/js/embed.js Normal file
View File

@ -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');
};

112
h5p/lib.php Normal file
View File

@ -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;
}

14
h5p/pix/icon.svg Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 23.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 345 150" style="enable-background:new 0 0 345 150;" xml:space="preserve">
<g>
<path d="M325.7,14.7C317.6,6.9,305.3,3,289,3h-43.5H234v31h-66l-5.4,22.2c4.5-2.1,10.9-4.2,15.3-5.3c4.4-1.1,8.8-0.9,13.1-0.9
c14.6,0,26.5,4.5,35.6,13.3c9.1,8.8,13.6,20,13.6,33.4c0,9.4-2.3,18.5-7,27.2s-11.3,15.4-19.9,20c-3.1,1.6-6.5,3.1-10.2,4.1h42.4
H259V95h25c18.2,0,31.7-4.2,40.6-12.5s13.3-19.9,13.3-34.6C337.9,33.6,333.8,22.5,325.7,14.7z M288.7,60.6c-3.5,3-9.6,4.4-18.3,4.4
H259V33h13.2c8.4,0,14.2,1.5,17.2,4.7c3.1,3.2,4.6,6.9,4.6,11.5C294,53.9,292.2,57.6,288.7,60.6z"/>
<path d="M176.5,76.3c-7.9,0-14.7,4.6-18,11.2L119,81.9L136.8,3h-23.6H101v62H51V3H7v145h44V95h50v53h12.2h42
c-6.7-2-12.5-4.6-17.2-8.1c-4.8-3.6-8.7-7.7-11.7-12.3c-3-4.6-5.3-9.7-7.3-16.5l39.6-5.7c3.3,6.6,10.1,11.1,17.9,11.1
c11.1,0,20.1-9,20.1-20.1S187.5,76.3,176.5,76.3z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,34 @@
{{!
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/>.
}}
{{!
@template core_message/h5pdiv
This template will render an div for h5p content.
Variables required for this template:
* h5pid - The database id for the H5P content
Example context (json):
{
"h5pid": 123
}
}}
<div class="h5p-content-wrapper">
<div class="h5p-content" data-content-id="{{h5pid}}"></div>
</div>

View File

@ -0,0 +1,32 @@
{{!
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/>.
}}
{{!
@template core_message/h5pembed
This template will render the embed code shown in the H5P content embed popup.
Variables required for this template:
* embedurl - The URL with the H5P file to embed
Example context (json):
{
"embedurl": "http://example.com/h5p/embed.php?url=testurl"
}
}}
<iframe src="{{embedurl}}" width=":w" height=":h" allowfullscreen="allowfullscreen"></iframe>

View File

@ -0,0 +1,51 @@
{{!
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/>.
}}
{{!
@template core_message/h5perror
This template will render the embed code shown in the H5P content embed popup.
Variables required for this template:
* h5picon - The icon
* message - The error message to display.
Example context (json):
{
"embedurl": "http://example.com/h5p/embed.php?url=testurl"
}
}}
<div class="d-flex h-100 position-relative align-items-center bg-light h5pmessages">
<div class="position-absolute py-2 bg-secondary" style="top: 0px; left: 0px; right: 0px;">
<div class="container">
<img src="{{{h5picon}}}" class="h5picon" alt="{{#str}}h5p, core_h5p{{/str}}" style="width: 50px; height: auto; opacity: 0.5">
</div>
</div>
<div class="container mt-5">
{{#exception}}
<div class="alert alert-block fade in alert-danger my-2" role="alert">
{{{ exception }}}
</div>
{{/exception}}
{{#error}}
<div class="alert alert-block fade in alert-warning my-2" role="alert">
{{{code}}} : {{{ message }}}
</div>
{{/error}}
</div>
</div>

View File

@ -0,0 +1,35 @@
{{!
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/>.
}}
{{!
@template core_h5p/h5piframe
This template will render an iframe for h5p content.
Variables required for this template:
* h5pid - The database id for the H5P content
Example context (json):
{
"h5pid": 123
}
}}
<div class="h5p-iframe-wrapper">
<iframe id="h5p-iframe-{{h5pid}}" class="h5p-iframe" data-content-id="{{h5pid}}"
style="height:1px; min-width: 100%" src="about:blank">
</iframe>
</div>

View File

@ -0,0 +1,32 @@
{{!
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/>.
}}
{{!
@template core_message/h5presize
This template will render the resize JS code.
Variables required for this template:
* resizeurl - The database id for the H5P content
Example context (json):
{
"resizeurl": "http://example.com/lib/h5p/js/h5p-resizer.js"
}
}}
<script src="{{resizeurl}}"></script>

View File

@ -20,5 +20,5 @@
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['privacy:metadata'] = 'H5P subsystem does not store any personal data.';
$string['h5pfilenotfound'] = 'H5P file not found';