I want to display some statistics about the comments on the current node in my node.tpl.php. Like this:
"There are 6 comments. User XYZ has posted the latest message on 3 december 2008."

I tried to use $node->last_comment_name in the node template. But this only works if the latest message was not a logged in user.

The problem is that the function _comment_update_node_statistics does not store the username for logged in users.
Instead it stores its user id, but the function comment_nodeapi does not load the last_comment_uid from the node_comment_statistics table.

So there is no way I can tell who sent the last comment if it was a logged in user

I think _comment_update_node_statistics should always store the username too. It does this check now while updating the statistics table:

$last_reply->uid ? '' : $last_reply->name

But the last reply name is set for logged in users as well. So, why the check?
The only thing i can image this check is there to prevent storing possible future outdated usernames. This field would reflect the username the user had while posting the comment, not the current one.

So would it be wise to just removed the check in _comment_update_node_statistics and always fill the last_comment_name field with $last_reply->name?

Other options are:
* Select the last_comment_uid too in comment_nodeapi and let other (theme/module) code figure out the username
* Join the (current) username from the users table while loading in comment_nodeapi (see below how the forum module does this)

By the way, the forum module works around this problem using this query:

(forum.module:532)
    // table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
    // used to join node_comment_statistics to users.
    $sql = "SELECT ncs.last_comment_timestamp, <strong>IF (ncs.last_comment_uid != 0, u2.name, ncs.last_comment_name)</strong> AS last_comment_name, ncs.last_comment_uid FROM {node} n INNER JOIN {users} u1 ON n.uid = u1.uid INNER JOIN {term_node} tn ON n.vid = tn.vid INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {users} u2 ON ncs.last_comment_uid=u2.uid WHERE n.status = 1 AND tn.tid = %d ORDER BY ncs.last_comment_timestamp DESC";
CommentFileSizeAuthor
#1 load_last_comment_uid.patch703 bytesedgar83
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

edgar83’s picture

Version: 6.6 » 6.8
Status: Active » Needs review
FileSize
703 bytes

Okay, my bugreport was a little confusing, maybe...
But anyway, I needed this to work properly, so I fixed it.

This patch fixes the comment_nodeapi.
The query selects the last_comment_uid from the node_comment_statistics table.

I can get the username from within my theming functions now.

rudders’s picture

Version: 6.8 » 6.10

Problem still seems present in 6.10

VangelisP’s picture

Title: Last_comment_name not set for logged in users » Thank you!

Although though nobody took the time to reply (probably nobody had the issue) or pass the patch to core, with your patch I managed to do what I was looking for.

A big THANKS for your effort & patch man! I really appreciate it!

Status: Needs review » Needs work

The last submitted patch, load_last_comment_uid.patch, failed testing.

pcambra’s picture

Title: Thank you! » Last_comment_name not set for logged in users

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.