You are here

function comment_goodness_get_display_ordinal in Comment goodness 7

Get the display ordinal for a comment, starting from 0.

Count the number of comments which appear before the comment we want to display, taking into account display settings, threading and sort order.

Parameters

$cid: The comment ID.

$node_type: The node type of the comment's parent.

Return value

The display ordinal for the comment.

See also

comment_get_display_ordinal()

1 call to comment_goodness_get_display_ordinal()
comment_goodness_get_display_page in ./comment_goodness.module
Return the page number for a comment.

File

./comment_goodness.module, line 601
Comment goodness provides newest to oldest comment sorting

Code

function comment_goodness_get_display_ordinal($cid, $node_type) {

  // Count how many comments (c1) are before $cid (c2) in display order. This is
  // the 0-based display ordinal.
  $query = db_select('comment', 'c1');
  $query
    ->innerJoin('comment', 'c2', 'c2.nid = c1.nid');
  $query
    ->addExpression('COUNT(*)', 'count');
  $query
    ->condition('c2.cid', $cid);
  if (!user_access('administer comments')) {
    $query
      ->condition('c1.status', COMMENT_PUBLISHED);
  }

  // Get the configured default sort ordering for this node type.
  $sort = variable_get('comment_default_sorting_' . $node_type, comment_goodness_OLDER_FIRST);
  $operation = $sort == comment_goodness_NEWER_FIRST ? '>' : '<';
  $mode = variable_get('comment_default_mode_' . $node_type, COMMENT_MODE_THREADED);
  if ($mode == COMMENT_MODE_FLAT) {

    // For flat comments, cid is used for ordering comments due to
    // unpredicatable behavior with timestamp, so we make the same assumption
    // here.
    $query
      ->condition('c1.cid', $cid, $operation);
  }
  else {

    // For threaded comments, the c.thread column is used for ordering.
    if ($sort == comment_goodness_NEWER_FIRST) {

      // For newer to older sorted comments, we look for how many comments have
      // a higher thread value than the current comment.
      $query
        ->where('c1.thread ' . $operation . ' c2.thread');
    }
    else {

      // We can use the vancode for comparison, but must remove the
      // trailing slash.
      // See comment_view_multiple().
      $query
        ->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) -1)) ' . $operation . ' SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) -1))');
    }
  }
  return $query
    ->execute()
    ->fetchField();
}