diff --git a/h5p/classes/file_storage.php b/h5p/classes/file_storage.php index 4af223480c0..ee2f215d03b 100644 --- a/h5p/classes/file_storage.php +++ b/h5p/classes/file_storage.php @@ -45,6 +45,8 @@ class file_storage implements \H5PFileStorage { public const CACHED_ASSETS_FILEAREA = 'cachedassets'; /** The export file area */ public const EXPORT_FILEAREA = 'export'; + /** The icon filename */ + public const ICON_FILENAME = 'icon.svg'; /** * @var \context $context Currently we use the system context everywhere. @@ -347,6 +349,40 @@ class file_storage implements \H5PFileStorage { // This is to be implemented when the h5p editor is introduced / created. } + /** + * Get the file URL or given library and then return it. + * + * @param int $itemid + * @param string $machinename + * @param int $majorversion + * @param int $minorversion + * @return string url or false if the file doesn't exist + */ + public function get_icon_url(int $itemid, string $machinename, int $majorversion, int $minorversion) { + $filepath = '/' . "{$machinename}-{$majorversion}.{$minorversion}" . '/'; + if ($file = $this->fs->get_file( + $this->context->id, + self::COMPONENT, + self::LIBRARY_FILEAREA, + $itemid, + $filepath, + self::ICON_FILENAME) + ) { + $iconurl = \moodle_url::make_pluginfile_url( + $this->context->id, + self::COMPONENT, + self::LIBRARY_FILEAREA, + $itemid, + $filepath, + $file->get_filename()); + + // Return image URL. + return $iconurl->out(); + } + + return false; + } + /** * Checks to see if content has the given file. * Used when saving content. diff --git a/h5p/libraries.php b/h5p/libraries.php index 66b4e581fbe..bffce142d14 100644 --- a/h5p/libraries.php +++ b/h5p/libraries.php @@ -67,10 +67,17 @@ $form->display(); // Load installed Libraries. $framework = $h5pfactory->get_framework(); +$filestorage = $h5pfactory->get_core()->fs; $libraries = $framework->loadLibraries(); $installed = []; foreach ($libraries as $libraryname => $versions) { foreach ($versions as $version) { + $version->icon = $filestorage->get_icon_url( + $version->id, + $version->machine_name, + $version->major_version, + $version->minor_version + ); $installed[] = $version; } } diff --git a/h5p/templates/h5plibraries.mustache b/h5p/templates/h5plibraries.mustache index 3ac8ee5436c..a6125fb0eee 100644 --- a/h5p/templates/h5plibraries.mustache +++ b/h5p/templates/h5plibraries.mustache @@ -25,7 +25,8 @@ "major_version": 1, "minor_version:": 0, "patch_version:": 0, - "runnable": 1 + "runnable": 1, + "icon": "icon.svg" }, { "title": "Collage", @@ -39,7 +40,8 @@ "major_version": 4, "minor_version:": 5, "patch_version:": 0, - "runnable": 0 + "runnable": 1, + "icon": "icon.svg" } ] } @@ -74,6 +76,14 @@ {{#runnable}} + {{#icon}} + + {{/icon}} + {{^icon}} + {{#pix}} b/h5p_library, core {{/pix}} + {{/icon}} {{{ title }}} {{{ major_version }}}.{{{ minor_version }}}.{{{ patch_version }}} diff --git a/h5p/tests/h5p_file_storage_test.php b/h5p/tests/h5p_file_storage_test.php index 17354a14c9f..d6199e40581 100644 --- a/h5p/tests/h5p_file_storage_test.php +++ b/h5p/tests/h5p_file_storage_test.php @@ -27,6 +27,7 @@ namespace core_h5p\local\tests; use core_h5p\file_storage; use core_h5p\autoloader; +use core_h5p\helper; use file_archive; use zip_archive; @@ -553,4 +554,67 @@ class h5p_file_storage_testcase extends \advanced_testcase { file_storage::LIBRARY_FILEAREA); $this->assertCount(7, $files); } + + /** + * Test get_icon_url() function behaviour. + * + * @dataProvider get_icon_url_provider + * @param string $filename The name of the H5P file to load. + * @param bool $expected Whether the icon should exist or not. + */ + public function test_get_icon_url(string $filename, bool $expected): void { + global $DB; + + $this->resetAfterTest(); + $factory = new \core_h5p\factory(); + + $admin = get_admin(); + + // Prepare a valid .H5P file. + $path = __DIR__ . '/fixtures/'.$filename; + + // Libraries can be updated when the file has been created by admin, even when the current user is not the admin. + $this->setUser($admin); + $file = helper::create_fake_stored_file_from_path($path, (int)$admin->id); + $factory->get_framework()->set_file($file); + $config = (object)[ + 'frame' => 1, + 'export' => 1, + 'embed' => 0, + 'copyright' => 0, + ]; + + $h5pid = helper::save_h5p($factory, $file, $config); + $h5p = $DB->get_record('h5p', ['id' => $h5pid]); + $h5plib = $DB->get_record('h5p_libraries', ['id' => $h5p->mainlibraryid]); + $iconurl = $this->h5p_file_storage->get_icon_url( + $h5plib->id, + $h5plib->machinename, + $h5plib->majorversion, + $h5plib->minorversion + ); + if ($expected) { + $this->assertContains(file_storage::ICON_FILENAME, $iconurl); + } else { + $this->assertFalse($iconurl); + } + } + + /** + * Data provider for test_get_icon_url(). + * + * @return array + */ + public function get_icon_url_provider(): array { + return [ + 'Icon included' => [ + 'filltheblanks.h5p', + true, + ], + 'Icon not included' => [ + 'greeting-card-887.h5p', + false, + ], + ]; + } } \ No newline at end of file diff --git a/pix/b/h5p_library.svg b/pix/b/h5p_library.svg new file mode 100644 index 00000000000..fb1b4ae741c --- /dev/null +++ b/pix/b/h5p_library.svg @@ -0,0 +1,71 @@ + +image/svg+xmltweeter feed + + + + + + + + + + + +tweeter feed + + + + + \ No newline at end of file