예제 #1
0
 /**
  * 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
     }
     // Create array of relevant columns
     $relevant_columns = array('contest_id', 'title', 'description', 'start_time', 'finish_time', 'public', 'alias', 'director_id', 'window_length', 'recommended');
     try {
         // Get all contests using only relevan columns
         $contests = ContestsDAO::getAllMultipleOrder(null, null, array(array('column' => 'recommended', 'type' => 'DESC'), array('column' => 'finish_time', 'type' => 'DESC')), $relevant_columns);
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     // DAO requires contest_id as relevant column but we don't want to expose it
     array_shift($relevant_columns);
     /**
      * Ok, lets go 1 by 1, and if its public, show it,
      * if its not, check if the user has access to it.
      * */
     $addedContests = array();
     foreach ($contests as $c) {
         // At most we want 1000 contests @TODO paginar correctamente
         if (sizeof($addedContests) == 1000) {
             break;
         }
         if ($c->getPublic()) {
             $c->toUnixTime();
             $contestInfo = $c->asFilteredArray($relevant_columns);
             $contestInfo['duration'] = is_null($c->getWindowLength()) ? $c->getFinishTime() - $c->getStartTime() : $c->getWindowLength() * 60;
             $addedContests[] = $contestInfo;
             continue;
         }
         /*
          * Ok, its not public, lets se if we have a
          * valid user
          * */
         if ($r['current_user_id'] === null) {
             continue;
         }
         /**
          * Ok, i have a user. Can he see this contest ?
          * */
         try {
             $contestUser = ContestsUsersDAO::getByPK($r['current_user_id'], $c->getContestId());
         } catch (Exception $e) {
             throw new InvalidDatabaseOperationException($e);
         }
         // Admins can see all contests
         if ($contestUser === null && !Authorization::IsContestAdmin($r['current_user_id'], $c)) {
             /**
              * Nope, he cant .
              * */
             continue;
         }
         /**
          * He can see it !
          *
          * */
         $c->toUnixTime();
         $contestInfo = $c->asFilteredArray($relevant_columns);
         $contestInfo['duration'] = is_null($c->getWindowLength()) ? $c->getFinishTime() - $c->getStartTime() : $c->getWindowLength() * 60;
         $addedContests[] = $contestInfo;
     }
     return array('number_of_results' => sizeof($addedContests), 'results' => $addedContests);
 }