Merge branch 'MDL-82606_404' of https://github.com/timhunt/moodle into MOODLE_404_STABLE

This commit is contained in:
Jun Pataleta 2024-08-07 11:12:05 +08:00
commit 59a377abbe
No known key found for this signature in database
GPG Key ID: F83510526D99E2C7

View File

@ -1105,32 +1105,49 @@ function filter_get_active_in_context($context) {
$contextids = str_replace('/', ',', trim($context->path, '/')); $contextids = str_replace('/', ',', trim($context->path, '/'));
// The following SQL is tricky. It is explained on // Postgres recordset performance is much better with a limit.
// http://docs.moodle.org/dev/Filter_enable/disable_by_context. // This should be much larger than anything needed in practice. The code below checks we don't hit this limit.
$sql = "SELECT active.filter, fc.name, fc.value $maxpossiblerows = 10000;
FROM (SELECT f.filter, MAX(f.sortorder) AS sortorder // The key line in the following query is the HAVING clause.
FROM {filter_active} f // If a filter is disabled at system context, then there is a row with active -9999 and depth 1,
JOIN {context} ctx ON f.contextid = ctx.id // so the -MIN is always large, and the MAX will be smaller than that and this filter won't be returned.
WHERE ctx.id IN ($contextids) // Otherwise, there will be a bunch of +/-1s at various depths,
GROUP BY filter // and this clause verifies there is a +1 that deeper than any -1.
HAVING MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth) $rows = $DB->get_recordset_sql("
) active SELECT active.filter, fc.name, fc.value
LEFT JOIN {filter_config} fc ON fc.filter = active.filter AND fc.contextid = $context->id
ORDER BY active.sortorder"; FROM (
$rs = $DB->get_recordset_sql($sql); SELECT fa.filter, MAX(fa.sortorder) AS sortorder
FROM {filter_active} fa
JOIN {context} ctx ON fa.contextid = ctx.id
WHERE ctx.id IN ($contextids)
GROUP BY fa.filter
HAVING MAX(fa.active * ctx.depth) > -MIN(fa.active * ctx.depth)
) active
LEFT JOIN {filter_config} fc ON fc.filter = active.filter AND fc.contextid = ?
ORDER BY active.sortorder
", [$context->id], 0, $maxpossiblerows);
// Massage the data into the specified format to return. // Massage the data into the specified format to return.
$filters = array(); $filters = [];
foreach ($rs as $row) { $rowcount = 0;
foreach ($rows as $row) {
$rowcount += 1;
if (!isset($filters[$row->filter])) { if (!isset($filters[$row->filter])) {
$filters[$row->filter] = array(); $filters[$row->filter] = [];
} }
if (!is_null($row->name)) { if (!is_null($row->name)) {
$filters[$row->filter][$row->name] = $row->value; $filters[$row->filter][$row->name] = $row->value;
} }
} }
$rows->close();
$rs->close(); if ($rowcount >= $maxpossiblerows) {
// If this ever did happen, which seems essentially impossible, then it would lead to very subtle and
// hard to understand bugs, so ensure it leads to an unmissable error.
throw new coding_exception('Hit the row limit that should never be hit in filter_get_active_in_context.');
}
return $filters; return $filters;
} }