From 946ea8283f6a8eb6a9b6a730bb10cf828a92f012 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 7 Feb 2023 16:52:51 +0800 Subject: [PATCH] refactor(contest/self_reviews): allow admins edit participants' self_reviews --- web/app/controllers/contest_inside.php | 165 +++++++++++-------------- web/app/views/contest-reviews.php | 102 +++++++++++---- 2 files changed, 154 insertions(+), 113 deletions(-) diff --git a/web/app/controllers/contest_inside.php b/web/app/controllers/contest_inside.php index 885fcb1..c023195 100644 --- a/web/app/controllers/contest_inside.php +++ b/web/app/controllers/contest_inside.php @@ -236,97 +236,85 @@ if ($cur_tab == 'dashboard') { $reply_question = null; } } elseif ($cur_tab == 'self_reviews') { - if (UOJContest::cur()->userHasMarkedParticipated(Auth::user())) { - $self_reviews_update_form = new UOJForm('self_review_update'); - $self_reviews_update_form->config['ctrl_enter_submit'] = true; - - $contest_problems = DB::selectAll([ - "select problem_id", - "from", "contests_problems", - "where", ["contest_id" => $contest['id']], - "order by level, problem_id" - ]); - for ($i = 0; $i < count($contest_problems); $i++) { - $contest_problems[$i]['problem'] = UOJContestProblem::query($contest_problems[$i]['problem_id']); + $self_reviews_update_form = new UOJForm('self_reviews_update'); + $self_reviews_update_form->config['ctrl_enter_submit'] = true; + $self_reviews_update_form->addHidden('self_reviews_update__username', '', function ($username, &$vdata) { + if (!validateUsername($username)) { + return '无效用户名'; } - for ($i = 0; $i < count($contest_problems); $i++) { - $content = DB::selectSingle([ - "select content", - "from", "contests_reviews", - "where", [ - "contest_id" => $contest['id'], - "problem_id" => $contest_problems[$i]['problem_id'], - "poster" => Auth::id(), - ], - ]); - $self_reviews_update_form->addTextArea('self_review_update__problem_' . $contest_problems[$i]['problem']->getLetter(), [ - 'div_class' => 'mb-3', - 'label' => '' . $contest_problems[$i]['problem']->getLetter() . ': ' . $contest_problems[$i]['problem']->info['title'], - 'default_value' => $content, - 'validator_php' => function ($content) { - if (strlen($content) > 200) { - return '总结不能超过200字'; - } - - return ''; - }, - ]); + if ($username != Auth::id() && !UOJContest::cur()->userCanManage(Auth::user())) { + return '权限不足'; } - $content = DB::selectSingle([ - "select content", - "from", "contests_reviews", - "where", [ - "contest_id" => $contest['id'], - "problem_id" => -1, - "poster" => Auth::id(), - ], - ]); - $self_reviews_update_form->addTextArea('self_review_update__overall', [ - 'label' => '比赛总结', - 'default_value' => $content, + return ''; + }, null); + + $contest_problems = array_map(fn ($row) => UOJContestProblem::query($row['problem_id']), DB::selectAll([ + "select problem_id", + "from", "contests_problems", + "where", ["contest_id" => $contest['id']], + "order by level, problem_id" + ])); + + foreach ($contest_problems as $cp) { + $self_reviews_update_form->addTextArea('self_reviews_update__problem_' . $cp->getLetter(), [ + 'div_class' => 'mb-3', + 'label' => $cp->getTitle(['with' => 'letter']), + 'default_value' => '', 'validator_php' => function ($content) { if (strlen($content) > 200) { - return '总结不能超过200字'; + return '长度超过限制'; } return ''; }, ]); - - $self_reviews_update_form->handle = function () use ($contest, $contest_problems) { - for ($i = 0; $i < count($contest_problems); $i++) { - if (isset($_POST['self_review_update__problem_' . $contest_problems[$i]['problem']->getLetter()])) { - DB::query([ - "replace into contests_reviews", - "(contest_id, problem_id, poster, content)", - "values", DB::tuple([ - $contest['id'], - $contest_problems[$i]['problem_id'], - Auth::id(), - $_POST['self_review_update__problem_' . $contest_problems[$i]['problem']->getLetter()], - ]), - ]); - } - } - - if (isset($_POST['self_review_update__overall'])) { - DB::query([ - "replace into contests_reviews", - "(contest_id, problem_id, poster, content)", - "values", DB::tuple([ - $contest['id'], - -1, - Auth::id(), - $_POST['self_review_update__overall'], - ]), - ]); - } - }; - - $self_reviews_update_form->runAtServer(); } + + $self_reviews_update_form->addTextArea('self_reviews_update__overall', [ + 'label' => '比赛总结', + 'validator_php' => function ($content) { + if (strlen($content) > 300) { + return '长度超过限制'; + } + + return ''; + }, + ]); + + $self_reviews_update_form->handle = function (&$vdata) use ($contest_problems) { + foreach ($contest_problems as $cp) { + DB::update([ + "replace into contests_reviews", + DB::bracketed_fields([ + "contest_id", + "problem_id", + "poster", + "content" + ]), + "values", DB::tuple([ + UOJContest::info('id'), + $cp->info['id'], + $_POST['self_reviews_update__username'], + $_POST['self_reviews_update__problem_' . $cp->getLetter()], + ]), + ]); + } + + DB::update([ + "replace into contests_reviews", + "(contest_id, problem_id, poster, content)", + "values", DB::tuple([ + UOJContest::info('id'), + -1, + $_POST['self_reviews_update__username'], + $_POST['self_reviews_update__overall'], + ]), + ]); + }; + + $self_reviews_update_form->runAtServer(); } function echoDashboard() { @@ -469,9 +457,12 @@ function echoStandings($is_after_contest_query = false) { } function echoSelfReviews() { - global $contest; + global $contest, $self_reviews_update_form; - uojIncludeView('contest-reviews', ['contest' => $contest] + UOJContest::cur()->queryResult()); + uojIncludeView('contest-reviews', [ + 'contest' => $contest, + 'self_reviews_update_form' => $self_reviews_update_form, + ] + UOJContest::cur()->queryResult()); } ?> @@ -538,17 +529,7 @@ function echoSelfReviews() { - - - -
- -
-

修改我的赛后总结

-
赛后总结支持 Markdown 语法。
- printHTML(); ?> -
- +

diff --git a/web/app/views/contest-reviews.php b/web/app/views/contest-reviews.php index d0478e0..52f9f3a 100644 --- a/web/app/views/contest-reviews.php +++ b/web/app/views/contest-reviews.php @@ -5,7 +5,9 @@ $parsedown = HTML::parsedown(['username_with_color' => true]); $purifier = HTML::purifier_inline(); foreach ($contest_data['people'] as $person) { - $reviews[$person[0]] = []; + $reviews[$person[0]] = [ + '_can_edit' => Auth::id() == $person[0] || UOJContest::cur()->userCanManage(Auth::user()), + ]; foreach ($contest_data['problems'] as $problem) { $content = DB::selectSingle([ @@ -18,7 +20,8 @@ foreach ($contest_data['people'] as $person) { ], ]); - $reviews[$person[0]][$problem] = $purifier->purify($parsedown->line($content)); + $reviews[$person[0]][$problem]['raw'] = $content; + $reviews[$person[0]][$problem]['html'] = $purifier->purify($parsedown->line($content)); } $content = DB::selectSingle([ @@ -31,19 +34,34 @@ foreach ($contest_data['people'] as $person) { ], ]); - $reviews[$person[0]]['all'] = $purifier->purify($parsedown->line($content)); + $reviews[$person[0]]['_all']['raw'] = $content; + $reviews[$person[0]]['_all']['html'] = $purifier->purify($parsedown->line($content)); } ?>
+ +