/** * 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"); }
/** * 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'); }