Skip to content

Commit 58a630c

Browse files
committed
Define a service function for printing as a user
Share some code which is used in multiple controllers, and allow this shared code to be used by API endpoints in the future.
1 parent ea44cfb commit 58a630c

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

webapp/src/Controller/Jury/PrintController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ public function showAction(Request $request): Response
5151
$originalfilename = $file->getClientOriginalName();
5252

5353
$langid = $data['langid'];
54-
$username = $this->getUser()->getUserIdentifier();
5554

5655
// Since this is the Jury interface, there's not necessarily a
5756
// team involved; do not set a teamname or location.
58-
$ret = $this->dj->printFile($realfile, $originalfilename, $langid, $username);
57+
$ret = $this->dj->printUserFile($realfile, $originalfilename, $langid);
5958

6059
return $this->render('jury/print_result.html.twig', [
6160
'success' => $ret[0],

webapp/src/Controller/Team/MiscController.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,12 @@ public function printAction(Request $request): Response
176176
$realfile = $file->getRealPath();
177177
$originalfilename = $file->getClientOriginalName();
178178

179-
$langid = $data['langid'];
180-
$username = $this->getUser()->getUserIdentifier();
181-
182-
$propertyAccessor = PropertyAccess::createPropertyAccessor();
183-
$team = $this->dj->getUser()->getTeam();
184-
if ($team->getLabel()) {
185-
$teamId = $team->getLabel();
186-
} else {
187-
$teamId = $team->getExternalid();
188-
}
189-
$ret = $this->dj->printFile($realfile, $originalfilename, $langid,
190-
$username, $team->getEffectiveName(), $teamId, $team->getLocation());
179+
$langid = $data['langid'];
180+
$ret = $this->dj->printUserFile(
181+
$realfile,
182+
$originalfilename,
183+
$langid
184+
);
191185

192186
return $this->render('team/print_result.html.twig', [
193187
'success' => $ret[0],

webapp/src/Service/DOMJudgeService.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,32 @@ public function openZipFile(string $filename): ZipArchive
725725
return $zip;
726726
}
727727

728+
/**
729+
* Print the given file using the print command.
730+
*
731+
* Returns array with two elements: first a boolean indicating
732+
* overall success, and second the data returned from the print command.
733+
*
734+
* @param string $filename The on-disk file to be printed out
735+
* @param string $origname The original filename as submitted by the team
736+
* @param string|null $language Langid of the programming language this file is in
737+
* @param bool $asTeam Print the file as the team associated with the user
738+
*/
739+
public function printUserFile(
740+
string $filename,
741+
string $origname,
742+
?string $language,
743+
?bool $asTeam = false,
744+
): array {
745+
$user = $this->getUser();
746+
$team = $user->getTeam();
747+
if ($asTeam && $team !== null) {
748+
$teamid = $team->getLabel() ?? $team->getExternalid();
749+
return $this->printFile($filename, $origname, $language, $user->getUserIdentifier(), $team->getEffectiveName(), $teamid, $team->getLocation());
750+
}
751+
return $this->printFile($filename, $origname, $language, $user->getUserIdentifier());
752+
}
753+
728754
/**
729755
* Print the given file using the print command.
730756
*

0 commit comments

Comments
 (0)