Skip to content

Commit

Permalink
Auto calculate num actions for jury tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickygerritsen authored and Suckzoo committed Sep 27, 2023
1 parent 9457175 commit bbe9787
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 253 deletions.
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/ContestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ public function indexAction(Request $request): Response
'upcoming_contest' => $upcomingContest,
'contests_table' => $contests_table,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') && !$contest->isLocked() ? 2 : 0,
]);
}

Expand Down
99 changes: 26 additions & 73 deletions webapp/src/Controller/Jury/ExecutableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
use App\Entity\Executable;
use App\Entity\ExecutableFile;
use App\Entity\ImmutableExecutable;
use App\Entity\Role;
use App\Form\Type\ExecutableType;
use App\Form\Type\ExecutableUploadType;
use App\Service\ConfigurationService;
use App\Service\DOMJudgeService;
use App\Service\EventLogService;
use App\Utils\Utils;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\Annotations as Rest;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
Expand All @@ -29,35 +26,19 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/jury/executables")
* @IsGranted("ROLE_JURY")
*/
#[IsGranted('ROLE_JURY')]
#[Route(path: '/jury/executables')]
class ExecutableController extends BaseController
{
protected EntityManagerInterface $em;
protected DOMJudgeService $dj;
protected ConfigurationService $config;
protected KernelInterface $kernel;
protected EventLogService $eventLogService;

public function __construct(
EntityManagerInterface $em,
DOMJudgeService $dj,
ConfigurationService $config,
KernelInterface $kernel,
EventLogService $eventLogService
) {
$this->em = $em;
$this->dj = $dj;
$this->config = $config;
$this->kernel = $kernel;
$this->eventLogService = $eventLogService;
}

/**
* @Route("", name="jury_executables")
*/
protected readonly EntityManagerInterface $em,
protected readonly DOMJudgeService $dj,
protected readonly ConfigurationService $config,
protected readonly KernelInterface $kernel,
protected readonly EventLogService $eventLogService
) {}

Check failure on line 39 in webapp/src/Controller/Jury/ExecutableController.php

View workflow job for this annotation

GitHub Actions / phpcs

Closing brace must be on a line by itself

#[Route(path: '', name: 'jury_executables')]
public function indexAction(Request $request): Response
{
$data = [];
Expand Down Expand Up @@ -116,15 +97,12 @@ public function indexAction(Request $request): Response
return $this->render('jury/executables.html.twig', [
'executables' => $executables_table,
'table_fields' => $table_fields,
'num_actions' => count($execactions),
'form' => $form->createView(),
'form' => $form,
]);
}

/**
* @Route("/add", name="jury_executable_add")
* @IsGranted("ROLE_ADMIN")
*/
#[IsGranted('ROLE_ADMIN')]
#[Route(path: '/add', name: 'jury_executable_add')]
public function addAction(Request $request): Response
{
$data = [];
Expand All @@ -141,7 +119,7 @@ public function addAction(Request $request): Response
$zip = $this->dj->openZipFile($archive->getRealPath());
$filename = $archive->getClientOriginalName();
$id = substr($filename, 0, strlen($filename) - strlen(".zip"));
if (! preg_match ('#^[a-z0-9_-]+$#i', $id)) {
if (! preg_match('#^[a-z0-9_-]+$#i', $id)) {
throw new InvalidArgumentException(sprintf("File base name '%s' must contain only alphanumerics", $id));
}
$description = $id;
Expand Down Expand Up @@ -183,13 +161,11 @@ public function addAction(Request $request): Response
}

return $this->render('jury/executable_add.html.twig', [
'form' => $form->createView(),
'form' => $form,
]);
}

/**
* @Route("/{execId}", name="jury_executable")
*/
#[Route(path: '/{execId}', name: 'jury_executable')]
public function viewAction(Request $request, string $execId): Response
{
/** @var Executable $executable */
Expand Down Expand Up @@ -227,7 +203,7 @@ public function viewAction(Request $request, string $execId): Response
$files = [];
foreach ($editorData['filenames'] as $idx => $filename) {
$newContent = str_replace("\r\n", "\n", $submittedData['source' . $idx]);
if (substr($newContent, -1) != "\n") {
if (!str_ends_with($newContent, "\n")) {
// Ace swallows the newline at the end of file. Let's re-add it like most editors do.
$newContent .= "\n";
}
Expand All @@ -241,18 +217,6 @@ public function viewAction(Request $request, string $execId): Response
$this->em->persist($executableFile);
$files[] = $executableFile;
}
$offset = count($files);
foreach ($editorData['skippedBinary'] as $idx => $skippedBinaryData) {
$origExecutableFile = $this->em->getRepository(ExecutableFile::class)->find($skippedBinaryData['execfileid']);
$executableFile = new ExecutableFile();
$executableFile
->setRank($idx + $offset)
->setIsExecutable($origExecutableFile->isExecutable())
->setFilename($origExecutableFile->getFilename())
->setFileContent($origExecutableFile->getFileContent());
$this->em->persist($executableFile);
$files[] = $executableFile;
}

$immutableExecutable = new ImmutableExecutable($files);
$this->em->persist($immutableExecutable);
Expand Down Expand Up @@ -301,9 +265,7 @@ public function viewAction(Request $request, string $execId): Response
]));
}

/**
* @Route("/{execId}/download", name="jury_executable_download")
*/
#[Route(path: '/{execId}/download', name: 'jury_executable_download')]
public function downloadAction(string $execId): Response
{
/** @var Executable $executable */
Expand All @@ -318,10 +280,8 @@ public function downloadAction(string $execId): Response
return Utils::streamAsBinaryFile($zipFileContent, $filename, 'zip');
}

/**
* @Route("/{execId}/delete/{rankToDelete}", name="jury_executable_delete_single")
* @IsGranted("ROLE_ADMIN")
*/
#[IsGranted('ROLE_ADMIN')]
#[Route(path: '/{execId}/delete/{rankToDelete}', name: 'jury_executable_delete_single')]
public function deleteSingleAction(Request $request, string $execId, int $rankToDelete): Response
{
/** @var Executable $executable */
Expand Down Expand Up @@ -388,9 +348,7 @@ public function deleteSingleAction(Request $request, string $execId, int $rankTo
}
}

/**
* @Route("/{execId}/download/{rank}", name="jury_executable_download_single")
*/
#[Route(path: '/{execId}/download/{rank}', name: 'jury_executable_download_single')]
public function downloadSingleAction(string $execId, int $rank): Response
{
/** @var Executable $executable */
Expand All @@ -410,10 +368,8 @@ public function downloadSingleAction(string $execId, int $rank): Response
throw new NotFoundHttpException(sprintf('No file with rank %d found.', $rank));
}

/**
* @Route("/{execId}/delete", name="jury_executable_delete")
* @IsGranted("ROLE_ADMIN")
*/
#[IsGranted('ROLE_ADMIN')]
#[Route(path: '/{execId}/delete', name: 'jury_executable_delete')]
public function deleteAction(Request $request, string $execId): Response
{
/** @var Executable $executable */
Expand Down Expand Up @@ -448,10 +404,7 @@ protected function dataForEditor(Executable $executable): array
$content = $file->getFileContent();
$rank = $file->getRank();
if (!mb_detect_encoding($content, null, true)) {
$skippedBinary[] = [
'filename' => $filename,
'execfileid' => $file->getExecFileId(),
];
$skippedBinary[] = $filename;
continue; // Skip binary files.
}
$filenames[] = $filename;
Expand All @@ -474,7 +427,7 @@ protected function dataForEditor(Executable $executable): array

private function getAceFilename(string $filename, string $content): string
{
if (strpos($filename, '.') === false) {
if (!str_contains($filename, '.')) {
// If the file does not contain a dot, see if we have a shebang which we can use as filename.
// We do this to hint the ACE editor to use a specific language.
[$firstLine] = explode("\n", $content, 2);
Expand Down
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/JudgehostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public function indexAction(Request $request): Response
$data = [
'judgehosts' => $judgehosts_table,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') ? 2 : 0,
'all_checked_in_recently' => $all_checked_in_recently,
'refresh' => [
'after' => 5,
Expand Down
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/LanguageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public function indexAction(): Response
'enabled_languages' => $enabled_languages,
'disabled_languages' => $disabled_languages,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') ? 2 : 0,
]);
}

Expand Down
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/ProblemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public function indexAction(): Response
$data = [
'problems' => $problems_table,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') ? 4 : 2,
];

return $this->render('jury/problems.html.twig', $data);
Expand Down
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/TeamAffiliationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public function indexAction(string $projectDir): Response
return $this->render('jury/team_affiliations.html.twig', [
'team_affiliations' => $team_affiliations_table,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') ? 2 : 0,
]);
}

Expand Down
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/TeamCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public function indexAction(): Response
return $this->render('jury/team_categories.html.twig', [
'team_categories' => $team_categories_table,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') ? 2 : 0,
]);
}

Expand Down
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public function indexAction(): Response
return $this->render('jury/teams.html.twig', [
'teams' => $teams_table,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') ? 3 : 1,
]);
}

Expand Down
1 change: 0 additions & 1 deletion webapp/src/Controller/Jury/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public function indexAction(): Response
return $this->render('jury/users.html.twig', [
'users' => $users_table,
'table_fields' => $table_fields,
'num_actions' => $this->isGranted('ROLE_ADMIN') ? 2 : 0,
]);
}

Expand Down
Loading

0 comments on commit bbe9787

Please sign in to comment.