]> git.mjollnir.org Git - moodle.git/commitdiff
accesslib: has_capability() now supports fake $USER for forum cron
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:20:24 +0000 (07:20 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:20:24 +0000 (07:20 +0000)
has_capability() can handle the fake user that forum cron sets up
and will load the appropriate accessdata into $USER->access.

This makes forum cron work again. A test comparison between before
this patchseries yields:

With 1 forum post, sent total 24 times
 - Before 11 000 DB queries (approx)
 - After     506 DB queries

With 6 forum posts, sent a total of 452 times
 - Before 47 876 DB queries
 - After   8 256 DB queries

There is a very high variability, but we are going from 100-500
queries per sent email to 18-21 queries per sent email. The
variability probably stems from 2 of the 6 posts being in a 200-user
forum.

Still huge - by the time we are sending the email, we should know
everything we need to know about the user, the forum/thread/post and
the form. The average should be well below 1 DB query per email sent!

lib/accesslib.php

index 01513d83c355bf018a756019e59c3d2876a86494..aeb2bcadfd439d94611f9e22b4861081b5893a1c 100755 (executable)
@@ -421,6 +421,22 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
         load_all_capabilities();
     }
 
+    if (FULLME === 'cron' && !isset($USER->access)) {
+        //
+        // In cron, some modules setup a 'fake' $USER,
+        // ensure we load the appropriate accessdata.
+        // Also: set $DIRTYCONTEXTS to empty
+        // 
+        if (!isset($ACCESS)) {
+            $ACCESS = array();
+        }
+        if (!isset($ACCESS[$userid])) {
+            load_user_accessdata($userid);
+        }
+        $USER->access = $ACCESS[$userid];
+        $DIRTYCONTEXTS = array();
+    }
+
     // Careful check for staleness...
     $clean = true;
     if (!isset($DIRTYCONTEXTS)) {