MDL-68636 core_contentbank: Changing get_icon function

This commit is contained in:
Amaia Anabitarte 2020-05-06 10:11:35 +02:00
parent f9830e456a
commit 6fc3477cc5
7 changed files with 81 additions and 16 deletions

View file

@ -73,12 +73,32 @@ class contenttype extends \core_contentbank\contenttype {
/**
* Returns the HTML code to render the icon for H5P content types.
*
* @param string $contentname The contentname to add as alt value to the icon.
* @param content $content The content to be displayed.
* @return string HTML code to render the icon
*/
public function get_icon(string $contentname): string {
global $OUTPUT;
return $OUTPUT->pix_icon('f/h5p-64', $contentname, 'moodle', ['class' => 'iconsize-big']);
public function get_icon(\core_contentbank\content $content): string {
global $OUTPUT, $DB;
$iconurl = $OUTPUT->image_url('f/h5p-64', 'moodle')->out(false);
$file = $content->get_file();
if (!empty($file)) {
$h5p = \core_h5p\api::get_content_from_pathnamehash($file->get_pathnamehash());
if (!empty($h5p)) {
\core_h5p\local\library\autoloader::register();
if ($h5plib = $DB->get_record('h5p_libraries', ['id' => $h5p->mainlibraryid])) {
$h5pfilestorage = new \core_h5p\file_storage();
$h5picon = $h5pfilestorage->get_icon_url(
$h5plib->id,
$h5plib->machinename,
$h5plib->majorversion,
$h5plib->minorversion);
if (!empty($h5picon)) {
$iconurl = $h5picon;
}
}
}
}
return $iconurl;
}
/**

View file

@ -105,4 +105,46 @@ class contenttype_h5p_contenttype_plugin_testcase extends advanced_testcase {
$this->assertFalse($coursetype->can_upload());
$this->assertFalse($systemtype->can_upload());
}
/**
* Tests get_icon result.
*
* @covers ::get_icon
*/
public function test_get_icon() {
global $CFG;
$this->resetAfterTest();
$systemcontext = context_system::instance();
$this->setAdminUser();
$contenttype = new contenttype_h5p\contenttype($systemcontext);
// Add an H5P fill the blanks file to the content bank.
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/filltheblanks.h5p';
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_h5p', 1, 0, $systemcontext, true, $filepath);
$filltheblanks = array_shift($contents);
// Add an H5P find the words file to the content bank.
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/find-the-words.h5p';
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_h5p', 1, 0, $systemcontext, true, $filepath);
$findethewords = array_shift($contents);
// Check before deploying the icon for both contents is the same: default one.
// Because we don't know specific H5P content type yet.
$defaulticon = $contenttype->get_icon($filltheblanks);
$this->assertEquals($defaulticon, $contenttype->get_icon($findethewords));
$this->assertContains('h5p', $defaulticon);
// Deploy one of the contents though the player to create the H5P DB entries and know specific content type.
$h5pplayer = new \core_h5p\player($findethewords->get_file_url(), new \stdClass(), true);
$h5pplayer->add_assets_to_page();
$h5pplayer->output();
// Once the H5P has been deployed, we know the specific H5P content type, so the icon returned is not default one.
$findicon = $contenttype->get_icon($findethewords);
$this->assertNotEquals($defaulticon, $findicon);
$this->assertContains('find', $findicon, '', true);
}
}