// Kill old daemon if it exists if (!empty($CFG->assignment_oj_daemon_pid)) { if (function_exists('posix_kill')) { // Linux mtrace('Killing old judged. PID = ' . $CFG->assignment_oj_daemon_pid); posix_kill($CFG->assignment_oj_daemon_pid, SIGTERM); // Wait for its quit while (posix_kill($CFG->assignment_oj_daemon_pid, 0)) { sleep(0); } mtrace('done'); $CFG->assignment_oj_daemon_pid = 0; } else { mtrace("It seems that a judged (PID: {$CFG->assignment_oj_daemon_pid}) is still running."); mtrace("It must be killed manually."); mtrace("If it has been killed, enter 'C' to continue."); $input = trim(fgets(STDIN)); if ($input != 'C' && $input != 'c') { die; } } } // Create daemon $oj = new assignment_onlinejudge(); if (function_exists('pcntl_fork')) { // Linux $oj->fork_daemon(); } else { // Windows $oj->daemon(); }
$formdata->mode = optional_param('mode', null, PARAM_ALPHA); $url = new moodle_url('/mod/assignment/type/onlinejudge/upload.php', array('contextid' => $contextid, 'id' => $id, 'offset' => $formdata->offset, 'forcerefresh' => $formdata->forcerefresh, 'userid' => $formdata->userid, 'mode' => $formdata->mode)); list($context, $course, $cm) = get_context_info_array($contextid); require_login($course, true, $cm); if (isguestuser()) { die; } if (!($assignment = $DB->get_record('assignment', array('id' => $cm->instance)))) { print_error('invalidid', 'assignment'); } $PAGE->set_url($url); $PAGE->set_context($context); $title = strip_tags($course->fullname . ': ' . get_string('modulename', 'assignment') . ': ' . format_string($assignment->name, true)); $PAGE->set_title($title); $PAGE->set_heading($title); $instance = new assignment_onlinejudge($cm->id, $assignment, $cm, $course); $submission = $instance->get_submission($formdata->userid, true); $filemanager_options = array('subdirs' => 1, 'maxbytes' => $assignment->maxbytes, 'maxfiles' => $assignment->var1, 'accepted_types' => '*', 'return_types' => FILE_INTERNAL); $mform = new mod_assignment_upload_form(null, array('contextid' => $contextid, 'userid' => $formdata->userid, 'options' => $filemanager_options)); if ($mform->is_cancelled()) { redirect(new moodle_url('/mod/assignment/view.php', array('id' => $cm->id))); } else { if ($formdata = $mform->get_data()) { $instance->upload($mform, $filemanager_options); die; } } echo $OUTPUT->header(); echo $OUTPUT->box_start('generalbox'); if ($instance->can_upload_file($submission) && $id == null) { $data = new stdClass();
<?php require_once $CFG->dirroot . '/mod/assignment/lib.php'; require_once $CFG->dirroot . '/mod/assignment/type/onlinejudge/assignment.class.php'; $settings->add(new admin_setting_configselect('assignment_oj_max_cpu', get_string('maxcpuusage', 'assignment_onlinejudge'), get_string('configmaxcpu', 'assignment_onlinejudge'), ASSIGNMENT_ONLINEJUDGE_MAX_CPU, assignment_onlinejudge::get_max_cpu_times(ASSIGNMENT_ONLINEJUDGE_MAX_CPU))); $settings->add(new admin_setting_configselect('assignment_oj_max_mem', get_string('maxmemusage', 'assignment_onlinejudge'), get_string('configmaxmem', 'assignment_onlinejudge'), ASSIGNMENT_ONLINEJUDGE_MAX_MEM, assignment_onlinejudge::get_max_memory_usages(ASSIGNMENT_ONLINEJUDGE_MAX_MEM)));
$testcase->id = $fromform->caseid[$i]; $testcase->sortorder = $i; if ($testcase->id != -1) { $DB->update_record('assignment_oj_testcases', $testcase); } else { $testcase->id = $DB->insert_record('assignment_oj_testcases', $testcase); } if ($testcase->usefile) { file_save_draft_area_files($testcase->inputfile, $context->id, 'mod_assignment', 'onlinejudge_input', $testcase->id); file_save_draft_area_files($testcase->outputfile, $context->id, 'mod_assignment', 'onlinejudge_output', $testcase->id); } unset($testcase); } redirect($CFG->wwwroot . '/mod/assignment/view.php?id=' . $id); } else { $assignmentinstance = new assignment_onlinejudge($cm->id, $assignment, $cm, $course); $assignmentinstance->view_header(); $testcases = $DB->get_records('assignment_oj_testcases', array('assignment' => $assignment->id), 'sortorder ASC'); $toform = array(); if ($testcases) { $i = 0; foreach ($testcases as $tstObj => $tstValue) { $toform["input[{$i}]"] = $tstValue->input; $toform["output[{$i}]"] = $tstValue->output; $toform["feedback[{$i}]"] = $tstValue->feedback; $toform["subgrade[{$i}]"] = $tstValue->subgrade; $toform["usefile[{$i}]"] = $tstValue->usefile; $toform["caseid[{$i}]"] = $tstValue->id; file_prepare_draft_area($toform["inputfile[{$i}]"], $context->id, 'mod_assignment', 'onlinejudge_input', $tstValue->id, array('subdirs' => 0, 'maxfiles' => 1)); file_prepare_draft_area($toform["outputfile[{$i}]"], $context->id, 'mod_assignment', 'onlinejudge_output', $tstValue->id, array('subdirs' => 0, 'maxfiles' => 1)); $i++;
} if (!($cm = get_coursemodule_from_instance('assignment', $assignment->id, $course->id))) { error(get_string('error_invalidcoursemodule', 'assignment')); } } require_login($course->id, false, $cm); if (!$userid) { $userid = $USER->id; } $context = get_context_instance(CONTEXT_MODULE, $cm->id); if ($userid != $USER->id and !has_capability('mod/assignment:grade', $context)) { print_error('denytoreadfile', 'assignment_onlinejudge'); } // Load up the required assignment code require 'assignment.class.php'; $assignmentinstance = new assignment_onlinejudge($cm->id, $assignment, $cm, $course); $filearea = $assignmentinstance->file_area_name($userid); if ($basedir = $assignmentinstance->file_area($userid)) { $fpath = "{$CFG->dataroot}/{$filearea}/{$file}"; } $ffurl = get_file_url("{$filearea}/{$file}"); if ($gestor = fopen($fpath, 'r')) { $code = fread($gestor, filesize($fpath)); if ($charset = mb_detect_encoding($code, 'UTF-8, GBK')) { $code = iconv($charset, 'utf-8', $code); } $code = htmlspecialchars($code); fclose($gestor); } else { error(get_string('filereaderror', 'assignment_onlinejudge')); }
} } else { if (!($assignment = get_record("assignment", "id", $a))) { error("Course module is incorrect"); } if (!($course = get_record("course", "id", $assignment->course))) { error("Course is misconfigured"); } if (!($cm = get_coursemodule_from_instance("assignment", $assignment->id, $course->id))) { error("Course Module ID was incorrect"); } } require_login($course->id, false, $cm); require_capability('mod/assignment:grade', get_context_instance(CONTEXT_MODULE, $cm->id)); require "{$CFG->dirroot}/mod/assignment/type/onlinejudge/assignment.class.php"; $assignmentinstance = new assignment_onlinejudge($cm->id, $assignment, $cm, $course); if ($force == 1 && confirm_sesskey()) { rejudge_showresult($assignmentinstance->rejudge_all()); } else { rejudge_notice(); } function rejudge_notice() { global $assignment, $id; print_header(get_string('notice')); $message = get_string('rejudgeallnotice', 'assignment_onlinejudge', $assignment->name); $link = 'rejudge.php?id=' . $id . '&force=1'; notice_okcancel($message, $link, array('sesskey' => sesskey())); print_footer('none'); } function rejudge_showresult($success = true)
} else { if (!($assignment = $DB->get_record("assignment", array("id" => $a)))) { print_error('invalidid', 'assignment'); } if (!($course = $DB->get_record("course", array("id" => $assignment->course)))) { print_error('coursemisconf', 'assignment'); } if (!($cm = get_coursemodule_from_instance("assignment", $assignment->id, $course->id))) { print_error('invalidcoursemodule'); } $url->param('a', $a); } $PAGE->set_url($url); require_login($course, true, $cm); require_capability('mod/assignment:grade', get_context_instance(CONTEXT_MODULE, $cm->id)); $assignmentinstance = new assignment_onlinejudge($cm->id, $assignment, $cm, $course); $pending = $DB->count_records('onlinejudge_tasks', array('status' => ONLINEJUDGE_STATUS_PENDING)); if ($pending > PREVENTION_THRESHHOLD) { // Prevent rejudge all requests if judged is busy $assignmentinstance->view_header(); echo $OUTPUT->heading(get_string('rejudgeall', 'assignment_onlinejudge')); echo $OUTPUT->box(get_string('rejudgelater', 'assignment_onlinejudge')); } else { if ($confirm == 1 && confirm_sesskey()) { $assignmentinstance->rejudge_all(); redirect($CFG->wwwroot . '/mod/assignment/submissions.php?id=' . $id, get_string('rejudgeallrequestsent', 'assignment_onlinejudge'), 10); } else { $optionsno = array('id' => $id); $optionsyes = array('id' => $id, 'confirm' => 1, 'sesskey' => sesskey()); $assignmentinstance->view_header(); echo $OUTPUT->heading(get_string('rejudgeall', 'assignment_onlinejudge'));
/** * Onlinejudge_task_judged event handler * * Update the grade and etc. * * @param object task */ function onlinejudge_task_judged($task) { global $DB; $sql = 'SELECT s.* FROM {assignment_submissions} s LEFT JOIN {assignment_oj_submissions} o ON s.id = o.submission WHERE o.task = ?'; if (!($submission = $DB->get_record_sql($sql, array($task->id)))) { return true; // true means the event is processed. false will cause retry } $cm = get_coursemodule_from_instance('assignment', $submission->assignment); $ass = new assignment_onlinejudge($cm->id, NULL, $cm); $sql = 'SELECT s.*, t.subgrade FROM {assignment_oj_submissions} s LEFT JOIN {assignment_oj_testcases} t ON s.testcase = t.id WHERE s.submission = ? AND s.latest = 1'; if (!($onlinejudges = $DB->get_records_sql($sql, array($submission->id)))) { return true; // true means the event is processed. false will cause retry } $finalgrade = 0; foreach ($onlinejudges as $oj) { if ($task = onlinejudge_get_task($oj->task)) { $task->grade = $ass->grade_marker($task->status, $oj->subgrade); if ($task->grade == -1) { // Not all testcases are judged, or judge engines internal error // In the case of internal error, keep old grade is reasonable // since most of ie is caused by system return true; } $finalgrade += $task->grade; } } $submission->grade = $finalgrade; $submission->timemarked = time(); $submission->mailed = 1; // do not notify student by mail $submission->teacher = get_admin()->id; $DB->update_record('assignment_submissions', $submission); $ass->update_grade($submission); return true; }