Skip to content

Commit

Permalink
- Added "x new comments" feature. Requires a SQL update.
Browse files Browse the repository at this point in the history
- Tidied up some comment related code in node.module.
  • Loading branch information
dbuytaert committed Dec 31, 2001
1 parent 69f1b4d commit d85e45b
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 20 deletions.
4 changes: 2 additions & 2 deletions modules/blog.module
Expand Up @@ -180,7 +180,7 @@ function blog_page_user($uid = 0, $date = 0) {
}

if ($blog->comment) {
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(node_get_comments($blog->nid), t("comment"), t("comments")) ."</a>";
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(comment_num_all($blog->nid), t("comment"), t("comments")) ."</a>";
}

$output .= "<tr><td><div style=\"margin-left: 20px;\"><b>". check_output($blog->title) ."</b></div></td><td align=\"right\">". $theme->links($links) ."</td></tr>";
Expand Down Expand Up @@ -217,7 +217,7 @@ function blog_page_last() {
}

if ($blog->comment) {
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(node_get_comments($blog->nid), t("comment"), t("comments")) ."</a>";
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(comment_num_all($blog->nid), t("comment"), t("comments")) ."</a>";
}

$output .= "<tr><td><b>". check_output($blog->title) ."</b></td><td align=\"right\">". $theme->links($links) ."</td></tr>";
Expand Down
4 changes: 2 additions & 2 deletions modules/blog/blog.module
Expand Up @@ -180,7 +180,7 @@ function blog_page_user($uid = 0, $date = 0) {
}

if ($blog->comment) {
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(node_get_comments($blog->nid), t("comment"), t("comments")) ."</a>";
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(comment_num_all($blog->nid), t("comment"), t("comments")) ."</a>";
}

$output .= "<tr><td><div style=\"margin-left: 20px;\"><b>". check_output($blog->title) ."</b></div></td><td align=\"right\">". $theme->links($links) ."</td></tr>";
Expand Down Expand Up @@ -217,7 +217,7 @@ function blog_page_last() {
}

if ($blog->comment) {
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(node_get_comments($blog->nid), t("comment"), t("comments")) ."</a>";
$links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(comment_num_all($blog->nid), t("comment"), t("comments")) ."</a>";
}

$output .= "<tr><td><b>". check_output($blog->title) ."</b></td><td align=\"right\">". $theme->links($links) ."</td></tr>";
Expand Down
55 changes: 53 additions & 2 deletions modules/comment.module
Expand Up @@ -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;

Expand Down Expand Up @@ -460,6 +501,7 @@ function comment_perm() {
}

function comment_link($type, $node = 0, $main = 0) {

if ($type == "admin" && user_access("administer comments")) {
$links[] = "<a href=\"admin.php?mod=comment\">comments</a>";
}
Expand All @@ -473,10 +515,19 @@ function comment_link($type, $node = 0, $main = 0) {
*/

if (user_access("access comments")) {
$links[] = "<a href=\"node.php?id=$node->nid#comment\">". format_plural(node_get_comments($node->nid), "comment", "comments") ."</a>";
$all = comment_num_all($node->nid);
$new = comment_num_new($node->nid);

$links[] = "<a href=\"node.php?id=$node->nid#comment\">". format_plural($all, "comment", "comments") . ($new ? ", $new ". t("new") : "") ."</a>";
}
}
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.
Expand All @@ -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:
Expand Down
55 changes: 53 additions & 2 deletions modules/comment/comment.module
Expand Up @@ -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;

Expand Down Expand Up @@ -460,6 +501,7 @@ function comment_perm() {
}

function comment_link($type, $node = 0, $main = 0) {

if ($type == "admin" && user_access("administer comments")) {
$links[] = "<a href=\"admin.php?mod=comment\">comments</a>";
}
Expand All @@ -473,10 +515,19 @@ function comment_link($type, $node = 0, $main = 0) {
*/

if (user_access("access comments")) {
$links[] = "<a href=\"node.php?id=$node->nid#comment\">". format_plural(node_get_comments($node->nid), "comment", "comments") ."</a>";
$all = comment_num_all($node->nid);
$new = comment_num_new($node->nid);

$links[] = "<a href=\"node.php?id=$node->nid#comment\">". format_plural($all, "comment", "comments") . ($new ? ", $new ". t("new") : "") ."</a>";
}
}
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.
Expand All @@ -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:
Expand Down
7 changes: 1 addition & 6 deletions modules/node.module
Expand Up @@ -14,15 +14,10 @@ function node_help() {
}
}

// TODO: still used by themes, yet doesn't return anything at the moment
// DEPRICATED: still used by themes, yet doesn't return anything at the moment
function node_index() {
}

function node_get_comments($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 node_teaser($body) {

$size = 400;
Expand Down
7 changes: 1 addition & 6 deletions modules/node/node.module
Expand Up @@ -14,15 +14,10 @@ function node_help() {
}
}

// TODO: still used by themes, yet doesn't return anything at the moment
// DEPRICATED: still used by themes, yet doesn't return anything at the moment
function node_index() {
}

function node_get_comments($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 node_teaser($body) {

$size = 400;
Expand Down
10 changes: 10 additions & 0 deletions update.php
Expand Up @@ -41,6 +41,7 @@
"2001-12-16" => "update_14",
"2001-12-24" => "update_15",
"2001-12-30" => "update_16",
"2001-12-31" => "update_17",
);

// Update functions
Expand Down Expand Up @@ -296,6 +297,15 @@ function update_16() {
update_sql("ALTER TABLE comments CHANGE lid nid int(10) NOT NULL;");
}

function update_17() {
update_sql("CREATE TABLE history (
uid int(10) DEFAULT '0' NOT NULL,
nid int(10) DEFAULT '0' NOT NULL,
timestamp int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (uid, nid)
);");
}

// System functions
function update_sql($sql) {
global $edit;
Expand Down

0 comments on commit d85e45b

Please sign in to comment.