Skip to content

Commit

Permalink
MDL-13305 , MDL-13304 - fixed memory leak in forum cron and saved 10%…
Browse files Browse the repository at this point in the history
… of queries when printing user pictures in mails; merged from MOODLE_19_STABLE
  • Loading branch information
skodak committed Feb 3, 2008
1 parent 8a37e90 commit a5cef9c
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions mod/forum/lib.php
Expand Up @@ -288,18 +288,19 @@ function forum_cron() {

// caching subscribed users of each forum
if (!isset($subscribedusers[$forumid])) {
if ($subusers = forum_subscribed_users($courses[$courseid], $forums[$forumid], 0, true)) {
if ($subusers = forum_subscribed_users($courses[$courseid], $forums[$forumid], 0, false)) {
foreach ($subusers as $postuser) {
// do not try to mail users with stopped email
if ($postuser->emailstop) {
add_to_log(SITEID, 'forum', 'mail blocked', '', '', 0, $postuser->id);
continue;
}
// this user is subscribed to this forum
$subscribedusers[$forumid][] = $postuser->id;
$subscribedusers[$forumid][$postuser->id] = $postuser->id;
// this user is a user we have to process later
$users[$postuser->id] = $postuser;
}
unset($subusers); // release memory
}
}

Expand Down Expand Up @@ -327,25 +328,27 @@ function forum_cron() {

foreach ($posts as $pid => $post) {

// Get info about the sending user
if (array_key_exists($post->userid, $users)) { // we might know him/her already
$userfrom = $users[$post->userid];
} else if (!$userfrom = get_record('user', 'id', $post->userid)) {
mtrace('Could not find user '.$post->userid);
continue;
}

// Set up the environment for the post, discussion, forum, course
$discussion = $discussions[$post->discussion];
$forum = $forums[$discussion->forum];
$course = $courses[$forum->course];
$cm = $coursemodules[$forum->id];

// Do some checks to see if we can bail out now
if (empty($subscribedusers[$forum->id]) || !in_array($userto->id, $subscribedusers[$forum->id])) {
if (!isset($subscribedusers[$forum->id][$userto->id])) {
continue; // user does not subscribe to this forum
}

// Get info about the sending user
if (array_key_exists($post->userid, $users)) { // we might know him/her already
$userfrom = $users[$post->userid];
} else if ($userfrom = get_record('user', 'id', $post->userid)) {
$users[$userfrom->id] = $userfrom; // fetch only once, we can add it to user list, it will be skipped anyway
} else {
mtrace('Could not find user '.$post->userid);
continue;
}

// setup global $COURSE properly - needed for roles and languages
course_setup($course); // More environment

Expand Down Expand Up @@ -2062,7 +2065,7 @@ function forum_make_mail_post(&$post, $user, $touser, $course,
$output = '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';

$output .= '<tr class="header"><td width="35" valign="top" class="picture left">';
$output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
$output .= print_user_picture($user, $course->id, $user->picture, false, true);
$output .= '</td>';

if ($post->parent) {
Expand Down

0 comments on commit a5cef9c

Please sign in to comment.