From 4501c3704582dabe56c5bc0def1eb768da367130 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 4 Sep 2020 18:53:32 +0200 Subject: [PATCH] MDL-70759 core_h5p: add renderer to let personalize styles This is a backport of MDL-69087 Existing mod_hvp pluging has a renderer to let Moodle instances alter styles, Javascript, semantics (fields in the editor) and content: https://github.com/h5p/moodle-mod_hvp/blob/stable/renderer.php The approach for core_h5p is exactly the same, to let people to reuse their existing code. --- h5p/ajax.php | 3 +++ h5p/classes/editor_framework.php | 17 ++++++++++++++- h5p/classes/framework.php | 16 +++----------- h5p/classes/output/renderer.php | 47 +++++++++++++++++++++++++++++++++++++++- h5p/classes/player.php | 31 +++++++++++++++++++++++++- h5p/tests/framework_test.php | 4 ++-- 6 files changed, 100 insertions(+), 18 deletions(-) diff --git a/h5p/ajax.php b/h5p/ajax.php index 1547b6f346c..b5d7638be00 100644 --- a/h5p/ajax.php +++ b/h5p/ajax.php @@ -44,6 +44,9 @@ $action = required_param('action', PARAM_ALPHA); $factory = new factory(); $editor = $factory->get_editor(); +// Set context to default system context. +$PAGE->set_context(null); + switch ($action) { // Load list of libraries or details for library. case 'libraries': diff --git a/h5p/classes/editor_framework.php b/h5p/classes/editor_framework.php index 8d7f9b4ec48..8f97a36a89e 100644 --- a/h5p/classes/editor_framework.php +++ b/h5p/classes/editor_framework.php @@ -265,7 +265,22 @@ class editor_framework implements H5peditorStorage { * minorVersion as properties. */ public function alterLibraryFiles(&$files, $libraries): void { - // This is to be implemented when the renderer is used. + global $PAGE; + + // Refactor dependency list. + $librarylist = []; + foreach ($libraries as $dependency) { + $librarylist[$dependency['machineName']] = [ + 'majorVersion' => $dependency['majorVersion'], + 'minorVersion' => $dependency['minorVersion'] + ]; + } + + $renderer = $PAGE->get_renderer('core_h5p'); + + $embedtype = 'editor'; + $renderer->h5p_alter_scripts($files['scripts'], $librarylist, $embedtype); + $renderer->h5p_alter_styles($files['styles'], $librarylist, $embedtype); } /** diff --git a/h5p/classes/framework.php b/h5p/classes/framework.php index 6d9d9c83aa5..25d0ad9650d 100644 --- a/h5p/classes/framework.php +++ b/h5p/classes/framework.php @@ -1093,20 +1093,10 @@ class framework implements \H5PFrameworkInterface { * @param int $minorversion The library's minor version */ public function alterLibrarySemantics(&$semantics, $name, $majorversion, $minorversion) { - global $DB; + global $PAGE; - $library = $DB->get_record('h5p_libraries', - array( - 'machinename' => $name, - 'majorversion' => $majorversion, - 'minorversion' => $minorversion, - ) - ); - - if ($library) { - $library->semantics = json_encode($semantics); - $DB->update_record('h5p_libraries', $library); - } + $renderer = $PAGE->get_renderer('core_h5p'); + $renderer->h5p_alter_semantics($semantics, $name, $majorversion, $minorversion); } /** diff --git a/h5p/classes/output/renderer.php b/h5p/classes/output/renderer.php index e5720c5d25c..0d25f2a92e2 100644 --- a/h5p/classes/output/renderer.php +++ b/h5p/classes/output/renderer.php @@ -37,4 +37,49 @@ use plugin_renderer_base; */ class renderer extends plugin_renderer_base { -} \ No newline at end of file + /** + * Alter which stylesheets are loaded for H5P. + * This is useful for adding custom styles or replacing existing ones. + * + * @param array|object $scripts List of stylesheets that will be loaded + * @param array $libraries Array of libraries indexed by the library's machineName + * @param string $embedtype Possible values: div, iframe, external, editor + */ + public function h5p_alter_styles(&$scripts, array $libraries, string $embedtype) { + } + + /** + * Alter which scripts are loaded for H5P. + * This is useful for adding custom scripts or replacing existing ones. + * + * @param array|object $scripts List of JavaScripts that will be loaded + * @param array $libraries Array of libraries indexed by the library's machineName + * @param string $embedtype Possible values: div, iframe, external, editor + */ + public function h5p_alter_scripts(&$scripts, array $libraries, string $embedtype) { + } + + /** + * Alter semantics before they are processed. This is useful for changing + * how the editor looks and how content parameters are filtered. + * + * @param object|object $semantics Semantics as object + * @param string $name Machine name of library + * @param int $majorversion Major version of library + * @param int $minorversion Minor version of library + */ + public function h5p_alter_semantics(&$semantics, $name, $majorversion, $minorversion) { + } + + /** + * Alter parameters of H5P content after it has been filtered through semantics. + * This is useful for adapting the content to the current context. + * + * @param array|object $parameters The content parameters for the library + * @param string $name The machine readable name of the library + * @param int $majorversion Major version of the library + * @param int $minorversion Minor version of the library + */ + public function h5p_alter_filtered_parameters(&$parameters, string $name, int $majorversion, int $minorversion) { + } +} diff --git a/h5p/classes/player.php b/h5p/classes/player.php index 6c5151bd199..dfd6041cb59 100644 --- a/h5p/classes/player.php +++ b/h5p/classes/player.php @@ -361,7 +361,7 @@ class player { $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); + $settings['contents'][$cid]['jsonContent'] = $this->get_filtered_parameters(); $files = $this->get_dependency_files(); if ($this->embedtype === 'div') { @@ -403,14 +403,43 @@ class player { } /** + * Get filtered parameters, modifying them by the renderer if the theme implements the h5p_alter_filtered_parameters function. + * + * @return string Filtered parameters. + */ + private function get_filtered_parameters(): string { + global $PAGE; + + $safeparams = $this->core->filterParameters($this->content); + $decodedparams = json_decode($safeparams); + $h5poutput = $PAGE->get_renderer('core_h5p'); + $h5poutput->h5p_alter_filtered_parameters( + $decodedparams, + $this->content['library']['name'], + $this->content['library']['majorVersion'], + $this->content['library']['minorVersion'] + ); + $safeparams = json_encode($decodedparams); + + return $safeparams; + } + + /** * Finds library dependencies of view * * @return array Files that the view has dependencies to */ private function get_dependency_files(): array { + global $PAGE; + $preloadeddeps = $this->core->loadContentDependencies($this->h5pid, 'preloaded'); $files = $this->core->getDependenciesFiles($preloadeddeps); + // Add additional asset files if required. + $h5poutput = $PAGE->get_renderer('core_h5p'); + $h5poutput->h5p_alter_scripts($files['scripts'], $preloadeddeps, $this->embedtype); + $h5poutput->h5p_alter_styles($files['styles'], $preloadeddeps, $this->embedtype); + return $files; } diff --git a/h5p/tests/framework_test.php b/h5p/tests/framework_test.php index 5dd94ca83f3..cee8266744b 100644 --- a/h5p/tests/framework_test.php +++ b/h5p/tests/framework_test.php @@ -1460,8 +1460,8 @@ class framework_testcase extends \advanced_testcase { // Get the semantics of 'Library1' from the DB. $currentsemantics = $DB->get_field('h5p_libraries', 'semantics', array('id' => $library1->id)); - // The semantics for Library1 should be successfully updated. - $this->assertEquals(json_encode($updatedsemantics), $currentsemantics); + // The semantics for Library1 shouldn't be updated. + $this->assertEquals($semantics, $currentsemantics); } /** -- 2.11.4.GIT