Skip to content

Commit

Permalink
MDL-67707 core_h5p: add public H5P player methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranrecio committed Mar 16, 2020
1 parent 1e7e255 commit f3c7e00
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
20 changes: 20 additions & 0 deletions h5p/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ public static function get_display_options(core $core, \stdClass $config): int {
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.
Expand Down
36 changes: 34 additions & 2 deletions h5p/classes/player.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,38 @@ public function __construct(string $url, \stdClass $config, bool $preventredirec
}
}

/**
* 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.
*
Expand Down Expand Up @@ -167,7 +199,7 @@ public function add_assets_to_page() {
'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(),
Expand Down Expand Up @@ -715,7 +747,7 @@ private function get_dependency_files(): array {
*
* @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();
Expand Down
6 changes: 4 additions & 2 deletions h5p/templates/h5pembed.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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>
15 changes: 11 additions & 4 deletions h5p/tests/helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -60,17 +60,24 @@ public function test_get_display_options(bool $frame, bool $export, bool $embed,
'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);
}

/**
* Data provider for test_get_display_options().
*
* @return array
*/
public function get_display_options_provider(): array {
public function display_options_provider(): array {
return [
'All display options disabled' => [
false,
Expand Down

0 comments on commit f3c7e00

Please sign in to comment.