Skip to content

Commit

Permalink
Merge branch 'MDL-67063-master' of git://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 11, 2020
2 parents e55d14e + 08fda3e commit 63e821f
Show file tree
Hide file tree
Showing 79 changed files with 629 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cache/stores/mongodb/MongoDB/
enrol/lti/ims-blti/
filter/algebra/AlgParser.pm
filter/tex/mimetex.*
h5p/h5plib/v124/joubel/core/
lib/editor/atto/plugins/html/yui/src/codemirror/
lib/editor/atto/plugins/html/yui/src/beautify/
lib/editor/atto/yui/src/rangy/js/*.*
Expand Down Expand Up @@ -63,7 +64,6 @@ lib/php-jwt/
lib/babel-polyfill/
lib/mdn-polyfills/
lib/emoji-data/
lib/h5p/
media/player/videojs/amd/src/video-lazy.js
media/player/videojs/amd/src/Youtube-lazy.js
media/player/videojs/videojs/
Expand Down
2 changes: 1 addition & 1 deletion .stylelintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cache/stores/mongodb/MongoDB/
enrol/lti/ims-blti/
filter/algebra/AlgParser.pm
filter/tex/mimetex.*
h5p/h5plib/v124/joubel/core/
lib/editor/atto/plugins/html/yui/src/codemirror/
lib/editor/atto/plugins/html/yui/src/beautify/
lib/editor/atto/yui/src/rangy/js/*.*
Expand Down Expand Up @@ -64,7 +65,6 @@ lib/php-jwt/
lib/babel-polyfill/
lib/mdn-polyfills/
lib/emoji-data/
lib/h5p/
media/player/videojs/amd/src/video-lazy.js
media/player/videojs/amd/src/Youtube-lazy.js
media/player/videojs/videojs/
Expand Down
17 changes: 15 additions & 2 deletions admin/settings/h5p.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@

defined('MOODLE_INTERNAL') || die();

// Settings page.
// H5P overview.
$ADMIN->add('h5p', new admin_externalpage('h5poverview', get_string('h5poverview', 'core_h5p'),
new moodle_url('/h5p/overview.php'), ['moodle/site:config']));
$ADMIN->add('h5p', new admin_externalpage('h5psettings', get_string('h5pmanage', 'core_h5p'),

// Manage H5P libraries page.
$ADMIN->add('h5p', new admin_externalpage('h5pmanagelibraries', get_string('h5pmanage', 'core_h5p'),
new moodle_url('/h5p/libraries.php'), ['moodle/site:config', 'moodle/h5p:updatelibraries']));

// H5P settings.
$defaulth5plib = \core_h5p\local\library\autoloader::get_default_handler();
if (!empty($defaulth5plib)) {
// As for now this page only has this setting, it will be hidden if there isn't any H5P libraries handler defined.
$settings = new admin_settingpage('h5psettings', new lang_string('h5psettings', 'core_h5p'));
$ADMIN->add('h5p', $settings);

$settings->add(new admin_settings_h5plib_handler_select('h5plibraryhandler', new lang_string('h5plibraryhandler', 'core_h5p'),
new lang_string('h5plibraryhandler_help', 'core_h5p'), $defaulth5plib));
}
4 changes: 3 additions & 1 deletion filter/displayh5p/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

defined('MOODLE_INTERNAL') || die;

use core_h5p\local\library\autoloader;

/**
* Display H5P filter
*
Expand Down Expand Up @@ -177,7 +179,7 @@ public function filterobject_prepare_replacement_callback($tagbegin, $tagend, $u

// We want to request the resizing script only once.
if (self::$loadresizerjs) {
$resizerurl = new moodle_url('/lib/h5p/js/h5p-resizer.js');
$resizerurl = autoloader::get_h5p_core_library_url('js/h5p-resizer.js');
$tagend .= '<script src="' . $resizerurl->out() . '"></script>';
self::$loadresizerjs = false;
}
Expand Down
60 changes: 0 additions & 60 deletions h5p/classes/autoloader.php

This file was deleted.

19 changes: 12 additions & 7 deletions h5p/classes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use H5PFrameworkInterface;
use stdClass;
use moodle_url;
use core_h5p\local\library\autoloader;

/**
* H5P core class, containing functions and storage shared by the other H5P classes.
Expand Down Expand Up @@ -138,20 +139,24 @@ protected function find_library(array $dependency): ?\stdClass {
}

/**
* Get core JavaScript files.
* Get the list of JS scripts to include on the page.
*
* @return array The array containg urls of the core JavaScript files
*/
public static function get_scripts(): array {
global $CFG;
$cachebuster = '?ver='.$CFG->jsrev;
$liburl = $CFG->wwwroot . '/lib/h5p/';
$urls = [];
global $PAGE;

$factory = new factory();
$jsrev = $PAGE->requires->get_jsrev();
$urls = [];
foreach (self::$scripts as $script) {
$urls[] = new moodle_url($liburl . $script . $cachebuster);
$urls[] = autoloader::get_h5p_core_library_url($script, [
'ver' => $jsrev,
]);
}
$urls[] = new moodle_url("/h5p/js/h5p_overrides.js");
$urls[] = new moodle_url("/h5p/js/h5p_overrides.js", [
'ver' => $jsrev,
]);

return $urls;
}
Expand Down
24 changes: 19 additions & 5 deletions h5p/classes/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@

defined('MOODLE_INTERNAL') || die();

use \core_h5p\framework as framework;
use \core_h5p\core as core;
use \H5PStorage as storage;
use \H5PValidator as validator;
use \H5PContentValidator as content_validator;
use core_h5p\local\library\autoloader;
use core_h5p\framework;
use core_h5p\core;
use H5PStorage as storage;
use H5PValidator as validator;
use H5PContentValidator as content_validator;

/**
* H5P factory class.
Expand All @@ -43,6 +44,9 @@
*/
class factory {

/** @var \core_h5p\local\library\autoloader The autoloader */
protected $autoloader;

/** @var \core_h5p\core The Moodle H5PCore implementation */
protected $core;

Expand All @@ -63,9 +67,19 @@ class factory {
*/
public function __construct() {
// Loading classes we need from H5P third party library.
$this->autoloader = new autoloader();
autoloader::register();
}

/**
* Returns an instance of the \core_h5p\local\library\autoloader class.
*
* @return \core_h5p\local\library\autoloader
*/
public function get_autoloader(): autoloader {
return $this->autoloader;
}

/**
* Returns an instance of the \core_h5p\framework class.
*
Expand Down
123 changes: 123 additions & 0 deletions h5p/classes/local/library/autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?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 autoloader management class.
*
* @package core_h5p
* @copyright 2019 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_h5p\local\library;

defined('MOODLE_INTERNAL') || die();

/**
* H5P autoloader management class.
*
* @package core_h5p
* @copyright 2019 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class autoloader {

/**
* Returns the list of plugins that can work as H5P library handlers (have class PLUGINNAME\local\library\handler)
* @return array with the format: pluginname => class
*/
public static function get_all_handlers(): array {
$handlers = [];
foreach (\core_component::get_plugin_types() as $ptype => $unused) {
$plugins = \core_component::get_plugin_list_with_class($ptype, 'local\library\handler') +
\core_component::get_plugin_list_with_class($ptype, 'local_library_handler');
// Allow plugins to have the class either with namespace or without (useful for unittest).
foreach ($plugins as $pname => $class) {
$handlers[$pname] = $class;
}
}

return $handlers;
}

/**
* Returns the default H5P library handler.
* @return string|null H5P library handler class
*/
public static function get_default_handler(): ?string {
$default = null;
$handlers = self::get_all_handlers();
if (!empty($handlers)) {
// The default handler will be the first in the list.
$default = array_shift($handlers);
}

return $default;
}

/**
* Returns the current H5P library handler class.
*
* @return string H5P library handler class
* @throws \moodle_exception
*/
public static function get_handler_classname(): string {
global $CFG;

$handlers = self::get_all_handlers();
if (!empty($CFG->h5plibraryhandler)) {
if (isset($handlers[$CFG->h5plibraryhandler])) {
return $handlers[$CFG->h5plibraryhandler];
}
}

// If no handler has been defined or it doesn't exist, return the default one.
$defaulthandler = self::get_default_handler();
if (empty($defaulthandler)) {
// If there is no default handler, throw an exception.
throw new \moodle_exception('noh5plibhandlerdefined', 'core_h5p');
}

return $defaulthandler;
}

/**
* Get the current version of the H5P core library.
*
* @return string
*/
public static function get_h5p_version(): string {
return component_class_callback(self::get_handler_classname(), 'get_h5p_version', []);
}

/**
* Get a URL for the current H5P Core Library.
*
* @param string $filepath The path within the h5p root
* @param array $params these params override current params or add new
* @return null|moodle_url
*/
public static function get_h5p_core_library_url(?string $filepath = null, ?array $params = null): ?\moodle_url {
return component_class_callback(self::get_handler_classname(), 'get_h5p_core_library_url', [$filepath, $params]);
}

/**
* Register the H5P autoloader.
*/
public static function register(): void {
component_class_callback(self::get_handler_classname(), 'register', []);
}
}
Loading

0 comments on commit 63e821f

Please sign in to comment.