From fde9d44286adba92715512213603ffdc3ad11892 Mon Sep 17 00:00:00 2001 From: Victor Deniz Falcon Date: Thu, 6 Aug 2020 12:20:03 +0100 Subject: [PATCH] MDL-68921 core_h5p: Add H5P libraries metadata settings An upgrade step updates installed H5P libraries --- h5p/classes/api.php | 1 + h5p/classes/editor_framework.php | 5 ++-- h5p/classes/framework.php | 4 ++++ h5p/tests/generator_test.php | 1 + lib/db/install.xml | 5 ++-- lib/db/upgrade.php | 41 ++++++++++++++++++++++++++++++++ version.php | 2 +- 7 files changed, 54 insertions(+), 5 deletions(-) diff --git a/h5p/classes/api.php b/h5p/classes/api.php index 6cd3484fc6182..cab34764a1a91 100644 --- a/h5p/classes/api.php +++ b/h5p/classes/api.php @@ -164,6 +164,7 @@ public static function get_contenttype_libraries(?string $fields = ''): array { unset($library->major_version); $library->minorVersion = (int) $library->minorversion; unset($library->minorversion); + $library->metadataSettings = json_decode($library->metadatasettings); // If we already add this library means that it is an old version,as the previous query was sorted by version. if (isset($added[$library->name])) { diff --git a/h5p/classes/editor_framework.php b/h5p/classes/editor_framework.php index c4b575c3951d3..8d7f9b4ec48db 100644 --- a/h5p/classes/editor_framework.php +++ b/h5p/classes/editor_framework.php @@ -228,7 +228,7 @@ public function getLibraries($libraries = null): ?array { if ($libraries !== null) { // Get details for the specified libraries. $librariesin = []; - $fields = 'title, runnable'; + $fields = 'title, runnable, metadatasettings'; foreach ($libraries as $library) { $params = [ @@ -242,11 +242,12 @@ public function getLibraries($libraries = null): ?array { if ($details) { $library->title = $details->title; $library->runnable = $details->runnable; + $library->metadataSettings = json_decode($details->metadatasettings); $librariesin[] = $library; } } } else { - $fields = 'id, machinename as name, title, majorversion, minorversion'; + $fields = 'id, machinename as name, title, majorversion, minorversion, metadatasettings'; $librariesin = api::get_contenttype_libraries($fields); } diff --git a/h5p/classes/framework.php b/h5p/classes/framework.php index 2e4a2cad26983..4b0542280abb7 100644 --- a/h5p/classes/framework.php +++ b/h5p/classes/framework.php @@ -685,6 +685,9 @@ public function set_file(\stored_file $file): void { * - dropLibraryCss(optional): list of associative arrays containing: * - machineName: machine name for the librarys that are to drop their css * - semantics(optional): Json describing the content structure for the library + * - metadataSettings(optional): object containing: + * - disable: 1 if metadata is disabled completely + * - disableExtraTitleField: 1 if the title field is hidden in the form * @param bool $new Whether it is a new or existing library. */ public function saveLibraryData(&$librarydata, $new = true) { @@ -722,6 +725,7 @@ public function saveLibraryData(&$librarydata, $new = true) { 'addto' => isset($librarydata['addTo']) ? json_encode($librarydata['addTo']) : null, 'coremajor' => isset($librarydata['coreApi']['majorVersion']) ? $librarydata['coreApi']['majorVersion'] : null, 'coreminor' => isset($librarydata['coreApi']['majorVersion']) ? $librarydata['coreApi']['minorVersion'] : null, + 'metadatasettings' => isset($librarydata['metadataSettings']) ? $librarydata['metadataSettings'] : null, ); if ($new) { diff --git a/h5p/tests/generator_test.php b/h5p/tests/generator_test.php index 478ab57d5edde..dc266f5f2f815 100644 --- a/h5p/tests/generator_test.php +++ b/h5p/tests/generator_test.php @@ -246,6 +246,7 @@ public function test_create_library_record() { 'addto' => '/regex11/', 'coremajor' => null, 'coreminor' => null, + 'metadatasettings' => null, ]; $this->assertEquals($expected, $data); diff --git a/lib/db/install.xml b/lib/db/install.xml index 8f91417fa6be4..a12fa687fbdef 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -4184,6 +4184,7 @@ + @@ -4273,4 +4274,4 @@ - \ No newline at end of file + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 94d0c9937f576..7677467279a73 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2548,5 +2548,46 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2020061501.09); } + if ($oldversion < 2020061501.11) { + + // Define field metadatasettings to be added to h5p_libraries. + $table = new xmldb_table('h5p_libraries'); + $field = new xmldb_field('metadatasettings', XMLDB_TYPE_TEXT, null, null, null, null, null, 'coreminor'); + + // Conditionally launch add field metadatasettings. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Get installed library files that have no metadata settings value. + $params = [ + 'component' => 'core_h5p', + 'filearea' => 'libraries', + 'filename' => 'library.json', + ]; + $sql = "SELECT l.id, f.id as fileid + FROM {files} f + LEFT JOIN {h5p_libraries} l ON f.itemid = l.id + WHERE f.component = :component + AND f.filearea = :filearea + AND f.filename = :filename"; + $libraries = $DB->get_records_sql($sql, $params); + + // Update metadatasettings field when the attribute is present in the library.json file. + $fs = get_file_storage(); + foreach ($libraries as $library) { + $jsonfile = $fs->get_file_by_id($library->fileid); + $jsoncontent = json_decode($jsonfile->get_content()); + if (isset($jsoncontent->metadataSettings)) { + unset($library->fileid); + $library->metadatasettings = json_encode($jsoncontent->metadataSettings); + $DB->update_record('h5p_libraries', $library); + } + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2020061501.11); + } + return true; } diff --git a/version.php b/version.php index 282aac5a5cb32..01bbad5a60ea3 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2020061501.10; // 20200615 = branching date YYYYMMDD - do not modify! +$version = 2020061501.11; // 20200615 = branching date YYYYMMDD - do not modify! // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '3.9.1+ (Build: 20200822)'; // Human-friendly version name