diff --git a/webapp/src/Service/DOMJudgeService.php b/webapp/src/Service/DOMJudgeService.php index 5101285bf8..0712c68ee9 100644 --- a/webapp/src/Service/DOMJudgeService.php +++ b/webapp/src/Service/DOMJudgeService.php @@ -830,7 +830,7 @@ public function getSamplesZipContent(ContestProblem $contestProblem): string return $zipFileContents; } - protected function addSamplesToZip(ZipArchive $zip, ContestProblem $problem, ?string $directory = null): void + protected function addSamplesToZip(?ZipArchive $zip, ContestProblem $problem, ?string $directory = null, bool $fullZip = true): bool { /** @var Testcase[] $testcases */ $testcases = $this->em->createQueryBuilder() @@ -849,6 +849,9 @@ protected function addSamplesToZip(ZipArchive $zip, ContestProblem $problem, ?st ->getResult(); foreach ($testcases as $index => $testcase) { + if (!$fullZip) { + return true; + } foreach (['input', 'output'] as $type) { $extension = Testcase::EXTENSION_MAPPING[$type]; @@ -870,6 +873,7 @@ protected function addSamplesToZip(ZipArchive $zip, ContestProblem $problem, ?st $zip->addFromString($filename, $content); } } + return false; } public function getSamplesZipStreamedResponse(ContestProblem $contestProblem): StreamedResponse @@ -879,7 +883,7 @@ public function getSamplesZipStreamedResponse(ContestProblem $contestProblem): S return Utils::streamAsBinaryFile($zipFileContent, $outputFilename, 'zip'); } - public function getSamplesZipForContest(Contest $contest): StreamedResponse + public function helperSamplesZipForContest(Contest $contest, bool $fullZip): StreamedResponse|bool { // Note, we reload the contest with the problems and attachments, to reduce the number of queries // We do not load the testcases here since addSamplesToZip loads them @@ -896,30 +900,42 @@ public function getSamplesZipForContest(Contest $contest): StreamedResponse ->getQuery() ->getSingleResult(); - $zip = new ZipArchive(); - if (!($tempFilename = tempnam($this->getDomjudgeTmpDir(), "export-"))) { - throw new ServiceUnavailableHttpException(null, 'Could not create temporary file.'); - } + $zip = null; + if ($fullZip) { + $zip = new ZipArchive(); + if (!($tempFilename = tempnam($this->getDomjudgeTmpDir(), "export-"))) { + throw new ServiceUnavailableHttpException(null, 'Could not create temporary file.'); + } - $res = $zip->open($tempFilename, ZipArchive::OVERWRITE); - if ($res !== true) { - throw new ServiceUnavailableHttpException(null, 'Could not create temporary zip file.'); + $res = $zip->open($tempFilename, ZipArchive::OVERWRITE); + if ($res !== true) { + throw new ServiceUnavailableHttpException(null, 'Could not create temporary zip file.'); + } } /** @var ContestProblem $problem */ foreach ($contest->getProblems() as $problem) { // We don't include the samples for interactive problems. if (!$problem->getProblem()->isInteractiveProblem()) { - $this->addSamplesToZip($zip, $problem, $problem->getShortname()); + $samplesFound = $this->addSamplesToZip($zip, $problem, $problem->getShortname(), fullZip: $fullZip);; + if (!$fullZip && $samplesFound) { + return true; + } } if ($problem->getProblem()->getProblemstatementType()) { + if (!$fullZip) { + return true; + } $filename = sprintf('%s/statement.%s', $problem->getShortname(), $problem->getProblem()->getProblemstatementType()); $zip->addFromString($filename, $problem->getProblem()->getProblemstatement()); } /** @var ProblemAttachment $attachment */ foreach ($problem->getProblem()->getAttachments() as $attachment) { + if (!$fullZip) { + return true; + } $filename = sprintf('%s/attachments/%s', $problem->getShortname(), $attachment->getName()); $zip->addFromString($filename, $attachment->getContent()->getContent()); if ($attachment->getContent()->isExecutable()) { @@ -934,10 +950,17 @@ public function getSamplesZipForContest(Contest $contest): StreamedResponse } if ($contest->getContestProblemsetType()) { + if (!$fullZip) { + return true; + } $filename = sprintf('contest.%s', $contest->getContestProblemsetType()); $zip->addFromString($filename, $contest->getContestProblemset()); } + if (!$fullZip) { + return false; + } + $zip->close(); $zipFileContents = file_get_contents($tempFilename); unlink($tempFilename); @@ -945,6 +968,16 @@ public function getSamplesZipForContest(Contest $contest): StreamedResponse return Utils::streamAsBinaryFile($zipFileContents, 'samples.zip', 'zip'); } + public function checkIfSamplesZipForContest(Contest $contest): bool + { + return self::helperSamplesZipForContest($contest, fullZip: false); + } + + public function getSamplesZipForContest(Contest $contest): StreamedResponse + { + return self::helperSamplesZipForContest($contest, fullZip: true); + } + /** * @throws NonUniqueResultException */ @@ -1097,6 +1130,7 @@ public function getTwigDataForProblemsAction( 'problems' => $problems, 'samples' => $samples, 'showLimits' => $showLimits, + 'showSamples' => $this->checkIfSamplesZipForContest($contest), 'defaultMemoryLimit' => $defaultMemoryLimit, 'timeFactorDiffers' => $timeFactorDiffers, 'clarifications' => $clars, diff --git a/webapp/templates/partials/problem_list.html.twig b/webapp/templates/partials/problem_list.html.twig index b85f2437df..7c042652dc 100644 --- a/webapp/templates/partials/problem_list.html.twig +++ b/webapp/templates/partials/problem_list.html.twig @@ -4,17 +4,24 @@