-
Notifications
You must be signed in to change notification settings - Fork 1
/
Contest.php
339 lines (280 loc) · 9.38 KB
/
Contest.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
<!DOCTYPE html>
<html lang="zh-cn">
<?php require_once('Php/HTML_Head.php') ?>
<?php
//比赛显示优先级
const ConDoing = 1; //进行中
const ConEnrolling = 2; //报名中
const ConnoStart = 3; //未开始
const ConOver = 4; //已结束
$ModeStr = array('', '正在进行中', '报名中', '未开始', '已结束');
$ModeCss = array('', 'label-success', 'label-success', 'label-primary', 'label-default');
$Addsql = '';
if (isset($_GET['Search'])) {
$SearchText = htmlspecialchars(addslashes(trim($_GET['Search'])));
if (can_edit_contest()) {
$Addsql .= " WHERE `Title` like '%" . $SearchText . "%'";
} else {
$Addsql .= " WHERE `Show`=1 AND `Title` like '%" . $SearchText . "%'";
}
}
//获取比赛数量
$sql = "SELECT count(*) AS `value` FROM `oj_contest`" . $Addsql;
if (can_edit_contest()) {
$sql = "SELECT count(*) AS `value` FROM oj_contest" . $Addsql;
}
$rs = oj_mysql_query($sql);
$ConCount = oj_mysql_fetch_array($rs);
$clength = $ConCount['value'];
//获取当前页数
$iPage = 1;
if (array_key_exists('Page', $_GET)) {
$iPage = intval($_GET['Page']);
}
$iPage = floor($iPage);
//定义常量,一页中最大显示数量
define("MaxRankNum", 20);
//定义常量,一页中最多显示按钮数量(奇数)
define("MaxButtonNum", 5);
//计算总页数
$AllPage = $clength / MaxRankNum;
//计算上一页的页数
$LastPage = ($iPage - 1 <= 0) ? 1 : ($iPage - 1);
//计算下一页的页数
$NextPage = $iPage * MaxRankNum < $clength ? $iPage + 1 : $iPage;
//最小页数
$MinPage = 1;
$iPage = $iPage >= $MinPage ? $iPage : 1;
//最大页数
$MaxPage = ceil(($clength * 1.0) / MaxRankNum);
$MaxPage = $MaxPage > 0 ? $MaxPage : 1;
$iPage = $iPage <= $MaxPage ? $iPage : $MaxPage;
//根据页数计算显示第一个的排名
$Rank = ($iPage - 1) * MaxRankNum;
//开始显示的按钮数字
$StaButNum;
//至结束显示的按钮数字
$EndButNum;
//如果最大页数小于等于一页中最多显示按钮数量
if ($MaxPage <= MaxButtonNum) {
$StaButNum = 1;
$EndButNum = $MaxPage;
} else {
//将当前页的数字当作中间的按钮
$iCenBuNum = $iPage;
//开始显示的按钮数字为 最多显示按钮数量/2
$StaButNum = $iCenBuNum - floor(MaxButtonNum / 2);
//至结束显示的按钮的数字为 最多显示按钮数量/2
$EndButNum = $iCenBuNum + floor(MaxButtonNum / 2);
//如果开始显示的数字<=0,说明不能把当前页的数字当作中间的按钮
if ($StaButNum <= 0) {
//将至结束显示的按钮的数字调整
$EndButNum -= $StaButNum - 1;
//开始显示的数字显示为1
$StaButNum -= $StaButNum - 1;
}
//如果结束显示的数字>最多显示按钮数量,说明不能把当前页的数字当作中间的按钮
if ($EndButNum > $MaxPage) {
//调整开始按钮的值
$StaButNum -= ($EndButNum - $MaxPage);
//结束显示的数字显示为最大值
$EndButNum = $MaxPage;
}
}
$LimitShowSql = " ORDER BY `OverTime` DESC,`ConID` DESC LIMIT " . ($iPage - 1) * MaxRankNum . ", " . MaxRankNum;
$sql = "SELECT * FROM `oj_contest` WHERE `Show`=1";
if (can_edit_contest()) {
$sql = "SELECT * FROM `oj_contest`";
}
if (isset($SearchText)) {
if (can_edit_contest()) {
$sql .= " WHERE `Title` like '%" . $SearchText . "%'";
} else {
$sql .= " AND `Title` like '%" . $SearchText . "%'";
}
}
$sql .= $LimitShowSql;
$result = oj_mysql_query($sql);
$NowDate = date('Y-m-d H:i:s');
$AllContest = array();
while ($result && $row = oj_mysql_fetch_array($result)) {
$iMode = ConOver;
if ($NowDate <= $row['StartTime']) {
if ($NowDate >= $row['EnrollStartTime'] && $NowDate <= $row['EnrollOverTime'])
$iMode = ConEnrolling;
else
$iMode = ConnoStart;
} else if ($NowDate <= $row['OverTime']) {
$iMode = ConDoing;
} else {
$iMode = ConOver;
}
$TF = get_user_tailsAndFight($row['Organizer']);
$AllContest[] = array(
"ConID" => $row['ConID'],
"Title" => $row['Title'],
"Organizer" => $row['Organizer'],
"Tails" => $TF['tails'],
"Fight" => $TF['fight'],
"Rule" => $row['Rule'],
"Type" => $row['Type'],
"Show" => $row['Show'],
"StartTime" => $row['StartTime'],
"OverTime" => $row['OverTime'],
"Mode" => $iMode
);
}
//按运行ID排序
function my_sort($a, $b)
{
if ($a['Mode'] == $b['Mode']) {
return $a['ConID'] < $b['ConID'] ? 1 : -1;
}
return $a['Mode'] < $b['Mode'] ? -1 : 1;
}
usort($AllContest, "my_sort");
?>
<body>
<?php require_once('Php/Page_Header.php') ?>
<?php
if (can_edit_contest()) {
?>
<script>
function changeContestStatus(contestID) {
$.get("/Contest/ContestStatus.php?ConID=" + contestID, function(msg) {
var obj = eval('(' + msg + ')');
if (obj.status === 0) {
location.reload();
}
});
}
</script>
<?php
}
?>
<div class="container">
<ul class="pagination">
<li><a href=<?php echo '"/Contest.php?Page=' . ($MinPage) . '"' ?>>«</a></li>
<li><a href=<?php echo '"/Contest.php?Page=' . ($LastPage) . '"' ?>><</a></li>
<?php
for ($i = $StaButNum; $i <= $EndButNum; $i++) {
if ($i == $iPage)
echo '<li class="active"><a href="/Contest.php?Page=' . $i . '">' . $i . '</a></li>';
else
echo '<li><a href="/Contest.php?Page=' . $i . '">' . $i . '</a></li>';
}
?>
<li><a href=<?php echo '"/Contest.php?Page=' . ($NextPage) . '"' ?>>></a></li>
<li><a href=<?php echo '"/Contest.php?Page=' . ($MaxPage) . '"' ?>>»</a></li>
</ul>
<form action="/Contest.php" id="problem-search">
<div class="input-group">
<div class="input-group-btn">
<?php
if (can_edit_contest()) {
echo '<a href="/NewContest.php" class="btn btn-default">新建比赛</a>';
}
?>
</div>
<input type="text" name="Search" class="form-control" placeholder="请输入标题" value=<?php echo (isset($SearchText) ? '"' . $SearchText . '"' : '""') ?>>
<div class="input-group-btn">
<button type="submit" class="btn btn-default" tabindex="-1">查找</button>
</div>
</div>
</form>
<div class="panel panel-default animated fadeInLeft">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>比赛ID</th>
<?php
if (can_edit_contest() || can_edit_contest()) {
echo '<th>功能</th>';
}
?>
<th>标题</th>
<th>比赛状态</th>
<th>开始时间</th>
<th>竞赛时长</th>
<th>比赛规则</th>
<th>类型</th>
<th>举办人</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < MaxRankNum; $i++) {
if (!isset($AllContest[$i]['ConID'])) {
continue;
}
echo '<tr>';
echo '<td>';
echo '<a href="Contest/Pandect.php?ConID=' . $AllContest[$i]['ConID'] . '">' . $AllContest[$i]['ConID'] . '</a> ';
echo '</td>';
if (can_edit_contest()) {
echo '<td>';
echo ' <a class="label label-warning" href="/NewContest.php?ConID=' . $AllContest[$i]['ConID'] . '">编辑</a>';
if ($AllContest[$i]['Show'] == 1) {
echo ' <a href="javascript:changeContestStatus(' . $AllContest[$i]['ConID'] . ')" class="label label-primary">隐藏</a>';
} else {
echo ' <a href="javascript:changeContestStatus(' . $AllContest[$i]['ConID'] . ')" class="label label-info">显示</a>';
}
echo '</td>';
}
echo '<td>';
echo '<a href="Contest/Pandect.php?ConID=' . $AllContest[$i]['ConID'] . '">' . $AllContest[$i]['Title'] . '</a>';
echo '</td>';
echo '<td>';
echo '<span class="label ' . $ModeCss[$AllContest[$i]['Mode']] . '">' . $ModeStr[$AllContest[$i]['Mode']] . '</span>';
echo '</td>';
echo '<td>' . $AllContest[$i]['StartTime'] . '</td>';
$Startdate = strtotime($AllContest[$i]['StartTime']);
$Enddate = strtotime($AllContest[$i]['OverTime']);
$Timediff = $Enddate - $Startdate;
$Days = intval($Timediff / 86400);
$Remain = $Timediff % 86400;
$Hours = intval($Remain / 3600);
$Remain = $Remain % 3600;
$Mins = intval($Remain / 60);
$Secs = $Remain % 60;
echo '<td>';
if ($Days > 0) {
echo $Days . ($Days == 1 ? ' day ' : ' days ');
}
echo $Hours . ':' . $Mins . ':' . $Secs;
echo '<td>' . $AllContest[$i]['Rule'] . '</td>';
if ($AllContest[$i]['Type'] == 1) {
echo '<td><font color="red">Private</font></td>';
} else {
echo '<td><font color="green">Public</font></td>';
}
echo '<td><a href="/OtherUser.php?User=' . $AllContest[$i]['Organizer'] . '" class=' . GetUserColor($AllContest[$i]['Fight']) . '>' . $AllContest[$i]['Organizer'] . ($AllContest[$i]['Tails'] ? '(' . $AllContest[$i]['Tails'] . ')' : '') . '</a></td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
<center>
<ul class="pagination">
<li><a href=<?php echo '"/Contest.php?Page=' . ($MinPage) . '"' ?>>«</a></li>
<li><a href=<?php echo '"/Contest.php?Page=' . ($LastPage) . '"' ?>><</a></li>
<?php
for ($i = $StaButNum; $i <= $EndButNum; $i++) {
if ($i == $iPage)
echo '<li class="active"><a href="/Contest.php?Page=' . $i . '">' . $i . '</a></li>';
else
echo '<li><a href="/Contest.php?Page=' . $i . '">' . $i . '</a></li>';
}
?>
<li><a href=<?php echo '"/Contest.php?Page=' . ($NextPage) . '"' ?>>></a></li>
<li><a href=<?php echo '"/Contest.php?Page=' . ($MaxPage) . '"' ?>>»</a></li>
</ul>
</center>
</div>
<?php
$PageActive = '#contest';
require_once('Php/Page_Footer.php');
?>
</body>
</html>