MDL-57101 filter: Load globally available filters on every page
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.
This commit is contained in:
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user