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);
}
/**
* 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
* 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.
*
@ -167,7 +199,7 @@ class player {
'exportUrl' => ($exporturl instanceof \moodle_url) ? $exporturl->out(false) : '',
'embedCode' => $this->get_embed_code($this->url->out(),
$displayoptions[ core::DISPLAY_OPTION_EMBED ]),
'resizeCode' => $this->get_resize_code(),
'resizeCode' => self::get_resize_code(),
'title' => $this->content['slug'],
'displayOptions' => $displayoptions,
'url' => self::get_embed_url($this->url->out())->out(),
@ -715,7 +747,7 @@ class player {
*
* @return string The HTML code with the resize script.
*/
private function get_resize_code(): string {
private static function get_resize_code(): string {
global $OUTPUT;
$template = new \stdClass();

View File

@ -28,5 +28,7 @@
}
}}
<iframe src="{{embedurl}}" width=":w" height=":h" allowfullscreen="allowfullscreen"></iframe>
<iframe src="{{embedurl}}" name="h5player" width=":w" height=":h"
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().
*
* @dataProvider get_display_options_provider
* @dataProvider display_options_provider
* @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 $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 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->resetAfterTest();
@ -60,9 +60,16 @@ class helper_testcase extends \advanced_testcase {
'embed' => $embed,
'copyright' => $copyright,
];
$displayoptions = helper::get_display_options($core, $config);
// Test getting display options.
$displayoptions = helper::get_display_options($core, $config);
$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
*/
public function get_display_options_provider(): array {
public function display_options_provider(): array {
return [
'All display options disabled' => [
false,