diff --git a/filter/mediaplugin/filter.php b/filter/mediaplugin/filter.php index e98f6d9d16e..726c1d1abd8 100644 --- a/filter/mediaplugin/filter.php +++ b/filter/mediaplugin/filter.php @@ -57,8 +57,8 @@ class filter_mediaplugin extends moodle_text_filter { } $jsinitialised = true; - $mediamanager = core_media_manager::instance(); - $mediamanager->setup($page); + // Set up the media manager so that media plugins requiring JS are initialised. + $mediamanager = core_media_manager::instance($page); } public function filter($text, array $options = array()) { diff --git a/lib/editor/tinymce/plugins/moodlemedia/preview.php b/lib/editor/tinymce/plugins/moodlemedia/preview.php index af946c04c11..cf4802fd56e 100644 --- a/lib/editor/tinymce/plugins/moodlemedia/preview.php +++ b/lib/editor/tinymce/plugins/moodlemedia/preview.php @@ -40,7 +40,7 @@ $PAGE->add_body_class('core_media_preview'); echo $OUTPUT->header(); -$mediarenderer = core_media_manager::instance(); +$mediarenderer = core_media_manager::instance($PAGE); if (isloggedin() and !isguestuser() and $mediarenderer->can_embed_url($url)) { require_sesskey(); diff --git a/lib/tests/medialib_test.php b/lib/tests/medialib_test.php index d2ccdc2a9dd..643ed07f90d 100644 --- a/lib/tests/medialib_test.php +++ b/lib/tests/medialib_test.php @@ -457,6 +457,23 @@ class core_medialib_testcase extends advanced_testcase { $this->assertContains('assertNotContains('assertSame($mediamanager1, $mediamanager2); + + $moodlepage3 = new moodle_page(); + $mediamanager3 = core_media_manager::instance($moodlepage3); + + $this->assertNotSame($mediamanager1, $mediamanager3); + } } /** @@ -517,4 +534,12 @@ class core_media_manager_test extends core_media_manager { } return $out; } + + /** + * Override the constructor to access it. + */ + public function __construct() { + global $PAGE; + parent::__construct($PAGE); + } } diff --git a/media/classes/manager.php b/media/classes/manager.php index 84b4adb9fe9..1d23ecb6ab8 100644 --- a/media/classes/manager.php +++ b/media/classes/manager.php @@ -97,30 +97,60 @@ class core_media_manager { /** @var core_media_manager caches a singleton instance */ static protected $instance; + /** @var moodle_page page this instance was initialised for */ + protected $page; + /** * Returns a singleton instance of a manager * + * Note as of Moodle 3.3, this will call setup for you. + * * @return core_media_manager */ - public static function instance() { - if (self::$instance === null) { - self::$instance = new self(); + public static function instance($page = null) { + // Use the passed $page if given, otherwise the $PAGE global. + if (!$page) { + global $PAGE; + $page = $PAGE; + } + if (self::$instance === null || ($page && self::$instance->page !== $page)) { + self::$instance = new self($page); } return self::$instance; } + /** + * Construct a new core_media_manager instance + * + * @param moodle_page $page The page we are going to add requirements to. + * @see core_media_manager::instance() + */ + protected function __construct($page) { + if ($page) { + $this->page = $page; + $players = $this->get_players(); + foreach ($players as $player) { + $player->setup($page); + } + } else { + debugging('Could not determine the $PAGE. Media plugins will not be set up', DEBUG_DEVELOPER); + } + } + /** * Setup page requirements. * * This should must only be called once per page request. * + * @deprecated Moodle 3.3, The setup is now done in ::instance() so there is no need to call this * @param moodle_page $page The page we are going to add requirements to. + * @see core_media_manager::instance() + * @todo MDL-57632 final deprecation */ public function setup($page) { - $players = $this->get_players(); - foreach ($players as $player) { - $player->setup($page); - } + debugging('core_media_manager::setup() is deprecated.' . + 'You only need to call core_media_manager::instance() now', DEBUG_DEVELOPER); + // No need to call ::instance from here, because the instance has already be set up. } /** diff --git a/media/player/videojs/tests/behat/modules.feature b/media/player/videojs/tests/behat/modules.feature new file mode 100644 index 00000000000..998a30c9cc5 --- /dev/null +++ b/media/player/videojs/tests/behat/modules.feature @@ -0,0 +1,50 @@ +@media @media_videojs +Feature: Embed videos without the media filter + In order to add helpful resources for students + As a teacher + I need to be able to embed videos URL, file and lesson modules + + Background: + Given I log in as "admin" + And I am on site homepage + And I navigate to "Turn editing on" node in "Front page settings" + + @javascript + Scenario: Add a video in a URL resource. Make sure media filters work + When I add a "URL" to section "1" and I fill the form with: + | Name | Video URL | + | Description | Example of a video url | + | External URL | http://download.moodle.org/mediatest/quicktime_320_180.mov | + And I follow "Video URL" + Then ".video-js" "css_element" should exist + And I am on site homepage + + @javascript + Scenario: Add a video as a File resource. Make sure media filters work + When I add a "File" to section "1" + And I set the following fields to these values: + | Name | Video File | + | Description | Example of a video file | + And I upload "media/player/videojs/tests/fixtures/test.mov" file to "Select files" filemanager + And I press "Save and display" + Then ".video-js" "css_element" should exist + + @javascript + Scenario: Add a video as content to a lesson. Make sure media filters work + When I add a "Lesson" to section "1" + And I set the following fields to these values: + | Name | Lesson with video | + | Description | Example of a video in a lesson | + And I expand all fieldsets + And I upload "media/player/videojs/tests/fixtures/test.mov" file to "Linked media" filemanager + And I press "Save and display" + And I follow "Add a content page" + And I set the following fields to these values: + | Page title | Placeholder content | + | Description | Just so we can preview the lesson | + And I press "Save page" + And I navigate to "Preview" in current page administration + And I follow "Click here to view" + And I switch to "lessonmediafile" window + Then ".video-js" "css_element" should exist + And I switch to the main window diff --git a/media/player/videojs/tests/fixtures/test.mov b/media/player/videojs/tests/fixtures/test.mov new file mode 100644 index 00000000000..c5e2b9c88ed Binary files /dev/null and b/media/player/videojs/tests/fixtures/test.mov differ diff --git a/media/upgrade.txt b/media/upgrade.txt new file mode 100644 index 00000000000..ef95d344622 --- /dev/null +++ b/media/upgrade.txt @@ -0,0 +1,5 @@ +This files describes API changes in /media/ plugins, +information provided here is intended especially for developers. + +=== 3.3 === +* core_media_manager setup() is now deprecated as it is now called when initialising core_media_manager::instance(). diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 4e02e441fc5..9a973c7f102 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -618,7 +618,7 @@ function lesson_get_media_html($lesson, $context) { $extension = resourcelib_get_extension($url->out(false)); - $mediamanager = core_media_manager::instance(); + $mediamanager = core_media_manager::instance($PAGE); $embedoptions = array( core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true diff --git a/mod/resource/locallib.php b/mod/resource/locallib.php index 062db7ec179..0dd1f1fe2a3 100644 --- a/mod/resource/locallib.php +++ b/mod/resource/locallib.php @@ -75,7 +75,7 @@ function resource_display_embed($resource, $cm, $course, $file) { $extension = resourcelib_get_extension($file->get_filename()); - $mediamanager = core_media_manager::instance(); + $mediamanager = core_media_manager::instance($PAGE); $embedoptions = array( core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true, diff --git a/mod/url/locallib.php b/mod/url/locallib.php index a29686c7d64..7d9bba291cf 100644 --- a/mod/url/locallib.php +++ b/mod/url/locallib.php @@ -305,7 +305,7 @@ function url_display_embed($url, $cm, $course) { $extension = resourcelib_get_extension($url->externalurl); - $mediamanager = core_media_manager::instance(); + $mediamanager = core_media_manager::instance($PAGE); $embedoptions = array( core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true