MDL-67707 core_h5p: add public H5P player methods

This commit is contained in:
Ferran Recio 2020-02-12 12:24:14 +01:00
parent 1e7e255d72
commit f3c7e00f13
4 changed files with 69 additions and 8 deletions

View file

@ -107,6 +107,26 @@ class helper {
return $core->getStorableDisplayOptions($disableoptions, 0); return $core->getStorableDisplayOptions($disableoptions, 0);
} }
/**
* Convert the int representation of display options into stdClass
*
* @param core $core The \core_h5p\core object
* @param int $displayint integer value representing display options
*
* @return int The representation of display options as int
*/
public static function decode_display_options(core $core, int $displayint = null): \stdClass {
$config = new \stdClass();
if ($displayint === null) {
$displayint = self::get_display_options($core, $config);
}
$displayarray = $core->getDisplayOptionsForEdit($displayint);
$config->export = $displayarray[core::DISPLAY_OPTION_DOWNLOAD] ?? 0;
$config->embed = $displayarray[core::DISPLAY_OPTION_EMBED] ?? 0;
$config->copyright = $displayarray[core::DISPLAY_OPTION_COPYRIGHT] ?? 0;
return $config;
}
/** /**
* Checks if the author of the .h5p file is "trustable". If the file hasn't been uploaded by a user with the * Checks if the author of the .h5p file is "trustable". If the file hasn't been uploaded by a user with the
* required capability, the content won't be deployed. * required capability, the content won't be deployed.

View file

@ -123,6 +123,38 @@ class player {
} }
} }
/**
* Get the encoded URL for embeding this H5P content.
*
* @param string $url Local URL of the H5P file to display.
* @param stdClass $config Configuration for H5P buttons.
* @param bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
*
* @return string The embedable code to display a H5P file.
*/
public static function display(string $url, \stdClass $config, bool $preventredirect = true): string {
global $OUTPUT;
$params = [
'url' => $url,
'preventredirect' => $preventredirect,
];
$optparams = ['frame', 'export', 'embed', 'copyright'];
foreach ($optparams as $optparam) {
if (!empty($config->$optparam)) {
$params[$optparam] = $config->$optparam;
}
}
$fileurl = new \moodle_url('/h5p/embed.php', $params);
$template = new \stdClass();
$template->embedurl = $fileurl->out(false);
$result = $OUTPUT->render_from_template('core_h5p/h5pembed', $template);
$result .= self::get_resize_code();
return $result;
}
/** /**
* Get the error messages stored in our H5P framework. * Get the error messages stored in our H5P framework.
* *
@ -167,7 +199,7 @@ class player {
'exportUrl' => ($exporturl instanceof \moodle_url) ? $exporturl->out(false) : '', 'exportUrl' => ($exporturl instanceof \moodle_url) ? $exporturl->out(false) : '',
'embedCode' => $this->get_embed_code($this->url->out(), 'embedCode' => $this->get_embed_code($this->url->out(),
$displayoptions[ core::DISPLAY_OPTION_EMBED ]), $displayoptions[ core::DISPLAY_OPTION_EMBED ]),
'resizeCode' => $this->get_resize_code(), 'resizeCode' => self::get_resize_code(),
'title' => $this->content['slug'], 'title' => $this->content['slug'],
'displayOptions' => $displayoptions, 'displayOptions' => $displayoptions,
'url' => self::get_embed_url($this->url->out())->out(), 'url' => self::get_embed_url($this->url->out())->out(),
@ -715,7 +747,7 @@ class player {
* *
* @return string The HTML code with the resize script. * @return string The HTML code with the resize script.
*/ */
private function get_resize_code(): string { private static function get_resize_code(): string {
global $OUTPUT; global $OUTPUT;
$template = new \stdClass(); $template = new \stdClass();

View file

@ -28,5 +28,7 @@
} }
}} }}
<iframe src="{{embedurl}}" name="h5player" width=":w" height=":h"
<iframe src="{{embedurl}}" width=":w" height=":h" allowfullscreen="allowfullscreen"></iframe> allowfullscreen="allowfullscreen" class="h5p-player w-100 border-0"
style="min-height: 230px;">
</iframe>

View file

@ -41,14 +41,14 @@ class helper_testcase extends \advanced_testcase {
/** /**
* Test the behaviour of get_display_options(). * Test the behaviour of get_display_options().
* *
* @dataProvider get_display_options_provider * @dataProvider display_options_provider
* @param bool $frame Whether the frame should be displayed or not * @param bool $frame Whether the frame should be displayed or not
* @param bool $export Whether the export action button should be displayed or not * @param bool $export Whether the export action button should be displayed or not
* @param bool $embed Whether the embed action button should be displayed or not * @param bool $embed Whether the embed action button should be displayed or not
* @param bool $copyright Whether the copyright action button should be displayed or not * @param bool $copyright Whether the copyright action button should be displayed or not
* @param int $expected The expectation with the displayoptions value * @param int $expected The expectation with the displayoptions value
*/ */
public function test_get_display_options(bool $frame, bool $export, bool $embed, bool $copyright, int $expected): void { public function test_display_options(bool $frame, bool $export, bool $embed, bool $copyright, int $expected): void {
$this->setRunTestInSeparateProcess(true); $this->setRunTestInSeparateProcess(true);
$this->resetAfterTest(); $this->resetAfterTest();
@ -60,9 +60,16 @@ class helper_testcase extends \advanced_testcase {
'embed' => $embed, 'embed' => $embed,
'copyright' => $copyright, 'copyright' => $copyright,
]; ];
$displayoptions = helper::get_display_options($core, $config);
// Test getting display options.
$displayoptions = helper::get_display_options($core, $config);
$this->assertEquals($expected, $displayoptions); $this->assertEquals($expected, $displayoptions);
// Test decoding display options.
$decoded = helper::decode_display_options($core, $expected);
$this->assertEquals($decoded->export, $config->export);
$this->assertEquals($decoded->embed, $config->embed);
$this->assertEquals($decoded->copyright, $config->copyright);
} }
/** /**
@ -70,7 +77,7 @@ class helper_testcase extends \advanced_testcase {
* *
* @return array * @return array
*/ */
public function get_display_options_provider(): array { public function display_options_provider(): array {
return [ return [
'All display options disabled' => [ 'All display options disabled' => [
false, false,