1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* For licensing terms, see /license.txt */ |
||
6 | |||
7 | namespace Chamilo\CourseBundle\Entity; |
||
8 | |||
9 | use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
||
10 | use ApiPlatform\Metadata\ApiFilter; |
||
11 | use ApiPlatform\Metadata\ApiResource; |
||
12 | use ApiPlatform\Metadata\Delete; |
||
13 | use ApiPlatform\Metadata\Get; |
||
14 | use ApiPlatform\Metadata\GetCollection; |
||
15 | use ApiPlatform\Metadata\Post; |
||
16 | use ApiPlatform\Metadata\Put; |
||
17 | use Chamilo\CoreBundle\Entity\AbstractResource; |
||
18 | use Chamilo\CoreBundle\Entity\ResourceInterface; |
||
19 | use Chamilo\CoreBundle\Filter\SidFilter; |
||
20 | use Chamilo\CoreBundle\State\CAttendanceStateProcessor; |
||
21 | use Chamilo\CourseBundle\Repository\CAttendanceRepository; |
||
22 | use Doctrine\Common\Collections\ArrayCollection; |
||
23 | use Doctrine\Common\Collections\Collection; |
||
24 | use Doctrine\ORM\Mapping as ORM; |
||
25 | use Stringable; |
||
26 | use Symfony\Component\Serializer\Annotation\Groups; |
||
27 | use Symfony\Component\Serializer\Annotation\MaxDepth; |
||
28 | use Symfony\Component\Validator\Constraints as Assert; |
||
29 | |||
30 | #[ApiResource( |
||
31 | shortName: 'Attendances', |
||
32 | operations: [ |
||
33 | new Put( |
||
34 | uriTemplate: '/attendances/{iid}/toggle_visibility', |
||
35 | openapiContext: [ |
||
36 | 'summary' => 'Toggle visibility of the attendance\'s associated ResourceLink', |
||
37 | ], |
||
38 | security: "is_granted('EDIT', object.resourceNode)", |
||
39 | name: 'toggle_visibility', |
||
40 | processor: CAttendanceStateProcessor::class |
||
41 | ), |
||
42 | new Put( |
||
43 | uriTemplate: '/attendances/{iid}/soft_delete', |
||
44 | openapiContext: [ |
||
45 | 'summary' => 'Soft delete the attendance', |
||
46 | ], |
||
47 | security: "is_granted('EDIT', object.resourceNode)", |
||
48 | name: 'soft_delete', |
||
49 | processor: CAttendanceStateProcessor::class |
||
50 | ), |
||
51 | new Delete(security: "is_granted('ROLE_TEACHER')"), |
||
52 | new Post( |
||
53 | uriTemplate: '/attendances/{iid}/calendars', |
||
54 | openapiContext: ['summary' => 'Add a calendar to an attendance.'], |
||
55 | denormalizationContext: ['groups' => ['attendance:write']], |
||
56 | name: 'calendar_add', |
||
57 | processor: CAttendanceStateProcessor::class |
||
58 | ), |
||
59 | new GetCollection( |
||
60 | openapiContext: [ |
||
61 | 'parameters' => [ |
||
62 | [ |
||
63 | 'name' => 'resourceNode.parent', |
||
64 | 'in' => 'query', |
||
65 | 'required' => true, |
||
66 | 'description' => 'Resource node Parent', |
||
67 | 'schema' => ['type' => 'integer'], |
||
68 | ], |
||
69 | ], |
||
70 | ], |
||
71 | ), |
||
72 | new Get(security: "is_granted('ROLE_USER')"), |
||
73 | new Post( |
||
74 | denormalizationContext: ['groups' => ['attendance:write']], |
||
75 | security: "is_granted('ROLE_TEACHER')", |
||
76 | validationContext: ['groups' => ['Default']] |
||
77 | ), |
||
78 | new Put( |
||
79 | denormalizationContext: ['groups' => ['attendance:write']], |
||
80 | security: "is_granted('ROLE_TEACHER')" |
||
81 | ), |
||
82 | ], |
||
83 | normalizationContext: [ |
||
84 | 'groups' => ['attendance:read', 'resource_node:read', 'resource_link:read'], |
||
85 | 'enable_max_depth' => true, |
||
86 | ], |
||
87 | denormalizationContext: ['groups' => ['attendance:write']], |
||
88 | paginationEnabled: true, |
||
89 | )] |
||
90 | #[ApiFilter(SearchFilter::class, properties: ['active' => 'exact', 'title' => 'partial', 'resourceNode.parent' => 'exact'])] |
||
91 | #[ApiFilter(filterClass: SidFilter::class)] |
||
92 | #[ORM\Table(name: 'c_attendance')] |
||
93 | #[ORM\Index(columns: ['active'], name: 'active')] |
||
94 | #[ORM\Entity(repositoryClass: CAttendanceRepository::class)] |
||
95 | class CAttendance extends AbstractResource implements ResourceInterface, Stringable |
||
96 | { |
||
97 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
98 | #[ORM\Id] |
||
99 | #[ORM\GeneratedValue] |
||
100 | #[Groups(['attendance:read'])] |
||
101 | protected ?int $iid = null; |
||
102 | |||
103 | #[Assert\NotBlank] |
||
104 | #[ORM\Column(name: 'title', type: 'text', nullable: false)] |
||
105 | #[Groups(['attendance:read', 'attendance:write'])] |
||
106 | protected string $title; |
||
107 | |||
108 | #[ORM\Column(name: 'description', type: 'text', nullable: true)] |
||
109 | #[Groups(['attendance:read', 'attendance:write'])] |
||
110 | protected ?string $description = null; |
||
111 | |||
112 | #[Assert\NotBlank] |
||
113 | #[ORM\Column(name: 'active', type: 'integer', nullable: false)] |
||
114 | #[Groups(['attendance:read', 'attendance:write'])] |
||
115 | protected int $active = 1; |
||
116 | |||
117 | #[ORM\Column(name: 'attendance_qualify_title', type: 'string', length: 255, nullable: true)] |
||
118 | #[Groups(['attendance:read', 'attendance:write'])] |
||
119 | protected ?string $attendanceQualifyTitle = null; |
||
120 | |||
121 | #[Assert\NotNull] |
||
122 | #[ORM\Column(name: 'attendance_qualify_max', type: 'integer', nullable: false)] |
||
123 | protected int $attendanceQualifyMax; |
||
124 | |||
125 | #[Assert\NotNull] |
||
126 | #[ORM\Column(name: 'attendance_weight', type: 'float', precision: 6, scale: 2, nullable: false)] |
||
127 | #[Groups(['attendance:read', 'attendance:write'])] |
||
128 | protected float $attendanceWeight = 0.0; |
||
129 | |||
130 | #[Assert\NotNull] |
||
131 | #[ORM\Column(name: 'locked', type: 'integer', nullable: false)] |
||
132 | protected int $locked; |
||
133 | |||
134 | /** |
||
135 | * @var Collection|CAttendanceCalendar[] |
||
136 | */ |
||
137 | #[ORM\OneToMany(mappedBy: 'attendance', targetEntity: CAttendanceCalendar::class, cascade: ['persist', 'remove'])] |
||
138 | #[Groups(['attendance:read'])] |
||
139 | #[MaxDepth(1)] |
||
140 | protected Collection $calendars; |
||
141 | |||
142 | /** |
||
143 | * @var Collection|CAttendanceResult[] |
||
144 | */ |
||
145 | #[ORM\OneToMany(mappedBy: 'attendance', targetEntity: CAttendanceResult::class, cascade: ['persist', 'remove'])] |
||
146 | protected Collection $results; |
||
147 | |||
148 | /** |
||
149 | * @var Collection|CAttendanceSheetLog[] |
||
150 | */ |
||
151 | #[ORM\OneToMany(mappedBy: 'attendance', targetEntity: CAttendanceSheetLog::class, cascade: ['persist', 'remove'])] |
||
152 | protected Collection $logs; |
||
153 | |||
154 | #[Groups(['attendance:read'])] |
||
155 | private ?int $doneCalendars = null; |
||
156 | |||
157 | public function __construct() |
||
158 | { |
||
159 | $this->description = ''; |
||
160 | $this->active = 1; |
||
161 | $this->attendanceQualifyMax = 0; |
||
162 | $this->locked = 0; |
||
163 | $this->calendars = new ArrayCollection(); |
||
164 | $this->results = new ArrayCollection(); |
||
165 | $this->logs = new ArrayCollection(); |
||
166 | } |
||
167 | |||
168 | public function __toString(): string |
||
169 | { |
||
170 | return $this->getTitle(); |
||
171 | } |
||
172 | |||
173 | public function setTitle(string $title): self |
||
174 | { |
||
175 | $this->title = $title; |
||
176 | |||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | public function getTitle(): string |
||
181 | { |
||
182 | return $this->title; |
||
183 | } |
||
184 | |||
185 | public function setDescription(string $description): self |
||
186 | { |
||
187 | $this->description = $description; |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | public function getDescription(): ?string |
||
193 | { |
||
194 | return $this->description; |
||
195 | } |
||
196 | |||
197 | public function setActive(int $active): self |
||
198 | { |
||
199 | $this->active = $active; |
||
200 | |||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | public function getActive(): int |
||
205 | { |
||
206 | return $this->active; |
||
207 | } |
||
208 | |||
209 | public function setAttendanceQualifyTitle(string $attendanceQualifyTitle): self |
||
210 | { |
||
211 | $this->attendanceQualifyTitle = $attendanceQualifyTitle; |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | public function getAttendanceQualifyTitle(): ?string |
||
217 | { |
||
218 | return $this->attendanceQualifyTitle; |
||
219 | } |
||
220 | |||
221 | public function setAttendanceQualifyMax(int $attendanceQualifyMax): self |
||
222 | { |
||
223 | $this->attendanceQualifyMax = $attendanceQualifyMax; |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Get attendanceQualifyMax. |
||
230 | */ |
||
231 | public function getAttendanceQualifyMax(): int |
||
232 | { |
||
233 | return $this->attendanceQualifyMax; |
||
234 | } |
||
235 | |||
236 | public function setAttendanceWeight(float $attendanceWeight): self |
||
237 | { |
||
238 | $this->attendanceWeight = $attendanceWeight; |
||
239 | |||
240 | return $this; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Get attendanceWeight. |
||
245 | */ |
||
246 | public function getAttendanceWeight(): float |
||
247 | { |
||
248 | return $this->attendanceWeight; |
||
249 | } |
||
250 | |||
251 | public function setLocked(int $locked): self |
||
252 | { |
||
253 | $this->locked = $locked; |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Get locked. |
||
260 | */ |
||
261 | public function getLocked(): int |
||
262 | { |
||
263 | return $this->locked; |
||
264 | } |
||
265 | |||
266 | public function getIid(): ?int |
||
267 | { |
||
268 | return $this->iid; |
||
269 | } |
||
270 | |||
271 | public function getCalendars(): Collection |
||
272 | { |
||
273 | return $this->calendars; |
||
274 | } |
||
275 | |||
276 | public function setCalendars(Collection $calendars): self |
||
277 | { |
||
278 | $this->calendars = $calendars; |
||
279 | |||
280 | return $this; |
||
281 | } |
||
282 | |||
283 | public function addCalendar(CAttendanceCalendar $calendar): self |
||
284 | { |
||
285 | if (!$this->calendars->contains($calendar)) { |
||
286 | $this->calendars->add($calendar); |
||
287 | $calendar->setAttendance($this); |
||
288 | } |
||
289 | |||
290 | return $this; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @return CAttendanceSheetLog[]|Collection |
||
295 | */ |
||
296 | public function getLogs(): array|Collection |
||
297 | { |
||
298 | return $this->logs; |
||
299 | } |
||
300 | |||
301 | public function getDoneCalendars(): int |
||
302 | { |
||
303 | return $this->calendars |
||
304 | ->filter(fn (CAttendanceCalendar $calendar) => $calendar->getDoneAttendance()) |
||
305 | ->count() |
||
306 | ; |
||
307 | } |
||
308 | |||
309 | public function setDoneCalendars(?int $count): self |
||
310 | { |
||
311 | $this->doneCalendars = $count; |
||
312 | |||
313 | return $this; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param CAttendanceSheetLog[]|Collection $logs |
||
318 | */ |
||
319 | public function setLogs(array|Collection $logs): self |
||
320 | { |
||
321 | $this->logs = $logs; |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @return CAttendanceResult[]|Collection |
||
328 | */ |
||
329 | public function getResults(): array|Collection |
||
330 | { |
||
331 | return $this->results; |
||
332 | } |
||
333 | |||
334 | public function setResults(Collection $results): self |
||
335 | { |
||
336 | $this->results = $results; |
||
337 | |||
338 | return $this; |
||
339 | } |
||
340 | |||
341 | public function getResourceIdentifier(): int |
||
342 | { |
||
343 | return $this->getIid(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
344 | } |
||
345 | |||
346 | public function getResourceName(): string |
||
347 | { |
||
348 | return $this->getTitle(); |
||
349 | } |
||
350 | |||
351 | public function setResourceName(string $name): self |
||
352 | { |
||
353 | return $this->setTitle($name); |
||
354 | } |
||
355 | } |
||
356 |