|
9 | 9 | use App\Entity\Submission;
|
10 | 10 | use App\Entity\Testcase;
|
11 | 11 | use App\Form\Type\SubmitProblemType;
|
| 12 | +use App\Form\Type\SubmitProblemPasteType; |
12 | 13 | use App\Service\ConfigurationService;
|
13 | 14 | use App\Service\DOMJudgeService;
|
14 | 15 | use App\Service\EventLogService;
|
@@ -60,47 +61,111 @@ public function createAction(Request $request, ?Problem $problem = null): Respon
|
60 | 61 | if ($problem !== null) {
|
61 | 62 | $data['problem'] = $problem;
|
62 | 63 | }
|
63 |
| - $form = $this->formFactory |
| 64 | + $formUpload = $this->formFactory |
64 | 65 | ->createBuilder(SubmitProblemType::class, $data)
|
65 | 66 | ->setAction($this->generateUrl('team_submit'))
|
66 | 67 | ->getForm();
|
67 | 68 |
|
68 |
| - $form->handleRequest($request); |
| 69 | + $formPaste = $this->formFactory |
| 70 | + ->createBuilder(SubmitProblemPasteType::class, $data) |
| 71 | + ->setAction($this->generateUrl('team_submit')) |
| 72 | + ->getForm(); |
69 | 73 |
|
70 |
| - if ($form->isSubmitted() && $form->isValid()) { |
| 74 | + $formUpload->handleRequest($request); |
| 75 | + $formPaste->handleRequest($request); |
| 76 | + if ($formUpload->isSubmitted() || $formPaste->isSubmitted()) { |
71 | 77 | if ($contest === null) {
|
72 | 78 | $this->addFlash('danger', 'No active contest');
|
73 | 79 | } elseif (!$this->dj->checkrole('jury') && !$contest->getFreezeData()->started()) {
|
74 | 80 | $this->addFlash('danger', 'Contest has not yet started');
|
75 | 81 | } else {
|
76 |
| - /** @var Problem $problem */ |
77 |
| - $problem = $form->get('problem')->getData(); |
78 |
| - /** @var Language $language */ |
79 |
| - $language = $form->get('language')->getData(); |
80 |
| - /** @var UploadedFile[] $files */ |
81 |
| - $files = $form->get('code')->getData(); |
82 |
| - if (!is_array($files)) { |
83 |
| - $files = [$files]; |
| 82 | + $problem = null; |
| 83 | + $language = null; |
| 84 | + $files = []; |
| 85 | + $entryPoint = null; |
| 86 | + $message = ''; |
| 87 | + |
| 88 | + if ($formUpload->isSubmitted() && $formUpload->isValid()) { |
| 89 | + $problem = $formUpload->get('problem')->getData(); |
| 90 | + $language = $formUpload->get('language')->getData(); |
| 91 | + $files = $formUpload->get('code')->getData(); |
| 92 | + if (!is_array($files)) { |
| 93 | + $files = [$files]; |
| 94 | + } |
| 95 | + $entryPoint = $formUpload->get('entry_point')->getData() ?: null; |
| 96 | + } elseif ($formPaste->isSubmitted() && $formPaste->isValid()) { |
| 97 | + $problem = $formPaste->get('problem')->getData(); |
| 98 | + $language = $formPaste->get('language')->getData(); |
| 99 | + $codeContent = $formPaste->get('code_content')->getData(); |
| 100 | + |
| 101 | + if ($codeContent == null || empty(trim($codeContent))) { |
| 102 | + $this->addFlash('danger', 'No code content provided.'); |
| 103 | + return $this->redirectToRoute('team_index'); |
| 104 | + } |
| 105 | + |
| 106 | + $tempDir = sys_get_temp_dir(); |
| 107 | + $tempFileName = sprintf( |
| 108 | + 'submission_%s_%s_%s.%s', |
| 109 | + $user->getUsername(), |
| 110 | + $problem->getName(), |
| 111 | + date('Y-m-d_H-i-s'), |
| 112 | + $language->getExtensions()[0] |
| 113 | + ); |
| 114 | + $tempFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $tempFileName); |
| 115 | + $tempFilePath = $tempDir . DIRECTORY_SEPARATOR . $tempFileName; |
| 116 | + file_put_contents($tempFilePath, $codeContent); |
| 117 | + |
| 118 | + $uploadedFile = new UploadedFile( |
| 119 | + $tempFilePath, |
| 120 | + $tempFileName, |
| 121 | + 'application/octet-stream', |
| 122 | + null, |
| 123 | + true |
| 124 | + ); |
| 125 | + |
| 126 | + $files = [$uploadedFile]; |
| 127 | + $entryPoint = $tempFileName; |
84 | 128 | }
|
85 |
| - $entryPoint = $form->get('entry_point')->getData() ?: null; |
86 |
| - $submission = $this->submissionService->submitSolution( |
87 |
| - $team, $this->dj->getUser(), $problem->getProbid(), $contest, $language, $files, 'team page', null, |
88 |
| - null, $entryPoint, null, null, $message |
89 |
| - ); |
90 |
| - |
91 |
| - if ($submission) { |
92 |
| - $this->addFlash( |
93 |
| - 'success', |
94 |
| - 'Submission done! Watch for the verdict in the list below.' |
| 129 | + |
| 130 | + if ($problem && $language && !empty($files)) { |
| 131 | + $submission = $this->submissionService->submitSolution( |
| 132 | + $team, |
| 133 | + $this->dj->getUser(), |
| 134 | + $problem->getProbid(), |
| 135 | + $contest, |
| 136 | + $language, |
| 137 | + $files, |
| 138 | + 'team page', |
| 139 | + null, |
| 140 | + null, |
| 141 | + $entryPoint, |
| 142 | + null, |
| 143 | + null, |
| 144 | + $message |
95 | 145 | );
|
96 |
| - } else { |
97 |
| - $this->addFlash('danger', $message); |
| 146 | + |
| 147 | + if ($submission) { |
| 148 | + $this->addFlash('success', 'Submission done! Watch for the verdict in the list below.'); |
| 149 | + } else { |
| 150 | + $this->addFlash('danger', $message); |
| 151 | + } |
| 152 | + |
| 153 | + return $this->redirectToRoute('team_index'); |
98 | 154 | }
|
99 |
| - return $this->redirectToRoute('team_index'); |
100 | 155 | }
|
101 | 156 | }
|
| 157 | + |
| 158 | + $active_tab = (bool) $this->config->get('default_submission_code_mode') == 0 ? 'paste' : 'upload'; |
| 159 | + if ($this->dj->getCookie('active_tab') != null) { |
| 160 | + $active_tab = $this->dj->getCookie('active_tab'); |
| 161 | + } |
102 | 162 |
|
103 |
| - $data = ['form' => $form->createView(), 'problem' => $problem]; |
| 163 | + $data = [ |
| 164 | + 'formupload' => $formUpload->createView(), |
| 165 | + 'formpaste' => $formPaste->createView(), |
| 166 | + 'active_tab' => $active_tab, |
| 167 | + 'problem' => $problem, |
| 168 | + ]; |
104 | 169 | $data['validFilenameRegex'] = SubmissionService::FILENAME_REGEX;
|
105 | 170 |
|
106 | 171 | if ($request->isXmlHttpRequest()) {
|
|
0 commit comments