From f3c7e00f135dfa775570560b274df57a72aa5b27 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Wed, 12 Feb 2020 12:24:14 +0100 Subject: [PATCH] MDL-67707 core_h5p: add public H5P player methods --- h5p/classes/helper.php | 20 ++++++++++++++++++ h5p/classes/player.php | 36 +++++++++++++++++++++++++++++++-- h5p/templates/h5pembed.mustache | 6 ++++-- h5p/tests/helper_test.php | 15 ++++++++++---- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/h5p/classes/helper.php b/h5p/classes/helper.php index d4137453aa1..1aaadb3f4b2 100644 --- a/h5p/classes/helper.php +++ b/h5p/classes/helper.php @@ -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. diff --git a/h5p/classes/player.php b/h5p/classes/player.php index b6c355e6422..9a86b449676 100644 --- a/h5p/classes/player.php +++ b/h5p/classes/player.php @@ -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(); diff --git a/h5p/templates/h5pembed.mustache b/h5p/templates/h5pembed.mustache index 9f261265bb2..a0808404fcb 100644 --- a/h5p/templates/h5pembed.mustache +++ b/h5p/templates/h5pembed.mustache @@ -28,5 +28,7 @@ } }} - - \ No newline at end of file + diff --git a/h5p/tests/helper_test.php b/h5p/tests/helper_test.php index 6158c532d66..c216a2ce126 100644 --- a/h5p/tests/helper_test.php +++ b/h5p/tests/helper_test.php @@ -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,