You are here

function oa_core_get_groups_by_user in Open Atrium Core 7.2

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

REPLACES og_get_groups_by_user() in OG and returns a list of groups including subspaces including via INHERITED membership

Parameters

$account: (optional) The user object to fetch group memberships for. Defaults to the acting user.

$group_type: (optional) The entity type of the groups to fetch. By default all group types will be fetched. (e.g. 'node', 'user')

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

Return value

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

8 calls to oa_core_get_groups_by_user()
oa_core_form_node_form_alter in ./oa_core.module
Implements hook_form_FORM_ID_alter() node_form.
oa_core_get_user_spaces in includes/oa_core.util.inc
Return a list of space ids that a user belongs to.
oa_core_login_get_destination in includes/oa_core.login.inc
Process all destination rules and return destination path. This function is intended to be used by external modules.
oa_core_node_grants in includes/oa_core.access.inc
Implements hook_node_grants(). Define node access grant realm for Open Atrium sections
oa_core_og_group_ref_process in includes/oa_core.fields.inc
Process function of the og_group_ref field.

... See full list

File

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

Code

function oa_core_get_groups_by_user($account = NULL, $group_type = NULL, $include_archived = FALSE) {
  if (!isset($account)) {
    global $user;
    $account = $user;
  }
  $cid = oa_core_get_cache_id(OA_CACHE_GROUPS_BY_USER, $account->uid, $include_archived, array(
    $group_type,
  ));
  if (oa_core_get_cache(OA_CACHE_GROUPS_BY_USER, $cid, $result)) {
    return $result;
  }

  // Do NOT call og_get_groups_by_user as it loads all entities
  // Here we do a faster db query
  $query = db_select('node', 'n');
  $query
    ->join('og_membership', 'ogm', 'ogm.gid = n.nid');
  $query
    ->fields('n', array(
    'nid',
  ))
    ->condition('ogm.etid', $account->uid)
    ->condition('ogm.entity_type', 'user')
    ->condition('ogm.state', OG_STATE_ACTIVE)
    ->condition('n.type', array(
    OA_SPACE_TYPE,
    OA_GROUP_TYPE,
  ), 'IN');
  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');
    }
  }
  $user_groups = drupal_map_assoc($query
    ->execute()
    ->fetchCol(0));
  if (module_exists('og_subgroups') && $user_groups) {
    $user_groups = oa_core_get_groups_by_parent($user_groups, array(
      OA_SPACE_TYPE,
      OA_GROUP_TYPE,
    ), NULL, $include_archived, NULL, TRUE);
  }
  oa_core_set_cache(OA_CACHE_GROUPS_BY_USER, $cid, $user_groups);
  return $user_groups;
}