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
 /**
  * Update a Contest
  *
  * @param Request $r
  * @return array
  * @throws InvalidDatabaseOperationException
  */
 public static function apiUpdate(Request $r)
 {
     if (OMEGAUP_LOCKDOWN) {
         throw new ForbiddenAccessException("lockdown");
     }
     // Authenticate request
     self::authenticateRequest($r);
     // Validate request
     self::validateCreateOrUpdate($r, true);
     // Update contest DAO
     if (!is_null($r["public"])) {
         // If going public
         if ($r["public"] == 1) {
             self::validateContestCanBePublic($r["contest"]);
         }
         $r["contest"]->setPublic($r["public"]);
     }
     $valueProperties = array("title", "description", "start_time" => array("transform" => function ($value) {
         return gmdate('Y-m-d H:i:s', $value);
     }), "finish_time" => array("transform" => function ($value) {
         return gmdate('Y-m-d H:i:s', $value);
     }), "window_length" => array("transform" => function ($value) {
         return $value == "NULL" ? NULL : $value;
     }), "scoreboard", "points_decay_factor", "partial_score", "submissions_gap", "feedback", "penalty" => array("transform" => function ($value) {
         return max(0, intval($value));
     }), "penalty_type", "penalty_calc_policy", "show_scoreboard_after", "contestant_must_register");
     self::updateValueProperties($r, $r["contest"], $valueProperties);
     // Push changes
     try {
         // Begin a new transaction
         ContestsDAO::transBegin();
         // Save the contest object with data sent by user to the database
         ContestsDAO::save($r["contest"]);
         // If the contest is private, add the list of allowed users
         if (!is_null($r["public"]) && $r["public"] != 1 && $r["hasPrivateUsers"]) {
             // Get current users
             $cu_key = new ContestsUsers(array("contest_id" => $r["contest"]->getContestId()));
             $current_users = ContestsUsersDAO::search($cu_key);
             $current_users_id = array();
             foreach ($current_users as $cu) {
                 array_push($current_users_id, $current_users->getUserId());
             }
             // Check who needs to be deleted and who needs to be added
             $to_delete = array_diff($current_users_id, $r["private_users_list"]);
             $to_add = array_diff($r["private_users_list"], $current_users_id);
             // Add users in the request
             foreach ($to_add as $userkey) {
                 // Create a temp DAO for the relationship
                 $temp_user_contest = new ContestsUsers(array("contest_id" => $r["contest"]->getContestId(), "user_id" => $userkey, "access_time" => "0000-00-00 00:00:00", "score" => 0, "time" => 0));
                 // Save the relationship in the DB
                 ContestsUsersDAO::save($temp_user_contest);
             }
             // Delete users
             foreach ($to_delete as $userkey) {
                 // Create a temp DAO for the relationship
                 $temp_user_contest = new ContestsUsers(array("contest_id" => $r["contest"]->getContestId(), "user_id" => $userkey));
                 // Delete the relationship in the DB
                 ContestsUsersDAO::delete(ContestProblemsDAO::search($temp_user_contest));
             }
         }
         if (!is_null($r['problems'])) {
             // Get current problems
             $p_key = new Problems(array("contest_id" => $r["contest"]->getContestId()));
             $current_problems = ProblemsDAO::search($p_key);
             $current_problems_id = array();
             foreach ($current_problems as $p) {
                 array_push($current_problems_id, $p->getProblemId());
             }
             // Check who needs to be deleted and who needs to be added
             $to_delete = array_diff($current_problems_id, self::$problems_id);
             $to_add = array_diff(self::$problems_id, $current_problems_id);
             foreach ($to_add as $problem) {
                 $contest_problem = new ContestProblems(array('contest_id' => $r["contest"]->getContestId(), 'problem_id' => $problem, 'points' => $r["problems"][$problem]['points']));
                 ContestProblemsDAO::save($contest_problem);
             }
             foreach ($to_delete as $problem) {
                 $contest_problem = new ContestProblems(array('contest_id' => $r["contest"]->getContestId(), 'problem_id' => $problem));
                 ContestProblemsDAO::delete(ContestProblemsDAO::search($contest_problem));
             }
         }
         // End transaction
         ContestsDAO::transEnd();
     } catch (Exception $e) {
         // Operation failed in the data layer, rollback transaction
         ContestsDAO::transRollback();
         throw new InvalidDatabaseOperationException($e);
     }
     // Expire contest-info cache
     Cache::deleteFromCache(Cache::CONTEST_INFO, $r["contest_alias"]);
     // Expire contest scoreboard cache
     Scoreboard::InvalidateScoreboardCache($r["contest"]->getContestId());
     // Happy ending
     $response = array();
     $response["status"] = 'ok';
     self::$log->info("Contest updated (alias): " . $r['contest_alias']);
     return $response;
 }
Esempio n. 4
0
 /**
  * Test sending runs after the window length expired
  *
  * @expectedException NotAllowedToSubmitException
  */
 public function testNewRunOutWindowLengthPublicContest()
 {
     // Set the context for the first contest
     $r = $this->setValidRequest();
     // Alter Contest window length to 20
     // This means: once I started the contest, I have 20 more mins
     // to finish it.
     $contest = ContestsDAO::getByAlias($r["contest_alias"]);
     $contest->setWindowLength(20);
     ContestsDAO::save($contest);
     // Alter first access time of our contestant such that he started
     // 21 minutes ago, this is, window length has expired by 1 minute
     $contest_user = ContestsUsersDAO::getByPK($this->contestant->getUserId(), $contest->getContestId());
     $contest_user->setAccessTime(date("Y-m-d H:i:s", time() - 21 * 60));
     //Window length is in minutes
     ContestsUsersDAO::save($contest_user);
     // Call API
     RunController::apiCreate($r);
 }