You are here

function oa_core_get_groups_by_user_access in Open Atrium Core 7.2

Get the group IDs and Titles of all the groups a user is an approved member of.

SIMILAR to oa_core_get_groups_by_user above but respects the NODE ACCESS of the spaces and allows an optional space status (published) Only returns spaces a user is ACTIVE in (not BLOCKED or PENDING) Also, returns both nids and titles in result

DOES NOT return INHERITED subspaces

Parameters

$account: (optional) The user object to fetch group memberships for. Defaults to the acting user. Can be a full user node or just a user uid

bool $include_archived: (optional) Whether to include archived nodes or not. By default, archived items aren't included.

int $status: (optional) The space node status to include. If NULL, all spaces are included. If NODE_PUBLISHED, then only include published spaces

array $page: (optional) Adds a pager to the query for the given page number

string $filter: (optional) Filters the space titles that are returned

string $type: (optional) Type of group. Defaults to OA_SPACE_TYPE

string $sort: (optional) Field to sort on. Use 'history' to sort by most recently accessed. Start with - to reverse sort direction

int $limit: (optional) Limit number of results

Return value

array An array with the group IDs or an empty array, or returns NULL if there was an error

2 calls to oa_core_get_groups_by_user_access()
oa_core_user_spaces_render in plugins/content_types/oa_core_user_spaces.inc
Render callback for the content visibility panel.
oa_messages_build_message_notifications_table in modules/oa_messages/oa_messages.admin.inc
Helper function to build the message notifications table.

File

includes/oa_core.util.inc, line 113
Code for Utility functions for OpenAtrium spaces

Code

function oa_core_get_groups_by_user_access($account = NULL, $include_archived = FALSE, $status = NULL, $page = NULL, $filter = NULL, $type = NULL, $sort = NULL, $limit = NULL) {
  global $user;
  if (!isset($account)) {
    $uid = $user->uid;
  }
  elseif (is_numeric($account)) {
    $uid = $account;
  }
  else {
    $uid = $account->uid;
  }
  $sort = !empty($sort) ? $sort : 'title';
  $limit = !empty($limit) ? $limit : 0;
  $type = !empty($type) ? $type : OA_SPACE_TYPE;

  // Only cache invocations without paging or filtering or sorting on history.
  // $user->uid is added to the cache key because the result also depends on the permissions of the viewing user.
  $cid = NULL;
  if (!isset($page) && empty($filter) && $sort !== 'history') {
    $cid = oa_core_get_cache_id(OA_CACHE_GROUPS_BY_USER_ACCESS, $uid, $include_archived, array(
      $status,
      $type,
      $sort,
      $limit,
      $user->uid,
    ));
    if (oa_core_get_cache(OA_CACHE_GROUPS_BY_USER_ACCESS, $cid, $result)) {
      return $result;
    }
  }
  $query = db_select('node', 'n');
  if (isset($page)) {
    if (!isset($_GET['page'])) {
      $_GET['page'] = $page;
    }
    $query = $query
      ->extend('PagerDefault')
      ->limit(20);
  }
  $query
    ->join('og_membership', 'ogm', 'ogm.gid = n.nid');
  if ($sort == 'history') {
    $query
      ->join('history', 'h', 'h.nid = n.nid');
    $query
      ->condition('h.uid', $uid);
    $query
      ->orderBy('h.timestamp', 'DESC');
  }
  $query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('ogm.etid', $uid)
    ->condition('ogm.entity_type', 'user')
    ->condition('ogm.state', OG_STATE_ACTIVE)
    ->addTag('node_access');
  if (!empty($sort) && $sort != 'history') {
    $dir = 'ASC';
    if (substr($sort, 0, 1) == '-') {
      $dir = 'DESC';
      $sort = substr($sort, 1, strlen($sort) - 1);
    }
    $query
      ->orderBy('n.' . $sort, $dir);

    // secondary sort on title
    $query
      ->orderBy('n.title');
  }
  if (isset($type)) {
    $query
      ->condition('n.type', $type);
  }
  if (isset($status)) {
    $query
      ->condition('n.status', $status);
  }
  if (!empty($filter)) {
    $query
      ->condition('n.title', '%' . db_like($filter) . '%', 'LIKE');
  }
  if ($limit > 0) {
    $query
      ->range(0, $limit);
  }
  if (module_exists('flag') && !$include_archived) {
    if ($flag = flag_get_flag('trash')) {
      $query
        ->leftJoin('flagging', 'a', "a.fid = :fid AND a.entity_id = n.nid", array(
        ':fid' => $flag->fid,
      ));

      // This makes sure that archived content isn't included, because 'uid'
      // will be NULL if the join didn't connect anything.
      $query
        ->isNull('a.uid');
    }
  }
  $result = $query
    ->execute()
    ->fetchAll();
  if ($cid !== NULL) {
    oa_core_set_cache(OA_CACHE_GROUPS_BY_USER_ACCESS, $cid, $result);
  }
  return $result;
}