_comment_update_node_statistics

Versions
4.6 – 7
_comment_update_node_statistics($nid)

Updates the comment statistics for a given node. This should be called any time a comment is added, deleted, or updated.

The following fields are contained in the node_comment_statistics table.

  • last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
  • last_comment_name: the name of the anonymous poster for the last comment
  • last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
  • comment_count: the total number of approved/published comments on this node.

Code

modules/comment.module, line 1667

<?php
function _comment_update_node_statistics($nid) {
  $count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = 0', $nid));

  // comments exist
  if ($count > 0) {
    $node = node_load(array('nid' => $nid));
    $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = 0 ORDER BY cid DESC', $nid, 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 ? NULL : $last_reply->name, $last_reply->uid, $nid);
  }

  // no comments
  else {
    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", 0, NULL, 0, 0, $nid);
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.