Skip to content

Commit

Permalink
Merge branch 'MDL-32931_22' of git://github.com/dmonllao/moodle into …
Browse files Browse the repository at this point in the history
…MOODLE_22_STABLE
  • Loading branch information
Sam Hemelryk committed Jul 31, 2012
2 parents ab474e3 + b729292 commit dd204f3
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions mod/forum/lib.php
Expand Up @@ -1221,23 +1221,33 @@ function forum_print_overview($courses,&$htmlarray) {
return;
}


// get all forum logs in ONE query (much better!)
// Courses to search for new posts
$coursessqls = array();
$params = array();
$sql = "SELECT instance,cmid,l.course,COUNT(l.id) as count FROM {log} l "
." JOIN {course_modules} cm ON cm.id = cmid "
." WHERE (";
foreach ($courses as $course) {
$sql .= '(l.course = ? AND l.time > ?) OR ';
$params[] = $course->id;
$params[] = $course->lastaccess;
}
$sql = substr($sql,0,-3); // take off the last OR

$sql .= ") AND l.module = 'forum' AND action = 'add post' "
." AND userid != ? GROUP BY cmid,l.course,instance";
// If the user has never entered into the course all posts are pending
if ($course->lastaccess == 0) {
$coursessqls[] = '(f.course = ?)';
$params[] = $course->id;

// Only posts created after the course last access
} else {
$coursessqls[] = '(f.course = ? AND p.created > ?)';
$params[] = $course->id;
$params[] = $course->lastaccess;
}
}
$params[] = $USER->id;
$coursessql = implode(' OR ', $coursessqls);

$sql = "SELECT f.id, COUNT(*) as count "
.'FROM {forum} f '
.'JOIN {forum_discussions} d ON d.forum = f.id '
.'JOIN {forum_posts} p ON p.discussion = d.id '
."WHERE ($coursessql) "
.'AND p.userid != ? '
.'GROUP BY f.id';

if (!$new = $DB->get_records_sql($sql, $params)) {
$new = array(); // avoid warnings
Expand Down

0 comments on commit dd204f3

Please sign in to comment.