コード例 #1
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;
 }