For a recent project, I needed comment activities to be checked by the comment_perm.module.

I am sure there are much better ways. Here is my quick solution. I am saving it here so that others may benefit, and hopefully provide better code!!

/**
 * Implementation of hook_views_query_alter().
 */
function mymodule_views_query_alter(&$view, &$query) {
  // For activity.module, we add support for comment_perm.module
  if ($view->base_table == 'activity') {

    $types = variable_get('comment_perm_node_types', '');
    $comment_perm_access = array();
    foreach ($types as $type) {
      if (user_access('comment on ' . $type . ' content')) {
        $comment_perm_access[] = $type;
      }
    }

    // If nothing is granted by comment_perm, assume they have no access to comments.
    $where = '';
    $args = array();
    if (!empty($comment_perm_access)) {
      $in = db_placeholders($comment_perm_access, 'text');
      $where = 'AND n.type NOT IN ($in)';
      $args = $comment_perm_access;
    }

    // Find all aids that the current user is not allowed to see.
    $aids = array();
    $result = db_query("SELECT a.aid FROM {activity} a INNER JOIN {node} n ON n.nid = a.nid WHERE a.type = 'comment' $where", $args);
    while ($obj = db_fetch_object($result)) {
      $aids[] = $obj->aid;
    }

    if (empty($aids)) {
      return;
    }

    // Exclude comment aids that the user is not allowed to see.
    $in = db_placeholders($aids, 'int');
    $view->query->add_where(0, "(activity.aid NOT IN ($in))", $aids);
  }
}

Comments

Scott Reynolds’s picture

Title: Use comment_perm.module permissions to check comment activities in activity.module » Comment Permissions Views handler for Node types
Project: Activity » Comment Permissions
Version: 6.x-2.x-dev » 6.x-1.x-dev
Component: Activity Contrib » Code
Status: Active » Postponed

Moving to Comment Permissions. They can provide a views handler that finds all the node types a user can comment on. You can then create a relationship from Activity -> Node and then apply that filter.

Scott Reynolds’s picture

Status: Postponed » Active

Didn't mean to postpone