refactor(contest): uoj_form_v2

This commit is contained in:
Baoshuo Ren 2023-01-15 21:35:24 +08:00
parent e2c3a3eb4e
commit 9d66356566
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
2 changed files with 35 additions and 42 deletions

View File

@ -80,12 +80,10 @@ if (UOJContest::cur()->userCanStartFinalTest(Auth::user())) {
if ($cur_tab == 'dashboard') { if ($cur_tab == 'dashboard') {
if ($contest['cur_progress'] <= CONTEST_IN_PROGRESS) { if ($contest['cur_progress'] <= CONTEST_IN_PROGRESS) {
$post_question = new UOJBs4Form('post_question'); $post_question = new UOJForm('post_question');
$post_question->addVTextArea( $post_question->addTextArea('qcontent', [
'qcontent', 'label' => '问题',
'问题', 'validator_php' => function ($content, &$vdata) {
'',
function ($content, &$vdata) {
if (!Auth::check()) { if (!Auth::check()) {
return '您尚未登录'; return '您尚未登录';
} }
@ -100,12 +98,11 @@ if ($cur_tab == 'dashboard') {
return ''; return '';
}, },
null ]);
);
$post_question->handle = function (&$vdata) use ($contest) { $post_question->handle = function (&$vdata) use ($contest) {
DB::insert([ DB::insert([
"insert into contests_asks", "insert into contests_asks",
"(contest_id, question, answer, username, post_time, is_hidden)", DB::bracketed_fields(["contest_id", "question", "answer", "username", "post_time", "is_hidden"]),
"values", DB::tuple([$contest['id'], $vdata['content'], '', Auth::id(), DB::now(), 1]) "values", DB::tuple([$contest['id'], $vdata['content'], '', Auth::id(), DB::now(), 1])
]); ]);
}; };
@ -115,13 +112,11 @@ if ($cur_tab == 'dashboard') {
} }
} elseif ($cur_tab == 'backstage') { } elseif ($cur_tab == 'backstage') {
if ($is_manager) { if ($is_manager) {
$post_notice = new UOJBs4Form('post_notice'); $post_notice = new UOJForm('post_notice');
$post_notice->addInput( $post_notice->addInput('title', [
'title', 'div_class' => 'mb-3',
'text', 'label' => '标题',
'标题', 'validator_php' => function ($title, &$vdata) {
'',
function ($title, &$vdata) {
if (!$title) { if (!$title) {
return '标题不能为空'; return '标题不能为空';
} }
@ -130,13 +125,10 @@ if ($cur_tab == 'dashboard') {
return ''; return '';
}, },
null ]);
); $post_notice->addTextArea('content', [
$post_notice->addTextArea( 'label' => '正文',
'content', 'validator_php' => function ($content, &$vdata) {
'正文',
'',
function ($content, &$vdata) {
if (!$content) { if (!$content) {
return '公告不能为空'; return '公告不能为空';
} }
@ -145,8 +137,7 @@ if ($cur_tab == 'dashboard') {
return ''; return '';
}, },
null ]);
);
$post_notice->handle = function (&$vdata) use ($contest) { $post_notice->handle = function (&$vdata) use ($contest) {
DB::insert([ DB::insert([
"insert into contests_notice", "insert into contests_notice",
@ -160,7 +151,7 @@ if ($cur_tab == 'dashboard') {
} }
if ($is_manager) { if ($is_manager) {
$reply_question = new UOJBs4Form('reply_question'); $reply_question = new UOJForm('reply_question');
$reply_question->addHidden( $reply_question->addHidden(
'rid', 'rid',
'0', '0',
@ -183,18 +174,21 @@ if ($cur_tab == 'dashboard') {
}, },
null null
); );
$reply_question->addVSelect('rtype', [ $reply_question->addSelect('rtype', [
'public' => '公开(如果该问题反复被不同人提出,或指出了题目中的错误,请选择该项)', 'div_class' => 'mb-3',
'private' => '非公开', 'label' => '回复类型',
'statement' => '请仔细阅读题面(非公开)', 'default' => 'private',
'no_comment' => '无可奉告(非公开)', 'options' => [
'no_play' => '请认真比赛(非公开)', 'public' => '公开(如果该问题反复被不同人提出,或指出了题目中的错误,请选择该项)',
], '回复类型', 'private'); 'private' => '非公开',
$reply_question->addVTextArea( 'statement' => '请仔细阅读题面(非公开)',
'rcontent', 'no_comment' => '无可奉告(非公开)',
'回复', 'no_play' => '请认真比赛(非公开)',
'', ],
function ($content, &$vdata) { ]);
$reply_question->addTextArea('rcontent', [
'label' => '回复',
'validator_php' => function ($content, &$vdata) {
if (!Auth::check()) { if (!Auth::check()) {
return '您尚未登录'; return '您尚未登录';
} }
@ -209,9 +203,8 @@ if ($cur_tab == 'dashboard') {
$vdata['content'] = HTML::escape($content); $vdata['content'] = HTML::escape($content);
return ''; return '';
}, },
null ]);
); $reply_question->handle = function (&$vdata) {
$reply_question->handle = function (&$vdata) use ($contest) {
$content = $vdata['content']; $content = $vdata['content'];
$is_hidden = 1; $is_hidden = 1;
switch ($_POST['rtype']) { switch ($_POST['rtype']) {

View File

@ -167,7 +167,7 @@ class UOJForm {
$this->add($name, $html, $config['validator_php'], $config['validator_js']); $this->add($name, $html, $config['validator_php'], $config['validator_js']);
} }
public function addTextarea($name, $config) { public function addTextArea($name, $config) {
$config += [ $config += [
'type' => 'text', 'type' => 'text',
'div_class' => '', 'div_class' => '',