Skip to content

Commit d3ee08d

Browse files
author
Amaia Anabitarte
committed
MDL-67060 core_h5p: Improving H5P libraries list
1 parent 4e90332 commit d3ee08d

File tree

5 files changed

+190
-2
lines changed

5 files changed

+190
-2
lines changed

h5p/classes/file_storage.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class file_storage implements \H5PFileStorage {
4545
public const CACHED_ASSETS_FILEAREA = 'cachedassets';
4646
/** The export file area */
4747
public const EXPORT_FILEAREA = 'export';
48+
/** The icon filename */
49+
public const ICON_FILENAME = 'icon.svg';
4850

4951
/**
5052
* @var \context $context Currently we use the system context everywhere.
@@ -347,6 +349,40 @@ public function moveContentDirectory($source, $contentid = null) {
347349
// This is to be implemented when the h5p editor is introduced / created.
348350
}
349351

352+
/**
353+
* Get the file URL or given library and then return it.
354+
*
355+
* @param int $itemid
356+
* @param string $machinename
357+
* @param int $majorversion
358+
* @param int $minorversion
359+
* @return string url or false if the file doesn't exist
360+
*/
361+
public function get_icon_url(int $itemid, string $machinename, int $majorversion, int $minorversion) {
362+
$filepath = '/' . "{$machinename}-{$majorversion}.{$minorversion}" . '/';
363+
if ($file = $this->fs->get_file(
364+
$this->context->id,
365+
self::COMPONENT,
366+
self::LIBRARY_FILEAREA,
367+
$itemid,
368+
$filepath,
369+
self::ICON_FILENAME)
370+
) {
371+
$iconurl = \moodle_url::make_pluginfile_url(
372+
$this->context->id,
373+
self::COMPONENT,
374+
self::LIBRARY_FILEAREA,
375+
$itemid,
376+
$filepath,
377+
$file->get_filename());
378+
379+
// Return image URL.
380+
return $iconurl->out();
381+
}
382+
383+
return false;
384+
}
385+
350386
/**
351387
* Checks to see if content has the given file.
352388
* Used when saving content.

h5p/libraries.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,17 @@
6767

6868
// Load installed Libraries.
6969
$framework = $h5pfactory->get_framework();
70+
$filestorage = $h5pfactory->get_core()->fs;
7071
$libraries = $framework->loadLibraries();
7172
$installed = [];
7273
foreach ($libraries as $libraryname => $versions) {
7374
foreach ($versions as $version) {
75+
$version->icon = $filestorage->get_icon_url(
76+
$version->id,
77+
$version->machine_name,
78+
$version->major_version,
79+
$version->minor_version
80+
);
7481
$installed[] = $version;
7582
}
7683
}

h5p/templates/h5plibraries.mustache

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"major_version": 1,
2626
"minor_version:": 0,
2727
"patch_version:": 0,
28-
"runnable": 1
28+
"runnable": 1,
29+
"icon": "icon.svg"
2930
},
3031
{
3132
"title": "Collage",
@@ -39,7 +40,8 @@
3940
"major_version": 4,
4041
"minor_version:": 5,
4142
"patch_version:": 0,
42-
"runnable": 0
43+
"runnable": 1,
44+
"icon": "icon.svg"
4345
}
4446
]
4547
}
@@ -74,6 +76,14 @@
7476
{{#runnable}}
7577
<tr class="">
7678
<td>
79+
{{#icon}}
80+
<img alt=""
81+
class="icon iconsize-big"
82+
src="{{{ icon }}}">
83+
{{/icon}}
84+
{{^icon}}
85+
{{#pix}} b/h5p_library, core {{/pix}}
86+
{{/icon}}
7787
{{{ title }}}
7888
</td>
7989
<td>{{{ major_version }}}.{{{ minor_version }}}.{{{ patch_version }}}</td>

h5p/tests/h5p_file_storage_test.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
use core_h5p\file_storage;
2929
use core_h5p\autoloader;
30+
use core_h5p\helper;
3031
use file_archive;
3132
use zip_archive;
3233

@@ -553,4 +554,67 @@ public function test_delete_library() {
553554
file_storage::LIBRARY_FILEAREA);
554555
$this->assertCount(7, $files);
555556
}
557+
558+
/**
559+
* Test get_icon_url() function behaviour.
560+
*
561+
* @dataProvider get_icon_url_provider
562+
* @param string $filename The name of the H5P file to load.
563+
* @param bool $expected Whether the icon should exist or not.
564+
*/
565+
public function test_get_icon_url(string $filename, bool $expected): void {
566+
global $DB;
567+
568+
$this->resetAfterTest();
569+
$factory = new \core_h5p\factory();
570+
571+
$admin = get_admin();
572+
573+
// Prepare a valid .H5P file.
574+
$path = __DIR__ . '/fixtures/'.$filename;
575+
576+
// Libraries can be updated when the file has been created by admin, even when the current user is not the admin.
577+
$this->setUser($admin);
578+
$file = helper::create_fake_stored_file_from_path($path, (int)$admin->id);
579+
$factory->get_framework()->set_file($file);
580+
$config = (object)[
581+
'frame' => 1,
582+
'export' => 1,
583+
'embed' => 0,
584+
'copyright' => 0,
585+
];
586+
587+
$h5pid = helper::save_h5p($factory, $file, $config);
588+
$h5p = $DB->get_record('h5p', ['id' => $h5pid]);
589+
$h5plib = $DB->get_record('h5p_libraries', ['id' => $h5p->mainlibraryid]);
590+
$iconurl = $this->h5p_file_storage->get_icon_url(
591+
$h5plib->id,
592+
$h5plib->machinename,
593+
$h5plib->majorversion,
594+
$h5plib->minorversion
595+
);
596+
if ($expected) {
597+
$this->assertContains(file_storage::ICON_FILENAME, $iconurl);
598+
} else {
599+
$this->assertFalse($iconurl);
600+
}
601+
}
602+
603+
/**
604+
* Data provider for test_get_icon_url().
605+
*
606+
* @return array
607+
*/
608+
public function get_icon_url_provider(): array {
609+
return [
610+
'Icon included' => [
611+
'filltheblanks.h5p',
612+
true,
613+
],
614+
'Icon not included' => [
615+
'greeting-card-887.h5p',
616+
false,
617+
],
618+
];
619+
}
556620
}

pix/b/h5p_library.svg

Lines changed: 71 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)