function processConnectToContestRequest($request) { $prfx = DB_PREFIX; //find user in table $row = Data::getRow(sprintf("SELECT {$prfx}user.*, {$prfx}contest.settings\r\n FROM {$prfx}user\r\n LEFT JOIN {$prfx}contest\r\n ON {$prfx}user.contest_id = {$prfx}contest.id\r\n WHERE login=%s AND contest_id=%s", Data::quote_smart($request->login), Data::quote_smart($request->contestID))); //test if there is at least one user if (!$row) { throwBusinessLogicError(12); } //test password if ($row['password'] !== $request->password) { throwBusinessLogicError(12); } //get contest settings and contest time $settings = Data::_unserialize($row['settings'], null); if (is_null($row['contest_start'])) { $now = getdate(); $now = $now[0]; $q = Data::composeUpdateQuery('user', array('contest_start' => DatePHPToMySQL($now)), "id={$row['id']}"); Data::submitModificationQuery($q); $row['contest_start'] = $now; } $contest_time = getCurrentContestTime($settings, DateMySQLToPHP($row['contest_start']), DateMySQLToPHP($row['contest_finish'])); if ($contest_time['interval'] === 'before' && $row['user_type'] === 'Participant') { throwBusinessLogicError(19); } //start new session $session_id = RequestUtils::createSession($row['id']); //get finish time if (is_null($settings)) { $finish_time = 0; } elseif ($settings->contestTiming->selfContestStart) { $finish_time = $row['contest_start'] + 60 * $settings->contestTiming->maxContestDuration; } else { $finish_time = $settings->finish; } if (is_null($finish_time)) { $finish_time = 0; } $res = new ConnectToContestResponse(); $res->sessionID = $session_id; $res->finishTime = $finish_time; $res->user = new UserDescription(); $res->user->userID = (int) $row['id']; $res->user->login = $request->login; $res->user->dataValue = Data::_unserialize($row['user_data'], array()); $res->user->userType = $row['user_type']; return $res; }
/** * Created by IntelliJ IDEA. * User: Посетитель * Date: 17.04.2009 * Time: 15:57:17 * To change this template use File | Settings | File Templates. */ function processStopContestRequest($request) { $user_row = RequestUtils::testSession($request->sessionID); $requested_contest_id = $user_row['contest_id']; if ($requested_contest_id <= 0) { throwBusinessLogicError(0); } $settings = Data::_unserialize($user_row['settings']); if (!$settings->contestTiming->selfContestStart) { throwBusinessLogicError(0); } $time = getCurrentContestTime($settings, DateMySQLToPHP($user_row['contest_start']), DateMySQLToPHP($user_row['contest_finish'])); if ($time['interval'] === 'before') { throwBusinessLogicError(19); } if ($time['interval'] === 'after') { throwBusinessLogicError(20); } $now = getdate(); $now = $now[0]; Data::submitModificationQuery(Data::composeUpdateQuery('user', array('contest_finish' => DatePHPToMySQL($now)), "id={$user_row['id']}")); }
function processSubmitSolutionRequest($request) { $prfx = DB_PREFIX; //get user_id or die, if session is invalid $userRow = RequestUtils::testSession($request->sessionID); $user_id = $userRow['id']; //authorize user for this operation // get contest ID $user_type = $userRow['user_type']; //get problem row $problem_row = Data::getRow(sprintf("SELECT * FROM {$prfx}problem WHERE id=%s", Data::quote_smart($request->problemID))); if (!$problem_row) { throwBusinessLogicError(4); } //get contest id of a problem $problem_contest_id = $problem_row['contest_id']; //test if we have rights to submit solution for the contest $contest_id = RequestUtils::getRequestedContest($problem_contest_id, $userRow['contest_id'], $user_type); if ($contest_id < 0) { throwBusinessLogicError(0); } //get all settings $contest_settings = Data::_unserialize($userRow['settings']); //test submission time $cur_time = getCurrentContestTime($contest_settings, DateMySQLToPHP($userRow['contest_start']), DateMySQLToPHP($userRow['contest_finish'])); if ($cur_time['interval'] === 'before') { throwBusinessLogicError(19); } if ($cur_time['interval'] === 'after') { throwBusinessLogicError(20); } $problem_settings = Data::_unserialize($problem_row['contest_settings']); //test that not all submission attempts were used $hist = Data::getRow(sprintf("SELECT COUNT(*) AS cnt FROM {$prfx}submission_history WHERE (problem_id=%s) AND (user_id=%s)", Data::quote_smart($request->problemID), Data::quote_smart($user_id))); if ($hist >= getSetting($contest_settings->problemsDefaultSettings->sendCount, $problem_settings->sendCount)) { throwBusinessLogicError(21); } //save submission result in history $cur_php_time = getdate(); $col_value = array(); $col_value['problem_id'] = $request->problemID; $col_value['user_id'] = $user_id; $col_value['submission'] = serialize($request->problemResult); $col_value['result'] = null; //serialize($check_result); $col_value['submission_time'] = DatePHPToMySQL($cur_php_time[0]); //TODO implement asynchronous plugin //get problem and create plugin $problem = new Problem(getProblemFile($request->problemID)); $plugin_alias = $problem->getServerPlugin(); require_once getServerPluginFile(); require_once getServerPluginFile($plugin_alias); $plugin = new $plugin_alias($problem); //check solution $last_result = $plugin->checkSolution(Data::getInsertedID(), $request->problemResult); $col_value['result'] = serialize($last_result); Data::submitModificationQuery(Data::composeInsertQuery('submission_history', $col_value)); //get result for result table and store in user $all_results = Data::_unserialize($userRow['results']); $user_result = ResultUtils::getUserResults($user_id, $request->problemID, getSetting($contest_settings->problemsDefaultSettings->tableResultChoice, $problem_settings->tableResultChoice), getSetting($contest_settings->problemsDefaultSettings->resultTransition, $problem_settings->resultTransition), $plugin, $last_result); //update user result for results table $all_results[$request->problemID] = $user_result; Data::submitModificationQuery(Data::composeUpdateQuery('user', array('results' => serialize($all_results)), 'id=' . Data::quote_smart($user_id))); //return submission result $res = new AcceptedResponse(); return $res; }
function processGetContestResultsRequest($request) { $prfx = DB_PREFIX; //get $is_anonymous, $contest_id, $user_contest_row, $user_contest_start_time if (!is_null($request->sessionID)) { $is_anonymous = false; $user_contest_row = RequestUtils::testSession($request->sessionID); $contest_id = RequestUtils::getRequestedContest($request->contestID, $user_contest_row['contest_id'], $user_contest_row['user_type']); if ($contest_id < 0) { throwBusinessLogicError(14); } $user_contest_start_time = DateMySQLToPHP($user_contest_row['contest_start']); $user_contest_finish_time = DateMySQLToPHP($user_contest_row['contest_finish']); } else { $is_anonymous = true; $contest_id = $request->contestID; $user_contest_start_time = null; //contest was not started for anonymous $user_contest_finish_time = null; //and was not finished } //get $serialized_contest_settings $need_request_for_contest_data = $is_anonymous || $user_contest_row['user_type'] === 'SuperAdmin'; if ($need_request_for_contest_data) { if ($contest_id === 0) { throwBusinessLogicError(14); } $contest_row = Data::getRow(sprintf("SELECT *\r\n FROM {$prfx}contest\r\n WHERE id=%s\r\n ", Data::quote_smart($contest_id))); if (!$contest_row) { throwBusinessLogicError(14); } $serialized_contest_settings = $contest_row['settings']; } else { $serialized_contest_settings = $user_contest_row['settings']; } //get $contest_settings $contest_settings = Data::_unserialize($serialized_contest_settings); //get $is_admin $is_admin = !$is_anonymous && ($user_contest_row['user_type'] === 'SuperAdmin' || $user_contest_row['user_type'] === 'ContestAdmin'); //get $permission $ctime = getCurrentContestTime($contest_settings, $user_contest_start_time, $user_contest_finish_time); if (!$is_admin) { if ($ctime['interval'] === 'before') { throwBusinessLogicError(19); } if ($ctime['interval'] === 'contest' && !$ctime['is_ending']) { $permission = $contest_settings->resultsAccessPolicy->contestPermission; } else { if ($ctime['is_ending']) { $permission = $contest_settings->resultsAccessPolicy->contestEndingPermission; } else { if ($ctime['interval'] === 'after' && !$ctime['is_ending']) { $permission = $contest_settings->resultsAccessPolicy->afterContestPermission; } } } } else { $permission = 'FullAccess'; } //test rights if ($permission === 'NoAccess') { throwBusinessLogicError(0); } if ($is_anonymous && $permission === "OnlySelfResults") { throwBusinessLogicError(0); } //get problem rows $all_problems_rows = Data::getRows(sprintf("SELECT *\r\n FROM {$prfx}problem\r\n WHERE {$prfx}problem.contest_id=%s\r\n ORDER BY {$prfx}problem.contest_pos ASC", Data::quote_smart($contest_id))); //get users rows if ($permission === 'FullAccess') { $all_users_rows = Data::getRows(sprintf("SELECT *\r\n FROM {$prfx}user\r\n WHERE contest_id=%s", Data::quote_smart($contest_id))); } else { /* if $permission === 'OnlySelfResults'*/ $all_users_rows = $user_contest_row; } //create result $result = new GetContestResultsResponse(); //fill columns ids $result->headers = array(); $result->minorHeaders = array(); //the first column with 'user_id' and 'login' if ($is_admin) { $result->headers[] = 'admin info'; $result->minorHeaders[] = array('id', 'login'); } //column with participant data $result->headers[] = 'participant'; //get participant subcolumns $data_subs = array(); $contest_user_data = $contest_settings->data; if ($contest_user_data) { foreach ($contest_settings->data as $df) { if ($is_admin || $df->showInResult) { $data_subs[] = $df->data; } } } $result->minorHeaders[] = $data_subs; //columns with problems $problem_ids = array(); $problem_cols = array(); while ($problem_row = Data::getNextRow($all_problems_rows)) { $problem_ids[] = $problem_row['id']; $result->headers[] = $problem_row['name']; $col_names = Data::_unserialize($problem_row['column_names']); $result->minorHeaders[] = $col_names; $problem_cols[] = $col_names; } //fill results table $result->table = array(); if ($permission === 'OnlySelfResults') { $result->table[] = getTableRow($user_contest_row, $is_admin, $problem_ids, $problem_cols, $contest_settings->data); $result->userLine = 0; } else { $ind = 0; $result->userLine = -1; while ($user_row = Data::getNextRow($all_users_rows)) { $result->table[] = getTableRow($user_row, $is_admin, $problem_ids, $problem_cols, $contest_settings->data); if ($user_row['id'] == $user_contest_row['id']) { $result->userLine = $ind; } $ind++; } } return $result; }