You are here

function oa_access_user_teams in Open Atrium Core 7.2

Get a list of this user's Teams.

Parameters

object|NULL $user : (Optional) The user to get groups for. If NULL, it will find groups for the currently logged in user.

integer $space_nid : (Optional) The nid of a Space. If given, this will include the Teams from the given Space. If not given, then Teams won't be included.

Return value

array An array of Team nids.

2 calls to oa_access_user_teams()
oa_access in modules/oa_access/oa_access.module
Determines if the user has a permission.
OpenAtriumAccessTestCase::testUserTeams in modules/oa_access/tests/oa_access.test

File

modules/oa_access/oa_access.module, line 375
Code for the Open Atrium Access module.

Code

function oa_access_user_teams($space_nid, $account = NULL) {
  global $user;
  if (!module_exists('oa_teams')) {
    return array();
  }
  if (is_null($account)) {
    $account = $user;
  }
  $cache =& drupal_static(__FUNCTION__, array());
  if (!isset($cache[$space_nid][$account->uid])) {
    $teams = array();
    $valid_teams = oa_teams_get_teams_for_space($space_nid);
    if (count($valid_teams) > 0) {

      // Then, we get all the user's oa_teams, optionally filtering by valid
      // teams for this particular Space.
      $query = db_select('field_data_' . OA_TEAM_USERS_FIELD, 'f')
        ->fields('f', array(
        'entity_id',
      ))
        ->condition('entity_type', 'node')
        ->condition('entity_id', array_keys($valid_teams), 'IN')
        ->condition('deleted', 0)
        ->condition(OA_TEAM_USERS_FIELD . '_target_id', $account->uid);
      foreach ($query
        ->execute() as $row) {
        $teams[$row->entity_id] = $row->entity_id;
      }
    }

    // All Space members are members of the magic 'All' team with an NID
    // that is equal to the Space's NID.
    if (og_is_member('node', $space_nid, 'user', $account)) {
      $teams[$space_nid] = $space_nid;
    }
    $cache[$space_nid][$account->uid] = $teams;
  }
  return $cache[$space_nid][$account->uid];
}