Permalink
Browse files

Improved speed of database calls for read tracking.

  • Loading branch information...
1 parent 4cff67d commit 1051a2a4a016813e5c0657a84df95fbfcdc8061d mchurch committed Feb 28, 2005
Showing with 80 additions and 8 deletions.
  1. +24 −5 mod/forum/index.php
  2. +38 −2 mod/forum/lib.php
  3. +18 −1 mod/forum/view.php
View
@@ -4,6 +4,19 @@
require_once("lib.php");
require_once("$CFG->libdir/rsslib.php");
+/// Page timer:
+if ($CFG->debug and $CFG->debug > 7) {
+function benchmark() {
+ // microtime() outputs the seconds and the milli seconds
+ // separated with a space, so you have to explode it to
+ // access the parts independently
+ list($usec, $sec) = explode(' ', microtime());
+ return ((double)$usec + (double)$sec);
+}
+$start = benchmark();
+}
+/// /Page timer.
+
optional_variable($id); // course
if ($id) {
@@ -128,8 +141,9 @@
if ($CFG->forum_trackreadposts) {
$groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
- $unread = forum_tp_count_forum_posts($forum->id, $groupid) -
- forum_tp_count_forum_read_records($USER->id, $forum->id, $groupid);
+// $unread = forum_tp_count_forum_posts($forum->id, $groupid) -
+// forum_tp_count_forum_read_records($USER->id, $forum->id, $groupid);
+ $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
if ($unread > 0) {
$unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
} else {
@@ -251,8 +265,9 @@
if ($CFG->forum_trackreadposts) {
$groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
- $unread = forum_tp_count_forum_posts($forum->id, $groupid) -
- forum_tp_count_forum_read_records($USER->id, $forum->id, $groupid);
+// $unread = forum_tp_count_forum_posts($forum->id, $groupid) -
+// forum_tp_count_forum_read_records($USER->id, $forum->id, $groupid);
+ $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
if ($unread > 0) {
$unreadlink = '<span class="unread">'.$unread.'</span>';
} else {
@@ -369,4 +384,8 @@
print_footer($course);
-?>
+/// Page timer:
+if ($CFG->debug and $CFG->debug > 7) echo 'Page took: ' . round((benchmark() - $start), 5) . ' seconds.';
+/// /Page timer.
+
+?>
View
@@ -2715,8 +2715,9 @@ function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5,
$discussion->unread = 0;
} else {
/// Add in the unread posts. Add one to the replies to include the original post.
- $discussion->unread = $discussion->replies+1 -
- forum_tp_count_discussion_read_records($USER->id, $discussion->discussion);
+// $discussion->unread = $discussion->replies+1 -
+// forum_tp_count_discussion_read_records($USER->id, $discussion->discussion);
+ $discussion->unread = forum_tp_count_discussion_unread_posts($USER->id, $discussion->discussion);
}
}
@@ -3258,6 +3259,21 @@ function forum_tp_count_discussion_read_records($userid, $discussionid) {
return (count_records_sql($sql));
}
+function forum_tp_count_discussion_unread_posts($userid, $discussionid) {
+ /// Returns the count of records for the provided user and discussion.
+ global $CFG;
+
+ $cutoffdate = isset($CFG->forum_oldpostdays) ? (time() - ($CFG->forum_oldpostdays*24*60*60)) : 0;
+
+ $sql = 'SELECT COUNT(p.id) '.
+ 'FROM '.$CFG->prefix.'forum_posts p '.
+ 'LEFT JOIN '.$CFG->prefix.'forum_read r ON r.postid = p.id AND r.userid = '.$userid.' '.
+ 'WHERE p.discussion = '.$discussionid.' '.
+ 'AND p.modified >= '.$cutoffdate.' AND r.id is NULL';
+
+ return (count_records_sql($sql));
+}
+
function forum_tp_count_forum_posts($forumid, $groupid=false) {
/// Returns the count of posts for the provided forum and [optionally] group.
global $CFG;
@@ -3293,6 +3309,26 @@ function forum_tp_count_forum_read_records($userid, $forumid, $groupid=false) {
return (count_records_sql($sql));
}
+function forum_tp_count_forum_unread_posts($userid, $forumid, $groupid=false) {
+ /// Returns the count of records for the provided user and forum and [optionally] group.
+ global $CFG;
+
+ $cutoffdate = isset($CFG->forum_oldpostdays) ? (time() - ($CFG->forum_oldpostdays*24*60*60)) : 0;
+
+ $groupsel = '';
+ if ($groupid !== false) {
+ $groupsel = ' AND (d.groupid = '.$groupid.' OR d.groupid = -1)';
+ }
+
+ $sql = 'SELECT COUNT(p.id) '.
+ 'FROM '.$CFG->prefix.'forum_posts p,'.$CFG->prefix.'forum_discussions d '.
+ 'LEFT JOIN '.$CFG->prefix.'forum_read r ON r.postid = p.id AND r.userid = '.$userid.' '.
+ 'WHERE d.forum = '.$forumid.$groupsel.' AND p.discussion = d.id '.
+ 'AND p.modified >= '.$cutoffdate.' AND r.id is NULL';
+
+ return (count_records_sql($sql));
+}
+
function forum_tp_delete_read_records($userid=-1, $postid=-1, $discussionid=-1, $forumid=-1) {
/// Deletes read records for the specified index. At least one parameter must be specified.
$select = '';
View
@@ -4,6 +4,19 @@
require_once("lib.php");
require_once("$CFG->libdir/rsslib.php");
+/// Page timer:
+if ($CFG->debug and $CFG->debug > 7) {
+function benchmark() {
+ // microtime() outputs the seconds and the milli seconds
+ // separated with a space, so you have to explode it to
+ // access the parts independently
+ list($usec, $sec) = explode(' ', microtime());
+ return ((double)$usec + (double)$sec);
+}
+$start = benchmark();
+}
+/// /Page timer.
+
$id = optional_param('id', 0, PARAM_INT); // Course Module ID
$f = optional_param('f', 0, PARAM_INT); // Forum ID
$mode = optional_param('mode', 0, PARAM_INT); // Display mode (for single forum)
@@ -241,4 +254,8 @@
print_footer($course);
-?>
+/// Page timer:
+if ($CFG->debug and $CFG->debug > 7) echo 'Page took: ' . round((benchmark() - $start), 5) . ' seconds.';
+/// /Page timer.
+
+?>

0 comments on commit 1051a2a

Please sign in to comment.