Beispiel #1
0
 /**
  * Assert that contest in the request actually exists in the DB
  * 
  * @param Request $r
  */
 public function assertContest(Request $r)
 {
     // Validate that data was written to DB by getting the contest by title
     $contest = new Contests();
     $contest->setTitle($r["title"]);
     $contests = ContestsDAO::search($contest);
     $contest = $contests[0];
     // Assert that we found our contest
     $this->assertNotNull($contest);
     $this->assertNotNull($contest->getContestId());
     // Assert data was correctly saved
     $this->assertEquals($r["description"], $contest->getDescription());
     $this->assertGreaterThanOrEqual($r["start_time"] - 1, Utils::GetPhpUnixTimestamp($contest->getStartTime()));
     $this->assertGreaterThanOrEqual($r["start_time"], Utils::GetPhpUnixTimestamp($contest->getStartTime()) + 1);
     $this->assertGreaterThanOrEqual($r["finish_time"] - 1, Utils::GetPhpUnixTimestamp($contest->getFinishTime()));
     $this->assertGreaterThanOrEqual($r["finish_time"], Utils::GetPhpUnixTimestamp($contest->getFinishTime()) + 1);
     $this->assertEquals($r["window_length"], $contest->getWindowLength());
     $this->assertEquals($r["public"], $contest->getPublic());
     $this->assertEquals($r["alias"], $contest->getAlias());
     $this->assertEquals($r["points_decay_factor"], $contest->getPointsDecayFactor());
     $this->assertEquals($r["partial_score"], $contest->getPartialScore());
     $this->assertEquals($r["submissions_gap"], $contest->getSubmissionsGap());
     $this->assertEquals($r["feedback"], $contest->getFeedback());
     $this->assertEquals($r["penalty"], $contest->getPenalty());
     $this->assertEquals($r["scoreboard"], $contest->getScoreboard());
     $this->assertEquals($r["penalty_type"], $contest->penalty_type);
     $this->assertEquals($r["penalty_calc_policy"], $contest->getPenaltyCalcPolicy());
 }
Beispiel #2
0
 /**
  * Creates a new contest
  *
  * @param Request $r
  * @return array
  * @throws DuplicatedEntryInDatabaseException
  * @throws InvalidDatabaseOperationException
  */
 public static function apiCreate(Request $r)
 {
     if (OMEGAUP_LOCKDOWN) {
         throw new ForbiddenAccessException("lockdown");
     }
     // Authenticate user
     self::authenticateRequest($r);
     // Validate request
     self::validateCreateOrUpdate($r);
     // Create and populate a new Contests object
     $contest = new Contests();
     $contest->setPublic($r["public"]);
     $contest->setTitle($r["title"]);
     $contest->setDescription($r["description"]);
     $contest->setStartTime(gmdate('Y-m-d H:i:s', $r["start_time"]));
     $contest->setFinishTime(gmdate('Y-m-d H:i:s', $r["finish_time"]));
     $contest->setWindowLength($r["window_length"] === "NULL" ? NULL : $r["window_length"]);
     $contest->setDirectorId($r["current_user_id"]);
     $contest->setRerunId(0);
     // NYI
     $contest->setAlias($r["alias"]);
     $contest->setScoreboard($r["scoreboard"]);
     $contest->setPointsDecayFactor($r["points_decay_factor"]);
     $contest->setPartialScore(is_null($r["partial_score"]) ? "1" : $r["partial_score"]);
     $contest->setSubmissionsGap($r["submissions_gap"]);
     $contest->setFeedback($r["feedback"]);
     $contest->setPenalty(max(0, intval($r["penalty"])));
     $contest->penalty_type = $r["penalty_type"];
     $contest->setPenaltyCalcPolicy(is_null($r["penalty_calc_policy"]) ? "sum" : $r["penalty_calc_policy"]);
     $contest->setLanguages(empty($r['languages']) ? null : $r['languages']);
     $contest->setScoreboardUrl(self::randomString(30));
     $contest->setScoreboardUrlAdmin(self::randomString(30));
     if (!is_null($r["show_scoreboard_after"])) {
         $contest->setShowScoreboardAfter($r["show_scoreboard_after"]);
     } else {
         $contest->setShowScoreboardAfter("1");
     }
     if ($r["public"] == 1) {
         self::validateContestCanBePublic($contest);
     }
     // Push changes
     try {
         // Begin a new transaction
         ContestsDAO::transBegin();
         // Save the contest object with data sent by user to the database
         ContestsDAO::save($contest);
         // If the contest is private, add the list of allowed users
         if ($r["public"] != 1 && $r["hasPrivateUsers"]) {
             foreach ($r["private_users_list"] as $userkey) {
                 // Create a temp DAO for the relationship
                 $temp_user_contest = new ContestsUsers(array("contest_id" => $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);
             }
         }
         if (!is_null($r['problems'])) {
             foreach ($r["problems"] as $problem) {
                 $contest_problem = new ContestProblems(array('contest_id' => $contest->getContestId(), 'problem_id' => $problem['id'], 'points' => $problem['points']));
                 ContestProblemsDAO::save($contest_problem);
             }
         }
         // End transaction transaction
         ContestsDAO::transEnd();
     } catch (Exception $e) {
         // Operation failed in the data layer, rollback transaction
         ContestsDAO::transRollback();
         // Alias may be duplicated, 1062 error indicates that
         if (strpos($e->getMessage(), "1062") !== FALSE) {
             throw new DuplicatedEntryInDatabaseException("aliasInUse", $e);
         } else {
             throw new InvalidDatabaseOperationException($e);
         }
     }
     self::$log->info("New Contest Created: " . $r['alias']);
     return array("status" => "ok");
 }
 /**
  * Assert that contest in the request actually exists in the DB
  *
  * @param Request $r
  */
 public function assertContest(Request $r)
 {
     // Validate that data was written to DB by getting the contest by title
     $contest = new Contests();
     $contest->setTitle($r['title']);
     $contests = ContestsDAO::search($contest);
     $contest = $contests[0];
     // Assert that we found our contest
     $this->assertNotNull($contest);
     $this->assertNotNull($contest->getContestId());
     // Assert data was correctly saved
     $this->assertEquals($r['description'], $contest->getDescription());
     $this->assertGreaterThanOrEqual($r['start_time'] - 1, Utils::GetPhpUnixTimestamp($contest->getStartTime()));
     $this->assertGreaterThanOrEqual($r['start_time'], Utils::GetPhpUnixTimestamp($contest->getStartTime()) + 1);
     $this->assertGreaterThanOrEqual($r['finish_time'] - 1, Utils::GetPhpUnixTimestamp($contest->getFinishTime()));
     $this->assertGreaterThanOrEqual($r['finish_time'], Utils::GetPhpUnixTimestamp($contest->getFinishTime()) + 1);
     $this->assertEquals($r['window_length'], $contest->getWindowLength());
     $this->assertEquals($r['public'], $contest->getPublic());
     $this->assertEquals($r['alias'], $contest->getAlias());
     $this->assertEquals($r['points_decay_factor'], $contest->getPointsDecayFactor());
     $this->assertEquals($r['partial_score'], $contest->getPartialScore());
     $this->assertEquals($r['submissions_gap'], $contest->getSubmissionsGap());
     $this->assertEquals($r['feedback'], $contest->getFeedback());
     $this->assertEquals($r['penalty'], $contest->getPenalty());
     $this->assertEquals($r['scoreboard'], $contest->getScoreboard());
     $this->assertEquals($r['penalty_type'], $contest->penalty_type);
     $this->assertEquals($r['penalty_calc_policy'], $contest->getPenaltyCalcPolicy());
     $this->assertEquals($r['recommended'], $contest->getRecommended());
 }
 /**
  * Creates a new contest
  *
  * @param Request $r
  * @return array
  * @throws DuplicatedEntryInDatabaseException
  * @throws InvalidDatabaseOperationException
  */
 public static function apiCreate(Request $r)
 {
     if (OMEGAUP_LOCKDOWN) {
         throw new ForbiddenAccessException('lockdown');
     }
     // Authenticate user
     self::authenticateRequest($r);
     // Validate request
     self::validateCreateOrUpdate($r);
     // Create and populate a new Contests object
     $contest = new Contests();
     $contest->setPublic($r['public']);
     $contest->setTitle($r['title']);
     $contest->setDescription($r['description']);
     $contest->setStartTime(gmdate('Y-m-d H:i:s', $r['start_time']));
     $contest->setFinishTime(gmdate('Y-m-d H:i:s', $r['finish_time']));
     $contest->setWindowLength($r['window_length'] === 'NULL' ? null : $r['window_length']);
     $contest->setDirectorId($r['current_user_id']);
     $contest->setRerunId(0);
     // NYI
     $contest->setAlias($r['alias']);
     $contest->setScoreboard($r['scoreboard']);
     $contest->setPointsDecayFactor($r['points_decay_factor']);
     $contest->setPartialScore(is_null($r['partial_score']) ? '1' : $r['partial_score']);
     $contest->setSubmissionsGap($r['submissions_gap']);
     $contest->setFeedback($r['feedback']);
     $contest->setPenalty(max(0, intval($r['penalty'])));
     $contest->penalty_type = $r['penalty_type'];
     $contest->setPenaltyCalcPolicy(is_null($r['penalty_calc_policy']) ? 'sum' : $r['penalty_calc_policy']);
     $contest->setLanguages(empty($r['languages']) ? null : $r['languages']);
     $contest->setScoreboardUrl(self::randomString(30));
     $contest->setScoreboardUrlAdmin(self::randomString(30));
     if (!is_null($r['show_scoreboard_after'])) {
         $contest->setShowScoreboardAfter($r['show_scoreboard_after']);
     } else {
         $contest->setShowScoreboardAfter('1');
     }
     if ($r['public'] == 1) {
         self::validateContestCanBePublic($contest);
     }
     // Push changes
     try {
         // Begin a new transaction
         ContestsDAO::transBegin();
         // Save the contest object with data sent by user to the database
         ContestsDAO::save($contest);
         // If the contest is private, add the list of allowed users
         if ($r['public'] != 1 && $r['hasPrivateUsers']) {
             foreach ($r['private_users_list'] as $userkey) {
                 // Create a temp DAO for the relationship
                 $temp_user_contest = new ContestsUsers(array('contest_id' => $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);
             }
         }
         if (!is_null($r['problems'])) {
             foreach ($r['problems'] as $problem) {
                 $contest_problem = new ContestProblems(array('contest_id' => $contest->getContestId(), 'problem_id' => $problem['id'], 'points' => $problem['points']));
                 ContestProblemsDAO::save($contest_problem);
             }
         }
         // End transaction transaction
         ContestsDAO::transEnd();
     } catch (Exception $e) {
         // Operation failed in the data layer, rollback transaction
         ContestsDAO::transRollback();
         // Alias may be duplicated, 1062 error indicates that
         if (strpos($e->getMessage(), '1062') !== false) {
             throw new DuplicatedEntryInDatabaseException('aliasInUse', $e);
         } else {
             throw new InvalidDatabaseOperationException($e);
         }
     }
     self::$log->info('New Contest Created: ' . $r['alias']);
     return array('status' => 'ok');
 }