comment_new_page_count

Definition

comment_new_page_count($num_comments, $new_replies, $node)
drupal/modules/comment/comment.module, line 363

Description

Calculate page number for first new comment.

Parameters

$num_comments Number of comments.

$new_replies Number of new replies.

$node The first new comment node.

Return value

"page=X" if the page number is greater than zero; empty string otherwise.

Code

function comment_new_page_count($num_comments, $new_replies, $node) {
  $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
  $mode = _comment_get_display_setting('mode', $node);
  $pagenum = NULL;
  $flat = in_array($mode, array(COMMENT_MODE_FLAT_COLLAPSED, COMMENT_MODE_FLAT_EXPANDED));
  if ($num_comments <= $comments_per_page) {
    // Only one page of comments.
    $pageno = 0;
  }
  elseif ($flat) {
    // Flat comments.
    $count = $num_comments - $new_replies;
    $pageno$count / $comments_per_page;
  }
  else {
    // Threaded comments.
    // Find the first thread with a new comment.
    $result = db_query_range('(SELECT thread
      FROM {comment}
      WHERE nid = :nid
        AND status = 0
      ORDER BY timestamp DESC)
      ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1))'
, array(':nid' => $node->nid), 0, $new_replies)
      ->fetchField();
    $thread = substr($result, 0, -1);
    $count = db_query('SELECT COUNT(*) FROM {comment} WHERE nid = :nid AND status = 0 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
      ':nid' => $node->nid,
      ':thread' => $thread))
      ->fetchField();
    $pageno$count / $comments_per_page;
  }

  if ($pageno >= 1) {
    $pagenum = "page=" . intval($pageno);
  }

  return $pagenum;
}