Skip to content

Commit

Permalink
MDL-57101 filter: Load globally available filters on every page
Browse files Browse the repository at this point in the history
Previous commits were overlooked, they should have passed the
system context to filter_manager::setup_page_for_fitlers(), the
page context was passed instead. Regardless, the processing of
the latter is too excessive for what we need, but, more importantly,
it does not return "Off, but available" filters which we also need.
  • Loading branch information
Frederic Massart committed Dec 1, 2016
1 parent 39a9ac4 commit 36e6789
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
67 changes: 67 additions & 0 deletions lib/filterlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,33 @@ public function setup_page_for_filters($page, $context) {
$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);
}
}
}


Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion lib/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,7 @@ public function initialise_theme_and_output() {

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();
Expand Down
33 changes: 33 additions & 0 deletions lib/tests/filterlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,37 @@ public function test_filter_manager_instance() {
$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']);
}
}
6 changes: 6 additions & 0 deletions media/classes/player.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ protected static function pick_video_size(&$width, &$height) {
/**
* 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
*/
Expand Down

0 comments on commit 36e6789

Please sign in to comment.