diff --git a/h5p/classes/core.php b/h5p/classes/core.php index 418304ba0ad..fb4eb272784 100644 --- a/h5p/classes/core.php +++ b/h5p/classes/core.php @@ -171,11 +171,16 @@ class core extends \H5PCore { $typesinstalled = []; foreach ($contenttypes->contentTypes as $type) { + // Don't fetch content types that require a higher H5P core API version. + if (!$this->is_required_core_api($type->coreApiVersionNeeded)) { + continue; + } + $library = [ 'machineName' => $type->id, 'majorVersion' => $type->version->major, 'minorVersion' => $type->version->minor, - 'patchVersion' => $type->version->patch, + 'patchVersion' => $type->version->patch ]; $factory = new \core_h5p\factory(); @@ -284,4 +289,21 @@ class core extends \H5PCore { return $contenttypes; } + + /** + * Checks that the required H5P core API version or higher is installed. + * + * @param stdClass $coreapi Object with properties major and minor for the core API version required. + * @return bool True if the required H5P core API version is installed. False if not. + */ + public function is_required_core_api($coreapi): bool { + if (isset($coreapi) && !empty($coreapi)) { + if (($coreapi->major > H5PCore::$coreApi['majorVersion']) || + (($coreapi->major == H5PCore::$coreApi['majorVersion']) && ($coreapi->minor > H5PCore::$coreApi['minorVersion']))) { + return false; + } + } + return true; + } + } diff --git a/h5p/classes/framework.php b/h5p/classes/framework.php index e5293cf2c3b..77a478865b7 100644 --- a/h5p/classes/framework.php +++ b/h5p/classes/framework.php @@ -720,6 +720,8 @@ class framework implements \H5PFrameworkInterface { 'droplibrarycss' => $droplibrarycss, 'semantics' => $librarydata['semantics'], '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, ); if ($new) { diff --git a/h5p/tests/generator/lib.php b/h5p/tests/generator/lib.php index 26ba067de82..e9981a92641 100644 --- a/h5p/tests/generator/lib.php +++ b/h5p/tests/generator/lib.php @@ -372,7 +372,9 @@ class core_h5p_generator extends \component_generator_base { 'majorversion' => $contenttype->version->major, 'minorversion' => $contenttype->version->minor, 'patchversion' => $contenttype->version->patch, - 'runnable' => 1 + 'runnable' => 1, + 'coremajor' => $contenttype->coreApiVersionNeeded->major, + 'coreminor' => $contenttype->coreApiVersionNeeded->minor ]; $DB->insert_record('h5p_libraries', (object) $library); } diff --git a/h5p/tests/generator_test.php b/h5p/tests/generator_test.php index 0ad41d4458d..3e41203ae64 100644 --- a/h5p/tests/generator_test.php +++ b/h5p/tests/generator_test.php @@ -231,7 +231,9 @@ class generator_testcase extends \advanced_testcase { 'preloadedcss' => 'css/example.css', 'droplibrarycss' => '', 'semantics' => 'Semantics example', - 'addto' => '/regex11/' + 'addto' => '/regex11/', + 'coremajor' => null, + 'coreminor' => null, ]; $this->assertEquals($expected, $data); diff --git a/h5p/tests/h5p_core_test.php b/h5p/tests/h5p_core_test.php index 4fef3e162a2..9bca6c6397a 100644 --- a/h5p/tests/h5p_core_test.php +++ b/h5p/tests/h5p_core_test.php @@ -60,26 +60,32 @@ class h5p_core_test extends \advanced_testcase { $this->markTestSkipped('PHPUNIT_LONGTEST is not defined'); } + // Get info of latest content types versions. + $contenttypes = $this->core->get_latest_content_types()->contentTypes; + // We are installing the first content type. + $librarydata = $contenttypes[0]; + $library = [ - 'machineName' => 'H5P.Accordion', - 'majorVersion' => 1, - 'minorVersion' => 0, - 'patchVersion' => 0, + 'machineName' => $librarydata->id, + 'majorVersion' => $librarydata->version->major, + 'minorVersion' => $librarydata->version->minor, + 'patchVersion' => $librarydata->version->patch, ]; - $sql = 'SELECT count(id) - FROM {files} - WHERE ' . $DB->sql_like('filepath', ':filepath'); - $params['filepath'] = "/{$library['machineName']}-%"; + // Verify that the content type is not yet installed. + $conditions['machinename'] = $library['machineName']; + $typeinstalled = $DB->count_records('h5p_libraries', $conditions); - $contentfiles = $DB->count_records_sql($sql, $params); - - $this->assertEquals(0, $contentfiles); + $this->assertEquals(0, $typeinstalled); + // Fetch the content type. $this->core->fetch_content_type($library); - $contentfiles = $DB->count_records_sql($sql, $params); - $this->assertGreaterThan(0, $contentfiles); + // Check that the content type is now installed. + $typeinstalled = $DB->get_record('h5p_libraries', $conditions); + $this->assertEquals($librarydata->id, $typeinstalled->machinename); + $this->assertEquals($librarydata->coreApiVersionNeeded->major, $typeinstalled->coremajor); + $this->assertEquals($librarydata->coreApiVersionNeeded->minor, $typeinstalled->coreminor); } /** diff --git a/lib/db/install.xml b/lib/db/install.xml index c7c8b8819c9..fcef7da2b25 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -4149,6 +4149,8 @@ + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index aeddaad3e36..05a6c213bb3 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3763,5 +3763,27 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2019103000.13); } + if ($oldversion < 2019110800.01) { + + // Define field coremajor to be added to h5p_libraries. + $table = new xmldb_table('h5p_libraries'); + $field = new xmldb_field('coremajor', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'addto'); + + // Conditionally launch add field coremajor. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('coreminor', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'coremajor'); + + // Conditionally launch add field coreminor. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2019110800.01); + } + return true; } diff --git a/version.php b/version.php index 2132125ee23..99366ecd159 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2019110800.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2019110800.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.