|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Book\Grade; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Section; |
|
6
|
|
|
use App\Entity\Student; |
|
7
|
|
|
use App\Entity\StudyGroupMembership; |
|
8
|
|
|
use App\Entity\Tuition; |
|
9
|
|
|
use App\Entity\TuitionGrade; |
|
10
|
|
|
use App\Repository\TuitionGradeRepositoryInterface; |
|
11
|
|
|
use App\Repository\TuitionRepositoryInterface; |
|
12
|
|
|
use App\Sorting\Sorter; |
|
13
|
|
|
use App\Sorting\StudentStrategy; |
|
14
|
|
|
use App\Sorting\TuitionGradeCategoryStrategy; |
|
15
|
|
|
use App\Sorting\TuitionStrategy; |
|
16
|
|
|
use App\Utils\ArrayUtils; |
|
17
|
|
|
|
|
18
|
|
|
class GradeOverviewHelper { |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(private readonly TuitionGradeRepositoryInterface $tuitionGradeRepository, private readonly TuitionRepositoryInterface $tuitionRepository, private readonly Sorter $sorter) { } |
|
21
|
|
|
|
|
22
|
|
|
public function computeOverviewForStudent(Student $student, Section $section): GradeOverview { |
|
23
|
|
|
$tuitions = $this->tuitionRepository->findAllByStudents([$student], $section); |
|
24
|
|
|
$categories = [ ]; |
|
25
|
|
|
|
|
26
|
|
|
foreach($tuitions as $tuition) { |
|
27
|
|
|
foreach($tuition->getGradeCategories() as $category) { |
|
28
|
|
|
$categories[$category->getId()] = $category; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$grades = ArrayUtils::createArrayWithKeys( |
|
33
|
|
|
$this->tuitionGradeRepository->findAllByStudent($student, $section), |
|
34
|
|
|
fn(TuitionGrade $grade) => sprintf('%s_%s', $grade->getTuition()->getId(), $grade->getCategory()->getId()) |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
$this->sorter->sort($tuitions, TuitionStrategy::class); |
|
38
|
|
|
|
|
39
|
|
|
$rows = [ ]; |
|
40
|
|
|
foreach($tuitions as $tuition) { |
|
41
|
|
|
if($tuition->getGradeCategories()->count() === 0) { |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$data = [ ]; |
|
46
|
|
|
|
|
47
|
|
|
foreach($tuition->getGradeCategories() as $category) { |
|
48
|
|
|
$key = sprintf('%s_%s', $tuition->getId(), $category->getId()); |
|
49
|
|
|
$data[$category->getUuid()->toString()] = $grades[$key] ?? null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$rows[] = new GradeRow($tuition, $data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->sorter->sort($categories, TuitionGradeCategoryStrategy::class); |
|
56
|
|
|
return new GradeOverview($categories, $rows); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function computeOverviewForTuition(Tuition $tuition): ?GradeOverview { |
|
60
|
|
|
$categories = $tuition->getGradeCategories()->toArray(); |
|
61
|
|
|
|
|
62
|
|
|
if(count($categories) === 0) { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$grades = ArrayUtils::createArrayWithKeys( |
|
67
|
|
|
$this->tuitionGradeRepository->findAllByTuition($tuition), |
|
68
|
|
|
fn(TuitionGrade $grade) => sprintf('%s_%s', $grade->getStudent()->getId(), $grade->getCategory()->getId()) |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$rows = [ ]; |
|
72
|
|
|
/** @var Student[] $students */ |
|
73
|
|
|
$students = $tuition->getStudyGroup()->getMemberships()->map(fn(StudyGroupMembership $membership) => $membership->getStudent())->toArray(); |
|
74
|
|
|
|
|
75
|
|
|
$this->sorter->sort($students, StudentStrategy::class); |
|
76
|
|
|
|
|
77
|
|
|
foreach($students as $student) { |
|
78
|
|
|
$data = [ ]; |
|
79
|
|
|
|
|
80
|
|
|
foreach($categories as $category) { |
|
81
|
|
|
$key = sprintf('%s_%s', $student->getId(), $category->getId()); |
|
82
|
|
|
$data[$category->getUuid()->toString()] = $grades[$key] ?? null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$rows[] = new GradeRow($student, $data); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->sorter->sort($categories, TuitionGradeCategoryStrategy::class); |
|
89
|
|
|
return new GradeOverview($categories, $rows); |
|
90
|
|
|
} |
|
91
|
|
|
} |