-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
judgelib.php
465 lines (402 loc) · 14.7 KB
/
judgelib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
// This file is part of Moodle - https://moodle.org
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* NOTICE OF COPYRIGHT
*
* Online Judge for Moodle
* https://github.com/hit-moodle/moodle-local_onlinejudge
*
* Copyright (C) 2009 onwards
* Sun Zhigang http://sunner.cn
* Andrew Naguib <andrew at fci helwan edu eg>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* online judge library
*
* @package local_onlinejudge
* @copyright 2011 Sun Zhigang (http://sunner.cn)
* @author Sun Zhigang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define("ONLINEJUDGE_STATUS_PENDING", 0);
define("ONLINEJUDGE_STATUS_ACCEPTED", 1);
define("ONLINEJUDGE_STATUS_ABNORMAL_TERMINATION", 2);
define("ONLINEJUDGE_STATUS_COMPILATION_ERROR", 3);
define("ONLINEJUDGE_STATUS_COMPILATION_OK", 4);
define("ONLINEJUDGE_STATUS_MEMORY_LIMIT_EXCEED", 5);
define("ONLINEJUDGE_STATUS_OUTPUT_LIMIT_EXCEED", 6);
define("ONLINEJUDGE_STATUS_PRESENTATION_ERROR", 7);
define("ONLINEJUDGE_STATUS_RESTRICTED_FUNCTIONS", 8);
define("ONLINEJUDGE_STATUS_RUNTIME_ERROR", 9);
define("ONLINEJUDGE_STATUS_TIME_LIMIT_EXCEED", 10);
define("ONLINEJUDGE_STATUS_WRONG_ANSWER", 11);
define("ONLINEJUDGE_STATUS_INTERNAL_ERROR", 21);
define("ONLINEJUDGE_STATUS_JUDGING", 22);
define("ONLINEJUDGE_STATUS_MULTI_STATUS", 23);
define("ONLINEJUDGE_STATUS_UNSUBMITTED", 255);
require_once(dirname(__FILE__) . '/exceptions.php');
$judgeplugins = get_list_of_plugins('local/onlinejudge/judge');
foreach ($judgeplugins as $dir) {
require_once("$CFG->dirroot/local/onlinejudge/judge/$dir/lib.php");
}
class judge_base {
// object of the task
protected $task;
// language id without judge id
protected $language;
function __construct($task) {
$this->task = $task;
$this->language = substr($this->task->language, 0, strpos($this->task->language, '-'));
}
/**
* Return an array of programming languages supported by this judge
*
* The array key must be the language's ID, such as c_sandbox, python_ideone.
* The array value must be a human-readable name of the language, such as 'C (local)', 'Python (ideone.com)'
*/
static function get_languages() {
return array();
}
/**
* Put options into task
*
* @param object options
* @return throw exceptions on error
*/
static function parse_options($options, & $task) {
$options = (array)$options;
// only common options are parsed here.
// special options should be parsed by childclass
foreach ($options as $key => $value) {
if ($key == 'memlimit' and $value > 1024 * 1024 * get_config('local_onlinejudge', 'maxmemlimit')) {
$value = 1024 * 1024 * get_config('local_onlinejudge', 'maxmemlimit');
}
if ($key == 'cpulimit' and $value > get_config('local_onlinejudge', 'maxcpulimit')) {
$value = get_config('local_onlinejudge', 'maxcpulimit');
}
$task->$key = $value;
}
}
/**
* Return the infomation of the compiler of specified language
*
* @param string $language ID of the language
* @return compiler information or null
*/
static function get_compiler_info($language) {
return array();
}
/**
* Whether the judge is avaliable
*
* @return true for yes, false for no
*/
static function is_available() {
return false;
}
/**
* Judge the current task
*
* @return bool [updated task or false]
*/
function judge() {
return false;
}
/**
* Compare the stdout of program and the output of testcase
*/
protected function diff() {
$task = &$this->task;
// convert data into UTF-8 charset if possible
$task->stdout = $this->convert_to_utf8($task->stdout);
$task->stderr = $this->convert_to_utf8($task->stderr);
$task->output = $this->convert_to_utf8($task->output);
// trim tailing return chars which are meaning less
$task->output = rtrim($task->output, "\r\n");
$task->stdout = rtrim($task->stdout, "\r\n");
if (strcmp($task->output, $task->stdout) == 0) return ONLINEJUDGE_STATUS_ACCEPTED; else {
$tokens = array();
$tok = strtok($task->output, " \n\r\t");
while ($tok !== false) {
$tokens[] = $tok;
$tok = strtok(" \n\r\t");
}
$tok = strtok($task->stdout, " \n\r\t");
foreach ($tokens as $anstok) {
if ($tok === false || $tok !== $anstok) return ONLINEJUDGE_STATUS_WRONG_ANSWER;
$tok = strtok(" \n\r\t");
}
if ($tok !== false) {
return ONLINEJUDGE_STATUS_WRONG_ANSWER;
}
return ONLINEJUDGE_STATUS_PRESENTATION_ERROR;
}
}
/**
* If string is not encoded in UTF-8, convert it into utf-8 charset
*/
protected function convert_to_utf8($string) {
$localwincharset = get_string('localewincharset', 'langconfig');
if (!empty($localwincharset) and !mb_check_encoding($string, 'UTF-8') and mb_check_encoding($string, $localwincharset)) {
return core_text::convert($string, $localwincharset);
} else {
return $string;
}
}
/**
* Save files of current task to a temp directory
*
* @return array of the full path of saved files
*/
protected function create_temp_files() {
$dstfiles = array();
$fs = get_file_storage();
$files = $fs->get_area_files(context_system::instance()->id, 'local_onlinejudge', 'tasks', $this->task->id, 'sortorder', false);
foreach ($files as $file) {
$path = onlinejudge_get_temp_dir() . $file->get_filepath();
$fullpath = $path . $file->get_filename();
if (!check_dir_exists($path)) {
throw new moodle_exception('errorcreatingdirectory', '', '', $path);
}
$file->copy_content_to($fullpath);
$dstfiles[] = $fullpath;
}
return $dstfiles;
}
}
/**
* Returns an sorted array of all programming languages supported
*
* The array key must be the language's ID, such as c_sandbox, python_ideone.
* The array value must be a human-readable name of the language, such as 'C (local)', 'Python (ideone.com)'
*/
function onlinejudge_get_languages() {
$langs = array();
$judgeclasses = onlinejudge_get_judge_classes();
foreach ($judgeclasses as $judgeclass) {
$langs = array_merge($langs, $judgeclass::get_languages());
}
asort($langs);
return $langs;
}
/**
* Return the human-readable name of specified language
*
* @param string $language ID of the language
* @return string [name]
*/
function onlinejudge_get_language_name($language) {
$langs = onlinejudge_get_languages();
return $langs[$language];
}
/**
* Return the infomation of the compiler of specified language
*
* @param string $language ID of the language
* @return null|string [compiler information]
*/
function onlinejudge_get_compiler_info($language) {
$judgeclasses = onlinejudge_get_judge_classes();
$judgeclass = 'judge_' . onlinejudge_judge_name($language);
return $judgeclass != 'judge_' ? $judgeclass::get_compiler_info($language) : false;
}
/**
* Submit task to judge
*
* @param int $cmid ID of coursemodule
* @param int $userid ID of user
* @param string $language ID of the language
* @param array $files array of stored_file of source code or array of filename => filecontent
* @param $component
* @param object $options include input, output and etc.
* @return bool|int [id of the task]
* @throws onlinejudge_exception
*/
function onlinejudge_submit_task($cmid, $userid, $language, $files, $component, $options) {
global $DB;
$task = new \stdClass;
$task->cmid = $cmid;
$task->userid = $userid;
$task->status = ONLINEJUDGE_STATUS_PENDING;
$task->submittime = time();
if (!array_key_exists($language, onlinejudge_get_languages())) {
throw new onlinejudge_exception('invalidlanguage', $language);
}
$task->language = $language;
$task->component = $component;
$judgeclass = 'judge_' . onlinejudge_judge_name($language);
$judgeclasses = onlinejudge_get_judge_classes();
if (!in_array($judgeclass, $judgeclasses)) {
throw new onlinejudge_exception('invalidjudgeclass', $judgeclass);
}
$judgeclass::parse_options($options, $task);
$task->id = $DB->insert_record('onlinejudge_tasks', $task);
$fs = get_file_storage();
$filerecord = new \stdClass;
$filerecord->contextid = context_system::instance()->id;
$filerecord->component = 'local_onlinejudge';
$filerecord->filearea = 'tasks';
$filerecord->itemid = $task->id;
foreach ($files as $key => $value) {
if ($value instanceof stored_file) {
$fs->create_file_from_storedfile($filerecord, $value);
} else {
$filerecord->filepath = dirname($key);
if (strpos($filerecord->filepath, '/') !== 0) {
$filerecord->filepath = '/' . $filerecord->filepath;
}
if (strrpos($filerecord->filepath, '/') !== strlen($filerecord->filepath) - 1) {
$filerecord->filepath .= '/';
}
$filerecord->filename = basename($key);
$fs->create_file_from_string($filerecord, $value);
}
}
return $task->id;
}
/**
* Judge specified task
*
* @param $taskorid object of task or task id
* @return stdClass task updated
* @throws Exception
* @throws onlinejudge_exception
*/
function onlinejudge_judge($taskorid) {
global $DB;
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'event' . DIRECTORY_SEPARATOR . 'onlinejudge_task_judged.php');
if (is_object($taskorid)) {
$task = $taskorid;
} else {
$task = $DB->get_record('onlinejudge_tasks', array('id' => $taskorid));
}
$task->judgetime = time();
$judgeclass = 'judge_' . onlinejudge_judge_name($task->language);
$judgeclasses = onlinejudge_get_judge_classes();
if (!in_array($judgeclass, $judgeclasses)) {
$task->status = ONLINEJUDGE_STATUS_INTERNAL_ERROR;
$task->infostudent = get_string('invalidjudgeclass', 'local_onlinejudge', $judgeclass);
$DB->update_record('onlinejudge_tasks', $task);
throw new onlinejudge_exception('invalidjudgeclass', $judgeclass);
}
$judge = new $judgeclass($task);
try {
$task = $judge->judge();
} catch (Exception $e) {
$task->status = ONLINEJUDGE_STATUS_INTERNAL_ERROR;
$task->infostudent = $e->getMessage();
$DB->update_record('onlinejudge_tasks', $task);
$context = context_module::instance($task->cmid);
$event = \mod_onlinejudge\event\onlinejudge_task_judged::create(array('context' => $context, 'objectid' => $task->id));
$event->add_record_snapshot('onlinejudge_tasks', $task);
$event->trigger();
throw $e;
}
$DB->update_record('onlinejudge_tasks', $task);
$context = context_module::instance($task->cmid);
$event = \mod_onlinejudge\event\onlinejudge_task_judged::create(array('context' => $context, 'objectid' => $task->id));
$event->add_record_snapshot('onlinejudge_tasks', $task);
$event->trigger();
return $task;
}
/**
* Return detail of the task
*
* @param int $taskid
* @return object of task or null if unavailable
*/
function onlinejudge_get_task($taskid) {
global $DB;
return $DB->get_record('onlinejudge_tasks', array('id' => $taskid));
}
/**
* Return the overall status of a list of tasks
*
* @param array $tasks
* @return int status
*/
function onlinejudge_get_overall_status($tasks) {
$status = ONLINEJUDGE_STATUS_UNSUBMITTED;
foreach ($tasks as $task) {
if (is_null($task)) // We can't give out any status on null task
return ONLINEJUDGE_STATUS_UNSUBMITTED;
if ($status == ONLINEJUDGE_STATUS_UNSUBMITTED) {
$status = $task->status;
} else if ($status != $task->status) {
$status = ONLINEJUDGE_STATUS_MULTI_STATUS;
break;
}
}
return $status;
}
function onlinejudge_get_judge_classes() {
global $CFG;
static $judgeclasses = array();
if (empty($judgeclasses)) {
if ($plugins = get_list_of_plugins('local/onlinejudge/judge')) {
foreach ($plugins as $plugin => $dir) {
$judgeclasses[] = "judge_$dir";
}
}
}
return $judgeclasses;
}
/**
* Parse judge engine name from language
*/
function onlinejudge_judge_name($language) {
return substr($language, strpos($language, '-') + 1);
}
/**
* Delete related records
*
* @param int $cmid
*/
function onlinejudge_delete_coursemodule($cmid) {
global $DB;
// Mark them as deleted only and keep the statistics.
// Delete them really in cron
return $DB->set_field('onlinejudge_tasks', 'deleted', 1, array('cmid' => $cmid));
}
function onlinejudge_get_temp_dir() {
global $CFG;
// Use static variable to suppress getmypid() calls
// The same process use the same temp dir so that
// it is possable to reuse some temp files
static $tmpdir = '';
if (empty($tmpdir)) {
$tmpdir = $CFG->dataroot . '/temp/onlinejudge/' . getmypid();
}
if (!check_dir_exists($tmpdir)) {
throw new moodle_exception('errorcreatingdirectory', '', '', $tmpdir);
}
return $tmpdir;
}
function onlinejudge_clean_temp_dir($content_only = true) {
remove_dir(onlinejudge_get_temp_dir(), $content_only);
}