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";
Comment | File | Size | Author |
---|---|---|---|
#1 | load_last_comment_uid.patch | 703 bytes | edgar83 |
Comments
Comment #1
edgar83 CreditAttribution: edgar83 commentedOkay, 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.
Comment #2
rudders CreditAttribution: rudders commentedProblem still seems present in 6.10
Comment #3
VangelisP CreditAttribution: VangelisP commentedAlthough 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!
Comment #5
pcambra