MDL-67296 core_h5p: minor changes
authorVíctor Déniz Falcón <victor@moodle.com>
Wed, 22 Jan 2020 14:48:16 +0000 (14:48 +0000)
committerVíctor Déniz Falcón <victor@moodle.com>
Wed, 22 Jan 2020 14:48:16 +0000 (14:48 +0000)
core.php-Avoid to use the factory for each content type
h5p_get_content_types_task-new method to simplify test execution
framework.php-returns error if you get a 404 response after requesting a file

h5p/classes/core.php
h5p/classes/framework.php
lib/classes/task/h5p_get_content_types_task.php

index fb4eb27..660396d 100644 (file)
@@ -27,6 +27,7 @@ namespace core_h5p;
 defined('MOODLE_INTERNAL') || die();
 
 require_once("$CFG->libdir/filelib.php");
+
 use H5PCore;
 use H5PFrameworkInterface;
 use stdClass;
@@ -170,6 +171,9 @@ class core extends \H5PCore {
 
         $typesinstalled = [];
 
+        $factory = new factory();
+        $framework = $factory->get_framework();
+
         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)) {
@@ -183,9 +187,6 @@ class core extends \H5PCore {
                 'patchVersion' => $type->version->patch
             ];
 
-            $factory = new \core_h5p\factory();
-            $framework = $factory->get_framework();
-
             $shoulddownload = true;
             if ($framework->getLibraryId($type->id, $type->version->major, $type->version->minor)) {
                 if (!$framework->isPatchedLibrary($library)) {
@@ -216,7 +217,7 @@ class core extends \H5PCore {
      * @return int|null Returns the id of the content type library installed, null otherwise.
      */
     public function fetch_content_type(array $library): ?int {
-        $factory = new \core_h5p\factory();
+        $factory = new factory();
 
         // Get a temp path to download the content type.
         $temppath = make_request_directory();
@@ -247,7 +248,9 @@ class core extends \H5PCore {
         $file->delete();
 
         $librarykey = static::libraryToString($library);
-        return $factory->get_storage()->h5pC->librariesJsonData[$librarykey]["libraryId"];
+        $libraryid = $factory->get_storage()->h5pC->librariesJsonData[$librarykey]["libraryId"];
+
+        return $libraryid;
     }
 
     /**
index 416f358..4818d86 100644 (file)
@@ -100,7 +100,7 @@ class framework implements \H5PFrameworkInterface {
         $response = download_file_content($url, null, $data, true, 300, 20,
                 false, $stream);
 
-        if (empty($response->error)) {
+        if (empty($response->error) && ($response->status != '404')) {
             return $response->results;
         } else {
             $this->setErrorMessage($response->error, 'failed-fetching-external-data');
index e41d402..b3e70c4 100644 (file)
@@ -44,13 +44,23 @@ class h5p_get_content_types_task extends scheduled_task {
         return get_string('h5pgetcontenttypestask', 'admin');
     }
 
+    /**
+     * Get an \core_h5p\core instance.
+     *
+     * @return \core_h5p\core
+     */
+    public function get_core() {
+        $factory = new factory();
+        $core = $factory->get_core();
+        return $core;
+    }
+
     /**
      * Execute the task.
      */
     public function execute() {
 
-        $factory = new factory();
-        $core = $factory->get_core();
+        $core = $this->get_core();
 
         $result = $core->fetch_latest_content_types();