Skip to content

Commit

Permalink
MDL-66609 core_h5p: Make use of upstream change for getting itemid
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Oct 29, 2019
1 parent 6faafc0 commit 9e67f5e
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 17 deletions.
113 changes: 113 additions & 0 deletions h5p/classes/core.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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 <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_h5p;

use H5PCore;
use stdClass;
use moodle_url;

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

/**
* H5P player class, for displaying any local H5P content.
*
* @package core_h5p
* @copyright 2019 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core extends \H5PCore {

protected $libraries;

protected function getDependencyPath(array $dependency): string {
$library = $this->find_library($dependency);

return "libraries/{$library->id}/{$library->machinename}-{$library->majorversion}.{$library->minorversion}";
}

public function get_dependency_roots(int $id): array {
$roots = [];
$dependencies = $this->h5pF->loadContentDependencies($id);
$context = \context_system::instance();
foreach ($dependencies as $dependency) {
$library = $this->find_library($dependency);
$roots[self::libraryToString($dependency, true)] = (moodle_url::make_pluginfile_url(
$context->id,
'core_h5p',
'libraries',
$library->id,
"/" . self::libraryToString($dependency, true),
''
))->out(false);
}

return $roots;
}

protected function find_library($dependency): \stdClass {
global $DB;
if (null === $this->libraries) {
$this->libraries = $DB->get_records('h5p_libraries');
}

$major = $dependency['majorVersion'];
$minor = $dependency['minorVersion'];
$patch = $dependency['patchVersion'];

foreach ($this->libraries as $library) {
if ($library->machinename !== $dependency['machineName']) {
continue;
}

if ($library->majorversion != $major) {
continue;
}
if ($library->minorversion != $minor) {
continue;
}
if ($library->patchversion != $patch) {
continue;
}

return $library;
}

return null;
}

public static function get_scripts(): array {
global $CFG;
$cachebuster = '?ver='.$CFG->jsrev;
$liburl = $CFG->wwwroot . '/lib/h5p/';
$urls = [];

foreach (self::$scripts as $script) {
$urls[] = new moodle_url($liburl . $script . $cachebuster);
}
$urls[] = new moodle_url("/h5p/js/h5p_overrides.js");

return $urls;
}
}
5 changes: 3 additions & 2 deletions h5p/classes/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,12 @@ private function get_file_elements_from_filepath(string $filepath): array {
}
// Get the filearea.
$filearea = array_shift($sections);
$itemid = array_shift($sections);
// Get the filepath.
$filepath = implode('/', $sections);
$filepath = '/' . $filepath . '/';

return ['filearea' => $filearea, 'filepath' => $filepath, 'filename' => $filename];
return ['filearea' => $filearea, 'filepath' => $filepath, 'filename' => $filename, 'itemid' => $itemid];
}

/**
Expand All @@ -620,4 +621,4 @@ private function get_itemid_for_file(string $filearea, string $filepath, string
return $DB->get_field('files', 'itemid', ['component' => self::COMPONENT, 'filearea' => $filearea, 'filepath' => $filepath,
'filename' => $filename]);
}
}
}
3 changes: 2 additions & 1 deletion h5p/classes/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,8 @@ public static function instance($type = null) {
$context = \context_system::instance();
$url = "{$CFG->wwwroot}/pluginfile.php/{$context->id}/core_h5p";

$core = new \H5PCore($interface, $fs, $url, $language, true);
require_once("{$CFG->libdir}/h5p/h5p.classes.php");
$core = new core($interface, $fs, $url, $language, true);
$core->aggregateAssets = !(isset($CFG->core_h5p_aggregate_assets) && $CFG->core_h5p_aggregate_assets === '0');
}

Expand Down
29 changes: 15 additions & 14 deletions h5p/classes/player.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class player {
private $url;

/**
* @var \H5PCore The H5PCore object.
* @var core The H5PCore object.
*/
private $core;

Expand Down Expand Up @@ -96,7 +96,7 @@ public function __construct(string $url, \stdClass $config) {
$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']);
$this->embedtype = core::determineEmbedType($this->content['embedType'], $this->content['library']['embedTypes']);
}
}

Expand Down Expand Up @@ -125,18 +125,18 @@ public function add_assets_to_page() {
$cid = $this->get_cid();
$systemcontext = \context_system::instance();

$disable = array_key_exists('disable', $this->content) ? $this->content['disable'] : \H5PCore::DISABLE_NONE;
$disable = array_key_exists('disable', $this->content) ? $this->content['disable'] : core::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']),
'library' => core::libraryToString($this->content['library']),
'fullScreen' => $this->content['library']['fullscreen'],
'exportUrl' => $this->get_export_settings($displayoptions[ \H5PCore::DISPLAY_OPTION_DOWNLOAD ]),
'exportUrl' => $this->get_export_settings($displayoptions[ core::DISPLAY_OPTION_DOWNLOAD ]),
'embedCode' => $this->get_embed_code($this->url->out(),
$displayoptions[ \H5PCore::DISPLAY_OPTION_EMBED ]),
$displayoptions[ core::DISPLAY_OPTION_EMBED ]),
'resizeCode' => $this->get_resize_code(),
'title' => $this->content['slug'],
'displayOptions' => $displayoptions,
Expand Down Expand Up @@ -401,10 +401,10 @@ private function get_display_options(\stdClass $config) : int {
}

$disableoptions = [
\H5PCore::DISPLAY_OPTION_FRAME => $frame,
\H5PCore::DISPLAY_OPTION_DOWNLOAD => $export,
\H5PCore::DISPLAY_OPTION_EMBED => $embed,
\H5PCore::DISPLAY_OPTION_COPYRIGHT => $copyright,
core::DISPLAY_OPTION_FRAME => $frame,
core::DISPLAY_OPTION_DOWNLOAD => $export,
core::DISPLAY_OPTION_EMBED => $embed,
core::DISPLAY_OPTION_COPYRIGHT => $copyright,
];

return $this->core->getStorableDisplayOptions($disableoptions, 0);
Expand Down Expand Up @@ -496,14 +496,14 @@ private function get_assets() : array {
$relpath = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $liburl);

// Add core stylesheets.
foreach (\H5PCore::$styles as $style) {
foreach (core::$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);
foreach (core::get_scripts() as $script) {
$settings['core']['scripts'][] = $script->out(false);
$this->jsrequires[] = $script;
}

$cid = $this->get_cid();
Expand Down Expand Up @@ -582,6 +582,7 @@ private function get_core_settings() : array {
'libraryConfig' => $this->core->h5pF->getLibraryConfig(),
'pluginCacheBuster' => $this->get_cache_buster(),
'libraryUrl' => $basepath . 'lib/h5p/js',
'moodleLibraryPaths' => $this->core->get_dependency_roots($this->h5pid),
);

return $settings;
Expand Down
9 changes: 9 additions & 0 deletions h5p/js/h5p_overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
H5P._getLibraryPath = H5P.getLibraryPath;
H5P.getLibraryPath = function (library) {
if (H5PIntegration.moodleLibraryPaths) {
if (H5PIntegration.moodleLibraryPaths[library]) {
return H5PIntegration.moodleLibraryPaths[library];
}
}
return H5P._getLibraryPath(library);
};

0 comments on commit 9e67f5e

Please sign in to comment.