You are here

function discussthis_comment_render in Discuss This! 6

\brief Render comments.

This function renders comments as the Comment module would do. (I started with a copy of comment_render())

There are mainly two things that we don't do in this version:

1) we do not want to have the pager

2) we want to use the Discuss This! counter

\note This function uses the default themes as defined in the comment module.

\todo The mode and sort are currently taken from the Topic linked to the node passed as a parameter. It may be preferable to have our own Discuss This! settings on a per node type.

\param[in] $node The node that will receive the comments

1 call to discussthis_comment_render()
discussthis_nodeapi in ./discussthis.module
\brief hook_nodeapi implementation

File

./discussthis.module, line 261
Associations discussions in forums with specific nodes

Code

function discussthis_comment_render($node) {

  // make sure the user wants to see something
  $comments_per_page = variable_get('discussthis_comments', 0);
  if ($comments_per_page <= 0) {
    return '';
  }

  // user can access comments?
  if (!user_access('access comments')) {
    return '';
  }

  // node has a topic attached?
  // (we are actually interested by the topic comments!)
  $nid = _discussthis_get_topic($node->nid);
  if (!$nid) {
    return '';
  }
  $topic = node_load(array(
    'nid' => $nid,
  ));

  // use the topic settings (or should we use the node?!)
  // TODO: maybe we want to add an entry in the node type
  //       so Discuss This! can have its own settings?
  $mode = _comment_get_display_setting('mode', $topic);
  $order = _comment_get_display_setting('sort', $topic);

  // using c.cid AS cid because of the SQL rewrite below
  $query = 'SELECT c.cid AS cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp,' . ' c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature,' . ' u.signature_format, u.picture, u.data, c.thread, c.status' . ' FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d';
  $query_args = array(
    $nid,
  );
  if (!user_access('administer comments')) {
    $query .= ' AND c.status = %d';
    $query_args[] = COMMENT_PUBLISHED;
  }
  if ($order == COMMENT_ORDER_NEWEST_FIRST) {
    if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
      $query .= ' ORDER BY c.cid DESC';
    }
    else {
      $query .= ' ORDER BY c.thread DESC';
    }
  }
  elseif ($order == COMMENT_ORDER_OLDEST_FIRST) {
    if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
      $query .= ' ORDER BY c.cid';
    }
    else {

      // See Comment module for more info.
      $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
    }
  }
  $query = db_rewrite_sql($query, 'c', 'cid');

  // Start a form, for use with comment control.
  $result = db_query_range($query, $query_args, 0, $comments_per_page);
  $divs = 0;
  $comments = '';
  drupal_add_css(drupal_get_path('module', 'comment') . '/comment.css');
  while ($comment = db_fetch_object($result)) {
    $comment = drupal_unpack($comment);
    $comment->depth = count(explode('.', $comment->thread)) - 1;
    if ($comment->uid) {
      $comment->name = $comment->registered_name;
    }
    if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
      if ($comment->depth > $divs) {
        $divs++;
        $comments .= '<div class="indented">';
      }
      else {
        while ($comment->depth < $divs) {
          $divs--;
          $comments .= '</div>';
        }
      }
    }
    if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
      $comments .= theme('comment_flat_collapsed', $comment, $topic);
    }
    elseif ($mode == COMMENT_MODE_FLAT_EXPANDED) {
      $comments .= theme('comment_flat_expanded', $comment, $topic);
    }
    elseif ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
      $comments .= theme('comment_thread_collapsed', $comment, $topic);
    }
    elseif ($mode == COMMENT_MODE_THREADED_EXPANDED) {
      $comments .= theme('comment_thread_expanded', $comment, $topic);
    }
  }
  for (; $divs > 0; --$divs) {
    $comments .= '</div>';
  }
  if ($comments) {
    return '<div class="discussthis-comments">' . theme('comment_wrapper', $comments, $topic) . '</div>';
  }
  return '';
}