From d85e45bf64cb2715f116eae1e0ea422419c0a358 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 31 Dec 2001 13:02:53 +0000 Subject: - Added "x new comments" feature. Requires a SQL update. - Tidied up some comment related code in node.module. --- modules/comment.module | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'modules/comment.module') diff --git a/modules/comment.module b/modules/comment.module index a31558972..dc58b5a57 100644 --- a/modules/comment.module +++ b/modules/comment.module @@ -12,6 +12,47 @@ function comment_settings($mode, $order, $threshold) { } } +function comment_num_all($nid) { + $comment = db_fetch_object(db_query("SELECT COUNT(c.nid) AS number FROM node n LEFT JOIN comments c ON n.nid = c.nid WHERE n.nid = '$nid' GROUP BY n.nid")); + return $comment->number ? $comment->number : 0; +} + +function comment_num_new($nid) { + global $user; + + if ($user->uid) { + + /* + ** Retrieve the timestamp at which the current user last viewed + ** the specified node and use this timestamp to find the number + ** of new comments. + */ + + $history = db_fetch_object(db_query("SELECT timestamp FROM history WHERE uid = '$user->uid' AND nid = '$nid'")); + $comment = db_fetch_object(db_query("SELECT COUNT(c.nid) AS number FROM node n LEFT JOIN comments c ON n.nid = c.nid WHERE n.nid = '$nid' AND timestamp > '". ($history->timestamp ? $history->timestamp : 0) ."' GROUP BY n.nid")); + + return $comment->number ? $comment->number : 0; + } + else { + return 0; + } + +} + +function comment_tag_new($nid) { + global $user; + + if ($user->uid) { + $result = db_query("SELECT timestamp FROM history WHERE uid = '$user->uid' AND nid = '$nid'"); + if (db_fetch_object($result)) { + db_query("UPDATE history SET timestamp = '". time() ."' WHERE uid = '$user->uid' AND nid = '$nid'"); + } + else { + db_query("INSERT INTO history (uid, nid, timestamp) VALUES ('$user->uid', '$nid', '". time() ."')"); + } + } +} + function comment_access($op, $comment) { global $user; @@ -460,6 +501,7 @@ function comment_perm() { } function comment_link($type, $node = 0, $main = 0) { + if ($type == "admin" && user_access("administer comments")) { $links[] = "comments"; } @@ -473,10 +515,19 @@ function comment_link($type, $node = 0, $main = 0) { */ if (user_access("access comments")) { - $links[] = "nid#comment\">". format_plural(node_get_comments($node->nid), "comment", "comments") .""; + $all = comment_num_all($node->nid); + $new = comment_num_new($node->nid); + + $links[] = "nid#comment\">". format_plural($all, "comment", "comments") . ($new ? ", $new ". t("new") : "") .""; } } else { + /* + ** Tag the node's comments as read: + */ + + comment_tag_new($node->nid); + /* ** Node page: add a "post comment" link if the user is allowed to ** post comments. @@ -493,7 +544,7 @@ function comment_link($type, $node = 0, $main = 0) { function comment_node_link($node) { - if (user_access("administer comments") && node_get_comments($node->nid)) { + if (user_access("administer comments") && comments_all($node->nid)) { /* ** Edit comments: -- cgit v1.2.3