1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\State; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Metadata\Operation; |
10
|
|
|
use ApiPlatform\State\ProcessorInterface; |
11
|
|
|
use Chamilo\CourseBundle\Entity\CAttendance; |
12
|
|
|
use Chamilo\CourseBundle\Entity\CAttendanceCalendar; |
13
|
|
|
use Chamilo\CourseBundle\Entity\CAttendanceCalendarRelGroup; |
14
|
|
|
use Chamilo\CourseBundle\Repository\CAttendanceCalendarRepository; |
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
18
|
|
|
|
19
|
|
|
final class CAttendanceStateProcessor implements ProcessorInterface |
20
|
|
|
{ |
21
|
|
|
public function __construct( |
22
|
|
|
private readonly ProcessorInterface $persistProcessor, |
23
|
|
|
private readonly EntityManagerInterface $entityManager, |
24
|
|
|
private readonly CAttendanceCalendarRepository $calendarRepo, |
25
|
|
|
private readonly RequestStack $requestStack |
26
|
|
|
) {} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Main process function for handling attendance and calendar operations. |
30
|
|
|
*/ |
31
|
|
|
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): void |
32
|
|
|
{ |
33
|
|
|
\assert($data instanceof CAttendance); |
34
|
|
|
|
35
|
|
|
$operationName = $operation->getName(); |
36
|
|
|
|
37
|
|
|
error_log("Processing $operationName"); |
38
|
|
|
|
39
|
|
|
match ($operationName) { |
40
|
|
|
'toggle_visibility' => $this->handleToggleVisibility($data), |
|
|
|
|
41
|
|
|
'soft_delete' => $this->handleSoftDelete($data), |
|
|
|
|
42
|
|
|
'calendar_add' => $this->handleAddCalendar($data), |
|
|
|
|
43
|
|
|
default => throw new BadRequestHttpException('Operation not supported.'), |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
$this->persistProcessor->process($data, $operation, $uriVariables, $context); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function handleToggleVisibility(CAttendance $attendance): void |
50
|
|
|
{ |
51
|
|
|
$attendance->setActive($attendance->getActive() === 1 ? 0 : 1); |
52
|
|
|
$this->entityManager->persist($attendance); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function handleSoftDelete(CAttendance $attendance): void |
56
|
|
|
{ |
57
|
|
|
$attendance->setActive(2); |
58
|
|
|
$this->entityManager->persist($attendance); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function handleAddCalendar(CAttendance $attendance): void |
62
|
|
|
{ |
63
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
64
|
|
|
$data = json_decode($request->getContent(), true); |
65
|
|
|
|
66
|
|
|
if (!$data) { |
67
|
|
|
throw new BadRequestHttpException('Request data is required to create a calendar.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$startDate = new \DateTime($data['startDate']); |
71
|
|
|
$repeatDate = $data['repeatDate'] ?? false; |
72
|
|
|
$repeatType = $data['repeatType'] ?? null; |
73
|
|
|
$repeatDays = $data['repeatDays'] ?? null; |
74
|
|
|
$endDate = $repeatDate ? new \DateTime($data['repeatEndDate']) : null; |
75
|
|
|
$groupId = $data['group'] ?? 0; |
76
|
|
|
|
77
|
|
|
$this->saveCalendar($attendance, $startDate, $groupId); |
78
|
|
|
|
79
|
|
|
if ($repeatDate && $repeatType && $endDate) { |
80
|
|
|
$interval = $this->getRepeatInterval($repeatType, $repeatDays); |
81
|
|
|
$currentDate = clone $startDate; |
82
|
|
|
|
83
|
|
|
while ($currentDate < $endDate) { |
84
|
|
|
$currentDate->add($interval); |
85
|
|
|
$this->saveCalendar($attendance, $currentDate, $groupId); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function saveCalendar(CAttendance $attendance, \DateTime $date, ?int $groupId): void |
91
|
|
|
{ |
92
|
|
|
$existingCalendar = $this->calendarRepo->findOneBy([ |
93
|
|
|
'attendance' => $attendance->getIid(), |
94
|
|
|
'dateTime' => $date, |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
if ($existingCalendar) { |
98
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$calendar = new CAttendanceCalendar(); |
103
|
|
|
$calendar->setAttendance($attendance); |
104
|
|
|
$calendar->setDateTime($date); |
105
|
|
|
$calendar->setDoneAttendance(false); |
106
|
|
|
$calendar->setBlocked(false); |
107
|
|
|
|
108
|
|
|
$this->entityManager->persist($calendar); |
109
|
|
|
$this->entityManager->flush(); |
110
|
|
|
|
111
|
|
|
if (!empty($groupId)) { |
112
|
|
|
$this->addAttendanceCalendarToGroup($calendar, $groupId); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function addAttendanceCalendarToGroup(CAttendanceCalendar $calendar, int $groupId): void |
117
|
|
|
{ |
118
|
|
|
$repository = $this->entityManager->getRepository(CAttendanceCalendarRelGroup::class); |
119
|
|
|
$repository->addGroupToCalendar($calendar->getIid(), $groupId); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getRepeatInterval(string $repeatType, ?int $repeatDays = null): \DateInterval |
123
|
|
|
{ |
124
|
|
|
return match ($repeatType) { |
125
|
|
|
'daily' => new \DateInterval('P1D'), |
126
|
|
|
'weekly' => new \DateInterval('P7D'), |
127
|
|
|
'bi-weekly' => new \DateInterval('P14D'), |
128
|
|
|
'every-x-days' => new \DateInterval("P{$repeatDays}D"), |
129
|
|
|
'monthly-by-date' => new \DateInterval('P1M'), |
130
|
|
|
default => throw new BadRequestHttpException('Invalid repeat type.'), |
131
|
|
|
}; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.