]> git.mjollnir.org Git - moodle.git/commitdiff
accesslib: Introducing cleanup_contexts() and use it in cron
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 06:50:53 +0000 (06:50 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 06:50:53 +0000 (06:50 +0000)
The newly minted cleanup_contexts() walks the context table
matching contexts with the tables they are referencing and
clearing any leftover contexts.

admin/cron.php
lib/accesslib.php

index d001e2e8075883eead3b8676d4a6c7225036abb4..c9e7b57939d23e872f215e1cca43d4c0672324ee 100644 (file)
         unset($authplugin);
     }
 
+    // Accesslib stuff
+    cleanup_contexts();
+
     if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {
 
         // check we're not before our runtime
index f99242300d67892a39311479fc46405b75a3010a..7321ffb83b844e4ff8c88c73b515696840c7e439 100755 (executable)
@@ -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.
  *