function test_solution($contest_id, $team_id, $prob_id)
{
    global $db, $test_system_mode, $cfg;
    require_once 'score.php';
    $res =& db_query('solution_by_id', array($contest_id, $team_id, $prob_id));
    $res->fetchInto($solution);
    $res->free();
    // check for actual submission
    if ($solution['submits'] == 0) {
        return 0;
    }
    $res =& db_query('problem_by_id', array($prob_id, $contest_id));
    if (!$res->fetchInto($problem)) {
        error("Problem {$prob_id} for contest ID:{$contest_id} is missing from DB.");
    }
    $res->free();
    $filter = null;
    if ($test_system_mode) {
        $prob = problem_load($problem);
        $filter = array_diff(array_keys($prob->tests), array_keys($prob->examples));
    }
    $ret = test_cases($problem, $solution, $filter, false);
    $res = db_query('contest_time', $contest_id);
    $res->fetchInto($row);
    // Apply time & resubmit penalties to score
    $score = score_resubmit_penalty($problem['weight'], $solution['submits'] - 1);
    $score = score_calc($score, $solution['elapsed'], $row['time']);
    if ($ret[0] != 1) {
        $score *= 1 - $cfg['score']['all_correct_bonus'];
        $score *= $ret[0];
    }
    if ($test_system_mode) {
        $res =& $db->autoExecute('solutions', array('passed' => $ret[1], 'score' => $score), DB_AUTOQUERY_UPDATE, 'contest_id = ' . $contest_id . ' AND team_id = ' . $team_id . ' AND prob_id = \'' . $prob_id . "'");
        if (PEAR::isError($res)) {
            error($res->toString());
        }
    }
    return $score;
}
function submit_perform($contest_id, $team_id, &$problem, &$solution, $mode = null, $custom = null)
{
    global $cfg;
    require_once 'tester.php';
    $prob = problem_load($problem);
    // Compile and Test first
    if ($custom != null) {
        $ret = test_cases($problem, $solution, $custom);
    } else {
        if ($mode == 'practice') {
            $ret = test_cases($problem, $solution);
        } else {
            // only test examples
            $ret = test_cases($problem, $solution, array_keys($prob->examples));
        }
    }
    $result = $ret[0];
    if ($mode == 'submit') {
        // Submit after testing
        if ($result == 1) {
            echo "<pre class=\"testing\">\n";
            echo "Checking for previous submission(s)...";
            $res = db_query('solution_by_id', array($contest_id, $team_id, $problem['prob_id']));
            $res->fetchInto($row);
            $res->free();
            $initial = $problem['weight'];
            if ($row['submits'] == '0') {
                echo "none\n";
            } else {
                echo "found\n";
                echo "Resubmission count...{$row['submits']}\n";
                $initial1 = score_resubmit_penalty($initial, $row['submits']);
                $percent = (1 - $initial1 / $initial) * 100;
                echo "Cumulative penalty...{$percent}%\n";
                $initial = $initial1;
            }
            $res = db_query('contest_time', $contest_id);
            $res->fetchInto($row2);
            $score = score_calc($initial, $row['elapsed2'], $row2['time']);
            db_query('submit_solution', array($solution['language'], $solution['source'], $score, $contest_id, $team_id, $problem['prob_id']));
            // Now that user has submitted, we clear his draft
            db_query('delete_draft_by_user', $_SESSION['user_id']);
            echo "</pre>\n<pre class=\"test_success\">";
            echo "<b>Solution to problem </b><i>{$problem['prob_id']}</i><b> submitted for ";
            printf("%.3f points!</b>\n", $score);
            echo "Note: The above score is not final. The actual score will be determined by the system tests.";
            echo "</pre>\n";
        } else {
            echo "<pre class=\"test_error\"><b>Solution failed to pass all tests. Refusing to submit!</b></pre>";
        }
    }
}