Merge branch 'MDL-57101-master-fixup' of https://github.com/FMCorz/moodle

This commit is contained in:
Andrew Nicols
2016-12-01 15:07:42 +08:00
7 changed files with 121 additions and 44 deletions
+67
View File
@@ -272,6 +272,33 @@ class filter_manager {
$filter->setup($page, $context);
}
}
/**
* Setup the page for globally available filters.
*
* This helps setting up the page for filters which may be applied to
* the page, even if they do not belong to the current context, or are
* not yet visible because the content is lazily added (ajax). This method
* always uses to the system context which determines the globally
* available filters.
*
* This should only ever be called once per request.
*
* @param moodle_page $page The page.
* @since Moodle 3.2
*/
public function setup_page_for_globally_available_filters($page) {
$context = context_system::instance();
$filterdata = filter_get_globally_enabled_filters_with_config();
foreach ($filterdata as $name => $config) {
if (isset($this->textfilters[$context->id][$name])) {
$filter = $this->textfilters[$context->id][$name];
} else {
$filter = $this->make_filter_object($name, $context, $config);
}
$filter->setup($page, $context);
}
}
}
@@ -694,6 +721,46 @@ function filter_get_globally_enabled() {
return $enabledfilters;
}
/**
* Get the globally enabled filters.
*
* This returns the filters which could be used in any context. Essentially
* the filters which are not disabled for the entire site.
*
* @return array Keys are filter names, and values the config.
*/
function filter_get_globally_enabled_filters_with_config() {
global $DB;
$sql = "SELECT f.filter, fc.name, fc.value
FROM {filter_active} f
LEFT JOIN {filter_config} fc
ON fc.filter = f.filter
AND fc.contextid = f.contextid
WHERE f.contextid = :contextid
AND f.active != :disabled
ORDER BY f.sortorder";
$rs = $DB->get_recordset_sql($sql, [
'contextid' => context_system::instance()->id,
'disabled' => TEXTFILTER_DISABLED
]);
// Massage the data into the specified format to return.
$filters = array();
foreach ($rs as $row) {
if (!isset($filters[$row->filter])) {
$filters[$row->filter] = array();
}
if ($row->name !== null) {
$filters[$row->filter][$row->name] = $row->value;
}
}
$rs->close();
return $filters;
}
/**
* Return the names of the filters that should also be applied to strings
* (when they are enabled).
+1 -1
View File
@@ -1601,7 +1601,7 @@ class moodle_page {
if (!during_initial_install()) {
$filtermanager = filter_manager::instance();
$filtermanager->setup_page_for_filters($this, $this->context);
$filtermanager->setup_page_for_globally_available_filters($this);
}
$this->_wherethemewasinitialised = debug_backtrace();
+33
View File
@@ -675,4 +675,37 @@ class core_filterlib_testcase extends advanced_testcase {
$this->assertInstanceOf('filter_manager', $filterman);
$this->assertInstanceOf('performance_measuring_filter_manager', $filterman);
}
public function test_filter_get_globally_enabled_filters_with_config() {
$this->setup_available_in_context_tests();
// Set few filters.
filter_set_global_state('one', TEXTFILTER_ON);
filter_set_global_state('three', TEXTFILTER_OFF, -1);
filter_set_global_state('two', TEXTFILTER_DISABLED);
// Set global config.
filter_set_local_config('one', $this->syscontext->id, 'test1a', 'In root');
filter_set_local_config('one', $this->syscontext->id, 'test1b', 'In root');
filter_set_local_config('two', $this->syscontext->id, 'test2a', 'In root');
filter_set_local_config('two', $this->syscontext->id, 'test2b', 'In root');
// Set child config.
filter_set_local_config('one', $this->childcontext->id, 'test1a', 'In child');
filter_set_local_config('one', $this->childcontext->id, 'test1b', 'In child');
filter_set_local_config('two', $this->childcontext->id, 'test2a', 'In child');
filter_set_local_config('two', $this->childcontext->id, 'test2b', 'In child');
filter_set_local_config('three', $this->childcontext->id, 'test3a', 'In child');
filter_set_local_config('three', $this->childcontext->id, 'test3b', 'In child');
// Check.
$actual = filter_get_globally_enabled_filters_with_config();
$this->assertCount(2, $actual);
$this->assertEquals(['three', 'one'], array_keys($actual)); // Checks sortorder.
$this->assertArrayHasKey('one', $actual);
$this->assertArrayNotHasKey('two', $actual);
$this->assertArrayHasKey('three', $actual);
$this->assertEquals(['test1a' => 'In root', 'test1b' => 'In root'], $actual['one']);
$this->assertEquals([], $actual['three']);
}
}
+4 -29
View File
@@ -142,44 +142,19 @@ class core_media_manager {
protected function get_players() {
// Save time by only building the list once.
if (!$this->players) {
// Get raw list of players.
$allplayers = $this->get_players_raw();
$sortorder = \core\plugininfo\media::get_enabled_plugins();
$this->players = [];
foreach ($sortorder as $key) {
if (array_key_exists($key, $allplayers)) {
$this->players[] = $allplayers[$key];
foreach ($sortorder as $name) {
$classname = "media_" . $name . "_plugin";
if (class_exists($classname)) {
$this->players[] = new $classname();
}
}
}
return $this->players;
}
/**
* Obtains a raw list of player objects that includes objects regardless
* of whether they are disabled or not, and without sorting.
*
* You can override this in a subclass if you need to add additional_
* players.
*
* The return array is be indexed by player name to make it easier to
* remove players in a subclass.
*
* @return array $players Array of core_media_player objects in any order
*/
protected function get_players_raw() {
$plugins = core_plugin_manager::instance()->get_plugins_of_type('media');
$rv = [];
foreach ($plugins as $name => $dir) {
$classname = "media_" . $name . "_plugin";
if (class_exists($classname)) {
$rv[$name] = new $classname();
}
}
return $rv;
}
/**
* Renders a media file (audio or video) using suitable embedded player.
*
+6 -11
View File
@@ -30,17 +30,6 @@ defined('MOODLE_INTERNAL') || die();
* Media players return embed HTML for a particular way of playing back audio
* or video (or another file type).
*
* In order to make the code more lightweight, this is not a plugin type
* (players cannot have their own settings, database tables, capabilities, etc).
* These classes are used only by core_media_renderer in outputrenderers.php.
* If you add a new class here (in core code) you must modify the
* get_players_raw function in that file to include it.
*
* If a Moodle installation wishes to add extra player objects they can do so
* by overriding that renderer in theme, and overriding the get_players_raw
* function. The new player class should then of course be defined within the
* custom theme or other suitable location, not in this file.
*
* @package core_media
* @copyright 2016 Marina Glancy
* @author 2011 The Open University
@@ -257,6 +246,12 @@ abstract class core_media_player {
/**
* Setup page requirements.
*
* The typical javascript requirements MUST not take action on the content
* directly. They are meant to load the required libraries and listen
* to events in order to know when to take action. The role of this method
* is not to provide a way for plugins to look for content to embed on the
* page. The {@link self::embed()} method is meant to be used for that.
*
* @param moodle_page $page The page we are going to add requirements to.
* @since Moodle 3.2
*/
+1 -1
View File
@@ -1 +1 @@
define(["jquery","media_videojs/video"],function(a,b){var c=function(){a(document).on(M.core.event.FILTER_CONTENT_UPDATED,d)},d=function(c,d){d.find(".mediaplugin_videojs audio, .mediaplugin_videojs video").each(function(){var c=a(this).attr("id"),d=a(this).data("setup");b(c,d)})};return{setUp:c}});
define(["jquery","media_videojs/video"],function(a,b){var c=function(){a(document).on(M.core.event.FILTER_CONTENT_UPDATED,d)},d=function(c,d){var e=".mediaplugin_videojs";d.find(e).addBack(e).find("audio, video").each(function(){var c=a(this).attr("id"),d=a(this).data("setup");b(c,d)})};return{setUp:c}});
+9 -2
View File
@@ -40,8 +40,15 @@ define(['jquery', 'media_videojs/video'], function($, videojs) {
* @param {NodeList} nodes List of new nodes.
*/
var notifyVideoJS = function(e, nodes) {
nodes.find('.mediaplugin_videojs audio, .mediaplugin_videojs video')
.each(function() {
var selector = '.mediaplugin_videojs';
// Find the descendants matching the expected parent of the audio and video
// tags. Then also addBack the nodes matching the same selector. Finally,
// we find the audio and video tags contained in those parents. Kind thanks
// to jQuery for the simplicity.
nodes.find(selector)
.addBack(selector)
.find('audio, video').each(function() {
var id = $(this).attr('id'),
config = $(this).data('setup');