MDL-67194 core_h5p: save core API version that content types require
authorVíctor Déniz Falcón <victor@moodle.com>
Mon, 11 Nov 2019 21:09:50 +0000 (21:09 +0000)
committerVíctor Déniz Falcón <victor@moodle.com>
Mon, 11 Nov 2019 21:09:50 +0000 (21:09 +0000)
h5p/classes/core.php
h5p/classes/framework.php
h5p/tests/generator/lib.php
h5p/tests/generator_test.php
h5p/tests/h5p_core_test.php
lib/db/install.xml
lib/db/upgrade.php
version.php

index 418304b..fb4eb27 100644 (file)
@@ -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;
+    }
+
 }
index e5293cf..77a4788 100644 (file)
@@ -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) {
index 26ba067..e9981a9 100644 (file)
@@ -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);
         }
index 0ad41d4..3e41203 100644 (file)
@@ -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);
index 4fef3e1..9bca6c6 100644 (file)
@@ -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);
     }
 
     /**
index c7c8b88..fcef7da 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20191015" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20191109" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
         <FIELD NAME="droplibrarycss" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="List of libraries that should not have CSS included if this library is used. Comma separated list."/>
         <FIELD NAME="semantics" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The semantics definition in json format"/>
         <FIELD NAME="addto" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Plugin configuration data"/>
+        <FIELD NAME="coremajor" TYPE="int" LENGTH="4" NOTNULL="false" SEQUENCE="false" COMMENT="H5P core API major version required"/>
+        <FIELD NAME="coreminor" TYPE="int" LENGTH="4" NOTNULL="false" SEQUENCE="false" COMMENT="H5P core API minor version required"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
index aeddaad..05a6c21 100644 (file)
@@ -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;
 }
index 2132125..99366ec 100644 (file)
@@ -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.