Skip to content

Commit

Permalink
MDL-67060 core_h5p: Improving H5P libraries list
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Feb 11, 2020
1 parent 4e90332 commit d3ee08d
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 2 deletions.
36 changes: 36 additions & 0 deletions h5p/classes/file_storage.php
Expand Up @@ -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.
Expand Down Expand Up @@ -347,6 +349,40 @@ public function moveContentDirectory($source, $contentid = null) {
// 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.
Expand Down
7 changes: 7 additions & 0 deletions h5p/libraries.php
Expand Up @@ -67,10 +67,17 @@

// 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;
}
}
Expand Down
14 changes: 12 additions & 2 deletions h5p/templates/h5plibraries.mustache
Expand Up @@ -25,7 +25,8 @@
"major_version": 1,
"minor_version:": 0,
"patch_version:": 0,
"runnable": 1
"runnable": 1,
"icon": "icon.svg"
},
{
"title": "Collage",
Expand All @@ -39,7 +40,8 @@
"major_version": 4,
"minor_version:": 5,
"patch_version:": 0,
"runnable": 0
"runnable": 1,
"icon": "icon.svg"
}
]
}
Expand Down Expand Up @@ -74,6 +76,14 @@
{{#runnable}}
<tr class="">
<td>
{{#icon}}
<img alt=""
class="icon iconsize-big"
src="{{{ icon }}}">
{{/icon}}
{{^icon}}
{{#pix}} b/h5p_library, core {{/pix}}
{{/icon}}
{{{ title }}}
</td>
<td>{{{ major_version }}}.{{{ minor_version }}}.{{{ patch_version }}}</td>
Expand Down
64 changes: 64 additions & 0 deletions h5p/tests/h5p_file_storage_test.php
Expand Up @@ -27,6 +27,7 @@

use core_h5p\file_storage;
use core_h5p\autoloader;
use core_h5p\helper;
use file_archive;
use zip_archive;

Expand Down Expand Up @@ -553,4 +554,67 @@ public function test_delete_library() {
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,
],
];
}
}
71 changes: 71 additions & 0 deletions pix/b/h5p_library.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d3ee08d

Please sign in to comment.