mirror of
https://github.com/moodle/moodle.git
synced 2025-04-22 00:42:54 +02:00
accesslib: Introducing cleanup_contexts() and use it in cron
The newly minted cleanup_contexts() walks the context table matching contexts with the tables they are referencing and clearing any leftover contexts.
This commit is contained in:
parent
40e0bea5cc
commit
17b0efae11
@ -368,6 +368,9 @@
|
||||
unset($authplugin);
|
||||
}
|
||||
|
||||
// Accesslib stuff
|
||||
cleanup_contexts();
|
||||
|
||||
if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {
|
||||
|
||||
// check we're not before our runtime
|
||||
|
@ -1780,11 +1780,11 @@ function create_system_context() {
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Create a new context record for use by all roles-related stuff
|
||||
* Remove a context record and any dependent entries
|
||||
* @param $level
|
||||
* @param $instanceid
|
||||
*
|
||||
* @return true if properly deleted
|
||||
* @return bool properly deleted
|
||||
*/
|
||||
function delete_context($contextlevel, $instanceid) {
|
||||
if ($context = get_context_instance($contextlevel, $instanceid)) {
|
||||
@ -1797,6 +1797,74 @@ function delete_context($contextlevel, $instanceid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove stale context records
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function cleanup_contexts() {
|
||||
global $CFG;
|
||||
|
||||
$sql = " SELECT " . CONTEXT_COURSECAT . " AS level,
|
||||
c.instanceid AS instanceid
|
||||
FROM {$CFG->prefix}context c
|
||||
LEFT OUTER JOIN {$CFG->prefix}course_categories AS t
|
||||
ON c.instanceid = t.id
|
||||
WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSECAT . "
|
||||
UNION
|
||||
SELECT " . CONTEXT_COURSE . " AS level,
|
||||
c.instanceid AS instanceid
|
||||
FROM {$CFG->prefix}context c
|
||||
LEFT OUTER JOIN {$CFG->prefix}course AS t
|
||||
ON c.instanceid = t.id
|
||||
WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSE . "
|
||||
UNION
|
||||
SELECT " . CONTEXT_MODULE . " AS level,
|
||||
c.instanceid AS instanceid
|
||||
FROM {$CFG->prefix}context c
|
||||
LEFT OUTER JOIN {$CFG->prefix}course_modules AS t
|
||||
ON c.instanceid = t.id
|
||||
WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_MODULE . "
|
||||
UNION
|
||||
SELECT " . CONTEXT_USER . " AS level,
|
||||
c.instanceid AS instanceid
|
||||
FROM {$CFG->prefix}context c
|
||||
LEFT OUTER JOIN {$CFG->prefix}user AS t
|
||||
ON c.instanceid = t.id
|
||||
WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_USER . "
|
||||
UNION
|
||||
SELECT " . CONTEXT_BLOCK . " AS level,
|
||||
c.instanceid AS instanceid
|
||||
FROM {$CFG->prefix}context c
|
||||
LEFT OUTER JOIN {$CFG->prefix}block_instance AS t
|
||||
ON c.instanceid = t.id
|
||||
WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_BLOCK . "
|
||||
UNION
|
||||
SELECT " . CONTEXT_GROUP . " AS level,
|
||||
c.instanceid AS instanceid
|
||||
FROM {$CFG->prefix}context c
|
||||
LEFT OUTER JOIN {$CFG->prefix}groups AS t
|
||||
ON c.instanceid = t.id
|
||||
WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_GROUP . "
|
||||
";
|
||||
$rs = get_recordset_sql($sql);
|
||||
if ($rs->RecordCount()) {
|
||||
begin_sql();
|
||||
$tx = true;
|
||||
while ($tx && $ctx = rs_fetch_next_record($rs)) {
|
||||
$tx = $tx && delete_context($ctx->level, $ctx->instanceid);
|
||||
}
|
||||
rs_close($rs);
|
||||
if ($tx) {
|
||||
commit_sql();
|
||||
return true;
|
||||
}
|
||||
rollback_sql();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that object with instanceid really exists in given context level.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user