diff --git a/mod/forum/lib.php b/mod/forum/lib.php index dd86b554659..8b9e303b132 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -220,7 +220,6 @@ function forum_cron() { $forums = array(); $courses = array(); $coursemodules = array(); - $postinfos = array(); $subscribedusers = array(); @@ -325,8 +324,10 @@ function forum_cron() { mtrace('Processing user '.$userto->id); - // we might want to add another layer - forums here (by checking array_keys($subscribedusers)) - // so that we can skip many posts + // init caches + $userto->viewfullnames = array(); + $userto->canpost = array(); + $userto->tracking = array(); foreach ($posts as $pid => $post) { @@ -354,13 +355,26 @@ function forum_cron() { // setup global $COURSE properly - needed for roles and languages course_setup($course); // More environment - // Get the context (from cache) - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); // Cached already - $post->modcontext = $modcontext; - $post->viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext); + // Fill caches + if (!isset($userto->viewfullnames[$forum->id])) { + $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $userto->viewfullnames[$forum->id] = has_capability('moodle/site:viewfullnames', $modcontext); + } + if (!isset($userto->canpost[$forum->id])) { + $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $userto->canpost[$forum->id] = forum_user_can_post($forum, $userto, null, $modcontext); + } + if (!isset($userfrom->groups[$forum->id])) { + if (!isset($userfrom->groups)) { + $userfrom->groups = array(); + $users[$userfrom->id]->groups = array(); + } + $userfrom->groups[$forum->id] = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid); + $users[$userfrom->id]->groups[$forum->id] = $userfrom->groups[$forum->id]; + } // Make sure groups allow this user to see this email - if ($discussion->groupid > 0 and $groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used + if ($discussion->groupid > 0 and $groupmode = groups_get_activity_groupmode($cm, $course)) { // Groups are being used if (!groups_group_exists($discussion->groupid)) { // Can't find group continue; // Be safe and don't send it to anyone } @@ -430,9 +444,13 @@ function forum_cron() { } else { $mailcount[$post->id]++; + if (!isset($userto->tracking[$forum->id])) { + $userto->tracking[$forum->id] = !$CFG->forum_usermarksread + && forum_tp_can_track_forums($forum, $userto) + && forum_tp_is_tracked($forum, $userto->id); + } // Mark post as read if forum_usermarksread is set off - if (!$CFG->forum_usermarksread && forum_tp_can_track_forums($forum, $userto) && - forum_tp_is_tracked($forum, $userto->id)) { + if ($userto->tracking[$forum->id]) { if (!forum_tp_mark_post_read($userto->id, $post, $forum->id)) { mtrace("Error: mod/forum/cron.php: Could not mark post $post->id read for user $userto->id". " while sending email."); @@ -569,6 +587,11 @@ function forum_cron() { $USER = $userto; course_setup(SITEID); + // init caches + $userto->viewfullnames = array(); + $userto->canpost = array(); + $userto->tracking = array(); + $postsubject = get_string('digestmailsubject', 'forum', format_string($site->shortname, true)); $headerdata = new object(); @@ -585,6 +608,8 @@ function forum_cron() { $posthtml .= "\n
\n"; $posthtml .= ''.get_string('digestmailheader', 'forum', $headerdata).'
| '; - $output .= print_user_picture($user, $course->id, $user->picture, false, true); + $output .= print_user_picture($userfrom, $course->id, $userfrom->picture, false, true); $output .= ' | '; if ($post->parent) { @@ -2079,17 +2122,27 @@ function forum_make_mail_post(&$post, $user, $touser, $course, } $output .= '
| '; - if ($group = groups_get_all_groups($course->id, $user->id)) { - $output .= print_group_picture($group, $course->id, false, true, true); + + if (isset($userfrom->groups)) { + $groups = $userfrom->groups[$forum->id]; + } else { + if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { + error('Course Module ID was incorrect'); + } + $group = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid); + } + + if ($groups) { + $output .= print_group_picture($groups, $course->id, false, true, true); } else { $output .= ' '; } @@ -3643,15 +3696,14 @@ function forum_user_can_see_post($forum, $discussion, $post, $user=NULL, $cm=NUL error('Course Module ID was incorrect'); } } - if (!isset($post->modcontext)) { - $post->modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); - } if (empty($user) || empty($user->id)) { $user = $USER; } - if (!has_capability('mod/forum:viewdiscussion', $post->modcontext, $user->id)) { + $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + + if (!has_capability('mod/forum:viewdiscussion', $modcontext, $user->id)) { return false; } @@ -3664,7 +3716,7 @@ function forum_user_can_see_post($forum, $discussion, $post, $user=NULL, $cm=NUL return (forum_user_has_posted($forum->id,$discussion->id,$user->id) || $firstpost->id == $post->id || - has_capability('mod/forum:viewqandawithoutposting', $post->modcontext, false, $user->id)); + has_capability('mod/forum:viewqandawithoutposting', $modcontext, false, $user->id)); } return true; } @@ -4981,7 +5033,7 @@ function forum_tp_can_track_forums($forum=false, $user=false) { if ($user === false) { // Must be logged in and not a guest. - $isauser = isloggedin() && !isguest(); + $isauser = isloggedin() && !isguestuser(); $user = $USER; } else { $isauser = true; |