|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\StudentAbsence; |
|
6
|
|
|
use App\Entity\StudentAbsenceAttachment; |
|
7
|
|
|
use App\Http\FlysystemFileResponse; |
|
8
|
|
|
use App\Repository\StudentAbsenceRepositoryInterface; |
|
9
|
|
|
use App\Section\SectionResolverInterface; |
|
10
|
|
|
use App\Settings\StudentAbsenceSettings; |
|
11
|
|
|
use DateTime; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
use League\Flysystem\FilesystemOperator; |
|
14
|
|
|
use Mimey\MimeTypes; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
19
|
|
|
use Twig\Environment; |
|
20
|
|
|
|
|
21
|
|
|
#[Route('/absence/export')] |
|
22
|
|
|
class StudentAbsenceExportController extends AbstractController { |
|
23
|
|
|
|
|
24
|
|
|
#[Route('', name: 'export_student_absences')] |
|
25
|
|
|
public function index(StudentAbsenceRepositoryInterface $repository, Request $request): Response { |
|
26
|
|
|
$start = $this->getDateTimeFromRequest($request, 'start'); |
|
27
|
|
|
$end = $this->getDateTimeFromRequest($request, 'end'); |
|
28
|
|
|
$uuids = [ ]; |
|
29
|
|
|
|
|
30
|
|
|
if($start !== null && $end !== null && $start < $end) { |
|
31
|
|
|
$uuids = $repository->findAllUuids($start, $end); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this->render('absences/students/export/index.html.twig', [ |
|
35
|
|
|
'start' => $start, |
|
36
|
|
|
'end' => $end, |
|
37
|
|
|
'uuids' => $uuids |
|
38
|
|
|
]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function getDateTimeFromRequest(Request $request, string $name): ?DateTime { |
|
42
|
|
|
$dateAsString = $request->query->get($name); |
|
43
|
|
|
|
|
44
|
|
|
if($dateAsString === null) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
return new DateTime($dateAsString); |
|
50
|
|
|
} catch(Exception $e) { |
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
#[Route('/{uuid}', name: 'export_student_absence')] |
|
56
|
|
|
public function absence(StudentAbsence $absence, SectionResolverInterface $sectionResolver, Environment $twig): Response { |
|
57
|
|
|
$section = $sectionResolver->getSectionForDate($absence->getFrom()->getDate()); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$json = [ |
|
60
|
|
|
'uuid' => $absence->getUuid()->toString(), |
|
61
|
|
|
'from' => [ |
|
62
|
|
|
'date' => $absence->getFrom()->getDate()->format('Y-m-d'), |
|
63
|
|
|
'lesson' => $absence->getFrom()->getLesson() |
|
64
|
|
|
], |
|
65
|
|
|
'until' => [ |
|
66
|
|
|
'date' => $absence->getUntil()->getDate()->format('Y-m-d'), |
|
67
|
|
|
'lesson' => $absence->getUntil()->getLesson() |
|
68
|
|
|
], |
|
69
|
|
|
'student' => [ |
|
70
|
|
|
'uuid' => $absence->getStudent()->getUuid()->toString(), |
|
71
|
|
|
'external_id' => $absence->getStudent()->getExternalId(), |
|
72
|
|
|
'firstname' => $absence->getStudent()->getFirstname(), |
|
73
|
|
|
'lastname' => $absence->getStudent()->getLastname(), |
|
74
|
|
|
'grade' => $section !== null ? $absence->getStudent()->getGrade($section)?->getName() : null |
|
75
|
|
|
], |
|
76
|
|
|
'metadata' => $twig->render('absences/students/export/metadata.txt.twig', [ |
|
77
|
|
|
'absence' => $absence |
|
78
|
|
|
]), |
|
79
|
|
|
'attachments' => [ ] |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
foreach($absence->getAttachments() as $attachment) { |
|
83
|
|
|
$json['attachments'][] = [ |
|
84
|
|
|
'uuid' => $attachment->getUuid()->toString(), |
|
85
|
|
|
'filename' => $attachment->getFilename() |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->json($json); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
#[Route('/attachment/{uuid}', name: 'export_student_absence_attachment')] |
|
93
|
|
|
public function attachment(StudentAbsenceAttachment $attachment, FilesystemOperator $studentAbsenceFilesystem, MimeTypes $mimeTypes, StudentAbsenceSettings $settings): Response { |
|
|
|
|
|
|
94
|
|
|
if($studentAbsenceFilesystem->fileExists($attachment->getPath()) !== true) { |
|
95
|
|
|
throw new NotFoundHttpException(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$extension = pathinfo($attachment->getFilename(), PATHINFO_EXTENSION); |
|
99
|
|
|
|
|
100
|
|
|
return new FlysystemFileResponse( |
|
101
|
|
|
$studentAbsenceFilesystem, |
|
102
|
|
|
$attachment->getPath(), |
|
103
|
|
|
$attachment->getFilename(), |
|
104
|
|
|
$mimeTypes->getMimeType($extension) |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |