-
Notifications
You must be signed in to change notification settings - Fork 271
add Paste code to submit feature #2720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
62985ba
56f076e
aa8ed35
3893628
3376c11
bf9d7bc
add44de
74e36d9
dbe2363
723f8d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
use App\Entity\Submission; | ||
use App\Entity\Testcase; | ||
use App\Form\Type\SubmitProblemType; | ||
use App\Form\Type\SubmitProblemPasteType; | ||
use App\Service\ConfigurationService; | ||
use App\Service\DOMJudgeService; | ||
use App\Service\EventLogService; | ||
|
@@ -60,47 +61,135 @@ | |
if ($problem !== null) { | ||
$data['problem'] = $problem; | ||
} | ||
$form = $this->formFactory | ||
$formUpload = $this->formFactory | ||
->createBuilder(SubmitProblemType::class, $data) | ||
->setAction($this->generateUrl('team_submit')) | ||
->getForm(); | ||
|
||
$form->handleRequest($request); | ||
$formPaste = $this->formFactory | ||
->createBuilder(SubmitProblemPasteType::class, $data) | ||
->setAction($this->generateUrl('team_submit')) | ||
->getForm(); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
$formUpload->handleRequest($request); | ||
$formPaste->handleRequest($request); | ||
if ($formUpload->isSubmitted() || $formPaste->isSubmitted()) { | ||
if ($contest === null) { | ||
$this->addFlash('danger', 'No active contest'); | ||
} elseif (!$this->dj->checkrole('jury') && !$contest->getFreezeData()->started()) { | ||
$this->addFlash('danger', 'Contest has not yet started'); | ||
} else { | ||
/** @var Problem $problem */ | ||
$problem = $form->get('problem')->getData(); | ||
/** @var Language $language */ | ||
$language = $form->get('language')->getData(); | ||
/** @var UploadedFile[] $files */ | ||
$files = $form->get('code')->getData(); | ||
if (!is_array($files)) { | ||
$files = [$files]; | ||
$problem = null; | ||
$language = null; | ||
$files = []; | ||
$entryPoint = null; | ||
$message = ''; | ||
|
||
if ($formUpload->isSubmitted() && $formUpload->isValid()) { | ||
$problem = $formUpload->get('problem')->getData(); | ||
$language = $formUpload->get('language')->getData(); | ||
$files = $formUpload->get('code')->getData(); | ||
if (!is_array($files)) { | ||
$files = [$files]; | ||
} | ||
$entryPoint = $formUpload->get('entry_point')->getData() ?: null; | ||
} elseif ($formPaste->isSubmitted() && $formPaste->isValid()) { | ||
$problem = $formPaste->get('problem')->getData(); | ||
$language = $formPaste->get('language')->getData(); | ||
$codeContent = $formPaste->get('code_content')->getData(); | ||
$problemShortName = $problem->getContestProblems()->first()->getShortName(); | ||
|
||
if ($codeContent == null || empty(trim($codeContent))) { | ||
$this->addFlash('danger', 'No code content provided.'); | ||
return $this->redirectToRoute('team_index'); | ||
} | ||
|
||
$saveFileDir = sys_get_temp_dir(); | ||
$saveFileName = sprintf( | ||
'%s.%s', | ||
$problemShortName, | ||
$language->getExtensions()[0] | ||
); | ||
$saveFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $saveFileName); | ||
|
||
if ($language->getExtensions()[0] == 'java' || $language->getExtensions()[0] == 'kt') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of hardcoding Java and Kotlin here, use the Similarly, use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to use what we built here so that the entry point works the same regardless of the upload method: https://github.com/DOMjudge/domjudge/blob/main/webapp/src/Form/Type/SubmitProblemType.php#L66 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't understand how to use $language->getRequireEntryPoint(). For example, in the case of Java, when using paste_code, the entry_point might still be no. In this case, how can I use this to determine the file name standard? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the remark here is that instead of listing the current languages which need an entrypoint, use that function @meisterT linked and use it from there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The function should be checking whether the language in the form requires an entrypoint, which seems to be different from my current situation, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would it be different? You check for 2 languages here and determine the entrypoint for those. If there are other languages like this we would also want to pick them up. People can add more languages besides those 2. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vmcj If so, there might be an issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, in the case of Java, it can be configured to not require an entry point. In this scenario, under the original upload mode, the uploaded file itself becomes the entry point. However, due to the characteristics of the Java language, the file name must match the main class name. In the case of the paste code mode, regardless of whether an entry point is required or not, the user must be allowed to specify the main class name. Otherwise, I won’t be able to save the file correctly, which will result in a compile error. |
||
$entryPoint = $formPaste->get('entry_point')->getData() ?: null; | ||
// Check for invalid characters in entry point name | ||
$invalidChars = '/[<>:"\/\\|?*]/'; | ||
if (preg_match($invalidChars, $entryPoint)) { | ||
$this->addFlash('danger', 'Invalid entry point name.'); | ||
return $this->redirectToRoute('team_index'); | ||
} | ||
$saveFileName = $entryPoint . '.' . $language->getExtensions()[0]; | ||
} else { | ||
$entryPoint = $saveFileName; | ||
} | ||
|
||
$saveFilePath = $saveFileDir . DIRECTORY_SEPARATOR . $saveFileName; | ||
file_put_contents($saveFilePath, $codeContent); | ||
|
||
$uploadedFile = new UploadedFile( | ||
$saveFilePath, | ||
$saveFileName, | ||
'application/octet-stream', | ||
null, | ||
true | ||
); | ||
$files = [$uploadedFile]; | ||
} | ||
$entryPoint = $form->get('entry_point')->getData() ?: null; | ||
$submission = $this->submissionService->submitSolution( | ||
$team, $this->dj->getUser(), $problem->getProbid(), $contest, $language, $files, 'team page', null, | ||
null, $entryPoint, null, null, $message | ||
); | ||
|
||
if ($submission) { | ||
$this->addFlash( | ||
'success', | ||
'Submission done! Watch for the verdict in the list below.' | ||
|
||
if ($problem && $language && !empty($files)) { | ||
$submission = $this->submissionService->submitSolution( | ||
$team, | ||
$this->dj->getUser(), | ||
$problem->getProbid(), | ||
$contest, | ||
$language, | ||
$files, | ||
'team page', | ||
null, | ||
null, | ||
$entryPoint, | ||
null, | ||
null, | ||
$message | ||
); | ||
} else { | ||
$this->addFlash('danger', $message); | ||
|
||
if ($submission) { | ||
$this->addFlash('success', 'Submission done! Watch for the verdict in the list below.'); | ||
} else { | ||
$this->addFlash('danger', $message); | ||
} | ||
|
||
return $this->redirectToRoute('team_index'); | ||
} | ||
return $this->redirectToRoute('team_index'); | ||
} | ||
} | ||
|
||
$active_tab_array = $this->config->get('default_submission_code_mode'); | ||
$active_tab = ""; | ||
if (count($active_tab_array) == 1) { | ||
$active_tab = reset($active_tab_array); | ||
$this->dj->setCookie('active_tab', $active_tab); | ||
} | ||
else if ($this->dj->getCookie('active_tab') != null) { | ||
$cookie_active_tab = $this->dj->getCookie('active_tab'); | ||
if(in_array($cookie_active_tab, $active_tab_array)) { | ||
$active_tab = $cookie_active_tab; | ||
} | ||
else { | ||
$active_tab = reset($active_tab_array); | ||
$this->dj->setCookie('active_tab', $active_tab); | ||
} | ||
} | ||
|
||
$data = ['form' => $form->createView(), 'problem' => $problem]; | ||
$data = [ | ||
'formupload' => $formUpload->createView(), | ||
'formpaste' => $formPaste->createView(), | ||
'active_tab' => $active_tab, | ||
'active_tab_array' => $active_tab_array, | ||
'problem' => $problem, | ||
]; | ||
$data['validFilenameRegex'] = SubmissionService::FILENAME_REGEX; | ||
|
||
if ($request->isXmlHttpRequest()) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Form\Type; | ||
|
||
use App\Entity\Language; | ||
use App\Entity\Problem; | ||
use App\Service\ConfigurationService; | ||
use App\Service\DOMJudgeService; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\EntityRepository; | ||
use Doctrine\ORM\Query\Expr\Join; | ||
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\Form; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\Validator\Constraints\Callback; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
|
||
class SubmitProblemPasteType extends AbstractType | ||
{ | ||
public function __construct( | ||
protected readonly DOMJudgeService $dj, | ||
protected readonly ConfigurationService $config, | ||
protected readonly EntityManagerInterface $em | ||
) { | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$user = $this->dj->getUser(); | ||
$contest = $this->dj->getCurrentContest($user->getTeam()->getTeamid()); | ||
|
||
$builder->add('code_content', HiddenType::class, [ | ||
'required' => true, | ||
]); | ||
$problemConfig = [ | ||
'class' => Problem::class, | ||
'query_builder' => fn(EntityRepository $er) => $er->createQueryBuilder('p') | ||
->join('p.contest_problems', 'cp', Join::WITH, 'cp.contest = :contest') | ||
->select('p', 'cp') | ||
->andWhere('cp.allowSubmit = 1') | ||
->setParameter('contest', $contest) | ||
->addOrderBy('cp.shortname'), | ||
'choice_label' => fn(Problem $problem) => sprintf( | ||
'%s - %s', | ||
$problem->getContestProblems()->first()->getShortName(), | ||
$problem->getName() | ||
), | ||
'placeholder' => 'Select a problem', | ||
]; | ||
$builder->add('problem', EntityType::class, $problemConfig); | ||
|
||
$builder->add('language', EntityType::class, [ | ||
'class' => Language::class, | ||
'query_builder' => fn(EntityRepository $er) => $er | ||
->createQueryBuilder('l') | ||
->andWhere('l.allowSubmit = 1'), | ||
'choice_label' => 'name', | ||
'placeholder' => 'Select a language', | ||
]); | ||
|
||
$builder->add('entry_point', TextType::class, [ | ||
'label' => 'Entry point', | ||
'required' => false, | ||
'help' => 'The entry point for your code.', | ||
'row_attr' => ['data-entry-point' => ''], | ||
'constraints' => [ | ||
new Callback(function ($value, ExecutionContextInterface $context) { | ||
/** @var Form $form */ | ||
$form = $context->getRoot(); | ||
/** @var Language $language */ | ||
$language = $form->get('language')->getData(); | ||
$langId = strtolower($language->getExtensions()[0]); | ||
if ($language->getRequireEntryPoint() && empty($value)) { | ||
$message = sprintf('%s required, but not specified', | ||
$language->getEntryPointDescription() ?: 'Entry point'); | ||
$context | ||
->buildViolation($message) | ||
->atPath('entry_point') | ||
->addViolation(); | ||
} | ||
|
||
if (in_array($langId, ['java', 'kt']) && empty($value)) { | ||
$message = sprintf('%s is required for %s language, but not specified', | ||
$language->getEntryPointDescription() ?: 'Entry point', | ||
ucfirst($langId)); | ||
$context | ||
->buildViolation($message) | ||
->atPath('entry_point') | ||
->addViolation(); | ||
} | ||
}), | ||
] | ||
]); | ||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($problemConfig) { | ||
$data = $event->getData(); | ||
if (isset($data['problem'])) { | ||
$problemConfig += ['row_attr' => ['class' => 'd-none']]; | ||
$event->getForm()->add('problem', EntityType::class, $problemConfig); | ||
} | ||
}); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.