/** * Get the set of user id's from a user or collection of users, and group */ protected function getIdSet($user, GroupId $group, SecurityToken $token) { $ids = array(); if ($user instanceof UserId) { $userId = $user->getUserId($token); if ($group == null) { return array($userId); } switch ($group->getType()) { case 'all': case 'friends': case 'groupId': $friendIds = ATutorDbFetcher::get()->getFriendIds($userId); if (is_array($friendIds) && count($friendIds)) { $ids = $friendIds; } break; case 'self': $ids[] = $userId; break; } } elseif (is_array($user)) { $ids = array(); foreach ($user as $id) { $ids = array_merge($ids, $this->getIdSet($id, $group, $token)); } } return $ids; }
/** * Fetch groups for a list of ids. * @param UserId The user id to perform the action for * @param GroupId optional grouping ID * @param token The SecurityToken for this request * @return ResponseItem a response item with the error code set if * there was a problem */ function getPersonGroups($userId, GroupId $groupId, SecurityToken $token) { $ids = $this->getIdSet($userId, $groupId, $token); $data = ATutorDbFetcher::get()->getPersonGroups($ids); // If the data array is empty, return empty DataCollection. return new DataCollection($data); }
/** * Delete activity * @param String userId = "@me" * @param String groupId = "@self" * @param String appId = auth.AppId * @param String activityId * @param AuthToken auth = HttpRequest.Authorization * @return Void * * Check http://www.atutor.ca/atutor/mantis/view.php?id=4230 for details regarding to $activityId */ public function deleteActivities($userId, $groupId, $appId, $activityIds, SecurityToken $token) { $ids = $this->getIdSet($userId, $groupId, $token); if (count($ids) < 1 || count($ids) > 1) { throw new SocialSpiException("Invalid user id or count", ResponseError::$BAD_REQUEST); } if (!ATutorDbFetcher::get()->deleteActivities($ids[0], $appId, $activityIds)) { throw new SocialSpiException("Invalid activity id(s)", ResponseError::$NOT_FOUND); } }
public function createMessage($userId, $appId, $message, $optionalMessageId, SecurityToken $token) { try { $messages = ATutorDbFetcher::get()->createMessage($userId, $token->getAppId(), $message); } catch (SocialSpiException $e) { throw $e; } catch (Exception $e) { throw new SocialSpiException("Invalid create message request: " . $e->getMessage(), ResponseError::$INTERNAL_ERROR); } }
public function getPeople($userId, $groupId, CollectionOptions $options, $fields, SecurityToken $token) { $ids = $this->getIdSet($userId, $groupId, $token); $allPeople = ATutorDbFetcher::get()->getPeople($ids, $fields, $options, $token); $totalSize = $allPeople['totalSize']; $people = array(); foreach ($ids as $id) { $person = null; if (is_array($allPeople) && isset($allPeople[$id])) { $person = $allPeople[$id]; if (!$token->isAnonymous() && $id == $token->getViewerId()) { $person->setIsViewer(true); } if (!$token->isAnonymous() && $id == $token->getOwnerId()) { $person->setIsOwner(true); } if (!in_array('@all', $fields)) { $newPerson = array(); $newPerson['isOwner'] = $person->isOwner; $newPerson['isViewer'] = $person->isViewer; $newPerson['displayName'] = $person->displayName; // Force these fields to always be present $fields[] = 'id'; $fields[] = 'displayName'; $fields[] = 'thumbnailUrl'; $fields[] = 'profileUrl'; foreach ($fields as $field) { if (isset($person->{$field}) && !isset($newPerson[$field])) { $newPerson[$field] = $person->{$field}; } } $person = $newPerson; } array_push($people, $person); } } $sorted = $this->sortPersonResults($people, $options); $collection = new RestfulCollection($people, $options->getStartIndex(), $totalSize); $collection->setItemsPerPage($options->getCount()); if (!$sorted) { $collection->setSorted(false); // record that we couldn't sort as requested } if ($options->getUpdatedSince()) { $collection->setUpdatedSince(false); // we can never process an updatedSince request } return $collection; }
public function updatePersonData(UserId $userId, GroupId $groupId, $appId, $fields, $values, SecurityToken $token) { if ($userId->getUserId($token) == null) { throw new SocialSpiException("Unknown person id.", ResponseError::$NOT_FOUND); } foreach ($fields as $key) { if (!self::isValidKey($key)) { throw new SocialSpiException("The person app data key had invalid characters", ResponseError::$BAD_REQUEST); } } switch ($groupId->getType()) { case 'self': foreach ($fields as $key) { $value = isset($values[$key]) ? $values[$key] : null; if (!ATutorDbFetcher::get()->setAppData($userId->getUserId($token), $key, $value, $token->getAppId())) { throw new SocialSpiException("Internal server error", ResponseError::$INTERNAL_ERROR); } } break; default: throw new SocialSpiException("We don't support updating data in batches yet", ResponseError::$NOT_IMPLEMENTED); break; } }
static function get() { // This object is a singleton if (!isset(ATutorDbFetcher::$fetcher)) { ATutorDbFetcher::$fetcher = new ATutorDbFetcher(); } return ATutorDbFetcher::$fetcher; }