Skip to content

Commit

Permalink
refactor(web/contest): contest manage v2 and contest confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
renbaoshuo committed Oct 23, 2022
1 parent 71901ef commit 1227acf
Show file tree
Hide file tree
Showing 12 changed files with 828 additions and 316 deletions.
40 changes: 29 additions & 11 deletions web/app/controllers/add_contest.php
Expand Up @@ -16,7 +16,23 @@
$time_form = new UOJForm('time');
$time_form->addVInput(
'name', 'text', '比赛标题', 'New Contest',
function($str) {
function($name, &$vdata) {
if ($name == '') {
return '标题不能为空';
}

if (strlen($name) > 100) {
return '标题过长';
}

$name = HTML::escape($name);

if ($name === '') {
return '无效编码';
}

$vdata['name'] = $name;

return '';
},
null
Expand All @@ -35,23 +51,25 @@ function($str, &$vdata) {
);
$time_form->addVInput(
'last_min', 'text', '时长(单位:分钟)', 180,
function($str) {
return !validateUInt($str) ? '必须为一个整数' : '';
function($str, &$vdata) {
if (!validateUInt($str)) {
return '必须为一个整数';
}

$vdata['last_min'] = $str;

return '';
},
null
);
$time_form->handle = function(&$vdata) {
$start_time_str = $vdata['start_time']->format('Y-m-d H:i:s');

$purifier = HTML::purifier_inline();

$esc_name = $_POST['name'];
$esc_name = $purifier->purify($esc_name);
$esc_name = DB::escape($esc_name);
$esc_name = DB::escape($vdata['name']);
$esc_last_min = DB::escape($vdata['last_min']);

DB::query("insert into contests (name, start_time, last_min, status) values ('$esc_name', '$start_time_str', ${_POST['last_min']}, 'unfinished')");
DB::query("insert into contests (name, start_time, last_min, status) values ('$esc_name', '$start_time_str', $esc_last_min, 'unfinished')");
};
$time_form->succ_href="/contests";
$time_form->succ_href = "/contests";
$time_form->runAtServer();
?>
<?php echoUOJPageHeader('添加比赛') ?>
Expand Down
77 changes: 77 additions & 0 deletions web/app/controllers/contest_confirmation.php
@@ -0,0 +1,77 @@
<?php
requireLib('bootstrap5');
requirePHPLib('form');

if (!validateUInt($_GET['id']) || !($contest = queryContest($_GET['id']))) {
become404Page();
}

genMoreContestInfo($contest);

if (!Auth::check()) {
redirectToLogin();
} elseif (!hasRegistered($myUser, $contest)) {
redirectTo("/contest/{$contest['id']}/register");
} elseif ($contest['cur_progress'] < CONTEST_IN_PROGRESS) {
redirectTo('/contests');
} elseif (hasParticipated($myUser, $contest) || $contest['cur_progress'] > CONTEST_IN_PROGRESS) {
redirectTo("/contest/{$contest['id']}");
}

$confirm_form = new UOJForm('confirm');
$confirm_form->submit_button_config['class_str'] = 'btn btn-primary';
$confirm_form->submit_button_config['margin_class'] = 'mt-3';
$confirm_form->submit_button_config['text'] = '我已核对信息,确认参加比赛';
$confirm_form->handle = function() use ($myUser, $contest) {
DB::update("update contests_registrants set has_participated = 1 where username = '{$myUser['username']}' and contest_id = {$contest['id']}");
};
$confirm_form->succ_href = "/contest/{$contest['id']}";
$confirm_form->runAtServer();
?>

<?php echoUOJPageHeader('确认参赛 - ' . HTML::stripTags($contest['name'])) ?>

<div class="card mw-100 mx-auto" style="width:800px">
<div class="card-body">
<h1 class="h2 card-title text-center mb-3">确认参赛</h1>

<p class="card-text text-center">您即将参加比赛 “<b><?= $contest['name'] ?></b>”,请在正式参赛前仔细核对以下比赛信息:</p>

<div class="table-responsive mx-auto" style="width:500px">
<table class="table">
<thead>
<tr>
<th style="width:40%"></th>
<th style="width:60%"></th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center">比赛名称</td>
<td><?= $contest['name'] ?></td>
</tr>
<tr>
<td class="text-center">参赛选手</td>
<td><?= getUserLink($myUser['username']) ?></td>
</tr>
<tr>
<td class="text-center">开始时间</td>
<td><?= $contest['start_time_str'] ?></td>
</tr>
<tr>
<td class="text-center">结束时间</td>
<td><?= $contest['end_time_str'] ?></td>
</tr>
<tr>
<td class="text-center">比赛赛制</td>
<td><?= $contest['extra_config']['contest_type'] ?: 'OI' ?></td>
</tr>
</tbody>
</table>
</div>

<?php $confirm_form->printHTML() ?>
</div>
</div>

<?php echoUOJPageFooter() ?>
25 changes: 18 additions & 7 deletions web/app/controllers/contest_inside.php
Expand Up @@ -12,21 +12,32 @@
}
genMoreContestInfo($contest);

if (!hasContestPermission(Auth::user(), $contest)) {
if (!hasContestPermission($myUser, $contest)) {
if ($contest['cur_progress'] == CONTEST_NOT_STARTED) {
header("Location: /contest/{$contest['id']}/register");
die();
redirectTo("/contest/{$contest['id']}/register");
} elseif ($contest['cur_progress'] == CONTEST_IN_PROGRESS) {
if ($myUser == null || !hasRegistered(Auth::user(), $contest)) {
becomeMsgPage("<h1>比赛正在进行中</h1><p>很遗憾,您尚未报名。比赛结束后再来看吧~</p>");
if (Auth::check()) {
if (!hasParticipated($myUser, $contest)) {
redirectTo("/contest/{$contest['id']}/confirm");
}
} else {
redirectToLogin();
}
} else {
if (!isNormalUser($myUser)) {
if (!hasRegistered($myUser, $contest) && !isNormalUser($myUser) && UOJConfig::$data['switch']['force-login']) {
become403Page();
}
}
} else {
if ($contest['cur_progress'] == CONTEST_IN_PROGRESS) {
if (hasRegistered($myUser, $contest)) {
if (!hasParticipated($myUser, $contest)) {
redirectTo("/contest/{$contest['id']}/confirm");
}
}
}
}

if (isset($_GET['tab'])) {
$cur_tab = $_GET['tab'];
} else {
Expand Down

0 comments on commit 1227acf

Please sign in to comment.