Skip to content
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

add Paste code to submit feature #2720

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions etc/db-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@
- category: Display
description: Options related to the DOMjudge user interface.
items:
- name: default_submission_code_mode
meisterT marked this conversation as resolved.
Show resolved Hide resolved
type: array_val
default_value: [upload]
public: true
description: Select the default submission method for the team
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the default or allowed submission methods? Also, it would be good to clarify that these are submission methods from the web interface, as there's also the submit client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowed submission methods, with the default set to upload.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is submit client?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as6325400 marked this conversation as resolved.
Show resolved Hide resolved
options:
- paste
- upload
- name: output_display_limit
type: int
default_value: 2000
Expand Down
22 changes: 22 additions & 0 deletions webapp/public/style_domjudge.css
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,25 @@ blockquote {
padding: 3px;
border-radius: 5px;
}

#submissionTabs.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
}

.editor-container {
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
padding: 10px;
margin-top: 10px;
margin-bottom: 10px;
background-color: #fafafa;
max-height: 400px;
overflow: auto;
}

.editor-container:hover {
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
139 changes: 114 additions & 25 deletions webapp/src/Controller/Team/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

if ($codeContent == null || empty(trim($codeContent))) {
$this->addFlash('danger', 'No code content provided.');
return $this->redirectToRoute('team_index');
}

$tempDir = sys_get_temp_dir();
$tempFileName = sprintf(
'submission_%s_%s_%s.%s',
$user->getUsername(),
$problem->getName(),
date('Y-m-d_H-i-s'),
$language->getExtensions()[0]
);
$tempFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $tempFileName);

if ($language->getExtensions()[0] == 'java' || $language->getExtensions()[0] == 'kt') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hardcoding Java and Kotlin here, use the $language->getRequireEntryPoint() instead.

Similarly, use $language->getEntryPointDescription() for a description to display.

Copy link
Member

@meisterT meisterT Oct 22, 2024

Choose a reason for hiding this comment

The 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:/DOMjudge/domjudge/blob/main/webapp/src/Form/Type/SubmitProblemType.php#L66

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

$entryPoint = $formPaste->get('entry_point')->getData() ?: null;
// Check for invalid characters in entry point name
$invalidChars = '/[<>:"\/\\|?*]/';
if(preg_match($invalidChars, $entryPoint)) {

Check failure on line 120 in webapp/src/Controller/Team/SubmissionController.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space(s) after IF keyword; 0 found
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some warnings on https:/DOMjudge/domjudge/pull/2720/files which you should still handle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still see them:
image

maybe you have to toggle a to see them.

$this->addFlash('danger', 'Invalid entry point name.');
return $this->redirectToRoute('team_index');
}
$tempFileName = $entryPoint . '.' . $language->getExtensions()[0];
}

Check failure on line 125 in webapp/src/Controller/Team/SubmissionController.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space after closing brace; newline found
else $entryPoint = $tempFileName;

Check failure on line 126 in webapp/src/Controller/Team/SubmissionController.php

View workflow job for this annotation

GitHub Actions / phpcs

Inline control structures are not allowed

$tempFilePath = $tempDir . DIRECTORY_SEPARATOR . $tempFileName;
file_put_contents($tempFilePath, $codeContent);

$uploadedFile = new UploadedFile(
$tempFilePath,
$tempFileName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use a simpler name here (and then of course use that as base for the entry point calculation). A nice simple naming scheme could be the shortName of the contestProblem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still want the pasted submission to have a special identifier in the filename, to differentiate it from uploads. Is it possible to do this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason the way of submitting is important? You could test with adding fileupload- to the name? You would need to test yourself if this still works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it "fileupload"? I thought it would be something like "Paste."

However, I now think using "shortName" directly is fine too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think the short name as given by the contest problem is ok, let's use that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the short name allows various characters. I will still use preg_replace('/[^a-zA-Z0-9_.-]/', '_', $tempFileName); to handle file naming using the short name.

'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);
}

Check failure on line 174 in webapp/src/Controller/Team/SubmissionController.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space after closing brace; newline found
else if ($this->dj->getCookie('active_tab') != null) {

Check warning on line 175 in webapp/src/Controller/Team/SubmissionController.php

View workflow job for this annotation

GitHub Actions / phpcs

Usage of ELSE IF is discouraged; use ELSEIF instead
$cookie_active_tab = $this->dj->getCookie('active_tab');
if(in_array($cookie_active_tab, $active_tab_array)) {

Check failure on line 177 in webapp/src/Controller/Team/SubmissionController.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space(s) after IF keyword; 0 found
$active_tab = $cookie_active_tab;
}

Check failure on line 179 in webapp/src/Controller/Team/SubmissionController.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space after closing brace; newline found
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()) {
Expand Down
108 changes: 108 additions & 0 deletions webapp/src/Form/Type/SubmitProblemPasteType.php
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]);

Check failure on line 78 in webapp/src/Form/Type/SubmitProblemPasteType.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
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);
}
});
}
}
2 changes: 2 additions & 0 deletions webapp/templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<script src="{{ asset("js/bootstrap.bundle.min.js") }}"></script>

<script src="{{ asset("js/domjudge.js") }}"></script>
<script src="{{ asset('js/ace/ace.js') }}"></script>
as6325400 marked this conversation as resolved.
Show resolved Hide resolved

{% for file in customAssetFiles('js') %}
<script src="{{ asset('js/custom/' ~ file) }}"></script>
{% endfor %}
Expand Down
45 changes: 45 additions & 0 deletions webapp/templates/team/partials/submit_scripts.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,43 @@
}
}

function maybeShowEntryPointByPasteCode() {
var langid = $('#submit_problem_paste_language').val();
console.log($('#submit_problem_paste_problem'));
if (langid === "") {
return;
}

var $entryPoint = $('[data-entry-point]');
var $entryPointLabel = $entryPoint.find('label');
var $entryPointInput = $entryPoint.find('input');
var entryPointDescription = getEntryPoint(langid);

if (langid === 'java' || langid === 'kt') {
$entryPoint.show();
$entryPointInput.attr('required', 'required');
} else if (entryPointDescription) {
$entryPoint.show();
$entryPointInput.attr('required', 'required');
} else {
$entryPoint.hide();
$entryPointInput.attr('required', null);
}

if (entryPointDescription) {
var $labelChildren = $entryPointLabel.children();
$entryPointLabel.text(entryPointDescription).append($labelChildren);
}

if (langid === 'java') {
$entryPointInput.val(entryPointDetectJava('main.java'));
} else if (langid === 'kt') {
$entryPointInput.val(entryPointDetectKt('main.kt'));
} else {
$entryPointInput.val('');
}
}

$(function () {
var $entryPoint = $('[data-entry-point]');
$entryPoint.hide();
Expand Down Expand Up @@ -104,6 +141,14 @@
maybeShowEntryPoint();
});

$body.on('change', '#submit_problem_paste_language', function () {
maybeShowEntryPointByPasteCode();
});

$body.on('change', '#submit_problem_paste_problem', function () {
maybeShowEntryPointByPasteCode();
});

$body.on('submit', 'form[name=submit_problem]', function () {
var langelt = document.getElementById("submit_problem_language");
var language = langelt.options[langelt.selectedIndex].value;
Expand Down
Loading
Loading