/** * Create new school * * @param Request $r * @return array * @throws InvalidDatabaseOperationException * @throws InvalidParameterException */ public static function apiCreate(Request $r) { self::authenticateRequest($r); Validators::isStringNonEmpty($r["name"], "name"); Validators::isNumber($r["state_id"], "state_id", false); if (!is_null($r["state_id"])) { try { $r["state"] = StatesDAO::getByPK($r["state_id"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (is_null($r["state"])) { throw new InvalidParameterException("parameterNotFound", "state"); } } // Create school object $school = new Schools(array("name" => $r["name"], "state_id" => $r["state_id"])); $school_id = 0; try { $existing = SchoolsDAO::findByName($r["name"]); if (count($existing) > 0) { $school_id = $existing[0]->getSchoolId(); } else { // Save in db SchoolsDAO::save($school); $school_id = $school->getSchoolId(); } } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } return array("status" => "ok", "school_id" => $school_id); }
/** * Resolves the target user for the API. If a username is provided in * the request, then we use that one. Otherwise, we use currently logged-in * user. * * Request must be authenticated before this function is called. * * @param Request $r * @return Users * @throws InvalidDatabaseOperationException * @throws NotFoundException */ protected static function resolveTargetUser(Request $r) { // By default use current user $user = $r['current_user']; if (!is_null($r['username'])) { Validators::isStringNonEmpty($r['username'], 'username'); try { $user = UsersDAO::FindByUsername($r['username']); if (is_null($user)) { throw new InvalidParameterException('parameterNotFound', 'Username'); } } catch (ApiException $e) { throw $e; } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } } return $user; }
/** * If no username provided: Gets the top N users who have solved more problems * If username provided: Gets rank for username provided * * @param Request $r * @return string * @throws InvalidDatabaseOperationException */ public static function apiRankByProblemsSolved(Request $r) { Validators::isNumber($r['offset'], 'offset', false); Validators::isNumber($r['rowcount'], 'rowcount', false); $r['user'] = null; if (!is_null($r['username'])) { Validators::isStringNonEmpty($r['username'], 'username'); try { $r['user'] = UsersDAO::FindByUsername($r['username']); if (is_null($r['user'])) { throw new NotFoundException('userNotExist'); } } catch (ApiException $e) { throw $e; } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } } // Defaults for offset and rowcount if (null == $r['offset']) { $r['offset'] = 0; } if (null == $r['rowcount']) { $r['rowcount'] = 100; } return self::getRankByProblemsSolved($r); }
/** * Validator for List API * * @param Request $r * @throws ForbiddenAccessException * @throws InvalidDatabaseOperationException * @throws NotFoundException */ private static function validateList(Request $r) { // Defaults for offset and rowcount if (!isset($r['offset'])) { $r['offset'] = 0; } if (!isset($r['rowcount'])) { $r['rowcount'] = 100; } if (!Authorization::IsSystemAdmin($r['current_user_id'])) { throw new ForbiddenAccessException('userNotAllowed'); } Validators::isNumber($r['offset'], 'offset', false); Validators::isNumber($r['rowcount'], 'rowcount', false); Validators::isInEnum($r['status'], 'status', array('new', 'waiting', 'compiling', 'running', 'ready'), false); Validators::isInEnum($r['verdict'], 'verdict', array('AC', 'PA', 'WA', 'TLE', 'MLE', 'OLE', 'RTE', 'RFE', 'CE', 'JE', 'NO-AC'), false); // Check filter by problem, is optional if (!is_null($r['problem_alias'])) { Validators::isStringNonEmpty($r['problem_alias'], 'problem'); try { $r['problem'] = ProblemsDAO::getByAlias($r['problem_alias']); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } if (is_null($r['problem'])) { throw new NotFoundException('problemNotFound'); } } Validators::isInEnum($r['language'], 'language', array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false); // Get user if we have something in username if (!is_null($r['username'])) { try { $r['user'] = UserController::resolveUser($r['username']); } catch (NotFoundException $e) { // If not found, simply ignore it $r['username'] = null; $r['user'] = null; } } }
/** * Validator for List API * * @param Request $r * @throws ForbiddenAccessException * @throws InvalidDatabaseOperationException * @throws NotFoundException */ private static function validateList(Request $r) { // Defaults for offset and rowcount if (!isset($r["offset"])) { $r["offset"] = 0; } if (!isset($r["rowcount"])) { $r["rowcount"] = 100; } if (!Authorization::IsSystemAdmin($r["current_user_id"])) { throw new ForbiddenAccessException("userNotAllowed"); } Validators::isNumber($r["offset"], "offset", false); Validators::isNumber($r["rowcount"], "rowcount", false); Validators::isInEnum($r["status"], "status", array('new', 'waiting', 'compiling', 'running', 'ready'), false); Validators::isInEnum($r["verdict"], "verdict", array("AC", "PA", "WA", "TLE", "MLE", "OLE", "RTE", "RFE", "CE", "JE", "NO-AC"), false); // Check filter by problem, is optional if (!is_null($r["problem_alias"])) { Validators::isStringNonEmpty($r["problem_alias"], "problem"); try { $r["problem"] = ProblemsDAO::getByAlias($r["problem_alias"]); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } if (is_null($r["problem"])) { throw new NotFoundException("problemNotFound"); } } Validators::isInEnum($r["language"], "language", array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false); // Get user if we have something in username if (!is_null($r["username"])) { try { $r["user"] = UserController::resolveUser($r["username"]); } catch (NotFoundException $e) { // If not found, simply ignore it $r["username"] = null; $r["user"] = null; } } }
/** * Returns a detailed report of the contest * * @param Request $r * @return array */ public static function apiReport(Request $r) { self::authenticateRequest($r); self::validateStats($r); $scoreboard = new Scoreboard($r["contest"]->getContestId(), true, $r["auth_token"]); // Check the filter if we have one Validators::isStringNonEmpty($r["filterBy"], "filterBy", false); $contestReport = $scoreboard->generate(true, true, isset($r["filterBy"]) ? null : $r["filterBy"]); $contestReport["status"] = "ok"; return $contestReport; }
/** * Returns a detailed report of the contest * * @param Request $r * @return array */ public static function apiReport(Request $r) { self::authenticateRequest($r); self::validateStats($r); $scoreboard = new Scoreboard($r['contest']->getContestId(), true, $r['auth_token']); // Check the filter if we have one Validators::isStringNonEmpty($r['filterBy'], 'filterBy', false); $contestReport = $scoreboard->generate(true, true, isset($r['filterBy']) ? null : $r['filterBy']); $contestReport['status'] = 'ok'; return $contestReport; }
/** * Validate list request * * @param Request $r */ private static function validateList(Request $r) { Validators::isNumber($r['offset'], 'offset', false); Validators::isNumber($r['rowcount'], 'rowcount', false); // Defaults for offset and rowcount if (!isset($r['page'])) { if (!isset($r['offset'])) { $r['offset'] = 0; } if (!isset($r['rowcount'])) { $r['rowcount'] = 1000; } } Validators::isStringNonEmpty($r['query'], 'query', false); }
/** * Create a scoreboard set to a group * * @param Request $r */ public static function apiCreateScoreboard(Request $r) { self::validateGroup($r); Validators::isValidAlias($r["alias"], "alias", true); Validators::isStringNonEmpty($r["name"], "name", true); Validators::isStringNonEmpty($r["description"], "description", false); try { $groupScoreboard = new GroupsScoreboards(array("group_id" => $r["group"]->group_id, "name" => $r["name"], "description" => $r["description"], "alias" => $r["alias"], "create_time" => gmdate('Y-m-d H:i:s', time()))); GroupsScoreboardsDAO::save($groupScoreboard); self::$log->info("New scoreboard created " . $r["alias"]); } catch (Exception $ex) { throw new InvalidDatabaseOperationException($ex); } return array("status" => "ok"); }
/** * Validate update API request * * @param Request $r * @throws InvalidDatabaseOperationException * @throws ForbiddenAccessException */ private static function validateUpdate(Request $r) { Validators::isNumber($r["clarification_id"], "clarificaion_id"); Validators::isStringNonEmpty($r["answer"], "answer", false); Validators::isInEnum($r["public"], "public", array('0', '1'), false); Validators::isStringNonEmpty($r["message"], "message", false); // Check that clarification exists try { $r['clarification'] = ClarificationsDAO::GetByPK($r["clarification_id"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (!Authorization::CanEditClarification($r["current_user_id"], $r["clarification"])) { throw new ForbiddenAccessException(); } }
/** * Create a scoreboard set to a group * * @param Request $r */ public static function apiCreateScoreboard(Request $r) { self::validateGroup($r); Validators::isValidAlias($r['alias'], 'alias', true); Validators::isStringNonEmpty($r['name'], 'name', true); Validators::isStringNonEmpty($r['description'], 'description', false); try { $groupScoreboard = new GroupsScoreboards(array('group_id' => $r['group']->group_id, 'name' => $r['name'], 'description' => $r['description'], 'alias' => $r['alias'], 'create_time' => gmdate('Y-m-d H:i:s', time()))); GroupsScoreboardsDAO::save($groupScoreboard); self::$log->info('New scoreboard created ' . $r['alias']); } catch (Exception $ex) { throw new InvalidDatabaseOperationException($ex); } return array('status' => 'ok'); }
/** * Validate list request * * @param Request $r */ private static function validateList(Request $r) { Validators::isNumber($r["offset"], "offset", false); Validators::isNumber($r["rowcount"], "rowcount", false); // Defaults for offset and rowcount if (!isset($r['page'])) { if (!isset($r["offset"])) { $r["offset"] = 0; } if (!isset($r["rowcount"])) { $r["rowcount"] = 1000; } } Validators::isStringNonEmpty($r["query"], "query", false); }