From 1051a2a4a016813e5c0657a84df95fbfcdc8061d Mon Sep 17 00:00:00 2001 From: mchurch Date: Mon, 28 Feb 2005 21:31:13 +0000 Subject: [PATCH] Improved speed of database calls for read tracking. --- mod/forum/index.php | 29 ++++++++++++++++++++++++----- mod/forum/lib.php | 40 ++++++++++++++++++++++++++++++++++++++-- mod/forum/view.php | 19 ++++++++++++++++++- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/mod/forum/index.php b/mod/forum/index.php index 1b94abbc528..1fc3f075f04 100644 --- a/mod/forum/index.php +++ b/mod/forum/index.php @@ -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 = ''.$unread.''; } 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 = ''.$unread.''; } 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. + +?> \ No newline at end of file diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 838e5978c2c..184ca95da51 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -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 = ''; diff --git a/mod/forum/view.php b/mod/forum/view.php index 57f2f1a4252..e808fe1a3ff 100644 --- a/mod/forum/view.php +++ b/mod/forum/view.php @@ -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. + +?> \ No newline at end of file