public static function setScoreboardPercentage($contestData, $percentage)
 {
     $contest = ContestsDAO::getByAlias($contestData['request']['alias']);
     $contest->setScoreboard($percentage);
     ContestsDAO::save($contest);
 }
示例#2
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;
 }
 /**
  * Try to view a contest before it has started
  *
  * @expectedException PreconditionFailedException
  */
 public function testContestNotStartedYet()
 {
     // Get a contest
     $contestData = ContestsFactory::createContest();
     // Get a user for our scenario
     $contestant = UserFactory::createUser();
     // Set contest to not started yet
     $contest = ContestsDAO::getByAlias($contestData['request']['alias']);
     $contest->setStartTime(Utils::GetTimeFromUnixTimestamp(Utils::GetPhpUnixTimestamp() + 30));
     ContestsDAO::save($contest);
     // Prepare our request
     $r = new Request();
     $r['contest_alias'] = $contestData['request']['alias'];
     // Log in the user
     $r['auth_token'] = $this->login($contestant);
     // Call api
     $response = ContestController::apiDetails($r);
 }
 /**
  * Given a contest_alias, sets the recommended flag on/off.
  * Only omegaUp admins can call this API.
  *
  * @param Request $r
  * @return array
  */
 public static function apiSetRecommended(Request $r)
 {
     self::authenticateRequest($r);
     if (!Authorization::IsSystemAdmin($r['current_user_id'])) {
         throw new ForbiddenAccessException('userNotAllowed');
     }
     // Validate & get contest_alias
     try {
         $r['contest'] = ContestsDAO::getByAlias($r['contest_alias']);
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     if (is_null($r['contest'])) {
         throw new NotFoundException('contestNotFound');
     }
     // Validate value param
     Validators::isInEnum($r['value'], 'value', array('0', '1'));
     $r['contest']->recommended = $r['value'];
     try {
         ContestsDAO::save($r['contest']);
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     return array('status' => 'ok');
 }
示例#5
0
 /**
  * Contest director is god, should be able to submit whenever he wants
  * for testing purposes
  */
 public function testInvalidRunInsideSubmissionsGapForContestDirector()
 {
     // Set the context for the first contest
     $r = $this->setValidRequest();
     $this->detourGraderCalls($this->exactly(2));
     // Log as contest director
     $r["auth_token"] = $this->login($this->contestData["director"]);
     // Set submissions gap of 20 seconds
     $contest = ContestsDAO::getByAlias($r["contest_alias"]);
     $contest->setSubmissionsGap(20);
     ContestsDAO::save($contest);
     // Call API
     $response = RunController::apiCreate($r);
     // Validate the run
     $this->assertRun($r, $response);
     // Send a second run. This one should not fail
     $response = RunController::apiCreate($r);
     // Validate the run
     $this->assertRun($r, $response);
 }