From 743462111fcf4480fe98244ae08c1093eb914179 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 30 Oct 2019 10:04:26 +0100 Subject: [PATCH] MDL-67076 core_h5p: Add preventredirect parameter to embed --- h5p/classes/player.php | 13 ++++++++++--- h5p/embed.php | 4 +++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/h5p/classes/player.php b/h5p/classes/player.php index 5605a972481..61252ac1a4d 100644 --- a/h5p/classes/player.php +++ b/h5p/classes/player.php @@ -80,17 +80,24 @@ class player { */ private $factory; + /** + * @var bool Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions. + */ + private $preventredirect; + /** * Inits the H5P player for rendering the 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 */ - public function __construct(string $url, \stdClass $config) { + public function __construct(string $url, \stdClass $config, bool $preventredirect = true) { if (empty($url)) { throw new \moodle_exception('h5pinvalidurl', 'core_h5p'); } $this->url = new \moodle_url($url); + $this->preventredirect = $preventredirect; $this->factory = new \core_h5p\factory(); @@ -313,14 +320,14 @@ class player { // For CONTEXT_MODULE, check if the user is enrolled in the course and has permissions view this .h5p file. if ($this->context->contextlevel == CONTEXT_MODULE) { // Require login to the course first (without login to the module). - require_course_login($course, true, null, false, true); + require_course_login($course, true, null, !$this->preventredirect, $this->preventredirect); // Now check if module is available OR it is restricted but the intro is shown on the course page. $cminfo = \cm_info::create($cm); if (!$cminfo->uservisible) { if (!$cm->showdescription || !$cminfo->is_visible_on_course_page()) { // Module intro is not visible on the course page and module is not available, show access error. - require_course_login($course, true, $cminfo, false, true); + require_course_login($course, true, $cminfo, !$this->preventredirect, $this->preventredirect); } } } diff --git a/h5p/embed.php b/h5p/embed.php index af236293bea..51599e92181 100644 --- a/h5p/embed.php +++ b/h5p/embed.php @@ -34,9 +34,11 @@ $config->export = optional_param('export', 0, PARAM_INT); $config->embed = optional_param('embed', 0, PARAM_INT); $config->copyright = optional_param('copyright', 0, PARAM_INT); +$preventredirect = optional_param('preventredirect', true, PARAM_BOOL); + $PAGE->set_url(new \moodle_url('/h5p/embed.php', array('url' => $url))); try { - $h5pplayer = new \core_h5p\player($url, $config); + $h5pplayer = new \core_h5p\player($url, $config, $preventredirect); $messages = $h5pplayer->get_messages(); } catch (\Exception $e) {