Passed
Push — master ( 7e6606...22e2ab )
by Angel Fernando Quiroz
07:27 queued 23s
created

CCalendarEvent::setSubscriptionItemId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CourseBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Delete;
14
use ApiPlatform\Metadata\Get;
15
use ApiPlatform\Metadata\GetCollection;
16
use ApiPlatform\Metadata\Post;
17
use ApiPlatform\Metadata\Put;
18
use Chamilo\CoreBundle\ApiResource\CalendarEvent;
19
use Chamilo\CoreBundle\Controller\Api\UpdateCCalendarEventAction;
20
use Chamilo\CoreBundle\Entity\AbstractResource;
21
use Chamilo\CoreBundle\Entity\AgendaReminder;
22
use Chamilo\CoreBundle\Entity\Career;
23
use Chamilo\CoreBundle\Entity\Promotion;
24
use Chamilo\CoreBundle\Entity\ResourceInterface;
25
use Chamilo\CoreBundle\Entity\ResourceLink;
26
use Chamilo\CoreBundle\Entity\Room;
27
use Chamilo\CoreBundle\Filter\CidFilter;
28
use Chamilo\CoreBundle\Filter\GlobalEventFilter;
29
use Chamilo\CoreBundle\Filter\SidFilter;
30
use Chamilo\CoreBundle\State\CalendarEventStateProvider;
31
use Chamilo\CoreBundle\State\CCalendarEventStateProcessor;
32
use Chamilo\CourseBundle\Repository\CCalendarEventRepository;
33
use DateTime;
34
use Doctrine\Common\Collections\ArrayCollection;
35
use Doctrine\Common\Collections\Collection;
36
use Doctrine\ORM\Mapping as ORM;
37
use Stringable;
38
use Symfony\Component\Serializer\Annotation\Groups;
39
use Symfony\Component\Uid\Uuid;
40
use Symfony\Component\Validator\Constraints as Assert;
41
42
/**
43
 * Calendar events.
44
 */
45
#[ApiResource(
46
    operations: [
47
        new Get(security: "is_granted('VIEW', object)"),
48
        new Put(
49
            controller: UpdateCCalendarEventAction::class,
50
            security: "is_granted('EDIT', object)",
51
            deserialize: false
52
        ),
53
        new Delete(security: "is_granted('DELETE', object)"),
54
        new GetCollection(
55
            paginationEnabled: false,
56
            security: "is_granted('ROLE_USER')",
57
            output: CalendarEvent::class,
58
            provider: CalendarEventStateProvider::class,
59
        ),
60
        new Post(
61
            securityPostDenormalize: "is_granted('CREATE', object)",
62
            processor: CCalendarEventStateProcessor::class
63
        ),
64
    ],
65
    normalizationContext: ['groups' => ['calendar_event:read', 'resource_node:read']],
66
    denormalizationContext: ['groups' => ['calendar_event:write']],
67
    security: "is_granted('ROLE_USER')"
68
)]
69
#[ORM\Table(name: 'c_calendar_event')]
70
#[ORM\Entity(repositoryClass: CCalendarEventRepository::class)]
71
#[ApiFilter(filterClass: SearchFilter::class, properties: ['allDay' => 'boolean'])]
72
#[ApiFilter(filterClass: DateFilter::class, strategy: 'exclude_null')]
73
#[ApiFilter(filterClass: CidFilter::class)]
74
#[ApiFilter(filterClass: SidFilter::class)]
75
#[ApiFilter(GlobalEventFilter::class, properties: ['type'])]
76
class CCalendarEvent extends AbstractResource implements ResourceInterface, Stringable
77
{
78
    public const COLOR_STUDENT_PUBLICATION = '#FF8C00';
79
80
    public const TYPE_INVITATION = 'invitation';
81
    public const TYPE_SUBSCRIPTION = 'subscription';
82
83
    public const SUBSCRIPTION_VISIBILITY_NO = 0;
84
    public const SUBSCRIPTION_VISIBILITY_ALL = 1;
85
    public const SUBSCRIPTION_VISIBILITY_CLASS = 2;
86
87
    #[Groups(['calendar_event:read'])]
88
    #[ORM\Column(name: 'iid', type: 'integer')]
89
    #[ORM\Id]
90
    #[ORM\GeneratedValue]
91
    protected ?int $iid = null;
92
93
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
94
    #[Assert\NotBlank]
95
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
96
    protected string $title;
97
98
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
99
    #[Assert\NotBlank]
100
    #[ORM\Column(name: 'content', type: 'text', nullable: true)]
101
    protected ?string $content = null;
102
103
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
104
    #[ORM\Column(name: 'start_date', type: 'datetime', nullable: true)]
105
    protected ?DateTime $startDate = null;
106
107
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
108
    #[ORM\Column(name: 'end_date', type: 'datetime', nullable: true)]
109
    protected ?DateTime $endDate = null;
110
111
    #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
112
    #[ORM\JoinColumn(name: 'parent_event_id', referencedColumnName: 'iid')]
113
    protected ?CCalendarEvent $parentEvent = null;
114
115
    /**
116
     * @var Collection<int, CCalendarEvent>
117
     */
118
    #[ORM\OneToMany(mappedBy: 'parentEvent', targetEntity: self::class)]
119
    protected Collection $children;
120
121
    /**
122
     * @var Collection<int, CCalendarEventRepeat>
123
     */
124
    #[ORM\OneToMany(
125
        mappedBy: 'event',
126
        targetEntity: CCalendarEventRepeat::class,
127
        cascade: ['persist'],
128
        orphanRemoval: true
129
    )]
130
    protected Collection $repeatEvents;
131
132
    #[Assert\NotNull]
133
    #[ORM\Column(name: 'all_day', type: 'boolean', nullable: false)]
134
    protected bool $allDay;
135
136
    #[ORM\Column(name: 'comment', type: 'text', nullable: true)]
137
    protected ?string $comment = null;
138
139
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
140
    #[ORM\Column(name: 'color', type: 'string', length: 20, nullable: true)]
141
    protected ?string $color = null;
142
143
    #[ORM\ManyToOne(targetEntity: Room::class)]
144
    #[ORM\JoinColumn(name: 'room_id', referencedColumnName: 'id')]
145
    protected ?Room $room = null;
146
147
    /**
148
     * @var Collection<int, CCalendarEventAttachment>
149
     */
150
    #[ORM\OneToMany(mappedBy: 'event', targetEntity: CCalendarEventAttachment::class, cascade: ['persist', 'remove'])]
151
    protected Collection $attachments;
152
153
    #[Groups(['calendar_event:write'])]
154
    #[ORM\Column(name: 'invitation_type', type: 'string', nullable: true)]
155
    protected ?string $invitationType = null;
156
157
    #[Groups(['calendar_event:write'])]
158
    #[Assert\NotNull]
159
    #[ORM\Column(name: 'collective', type: 'boolean')]
160
    protected bool $collective = false;
161
162
    #[Groups(['calendar_event:write'])]
163
    #[ORM\Column(name: 'subscription_visibility', type: 'integer')]
164
    protected int $subscriptionVisibility = self::SUBSCRIPTION_VISIBILITY_NO;
165
166
    #[Groups(['calendar_event:write'])]
167
    #[ORM\Column(name: 'subscription_item_id', type: 'integer', nullable: true)]
168
    protected ?int $subscriptionItemId = null;
169
170
    #[Groups(['calendar_event:write'])]
171
    #[ORM\Column(name: 'max_attendees', type: 'integer')]
172
    protected int $maxAttendees = 0;
173
174
    /**
175
     * @var Collection<int, AgendaReminder>
176
     */
177
    #[Groups(['calendar_event:write'])]
178
    #[ORM\OneToMany(mappedBy: 'event', targetEntity: AgendaReminder::class, cascade: ['persist'], orphanRemoval: true)]
179
    private Collection $reminders;
180
181
    #[ORM\ManyToOne(targetEntity: Career::class)]
182
    #[ORM\JoinColumn(name: 'career_id', referencedColumnName: 'id', nullable: true)]
183
    protected ?Career $career = null;
184
185
    #[ORM\ManyToOne(targetEntity: Promotion::class)]
186
    #[ORM\JoinColumn(name: 'promotion_id', referencedColumnName: 'id', nullable: true)]
187
    protected ?Promotion $promotion = null;
188
189
    public function __construct()
190
    {
191
        $this->children = new ArrayCollection();
192
        $this->attachments = new ArrayCollection();
193
        $this->repeatEvents = new ArrayCollection();
194
        $this->allDay = false;
195
        $this->collective = false;
196
        $this->reminders = new ArrayCollection();
197
    }
198
199
    public function __toString(): string
200
    {
201
        return $this->getTitle();
202
    }
203
204
    public function getTitle(): string
205
    {
206
        return $this->title;
207
    }
208
209
    public function setTitle(string $title): self
210
    {
211
        $this->title = $title;
212
213
        return $this;
214
    }
215
216
    public function getContent(): ?string
217
    {
218
        return $this->content;
219
    }
220
221
    public function setContent(string $content): self
222
    {
223
        $this->content = $content;
224
225
        return $this;
226
    }
227
228
    public function getStartDate(): ?DateTime
229
    {
230
        return $this->startDate;
231
    }
232
233
    public function setStartDate(?DateTime $startDate): self
234
    {
235
        $this->startDate = $startDate;
236
237
        return $this;
238
    }
239
240
    public function getEndDate(): ?DateTime
241
    {
242
        return $this->endDate;
243
    }
244
245
    public function setEndDate(?DateTime $endDate): self
246
    {
247
        $this->endDate = $endDate;
248
249
        return $this;
250
    }
251
252
    public function getParentEvent(): ?self
253
    {
254
        return $this->parentEvent;
255
    }
256
257
    public function setParentEvent(self $parent): self
258
    {
259
        $this->parentEvent = $parent;
260
261
        return $this;
262
    }
263
264
    public function addChild(self $event): self
265
    {
266
        if (!$this->getChildren()->contains($event)) {
267
            $this->getChildren()->add($event);
268
        }
269
270
        return $this;
271
    }
272
273
    /**
274
     * @return Collection<int, CCalendarEvent>
275
     */
276
    public function getChildren(): Collection
277
    {
278
        return $this->children;
279
    }
280
281
    /**
282
     * @param Collection<int, CCalendarEvent> $children
283
     */
284
    public function setChildren(Collection $children): self
285
    {
286
        $this->children = $children;
287
288
        return $this;
289
    }
290
291
    public function isAllDay(): bool
292
    {
293
        return $this->allDay;
294
    }
295
296
    public function setAllDay(bool $allDay): self
297
    {
298
        $this->allDay = $allDay;
299
300
        return $this;
301
    }
302
303
    public function getComment(): ?string
304
    {
305
        return $this->comment;
306
    }
307
308
    public function setComment(string $comment): self
309
    {
310
        $this->comment = $comment;
311
312
        return $this;
313
    }
314
315
    public function getRoom(): ?Room
316
    {
317
        return $this->room;
318
    }
319
320
    public function setRoom(Room $room): self
321
    {
322
        $this->room = $room;
323
324
        return $this;
325
    }
326
327
    public function getColor(): ?string
328
    {
329
        return $this->color;
330
    }
331
332
    public function setColor(string $color): self
333
    {
334
        $this->color = $color;
335
336
        return $this;
337
    }
338
339
    /**
340
     * @return Collection<int, CCalendarEventAttachment>
341
     */
342
    public function getAttachments(): Collection
343
    {
344
        return $this->attachments;
345
    }
346
347
    public function setAttachments(Collection $attachments): self
348
    {
349
        $this->attachments = $attachments;
350
351
        return $this;
352
    }
353
354
    public function addAttachment(CCalendarEventAttachment $attachment): self
355
    {
356
        $this->attachments->add($attachment);
357
358
        return $this;
359
    }
360
361
    /**
362
     * @return Collection<int, CCalendarEventRepeat>
363
     */
364
    public function getRepeatEvents(): Collection
365
    {
366
        return $this->repeatEvents;
367
    }
368
369
    /**
370
     * @param Collection<int, CCalendarEventRepeat> $repeatEvents
371
     */
372
    public function setRepeatEvents(Collection $repeatEvents): self
373
    {
374
        $this->repeatEvents = $repeatEvents;
375
376
        return $this;
377
    }
378
379
    public function getResourceIdentifier(): int|Uuid
380
    {
381
        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 Symfony\Component\Uid\Uuid|integer. Consider adding an additional type-check to rule them out.
Loading history...
382
    }
383
384
    public function getIid(): ?int
385
    {
386
        return $this->iid;
387
    }
388
389
    public function getResourceName(): string
390
    {
391
        return $this->getTitle();
392
    }
393
394
    public function setResourceName(string $name): self
395
    {
396
        return $this->setTitle($name);
397
    }
398
399
    public function isCollective(): bool
400
    {
401
        return $this->collective;
402
    }
403
404
    public function setCollective(bool $collective): self
405
    {
406
        $this->collective = $collective;
407
408
        return $this;
409
    }
410
411
    public function getInvitationType(): ?string
412
    {
413
        return $this->invitationType;
414
    }
415
416
    public function setInvitationType(string $invitationType): self
417
    {
418
        $this->invitationType = $invitationType;
419
420
        return $this;
421
    }
422
423
    public function getSubscriptionVisibility(): int
424
    {
425
        return $this->subscriptionVisibility;
426
    }
427
428
    public function setSubscriptionVisibility(int $subscriptionVisibility): self
429
    {
430
        $this->subscriptionVisibility = $subscriptionVisibility;
431
432
        return $this;
433
    }
434
435
    public function getSubscriptionItemId(): ?int
436
    {
437
        return $this->subscriptionItemId;
438
    }
439
440
    public function setSubscriptionItemId(?int $subscriptionItemId): self
441
    {
442
        $this->subscriptionItemId = $subscriptionItemId;
443
444
        return $this;
445
    }
446
447
    public function getMaxAttendees(): int
448
    {
449
        return $this->maxAttendees;
450
    }
451
452
    public function setMaxAttendees(int $maxAttendees): self
453
    {
454
        $this->maxAttendees = $maxAttendees;
455
456
        return $this;
457
    }
458
459
    /**
460
     * @return Collection<int, AgendaReminder>
461
     */
462
    public function getReminders(): Collection
463
    {
464
        return $this->reminders;
465
    }
466
467
    public function addReminder(AgendaReminder $reminder): static
468
    {
469
        if (!$this->reminders->contains($reminder)) {
470
            $this->reminders->add($reminder);
471
            $reminder->setEvent($this);
472
        }
473
474
        return $this;
475
    }
476
477
    public function removeReminder(AgendaReminder $reminder): static
478
    {
479
        if ($this->reminders->removeElement($reminder)) {
480
            // set the owning side to null (unless already changed)
481
            if ($reminder->getEvent() === $this) {
482
                $reminder->setEvent(null);
483
            }
484
        }
485
486
        return $this;
487
    }
488
489
    public function getCareer(): ?Career
490
    {
491
        return $this->career;
492
    }
493
494
    public function setCareer(?Career $career): self
495
    {
496
        $this->career = $career;
497
498
        return $this;
499
    }
500
501
    public function getPromotion(): ?Promotion
502
    {
503
        return $this->promotion;
504
    }
505
506
    public function setPromotion(?Promotion $promotion): self
507
    {
508
        $this->promotion = $promotion;
509
510
        return $this;
511
    }
512
513
    public function determineType(): string
514
    {
515
        $resourceLinks = $this->resourceNode->getResourceLinks();
0 ignored issues
show
Bug introduced by
The method getResourceLinks() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

515
        /** @scrutinizer ignore-call */ 
516
        $resourceLinks = $this->resourceNode->getResourceLinks();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
516
517
        foreach ($resourceLinks as $link) {
518
            if (null === $link->getCourse()
519
                && null === $link->getSession()
520
                && null === $link->getGroup()
521
                && null === $link->getUser()
522
            ) {
523
                return 'global';
524
            }
525
526
            if (null !== $link->getCourse()) {
527
                return 'course';
528
            }
529
530
            if (null !== $link->getSession()) {
531
                return 'session';
532
            }
533
        }
534
535
        return 'personal';
536
    }
537
}
538