You are here

function commentactivity_comment in Activity 5.3

Same name and namespace in other branches
  1. 5.4 contrib/commentactivity/commentactivity.module \commentactivity_comment()
  2. 6 contrib/commentactivity/commentactivity.module \commentactivity_comment()

Implementation of hook_comment().

File

contrib/commentactivity/commentactivity.module, line 78

Code

function commentactivity_comment($comment, $op) {
  switch ($op) {

    // $comment is array for insert/update, is object for delete
    case 'insert':
    case 'update':
      if ($comment['status'] == COMMENT_NOT_PUBLISHED) {
        break;
      }
    case 'delete':
      if ($comment->status == COMMENT_NOT_PUBLISHED) {
        break;
      }
    case 'publish':
    case 'unpublish':

      /*
          $comment:
          [cid] => 2  // the new comment id
          [pid] => 1  // the parent comment id
          [nid] => 1 // the parent node id
          [uid] => 1 // the author id.
      */

      // cast $comment to array
      $comment = (array) $comment;
      $node = node_load($comment['nid']);
      $node_author = db_fetch_object(db_query('SELECT uid, name FROM {users} WHERE uid = %d', $node->uid));
      if ($comment['pid']) {
        if ($parent_comment_author_uid = db_result(db_query("SELECT uid FROM {comments} WHERE cid = %d", $comment['pid']))) {
          $parent_comment_author = user_load(array(
            'uid' => $parent_comment_author_uid,
          ));
          $parent_comment = db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $comment['pid']));
        }
      }

      // This is all the information you'll need later in order to build
      // the activity message.
      $user = user_load(array(
        'uid' => $comment['uid'],
      ));
      $data = array(
        'author-uid' => $user->uid,
        'subject' => l($comment['subject'], 'node/' . $node->nid, array(), NULL, 'comment-' . $comment['cid']),
        'parent-node' => l($node->title, 'node/' . $node->nid),
        'parent-node-author' => theme('username', $node_author),
        'parent-node-type' => $node->type,
      );
      $target_users_roles = array();

      // The order in which elements are added to $target_users_roles is
      // important. Since several of the roles can be the same person
      // eg. a person comments on their own node or replies to their own
      // comment, they may overwrite each other in the array.
      // This is fine as long as the most important role wins...
      if (is_object($parent_comment_author)) {
        $data['parent-comment'] = $parent_comment;
        $data['parent-comment-author'] = theme('username', $parent_comment_author);
        $target_users_roles[$parent_comment_author_uid] = 'parent comment author';
      }
      $target_users_roles = array(
        ACTIVITY_ALL => 'all',
        $node->uid => 'parent node author',
        // and the most important role is 'author' so we add it last.
        $comment['uid'] => 'author',
      );
      activity_insert('commentactivity', 'comment', $op, $data, $target_users_roles);
      break;
  }
}