From deacd0dfc195bca41af631114804d29937337cd8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 17 Jan 2024 02:11:31 +0900 Subject: . --- services/app/src/Forms/AnswerNewForm.php | 116 +++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 services/app/src/Forms/AnswerNewForm.php (limited to 'services/app/src/Forms/AnswerNewForm.php') diff --git a/services/app/src/Forms/AnswerNewForm.php b/services/app/src/Forms/AnswerNewForm.php new file mode 100644 index 0000000..a07a172 --- /dev/null +++ b/services/app/src/Forms/AnswerNewForm.php @@ -0,0 +1,116 @@ +quiz->quiz_id} - 提出"; + } + + public function redirectUrl(): string + { + $answer = $this->answer; + assert(isset($answer)); + return $this->routeParser->urlFor( + 'answer_view', + ['qslug' => $this->quiz->slug, 'anum' => "$answer->answer_number"], + ); + } + + protected function submitLabel(): string + { + return '投稿'; + } + + /** + * @return list + */ + protected function items(): array + { + return [ + new FormItem( + name: 'code', + type: 'textarea', + label: 'コード', + isRequired: true, + extra: 'rows="3" cols="80"', + ), + ]; + } + + /** + * @return array{quiz: Quiz, is_closed: bool} + */ + public function getRenderContext(): array + { + return [ + 'quiz' => $this->quiz, + 'is_closed' => $this->quiz->isClosedToAnswer(), + ]; + } + + public function submit(): void + { + if ($this->quiz->isClosedToAnswer()) { + $this->state->setErrors(['general' => 'この問題の回答は締め切られました']); + throw new FormSubmissionFailureException(); + } + + $code = $this->state->get('code') ?? ''; + + try { + $answer_id = $this->conn->transaction(function () use ($code) { + $answer_id = $this->answerRepo->create( + $this->quiz->quiz_id, + $this->currentUser->user_id, + $code, + ); + $this->testcaseExecutionRepo->enqueueForSingleAnswer( + $answer_id, + $this->quiz->quiz_id, + ); + return $answer_id; + }); + } catch (EntityValidationException $e) { + $this->state->setErrors($e->toFormErrors()); + throw new FormSubmissionFailureException(previous: $e); + } + $answer = $this->answerRepo->findById($answer_id); + assert(isset($answer)); + $this->answer = $answer; + } +} -- cgit v1.2.3-70-g09d2