From 8d87fda2334239f78e1e11d546b134e2744124d6 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 12 Nov 2022 10:25:46 +0800 Subject: [PATCH] refactor(web/contest): remove UOJContest::queryContestsHasProblem() use UOJProblem::findInContests() instead. --- web/app/models/UOJBlog.php | 18 +++++++++++++----- web/app/models/UOJContest.php | 10 ---------- web/app/models/UOJProblem.php | 14 +++++--------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/web/app/models/UOJBlog.php b/web/app/models/UOJBlog.php index caeb620..0f0a5c7 100644 --- a/web/app/models/UOJBlog.php +++ b/web/app/models/UOJBlog.php @@ -38,11 +38,10 @@ class UOJBlog { return false; } - if ($problem_id = $this->getSolutionProblemId()) { - $contests = UOJContest::queryContestsHasProblem(UOJProblem::query($problem_id)); - - foreach ($contests as $contest) { - if ($contest->userHasRegistered($user) && $contest->progress() == CONTEST_IN_PROGRESS) { + $problem = $this->getSolutionProblem(); + if ($problem) { + foreach ($problem->findInContests() as $cp) { + if ($cp->contest->userHasRegistered($user) && $cp->contest->progress() == CONTEST_IN_PROGRESS) { return false; } } @@ -218,6 +217,15 @@ class UOJBlog { ], ]); } + + public function getSolutionProblem() { + return UOJProblem::query(DB::selectSingle([ + DB::lc(), "select 1 from problems_solutions", + "where", [ + "blog_id" => $this->info['id'], + ], + ])); + } } UOJBlog::$table_for_content = 'blogs'; diff --git a/web/app/models/UOJContest.php b/web/app/models/UOJContest.php index ab3186e..2fd0ad7 100644 --- a/web/app/models/UOJContest.php +++ b/web/app/models/UOJContest.php @@ -28,16 +28,6 @@ class UOJContest { ])); } - public static function queryContestsHasProblem(UOJProblem $problem) { - return array_map(fn ($x) => UOJContest::query($x['contest_id']), DB::selectAll([ - "select contest_id from contests_problems", - "where", [ - "problem_id" => $problem->info['id'], - ], - "order by contest_id", - ])); - } - public static function userCanManageSomeContest(array $user = null) { if (!$user) { return false; diff --git a/web/app/models/UOJProblem.php b/web/app/models/UOJProblem.php index 48fc537..106ada0 100644 --- a/web/app/models/UOJProblem.php +++ b/web/app/models/UOJProblem.php @@ -289,10 +289,8 @@ class UOJProblem { return false; } - $contests = UOJContest::queryContestsHasProblem($this); - - foreach ($contests as $contest) { - if ($contest->userHasRegistered($user) && $contest->progress() <= CONTEST_IN_PROGRESS) { + foreach ($this->findInContests() as $cp) { + if ($cp->contest->userHasRegistered($user) && $cp->contest->progress() == CONTEST_IN_PROGRESS) { return false; } } @@ -326,12 +324,10 @@ class UOJProblem { } public function additionalSubmissionComponentsCannotBeSeenByUser(array $user = null, UOJSubmission $submission) { - $contests = UOJContest::queryContestsHasProblem($this); - - foreach ($contests as $contest) { - if ($contest->userHasRegistered($user) && $contest->progress() == CONTEST_IN_PROGRESS) { + foreach ($this->findInContests() as $cp) { + if ($cp->contest->userHasRegistered($user) && $cp->contest->progress() == CONTEST_IN_PROGRESS) { if ($submission->userIsSubmitter($user)) { - if ($contest->getJudgeTypeInContest() == 'no-details') { + if ($cp->contest->getJudgeTypeInContest() == 'no-details') { return ['low_level_details']; } else { return [];