Passed
Pull Request — master (#7217)
by
unknown
10:21
created

CAttendance::isRequireUnique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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