Esempio n. 1
0
 public static function CheckAndSaveFirstTimeAccess($user_id, $contest_id)
 {
     $contest_user = self::getByPK($user_id, $contest_id);
     // If is null, add our contest_user relationship
     if (is_null($contest_user)) {
         $contest_user = new ContestsUsers();
         $contest_user->setUserId($user_id);
         $contest_user->setContestId($contest_id);
         $contest_user->setAccessTime(date("Y-m-d H:i:s"));
         $contest_user->setScore(0);
         $contest_user->setTime(0);
         ContestsUsersDAO::save($contest_user);
     } else {
         if ($contest_user->getAccessTime() === "0000-00-00 00:00:00") {
             // If its set to default time, update it
             $contest_user->setAccessTime(date("Y-m-d H:i:s"));
             ContestsUsersDAO::save($contest_user);
         }
     }
     return $contest_user;
 }
 public static function CheckAndSaveFirstTimeAccess($user_id, $contest_id, $grant_access = false)
 {
     $contest_user = self::getByPK($user_id, $contest_id);
     if (is_null($contest_user)) {
         if (!$grant_access) {
             // User was not authorized to do this.
             throw new ForbiddenAccessException();
         }
         $contest_user = new ContestsUsers();
         $contest_user->setUserId($user_id);
         $contest_user->setContestId($contest_id);
         $contest_user->setAccessTime(date('Y-m-d H:i:s'));
         $contest_user->setScore(0);
         $contest_user->setTime(0);
         ContestsUsersDAO::save($contest_user);
     } elseif ($contest_user->getAccessTime() === '0000-00-00 00:00:00') {
         // If its set to default time, update it
         $contest_user->setAccessTime(date('Y-m-d H:i:s'));
         ContestsUsersDAO::save($contest_user);
     }
     return $contest_user;
 }
Esempio n. 3
0
 /**
  * Returns ALL users participating in a contest
  *
  * @param Request $r
  * @return array
  * @throws InvalidDatabaseOperationException
  */
 public static function apiUsers(Request $r)
 {
     // Authenticate request
     self::authenticateRequest($r);
     Validators::isStringNonEmpty($r["contest_alias"], "contest_alias");
     try {
         $contest = ContestsDAO::getByAlias($r["contest_alias"]);
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     if (!Authorization::IsContestAdmin($r["current_user_id"], $contest)) {
         throw new ForbiddenAccessException();
     }
     // Get users from DB
     $contest_user_key = new ContestsUsers();
     $contest_user_key->setContestId($contest->getContestId());
     try {
         $db_results = ContestsUsersDAO::search($contest_user_key);
     } catch (Exception $e) {
         // Operation failed in the data layer
         throw new InvalidDatabaseOperationException($e);
     }
     $users = array();
     // Add all users to an array
     foreach ($db_results as $result) {
         $user_id = $result->getUserId();
         $user = UsersDAO::getByPK($user_id);
         $users[] = array("user_id" => $user_id, "username" => $user->getUsername(), 'access_time' => $result->access_time, 'country' => $user->getCountryId());
     }
     $response = array();
     $response["users"] = $users;
     $response["status"] = "ok";
     return $response;
 }
Esempio n. 4
0
 /**
  * Get Contests which a certain user has participated in
  * 
  * @param Request $r
  * @return Contests array
  * @throws InvalidDatabaseOperationException
  */
 public static function apiContestStats(Request $r)
 {
     self::authenticateOrAllowUnauthenticatedRequest($r);
     $response = array();
     $response["contests"] = array();
     $user = self::resolveTargetUser($r);
     $contest_user_key = new ContestsUsers();
     $contest_user_key->setUserId($user->getUserId());
     // Get contests where user had at least 1 run
     try {
         $contestsParticipated = ContestsDAO::getContestsParticipated($user->getUserId());
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     $contests = array();
     foreach ($contestsParticipated as $contest) {
         // Get user ranking
         $scoreboardR = new Request(array("auth_token" => $r["auth_token"], "contest_alias" => $contest->getAlias(), "token" => $contest->getScoreboardUrlAdmin()));
         $scoreboardResponse = ContestController::apiScoreboard($scoreboardR);
         // Grab the place of the current user in the given contest
         $contests[$contest->getAlias()]["place"] = null;
         foreach ($scoreboardResponse["ranking"] as $userData) {
             if ($userData["username"] == $user->getUsername()) {
                 $contests[$contest->getAlias()]["place"] = $userData["place"];
                 break;
             }
         }
         $contest->toUnixTime();
         $contests[$contest->getAlias()]["data"] = $contest->asArray();
     }
     $response["contests"] = $contests;
     $response["status"] = "ok";
     return $response;
 }