From: martinlanghoff Date: Wed, 19 Sep 2007 07:20:24 +0000 (+0000) Subject: accesslib: has_capability() now supports fake $USER for forum cron X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=1a9b67874fe7300f7820eb664a25d48f3f5f6f7d;p=moodle.git accesslib: has_capability() now supports fake $USER for forum cron 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! --- diff --git a/lib/accesslib.php b/lib/accesslib.php index 01513d83c3..aeb2bcadfd 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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)) {