I would very much like to modify the tracker on my site so that it includes, for every node, a link to the last comment that was posted. This feature is pretty common in forum software like PHPBB, Simple Machines, etc.

However, after filing a feature request for the Views module (http://drupal.org/node/292126 for details) I received a wontfix response from the developer.

After investigating the reasons given, I have to agree with him that it's pretty difficult to do. Yes, it's possible, but it's certainly not trivial. And the thing is, it could be trivial!

All that needs to happen is for the comment module to store the last comment's cid in the node_comment_statistics table whenever it updates that table. I mean, if we look at the code, the cid even gets retrieved from the database, it just never gets used...

    $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1));
    db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Well, isn't that what comment_new_page_count() is for? [1]

The current data structure assume that most of the time the number of comments of a node will not be superior to the maximum number of comment per page.

A last_cid could be a nice addition, anyway.

[1] http://api.drupal.org/api/function/comment_new_page_count/6

Daitaka’s picture

I'll certainly look into it.

However, so far I've found that the 'new ' status of comments and nodes does not accurately reflect what I'm looking for. I want a link to the last comment made in a forum thread, no matter how many years ago it was posted. Drupal's 'new' status seems to be based solely on what was posted since my last visit to the site, and on any subsequent visits, if there is nothing new then there's also no link available.

Pasqualle’s picture

Version: 6.3 » 7.x-dev

#188847: ifac follow-up: easy way to get to end of issue

I think if a simple SQL to get the last comment can run on d.o. then it can run on your site also..

Michelle’s picture

Just to add another use case, I finally had to drop the last comment from the last post info in advanced forum because it was just to costly to get. Having this would have made my life much easier. I think it would be a good addition.

Michelle

sun’s picture

Views is unable to display anything for the last comment: #196439: A 'Last Comment' field/relationship would be nice

I have no idea why we store arbitrary values instead of the {comment}.cid - that's totally insane.

merlinofchaos’s picture

Storing arbitrary values is actually rather handy for the nodecomment.module

That said, a cid would still be very valuable.

sun’s picture

Status: Active » Needs review
FileSize
5.33 KB

Quite simple + even allows to simplify some existing queries (perhaps even more, but these were the obvious instances I found by grepping).

Status: Needs review » Needs work

The last submitted patch failed testing.

sun’s picture

Status: Needs work » Needs review
FileSize
6.12 KB

oopsie. ;)

Status: Needs review » Needs work

The last submitted patch failed testing.

catch’s picture

This would almost certainly help with some barmy forum queries too (and if not, forum feature requests).

sun’s picture

Status: Needs work » Needs review
FileSize
6.11 KB

This one should pass again.

Status: Needs review » Needs work

The last submitted patch failed testing.

sun’s picture

Category: feature » task
Status: Needs work » Needs review
FileSize
6.43 KB

Now for real.

Also fixing a missing 'node_access' query tag for the recent comments block that's touched by this patch.

sun’s picture

ok - final review, anyone?

Dries’s picture

I vaguely remember we spent a lot of time optimizing that code in presence of large data sets. Maybe it has been deprecated, maybe it had good reasons, maybe I'm mistaken. I'll try to dig the archives tomorrow to see if I can dig up the details.

Dries’s picture

Status: Needs review » Reviewed & tested by the community

Mmm, I tried to look for it but can't find anything. This is probably RTBC then. Let's mark it as RTBC and see if someone objects ...

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Nick Brown’s picture

Any chance of this being backported to druapl 6.x ?

Tarsjusz’s picture

backporting is definately a good idea

grendzy’s picture

Opened a follow-up here: #1149912: Recent comments block displays old, out-of-order comments.

The reason for orderBy('ncs.last_comment_timestamp') isn't clear... It seems to produce nonsensical results.

Dean Reilly’s picture

In case anyone stumbles on this like I did, I've been working on back-porting this patch to Drupal 6 via a module. You can take a look at how far I've got here:

http://drupal.org/sandbox/DeanReilly/1587188

It seems to be working pretty well but it won't match the core patch for performance due to the nature of comment hooks in Drupal 6.