/** * Returns a list of contests * * @param Request $r * @return array * @throws InvalidDatabaseOperationException */ public static function apiList(Request $r) { // Check who is visiting, but a not logged user can still view // the list of contests try { self::authenticateRequest($r); } catch (UnauthorizedException $e) { // Do nothing. } try { $contests = array(); if ($r['current_user_id'] === null) { // Get all public contests Cache::getFromCacheOrSet(Cache::CONTESTS_LIST_PUBLIC, '', $r, function (Request $r) { return ContestsDAO::getAllPublicContests(); }, $contests); } elseif (Authorization::IsSystemAdmin($r['current_user_id'])) { // Get all contests Cache::getFromCacheOrSet(Cache::CONTESTS_LIST_SYSTEM_ADMIN, '', $r, function (Request $r) { return ContestsDAO::getAllContests(); }, $contests); } else { // Get all public+private contests $contests = ContestsDAO::getAllContestsForUser($r['current_user_id']); } } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } // Filter returned values by these columns $relevantColumns = array('contest_id', 'title', 'description', 'start_time', 'finish_time', 'public', 'alias', 'director_id', 'window_length', 'recommended'); $addedContests = array(); foreach ($contests as $c) { $contestInfo = $c->asFilteredArray($relevantColumns); $contestInfo['duration'] = is_null($c->getWindowLength()) ? $c->getFinishTime() - $c->getStartTime() : $c->getWindowLength() * 60; $addedContests[] = $contestInfo; } return array('number_of_results' => sizeof($addedContests), 'results' => $addedContests); }