refactor(contest/manage): uoj_form_v2

This commit is contained in:
Baoshuo Ren 2023-01-15 08:05:00 +08:00
parent 3b1def5754
commit 87013054c4
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
2 changed files with 164 additions and 189 deletions

View File

@ -37,72 +37,73 @@ if (!isset($tabs_info[$cur_tab])) {
} }
if ($cur_tab == 'profile') { if ($cur_tab == 'profile') {
$profile_form = new UOJBs4Form('time'); $profile_form = new UOJForm('time');
$profile_form->addVInput( $profile_form->addInput(
'name', 'name',
'text', [
'比赛标题', 'label' => '比赛标题',
$contest['name'], 'default_value' => UOJContest::info('name'),
function ($name, &$vdata) { 'validator_php' => function ($name, &$vdata) {
if ($name == '') { if ($name == '') {
return '标题不能为空'; return '标题不能为空';
} }
if (strlen($name) > 100) { if (strlen($name) > 100) {
return '标题过长'; return '标题过长';
} }
$name = HTML::escape($name); $name = HTML::escape($name);
if ($name === '') { if ($name === '') {
return '无效编码'; return '无效编码';
} }
$vdata['name'] = $name; $vdata['name'] = $name;
return ''; return '';
}, },
null ],
); );
$profile_form->addVInput( $profile_form->addInput(
'start_time', 'start_time',
'text', [
'开始时间', 'div_class' => 'mt-3',
$contest['start_time_str'], 'label' => '开始时间',
function ($str, &$vdata) { 'default_value' => UOJContest::info('start_time_str'),
try { 'validator_php' => function ($start_time, &$vdata) {
$vdata['start_time'] = new DateTime($str); try {
} catch (Exception $e) { $vdata['start_time'] = new DateTime($start_time);
return '无效时间格式'; } catch (Exception $e) {
} return '无效时间格式';
return ''; }
}, return '';
null },
]
); );
$profile_form->addVInput( $profile_form->addInput(
'last_min', 'last_min',
'text', [
'时长(单位:分钟)', 'div_class' => 'mt-3',
$contest['last_min'], 'label' => '时长',
function ($str, &$vdata) { 'help' => '单位为分钟。',
if (!validateUInt($str)) { 'default_value' => UOJContest::info('last_min'),
return '必须为一个整数'; 'validator_php' => function ($last_min, &$vdata) {
} if (!validateUInt($last_min)) {
return '必须为一个整数';
$vdata['last_min'] = $str; }
$vdata['last_min'] = $last_min;
return ''; return '';
}, },
null ]
); );
$profile_form->handle = function (&$vdata) use ($contest) { $profile_form->handle = function (&$vdata) {
DB::update([ DB::update([
"update contests", "update contests",
"set", [ "set", [
"name" => $vdata['name'], "name" => $vdata['name'],
"start_time" => $vdata['start_time']->format('Y-m-d H:i:s'), "start_time" => UOJTime::time2str($vdata['start_time']),
"last_min" => $vdata['last_min'], "last_min" => $vdata['last_min'],
], "where", ["id" => $contest['id']] ], "where", ["id" => UOJContest::info('id')]
]); ]);
dieWithJsonData(['status' => 'success', 'message' => '修改成功']); dieWithJsonData(['status' => 'success', 'message' => '修改成功']);
@ -162,51 +163,58 @@ EOD);
dieWithAlert('移除成功!'); dieWithAlert('移除成功!');
} }
$add_problem_form = new UOJBs4Form('add_problem'); $add_problem_form = new UOJForm('add_problem');
$add_problem_form->addVInput( $add_problem_form->addInput(
'problem_id', 'problem_id',
'text', [
'题目 ID', 'label' => '题目 ID',
'', 'validator_php' => function ($problem_id, &$vdata) {
function ($problem_id, &$vdata) { $problem = UOJProblem::query($problem_id);
$problem = UOJProblem::query($problem_id); if (!$problem) {
if (!$problem) { return '题目不存在。';
return '题目不存在。'; }
}
if (!$problem->userCanManage(Auth::user())) { if (!$problem->userCanManage(Auth::user())) {
return "无权添加此题目。"; return "无权添加此题目。";
} }
if (UOJContest::cur()->hasProblem($problem)) { if (UOJContest::cur()->hasProblem($problem)) {
return "题目已经在本场比赛中。"; return "题目已经在本场比赛中。";
} }
$vdata['problem_id'] = $problem_id; $vdata['problem_id'] = $problem_id;
return ''; return '';
}, },
null ]
); );
$add_problem_form->addVSelect('judge_config', [ $add_problem_form->addSelect('judge_config', [
'default' => '默认', 'div_class' => 'mt-3',
'sample' => '只测样例', 'label' => '评测设置',
'no-details' => '测试全部数据,对于每个测试点显示得分但不显示详情', 'options' => [
'full' => '测试全部数据', 'default' => '默认',
], '评测设置', 'default'); 'sample' => '只测样例',
$add_problem_form->addVCheckboxes('bonus', ['0' => '否', '1' => '是'], '是否为 bonus 题ACM 赛制)', '0'); 'no-details' => '测试全部数据,对于每个测试点显示得分但不显示详情',
'full' => '测试全部数据',
],
'default_value' => 'default',
]);
$add_problem_form->addCheckbox('bonus', [
'div_class' => 'form-check mt-3',
'label' => '是否为 bonus 题(针对 ACM 赛制)',
]);
$add_problem_form->handle = function (&$vdata) use ($contest) { $add_problem_form->handle = function (&$vdata) use ($contest) {
$level = DB::selectFirst([ $level = DB::selectSingle([
"select", "max(level)", "select", "max(level)",
"from", "contests_problems", "from", "contests_problems",
"where", [ "where", [
"contest_id" => $contest['id'], "contest_id" => UOJContest::info('id'),
] ]
])["max(level)"]; ]);
DB::insert([ DB::insert([
"insert ignore into contests_problems", "insert ignore into contests_problems",
"(contest_id, problem_id, level)", "(contest_id, problem_id, level)",
"values", DB::tuple([$contest['id'], $vdata['problem_id'], $level + 1]) "values", DB::tuple([UOJContest::info('id'), $vdata['problem_id'], $level + 1])
]); ]);
$judge_type = $_POST['judge_config']; $judge_type = $_POST['judge_config'];
@ -220,13 +228,13 @@ EOD);
DB::update([ DB::update([
"update contests", "update contests",
"set", ["extra_config" => $esc_extra_config], "set", ["extra_config" => $esc_extra_config],
"where", ["id" => $contest['id']] "where", ["id" => UOJContest::info('id')]
]); ]);
dieWithJsonData(['status' => 'success', 'message' => "题目 #{$vdata['problem_id']} 添加成功!"]); dieWithJsonData(['status' => 'success', 'message' => "题目 #{$vdata['problem_id']} 添加成功!"]);
}; };
$add_problem_form->submit_button_config['text'] = '添加'; $add_problem_form->config['submit_button']['text'] = '添加';
$add_problem_form->submit_button_config['class_str'] = 'btn btn-secondary mt-3'; $add_problem_form->config['submit_button']['class'] = 'btn btn-secondary mt-3';
$add_problem_form->setAjaxSubmit(<<<EOD $add_problem_form->setAjaxSubmit(<<<EOD
function(res) { function(res) {
if (res.status === 'success') { if (res.status === 'success') {
@ -260,7 +268,6 @@ EOD);
dieWithAlert('用户不是这场比赛的管理员。'); dieWithAlert('用户不是这场比赛的管理员。');
} }
DB::delete([ DB::delete([
"delete from contests_permissions", "delete from contests_permissions",
"where", [ "where", [
@ -271,40 +278,40 @@ EOD);
dieWithAlert('移除成功!'); dieWithAlert('移除成功!');
} }
$add_manager_form = new UOJBs4Form('add_manager'); $add_manager_form = new UOJForm('add_manager');
$add_manager_form->addVInput( $add_manager_form->addInput(
'username', 'username',
'text', [
'用户名', 'label' => '用户名',
'', 'validator_php' => function ($username, &$vdata) {
function ($username, &$vdata) use ($contest) { $user = UOJUser::query($username);
$user = UOJUser::query($username);
if (!$user) { if (!$user) {
return '用户不存在'; return '用户不存在';
} }
if (UOJContest::cur()->userCanManage($user)) { if (UOJContest::cur()->userCanManage($user)) {
return '用户已经是这场比赛的管理员'; return '用户已经是这场比赛的管理员';
} }
$vdata['username'] = $username; $vdata['username'] = $username;
return ''; return '';
}, },
null ]
); );
$add_manager_form->handle = function (&$vdata) use ($contest) { $add_manager_form->handle = function (&$vdata) {
DB::insert([ DB::insert([
"insert into contests_permissions", "insert into contests_permissions",
"(contest_id, username)", DB::bracketed_fields(["contest_id", "username"]),
"values", DB::tuple([$contest['id'], $vdata['username']]) "values",
DB::tuple([UOJContest::info('id'), $vdata['username']])
]); ]);
dieWithJsonData(['status' => 'success', 'message' => '已将用户名为 ' . $vdata['username'] . ' 的用户设置为本场比赛的管理者。']); dieWithJsonData(['status' => 'success', 'message' => '已将用户名为 ' . $vdata['username'] . ' 的用户设置为本场比赛的管理者。']);
}; };
$add_manager_form->submit_button_config['text'] = '添加'; $add_manager_form->config['submit_button']['text'] = '添加';
$add_manager_form->submit_button_config['class_str'] = 'btn btn-secondary mt-3'; $add_manager_form->config['submit_button']['class'] = 'btn btn-secondary mt-3';
$add_manager_form->setAjaxSubmit(<<<EOD $add_manager_form->setAjaxSubmit(<<<EOD
function(res) { function(res) {
if (res.status === 'success') { if (res.status === 'success') {
@ -326,83 +333,68 @@ function(res) {
EOD); EOD);
$add_manager_form->runAtServer(); $add_manager_form->runAtServer();
} elseif ($cur_tab == 'others') { } elseif ($cur_tab == 'others') {
$version_form = new UOJBs4Form('version'); $rule_form = new UOJForm('basic_rule');
$version_form->addVSelect('standings_version', [ $rule_form->addSelect('basic_rule', [
'1' => '1', 'label' => '比赛类型',
'2' => '2', 'options' => [
], '比赛排名版本', $contest['extra_config']['standings_version']); 'OI' => 'OI',
$version_form->handle = function () use ($contest) { 'IOI' => 'IOI',
$contest['extra_config']['standings_version'] = $_POST['standings_version']; 'ACM' => 'ACM',
$esc_extra_config = json_encode($contest['extra_config']); ],
DB::update([ 'default_value' => $contest['extra_config']['basic_rule'],
"update contests", ]);
"set", ["extra_config" => $esc_extra_config], $rule_form->addSelect('free_registration', [
"where", ["id" => $contest['id']], 'div_class' => 'mt-3',
]); 'label' => '报名方式',
'options' => [
dieWithJsonData(['status' => 'success', 'message' => '修改成功']); 1 => '所有人都可以自由报名',
}; 0 => '只能由管理员帮选手报名'
$version_form->setAjaxSubmit(<<<EOD ],
function(res) { 'default_value' => $contest['extra_config']['free_registration'],
if (res.status === 'success') { ]);
$('#version-result-alert') $rule_form->addCheckbox('extra_registration', [
.html('修改成功!') 'div_class' => 'form-check mt-3',
.addClass('alert-success') 'label' => '允许额外报名',
.removeClass('alert-danger') 'checked' => $contest['extra_config']['extra_registration'] ?: true,
.show(); ]);
} else { $rule_form->addSelect('individual_or_team', [
$('#version-result-alert') 'div_class' => 'mt-3',
.html('修改失败。' + (res.message || '')) 'label' => '个人赛/团体赛',
.removeClass('alert-success') 'options' => [
.addClass('alert-danger') 'individual' => '个人赛',
.show(); 'team' => '团体赛',
} ],
'default_value' => $contest['extra_config']['individual_or_team'],
$(window).scrollTop(0); ]);
} $rule_form->addInput('max_n_submissions_per_problem', [
EOD); 'div_class' => 'mt-3',
$version_form->runAtServer(); 'label' => '每题最高提交次数',
'type' => 'number',
$rule_form = new UOJBs4Form('basic_rule'); 'default_value' => $contest['extra_config']['max_n_submissions_per_problem'],
$rule_form->addVSelect('basic_rule', [ 'help' => '设为 -1 表示无限制(系统默认频率限制仍有效)。',
'OI' => 'OI', 'validator_php' => function ($str) {
'IOI' => 'IOI',
'ACM' => 'ACM',
], '比赛类型', $contest['extra_config']['basic_rule']);
$rule_form->addVSelect('free_registration', [
1 => '所有人都可以自由报名',
0 => '只能由管理员帮选手报名'
], "报名方式", $contest['extra_config']['free_registration']);
$rule_form->addVCheckboxes('extra_registration', [
'0' => '禁止',
'1' => '允许'
], '是否允许额外报名', isset($contest['extra_config']['extra_registration']) ? $contest['extra_config']['extra_registration'] : '1');
$rule_form->addVSelect('individual_or_team', [
'individual' => '个人赛',
'team' => '团体赛'
], "个人赛/团体赛", $contest['extra_config']['individual_or_team']);
$rule_form->addVInput(
'max_n_submissions_per_problem',
'text',
'每题最高提交次数(-1 表示不限制)',
$contest['extra_config']['max_n_submissions_per_problem'],
function ($str) {
return !validateUInt($str) && $str !== '-1' ? '必须为一个非负整数或 -1' : ''; return !validateUInt($str) && $str !== '-1' ? '必须为一个非负整数或 -1' : '';
}, },
null ]);
); $rule_form->addSelect('standings_version', [
'div_class' => 'mt-3',
'label' => '比赛排名版本',
'options' => [1 => 1, 2 => 2],
'default_value' => $contest['extra_config']['standings_version'],
]);
$rule_form->handle = function () use ($contest) { $rule_form->handle = function () use ($contest) {
$contest['extra_config']['basic_rule'] = $_POST['basic_rule']; $contest['extra_config']['basic_rule'] = $_POST['basic_rule'];
$contest['extra_config']['free_registration'] = (int)$_POST['free_registration']; $contest['extra_config']['free_registration'] = (int)$_POST['free_registration'];
$contest['extra_config']['individual_or_team'] = $_POST['individual_or_team']; $contest['extra_config']['individual_or_team'] = $_POST['individual_or_team'];
$contest['extra_config']['max_n_submissions_per_problem'] = (int)$_POST['max_n_submissions_per_problem']; $contest['extra_config']['max_n_submissions_per_problem'] = (int)$_POST['max_n_submissions_per_problem'];
$contest['extra_config']['extra_registration'] = (int)$_POST['extra_registration']; $contest['extra_config']['extra_registration'] = (int)$_POST['extra_registration'];
$contest['extra_config']['standings_version'] = (int)$_POST['standings_version'];
$esc_extra_config = json_encode($contest['extra_config']); $esc_extra_config = json_encode($contest['extra_config']);
DB::update([ DB::update([
"update contests", "update contests",
"set", ["extra_config" => $esc_extra_config], "set", ["extra_config" => $esc_extra_config],
"where", ["id" => $contest['id']] "where", ["id" => UOJContest::info('id')]
]); ]);
dieWithJsonData(['status' => 'success', 'message' => '修改成功']); dieWithJsonData(['status' => 'success', 'message' => '修改成功']);
@ -722,9 +714,6 @@ EOD);
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" href="#type" data-bs-toggle="tab" data-bs-target="#type">规则</a> <a class="nav-link active" href="#type" data-bs-toggle="tab" data-bs-target="#type">规则</a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="#standings-version" data-bs-toggle="tab" data-bs-target="#standings-version">排名版本</a>
</li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="#blogs" data-bs-toggle="tab" data-bs-target="#blogs">比赛资料</a> <a class="nav-link" href="#blogs" data-bs-toggle="tab" data-bs-target="#blogs">比赛资料</a>
</li> </li>
@ -751,20 +740,6 @@ EOD);
</div> </div>
</div> </div>
</div> </div>
<div class="tab-pane" id="standings-version">
<div id="version-result-alert" class="alert" role="alert" style="display: none"></div>
<div class="row row-cols-1 row-cols-md-2">
<div class="col">
<?php $version_form->printHTML(); ?>
</div>
<div class="col mt-3 mt-md-0">
<h5>注意事项</h5>
<ul class="mb-0">
<li>正常情况下无需调整此项设置。</li>
</ul>
</div>
</div>
</div>
<div class="tab-pane" id="blogs"> <div class="tab-pane" id="blogs">
<div id="blogs-result-alert" class="alert" role="alert" style="display: none"></div> <div id="blogs-result-alert" class="alert" role="alert" style="display: none"></div>
<div class="row row-cols-1 row-cols-md-2"> <div class="row row-cols-1 row-cols-md-2">

View File

@ -130,7 +130,7 @@ class UOJForm {
'label_class' => 'form-label', 'label_class' => 'form-label',
'placeholder' => '', 'placeholder' => '',
'help' => '', 'help' => '',
'help_class' => '', 'help_class' => 'form-text',
'validator_php' => function ($x) { 'validator_php' => function ($x) {
return ''; return '';
}, },