-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresult.php
More file actions
204 lines (192 loc) · 7.95 KB
/
result.php
File metadata and controls
204 lines (192 loc) · 7.95 KB
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
<?php
session_start() ;
$db_host = 'localhost' ;
$db_database = 'pd course' ;
$db_username = 'root' ;
$connection = mysql_connect($db_host, $db_username, 'pdogsserver');
if (!$connection)
die ("connection failed".mysql_error()) ;
mysql_query("SET NAMES 'utf8'");
$selection = mysql_select_db($db_database) ;
if (!$selection)
die ("selection failed".mysql_error()) ;
date_default_timezone_set('Asia/Taipei');
$datetime = date ("Y-m-d H:i:s");
if (!isset($_SESSION['account'])){
header ("Location:index.php") ;
} else {
$acc = mysql_real_escape_string($_SESSION['account']);
$problem_dir = '.\\student\\'.$acc.'\\'.$_POST['problem_num'];
$log_dir = '.\\student\\'.$acc.'\\'.$_POST['problem_num'].'\\log';
$ans_dir = '.\\student\\'.$acc.'\\'.$_POST['problem_num'].'\\answer';
if (!is_dir($problem_dir))
mkdir($problem_dir);
if (!is_dir($log_dir))
mkdir($log_dir);
if (!is_dir($ans_dir))
mkdir($ans_dir);
$judge_dir = '.\\judgement\\'.$_POST['problem_num'];
$upfile = $problem_dir.'\\'.$acc.'-'.$_POST['problem_num'].'.cpp';
$pdffile = $problem_dir.'\\'.$acc.'-'.$_POST['problem_num'].'.pdf';
$exefile = $problem_dir.'\\'.$acc.'-'.$_POST['problem_num'].'.exe';
$exename = $acc.'-'.$_POST['problem_num'].'.exe';
$compile_logfile = $problem_dir.'\\log\\compile_err_log.txt';
$run_logfile = $problem_dir.'\\log\\run_err_log.txt';
$all_logfile = '.\\judgement\\upload_log.txt';
$outputfile = $problem_dir.'\\answer\\output.txt';
$resultfile = $problem_dir.'\\answer\\score.txt';
$exec_timefile = $problem_dir.'\\answer\\exec_time.txt';
$testfile = $judge_dir.'\\testing_data.txt';
if (file_exists($upfile)) unlink($upfile);
if (file_exists($exefile)) unlink($exefile);
if (file_exists($outputfile)) unlink($outputfile);
if (file_exists($resultfile)) unlink($resultfile);
if (file_exists($pdffile)) unlink($pdffile);
$status = '';
$score = 0;
$exec_result = 0;
$return = -1;
$fc = $_POST['problem_num'][0];
$ID = $_POST['problem_num'];
//根據是PD作業或是LAB作業取出題號
if ($fc == "P"){
$query = "SELECT deadline FROM pd_hw WHERE p_id = '".$_POST['problem_num']."'";
$command_judge = 'python judge.py '.$acc.' '.$_POST['problem_num'];
$query_score = "SELECT total_score FROM pd_hw WHERE p_id = '".$_POST['problem_num']."'";
} else if($fc == "L"){
$query = "SELECT deadline FROM lab_hw WHERE lab_id = '".$_POST['problem_num']."'";
$command_judge = 'python labjudge.py '.$acc.' '.$_POST['problem_num'];
$query_score = "SELECT total_score FROM lab_hw WHERE lab_id = '".$_POST['problem_num']."'";
}
$time = mysql_query($query);
$fetch_time = mysql_fetch_row($time);
if (isset($_FILES['pdfupload'])){ //pdf 檔案上傳
if ( $fetch_time[0] > $datetime ){ //如果還沒超過死線
move_uploaded_file($_FILES['pdfupload']['tmp_name'], $pdffile);
}
}
if (isset($_FILES['upload'])){ // 程式碼檔案上傳
move_uploaded_file($_FILES['upload']['tmp_name'], $upfile);
//如果繳交的題目還沒超過死線
if ( $fetch_time[0] > $datetime ){
//編譯.cpp檔
if (file_exists($upfile)){
$fp = fopen($compile_logfile, 'a');
fwrite($fp, '['.$datetime.'] :'."\n");
fclose($fp);
$fp = fopen($run_logfile, 'a');
fwrite($fp, '['.$datetime.'] :'."\n");
fclose($fp);
//$command = 'g++ '.$upfile.' -o '.$exefile.' -enable-auto-import 2>> '.$compile_logfile;
$command = 'g++ '.$upfile.' -o '.$exefile.' 2>> '.$compile_logfile;
system($command, $return);
if ($return == 0){
//如果成功編譯出.exe檔 執行程式
//ex. hw.exe < testing_data.txt > output.txt 2>> log.txt
//$command = $exefile.' < '.$testfile.' > '.$outputfile.' 2>> '.$run_logfile;
//$command = 'python timeout.py '.$exefile.' '.$testfile.' '.$outputfile.' '.$run_logfile.' '.$exec_timefile.' '.$exename.' 2>> '.$run_logfile;
$command = 'python timeout.py '.$exefile.' '.$testfile.' '.$outputfile.' '.$run_logfile.' '.$exec_timefile.' '.$exename.' 3';
if ($exec_result = exec($command, $return)){ //如果執行成功 比對結果
if ($exec_result != NULL and $exec_result == 'Time limit exceed'){
$status = 'Time limit exceed';
$exec_result = 3;
} else if ($exec_result != NULL and $exec_result == 'Runtime error'){
$status = 'Runtime error';
$exec_result = 0;
} else{
//ex. python judge.py b01705001 PD14-1
$score = exec($command_judge, $return);
$s = mysql_query($query_score);
$fetch_s = mysql_fetch_row($s);
if ($score == $fetch_s[0]){
$status = 'Accepted';
} else {
$status = 'Wrong answer';
}
}
} else {
//runtime error
$status = 'Runtime error';
}
} else {
//compile error
$status = 'Compilation error';
}
} else {
//upload error
$status = 'System upload error';
}
$fp = fopen($all_logfile, 'a');
fwrite($fp, '['.$datetime.'] : '.$acc.' submits a file for problem '.$_POST['problem_num']."\n");
fclose($fp);
$query = "SELECT s_id FROM student WHERE account = '".$acc."'" ;
$id = mysql_query($query);
$fetch_id = mysql_fetch_row($id);
//echo $_POST['problem_num'][4];
if ($fc == "P"){
$insert = "INSERT INTO pd_score(s_id, p_id, status, time, exec_time, score)
VALUES ('$fetch_id[0]', '$ID', '$status', '$datetime', '$exec_result', '$score')" ;
} else if($fc == "L"){
$insert = "INSERT INTO lab_score(s_id, lab_id, status, time, exec_time, score)
VALUES ('$fetch_id[0]', '$ID', '$status', '$datetime', '$exec_result', '$score')" ;
}
$success = mysql_query($insert);
} else { //超過死線
echo "<div class='hero-unit upload_section'><p>deadline is passed!</p></div>";
}
}
if (isset($_FILES['upload']) or isset($_FILES['pdfupload'])){
?> <!-- 改作業序號看這裡 -->
<div class="hero-unit upload_section">
<table class="table table-hover"><?php
if (isset($_FILES['pdfupload'])){ ?>
<p>PDF file is submitted successfully!</p>
<br> <?php
}
if (isset($_FILES['upload'])){?>
<thead>
<tr>
<th>Problem</th>
<th>Status</th>
<th>Run time</th>
<th>Submission date</th>
<th>Score</th>
</tr>
</thead>
<tbody> <?php
if ($fc == "P"){
$query_rec = "SELECT p_id, status, exec_time, time, score FROM pd_score NATURAL JOIN student
WHERE account = '".$acc."' ORDER BY time DESC";
} else if ($fc == "L"){
$query_rec = "SELECT lab_id, status, exec_time, time, score FROM lab_score NATURAL JOIN student
WHERE account = '".$acc."' ORDER BY time DESC";
}
$rec = mysql_query($query_rec);
$fetch_rec = mysql_fetch_row($rec);
if ($fetch_rec[1] == 'Accepted'){
?><tr class="accept"><?php
} else if ($fetch_rec[1] == 'Compilation error' or $fetch_rec[1] == 'Runtime error'){
?><tr class="error"><?php
} else if ($fetch_rec[1] == 'Time limit exceed'){
?><tr class="time_exceed"><?php
} else if ($fetch_rec[1] == 'Wrong answer'){
?><tr class="wrong_answer"><?php
} else if ($fetch_rec[1] == 'System upload error'){
?><tr class="upload_error"><?php
} ?>
<td><?php echo $fetch_rec[0];?></td>
<td><?php echo $fetch_rec[1];?></td>
<td><?php echo $fetch_rec[2].'s';?></td>
<td><?php echo $fetch_rec[3];?></td>
<td><?php echo $fetch_rec[4];?></td>
</tr>
</tbody> <?php
} ?>
</table>
</div> <?php
}
if (!isset($_FILES['upload']) and !isset($_FILES['pdfupload']) ){ //沒有上傳 理論上不會發生 因為沒辦法按下upload鈕
echo 'no upload';
}
}
?>