Ejemplo n.º 1
0
function processAdjustContestRequest($request)
{
    if (!$request->contest) {
        throwBusinessLogicError(1, 'contest is null');
    }
    //get user_id or die, if session is invalid
    $userRow = RequestUtils::testSession($request->sessionID);
    //authorize user for this operation
    // get contest ID
    $user_type = $userRow['user_type'];
    $contest_id = RequestUtils::getRequestedContest($request->contest->contestID, $userRow['contest_id'], $user_type);
    if ($user_type === "Participant") {
        $contest_id = -1;
    }
    if ($contest_id < 0) {
        throwBusinessLogicError(0);
    }
    queryForContestDescription($request->contest, $contest_id);
    //now adjust problems
    if (!is_null($request->problems)) {
        $tmp_files = queriesToAdjustProblems($request->problems, $contest_id);
    }
    Data::execPendingQueries();
    $new_ids = Data::getInsertedIDs();
    $id_ind = 0;
    //rename temporary files and fill responseIDs
    if (!is_null($request->problems)) {
        $responseIDs = array();
        $probs_cnt = count($request->problems);
        for ($i = 0; $i < $probs_cnt; $i++) {
            $p = $request->problems[$i];
            $tmp = $tmp_files[$i];
            if ($tmp) {
                $new_id = $p->id;
                if ($new_id < 0) {
                    $new_id = $new_ids[$id_ind++];
                }
                @rename($tmp, getProblemFile($new_id));
                $responseIDs[] = $new_id;
            } else {
                $responseIDs[] = $p->id;
                if ($p->id < 0) {
                    //for new tasks it must have been created a temporary file
                    throwServerProblem(202);
                }
            }
        }
    } else {
        $responseIDs = NULL;
    }
    $response = new AdjustContestResponse();
    $response->problemIDs = $responseIDs;
    return $response;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
function processGetContestDataRequest($request)
{
    $prfx = DB_PREFIX;
    $is_anonymous = is_null($request->sessionID);
    if (!$is_anonymous) {
        //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'];
        //compare requested contest and user contest
        $contest_id = RequestUtils::getRequestedContest($request->contestID, $userRow['contest_id'], $user_type);
    } else {
        $contest_id = $request->contestID;
    }
    if ($contest_id <= 0) {
        throwBusinessLogicError(0);
    }
    //create response
    $res = new GetContestDataResponse();
    //fill contest description with data
    //query db
    $row = Data::getRow(sprintf("SELECT * FROM {$prfx}contest WHERE id=%s", Data::quote_smart($contest_id))) or throwBusinessLogicError(14);
    //TODO remove this code duplication, the code is similar to AvailableContests.php
    $c = Data::_unserialize($row['settings']);
    $c->contestID = (int) $row['id'];
    $res->contest = $c;
    //fill problem data
    if ($is_anonymous) {
        return $res;
    }
    //query db to find out problems
    $problems_rows = Data::getRows(sprintf("SELECT * FROM {$prfx}problem WHERE contest_id=%s ORDER BY contest_pos ASC", Data::quote_smart($contest_id)));
    //fill problems data
    $res->problems = array();
    $info_type = $request->infoType;
    $extended_data = $request->extendedData;
    while ($row = Data::getNextRow($problems_rows)) {
        $pd = new ProblemDescription();
        $res->problems[] = $pd;
        $pd->id = (int) $row['id'];
        $pd->settings = Data::_unserialize($row['contest_settings']);
        //do we need any information
        if ($info_type == 'NoInfo') {
            continue;
        }
        //do we need to return some info for this problem
        if (!is_null($extended_data) && !in_array($pd->id, $extended_data)) {
            continue;
        }
        $problem = new Problem(getProblemFile($pd->id));
        if ($info_type !== 'NoInfo') {
            //fill extended data: statement or statementData and answerData
            if ($info_type === "ParticipantInfo") {
                $pd->problem = $problem->getParticipantVersion($user_id)->getProblemBytes();
            } elseif ($info_type === "AdminInfo") {
                if ($user_type === "Participant") {
                    throwBusinessLogicError(0);
                }
                $pd->problem = $problem->getProblemBytes();
            }
        }
    }
    return $res;
}