Passed
Push — master ( ade321...56ef38 )
by Julito
07:36
created

Session::getShowDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Core\Annotation\ApiFilter;
10
use ApiPlatform\Core\Annotation\ApiResource;
11
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
12
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
13
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
14
use DateTime;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\Common\Collections\Criteria;
18
use Doctrine\ORM\Mapping as ORM;
19
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
20
use Symfony\Component\Serializer\Annotation\Groups;
21
use Symfony\Component\Validator\Constraints as Assert;
22
23
/**
24
 * @ORM\Table(
25
 *     name="session",
26
 *     uniqueConstraints={
27
 *         @ORM\UniqueConstraint(name="name", columns={"name"})
28
 *     }
29
 * )
30
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\SessionListener"})
31
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\SessionRepository")
32
 * @UniqueEntity("name")
33
 */
34
#[ApiResource(
35
    collectionOperations: [
36
        'get' => [
37
            'security' => "is_granted('ROLE_ADMIN')",
38
        ],
39
        'post' => [
40
            'security' => "is_granted('ROLE_ADMIN')",
41
        ],
42
    ],
43
    itemOperations: [
44
        'get' => [
45
            'security' => "is_granted('ROLE_ADMIN') or is_granted('VIEW', object)",
46
        ],
47
        'put' => [
48
            'security' => "is_granted('ROLE_ADMIN')",
49
        ],
50
    ],
51
    attributes: [
52
        'security' => "is_granted('ROLE_ADMIN')",
53
    ],
54
    denormalizationContext: [
55
        'groups' => ['session:write'],
56
    ],
57
    normalizationContext: [
58
        'groups' => ['session:read'],
59
    ],
60
)]
61
#[ApiFilter(SearchFilter::class, properties: ['name' => 'partial'])]
62
#[ApiFilter(PropertyFilter::class)]
63
#[ApiFilter(OrderFilter::class, properties: ['id', 'name'])]
64
65
class Session implements ResourceWithAccessUrlInterface
66
{
67
    public const VISIBLE = 1;
68
    public const READ_ONLY = 2;
69
    public const INVISIBLE = 3;
70
    public const AVAILABLE = 4;
71
72
    public const STUDENT = 0;
73
    public const DRH = 1;
74
    public const COURSE_COACH = 2;
75
    public const GENERAL_COACH = 3;
76
    public const SESSION_ADMIN = 4;
77
78
    /**
79
     * @ORM\Column(name="id", type="integer")
80
     * @ORM\Id
81
     * @ORM\GeneratedValue()
82
     */
83
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
84
    protected ?int $id = null;
85
86
    /**
87
     * @var Collection|SessionRelCourse[]
88
     *
89
     * @ORM\OrderBy({"position"="ASC"})
90
     * @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
91
     */
92
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
93
    protected Collection $courses;
94
95
    /**
96
     * @var Collection|SessionRelUser[]
97
     *
98
     * @ORM\OneToMany(targetEntity="SessionRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
99
     */
100
    #[Groups(['session:read', 'session_rel_user:read'])]
101
    protected Collection $users;
102
103
    /**
104
     * @var Collection|SessionRelCourseRelUser[]
105
     *
106
     * @ORM\OneToMany(
107
     *     targetEntity="SessionRelCourseRelUser",
108
     *     mappedBy="session",
109
     *     cascade={"persist"},
110
     *     orphanRemoval=true
111
     * )
112
     */
113
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
114
    protected Collection $sessionRelCourseRelUsers;
115
116
    /**
117
     * @var Collection|SkillRelCourse[]
118
     * @ORM\OneToMany(targetEntity="SkillRelCourse", mappedBy="session", cascade={"persist", "remove"})
119
     */
120
    protected Collection $skills;
121
122
    /**
123
     * @var Collection|SkillRelUser[]
124
     *
125
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="session", cascade={"persist"})
126
     */
127
    protected Collection $issuedSkills;
128
129
    /**
130
     * @var AccessUrlRelSession[]|Collection
131
     *
132
     * @ORM\OneToMany(
133
     *     targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelSession",
134
     *     mappedBy="session",
135
     *     cascade={"persist"}, orphanRemoval=true
136
     * )
137
     */
138
    protected Collection $urls;
139
140
    /**
141
     * @var Collection|ResourceLink[]
142
     *
143
     * @ORM\OneToMany(targetEntity="ResourceLink", mappedBy="session", cascade={"remove"}, orphanRemoval=true)
144
     */
145
    protected Collection $resourceLinks;
146
147
    protected AccessUrl $currentUrl;
148
149
    protected ?Course $currentCourse = null;
150
151
    /**
152
     * @ORM\Column(name="name", type="string", length=150)
153
     */
154
    #[Assert\NotBlank]
155
    #[Groups(['session:read', 'session:write', 'session_rel_course_rel_user:read', 'document:read', 'session_rel_user:read'])]
156
    protected string $name;
157
158
    /**
159
     * @ORM\Column(name="description", type="text", nullable=true, unique=false)
160
     */
161
    #[Groups(['session:read', 'session:write'])]
162
    protected ?string $description;
163
164
    /**
165
     * @ORM\Column(name="show_description", type="boolean", nullable=true)
166
     */
167
    #[Groups(['session:read', 'session:write'])]
168
    protected ?bool $showDescription;
169
170
    /**
171
     * @ORM\Column(name="duration", type="integer", nullable=true)
172
     */
173
    #[Groups(['session:read', 'session:write'])]
174
    protected ?int $duration = null;
175
176
    /**
177
     * @ORM\Column(name="nbr_courses", type="integer", nullable=false, unique=false)
178
     */
179
    #[Groups(['session:read'])]
180
    protected int $nbrCourses;
181
182
    /**
183
     * @ORM\Column(name="nbr_users", type="integer", nullable=false, unique=false)
184
     */
185
    #[Groups(['session:read'])]
186
    protected int $nbrUsers;
187
188
    /**
189
     * @ORM\Column(name="nbr_classes", type="integer", nullable=false, unique=false)
190
     */
191
    #[Groups(['session:read'])]
192
    protected int $nbrClasses;
193
194
    /**
195
     * @ORM\Column(name="visibility", type="integer")
196
     */
197
    #[Groups(['session:read', 'session:write'])]
198
    protected int $visibility;
199
200
    /**
201
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Promotion", inversedBy="sessions", cascade={"persist"})
202
     * @ORM\JoinColumn(name="promotion_id", referencedColumnName="id", onDelete="CASCADE")
203
     */
204
    protected ?Promotion $promotion = null;
205
206
    /**
207
     * @ORM\Column(name="display_start_date", type="datetime", nullable=true, unique=false)
208
     */
209
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
210
    protected ?DateTime $displayStartDate;
211
212
    /**
213
     * @ORM\Column(name="display_end_date", type="datetime", nullable=true, unique=false)
214
     */
215
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
216
    protected ?DateTime $displayEndDate;
217
218
    /**
219
     * @ORM\Column(name="access_start_date", type="datetime", nullable=true, unique=false)
220
     */
221
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
222
    protected ?DateTime $accessStartDate;
223
224
    /**
225
     * @ORM\Column(name="access_end_date", type="datetime", nullable=true, unique=false)
226
     */
227
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
228
    protected ?DateTime $accessEndDate;
229
230
    /**
231
     * @ORM\Column(name="coach_access_start_date", type="datetime", nullable=true, unique=false)
232
     */
233
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
234
    protected ?DateTime $coachAccessStartDate;
235
236
    /**
237
     * @ORM\Column(name="coach_access_end_date", type="datetime", nullable=true, unique=false)
238
     */
239
    #[Groups(['session:read', 'session_rel_user:read', 'session_rel_course_rel_user:read'])]
240
    protected ?DateTime $coachAccessEndDate;
241
242
    /**
243
     * @ORM\Column(name="position", type="integer", nullable=false, options={"default":0})
244
     */
245
    protected int $position;
246
247
    /**
248
     * @ORM\Column(name="status", type="integer", nullable=false)
249
     */
250
    #[Groups(['session:read'])]
251
    protected int $status;
252
253
    /**
254
     * @ORM\ManyToOne(targetEntity="SessionCategory", inversedBy="sessions")
255
     * @ORM\JoinColumn(name="session_category_id", referencedColumnName="id")
256
     */
257
    #[Groups(['session:read', 'session:write'])]
258
    protected ?SessionCategory $category = null;
259
260
    /**
261
     * @ORM\Column(name="send_subscription_notification", type="boolean", nullable=false, options={"default":false})
262
     */
263
    protected bool $sendSubscriptionNotification;
264
265
    public function __construct()
266
    {
267
        $this->skills = new ArrayCollection();
268
        $this->issuedSkills = new ArrayCollection();
269
        $this->resourceLinks = new ArrayCollection();
270
        $this->courses = new ArrayCollection();
271
        $this->users = new ArrayCollection();
272
        $this->sessionRelCourseRelUsers = new ArrayCollection();
273
        $this->urls = new ArrayCollection();
274
275
        $this->duration = 0;
276
        $this->description = '';
277
        $this->nbrClasses = 0;
278
        $this->nbrUsers = 0;
279
        $this->nbrCourses = 0;
280
        $this->sendSubscriptionNotification = false;
281
282
        $now = new DateTime();
283
        $this->displayStartDate = $now;
284
        $this->displayEndDate = $now;
285
        $this->accessStartDate = $now;
286
        $this->accessEndDate = $now;
287
        $this->coachAccessStartDate = $now;
288
        $this->coachAccessEndDate = $now;
289
        $this->visibility = 1;
290
        $this->showDescription = false;
291
        $this->category = null;
292
        $this->status = 0;
293
        $this->position = 0;
294
    }
295
296
    public function __toString(): string
297
    {
298
        return $this->getName();
299
    }
300
301
    public static function getRelationTypeList(): array
302
    {
303
        return [
304
            self::STUDENT,
305
            self::DRH,
306
            self::COURSE_COACH,
307
            self::GENERAL_COACH,
308
            self::SESSION_ADMIN,
309
        ];
310
    }
311
312
    public function getDuration(): ?int
313
    {
314
        return $this->duration;
315
    }
316
317
    public function setDuration(int $duration): self
318
    {
319
        $this->duration = $duration;
320
321
        return $this;
322
    }
323
324
    public function getShowDescription(): bool
325
    {
326
        return $this->showDescription;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->showDescription could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
327
    }
328
329
    public function setShowDescription(bool $showDescription): self
330
    {
331
        $this->showDescription = $showDescription;
332
333
        return $this;
334
    }
335
336
    /**
337
     * Get id.
338
     *
339
     * @return int
340
     */
341
    public function getId()
342
    {
343
        return $this->id;
344
    }
345
346
    /**
347
     * @return Collection
348
     */
349
    public function getUsers()
350
    {
351
        return $this->users;
352
    }
353
354
    public function setUsers(Collection $users): self
355
    {
356
        $this->users = new ArrayCollection();
357
358
        foreach ($users as $user) {
359
            $this->addUser($user);
360
        }
361
362
        return $this;
363
    }
364
365
    public function addUser(SessionRelUser $user): void
366
    {
367
        $user->setSession($this);
368
369
        if (!$this->hasUser($user)) {
370
            $this->users->add($user);
371
            $this->nbrUsers++;
372
        }
373
    }
374
375
    public function addUserInSession(int $relationType, User $user): self
376
    {
377
        $sessionRelUser = (new SessionRelUser())
378
            ->setSession($this)
379
            ->setUser($user)
380
            ->setRelationType($relationType)
381
        ;
382
383
        $this->addUser($sessionRelUser);
384
385
        return $this;
386
    }
387
388
    public function hasUser(SessionRelUser $subscription): bool
389
    {
390
        if (0 !== $this->getUsers()->count()) {
391
            $criteria = Criteria::create()->where(
392
                Criteria::expr()->eq('user', $subscription->getUser())
393
            )->andWhere(
394
                Criteria::expr()->eq('session', $subscription->getSession())
395
            )->andWhere(
396
                Criteria::expr()->eq('relationType', $subscription->getRelationType())
397
            );
398
399
            $relation = $this->getUsers()->matching($criteria);
400
401
            return $relation->count() > 0;
402
        }
403
404
        return false;
405
    }
406
407
    /**
408
     * @return Collection
409
     */
410
    public function getCourses()
411
    {
412
        return $this->courses;
413
    }
414
415
    public function setCourses(ArrayCollection $courses): void
416
    {
417
        $this->courses = new ArrayCollection();
418
419
        foreach ($courses as $course) {
420
            $this->addCourses($course);
421
        }
422
    }
423
424
    public function addCourses(SessionRelCourse $course): void
425
    {
426
        $course->setSession($this);
427
        $this->courses->add($course);
428
    }
429
430
    public function hasCourse(Course $course): bool
431
    {
432
        if (0 !== $this->getCourses()->count()) {
433
            $criteria = Criteria::create()->where(
434
                Criteria::expr()->eq('course', $course)
435
            );
436
            $relation = $this->getCourses()->matching($criteria);
437
438
            return $relation->count() > 0;
439
        }
440
441
        return false;
442
    }
443
444
    /**
445
     * Remove $course.
446
     */
447
    public function removeCourses(SessionRelCourse $course): void
448
    {
449
        foreach ($this->courses as $key => $value) {
450
            if ($value->getId() === $course->getId()) {
451
                unset($this->courses[$key]);
452
            }
453
        }
454
    }
455
456
    /**
457
     * Remove course subscription for a user.
458
     * If user status in session is student, then decrease number of course users.
459
     */
460
    public function removeUserCourseSubscription(User $user, Course $course): void
461
    {
462
        foreach ($this->sessionRelCourseRelUsers as $i => $sessionRelUser) {
463
            if ($sessionRelUser->getCourse()->getId() === $course->getId() &&
464
                $sessionRelUser->getUser()->getId() === $user->getId()) {
465
                if (self::STUDENT === $this->sessionRelCourseRelUsers[$i]->getStatus()) {
466
                    $sessionCourse = $this->getCourseSubscription($course);
467
                    $sessionCourse->setNbrUsers($sessionCourse->getNbrUsers() - 1);
468
                }
469
470
                unset($this->sessionRelCourseRelUsers[$i]);
471
            }
472
        }
473
    }
474
475
    /**
476
     * @param int $status if not set it will check if the user is registered
477
     *                    with any status
478
     */
479
    public function hasUserInCourse(User $user, Course $course, int $status = null): bool
480
    {
481
        $relation = $this->getUserInCourse($user, $course, $status);
482
483
        return $relation->count() > 0;
484
    }
485
486
    public function hasStudentInCourse(User $user, Course $course): bool
487
    {
488
        return $this->hasUserInCourse($user, $course, self::STUDENT);
489
    }
490
491
    public function hasCourseCoachInCourse(User $user, Course $course = null): bool
492
    {
493
        if (null === $course) {
494
            return false;
495
        }
496
497
        return $this->hasUserInCourse($user, $course, self::COURSE_COACH);
498
    }
499
500
    public function getUserInCourse(User $user, Course $course, ?int $status = null): Collection
501
    {
502
        $criteria = Criteria::create()
503
            ->where(
504
                Criteria::expr()->eq('course', $course)
505
            )->andWhere(
506
                Criteria::expr()->eq('user', $user)
507
            );
508
509
        if (null !== $status) {
510
            $criteria->andWhere(
511
                Criteria::expr()->eq('status', $status)
512
            );
513
        }
514
515
        return $this->getSessionRelCourseRelUsers()->matching($criteria);
516
    }
517
518
    public function getAllUsersFromCourse(int $status): Collection
519
    {
520
        $criteria = Criteria::create()
521
            ->where(
522
                Criteria::expr()->eq('status', $status)
523
            )
524
        ;
525
526
        return $this->getSessionRelCourseRelUsers()->matching($criteria);
527
    }
528
529
    public function getSessionRelCourseByUser(User $user, ?int $status = null): Collection
530
    {
531
        $criteria = Criteria::create()
532
            ->where(
533
                Criteria::expr()->eq('user', $user)
534
            )
535
        ;
536
537
        if (null !== $status) {
538
            $criteria->andWhere(
539
                Criteria::expr()->eq('status', $status)
540
            );
541
        }
542
543
        return $this->sessionRelCourseRelUsers->matching($criteria);
544
    }
545
546
    public function setName(string $name): self
547
    {
548
        $this->name = $name;
549
550
        return $this;
551
    }
552
553
    public function getName(): string
554
    {
555
        return $this->name;
556
    }
557
558
    public function setDescription(string $description): self
559
    {
560
        $this->description = $description;
561
562
        return $this;
563
    }
564
565
    public function getDescription(): ?string
566
    {
567
        return $this->description;
568
    }
569
570
    public function setNbrCourses(int $nbrCourses): self
571
    {
572
        $this->nbrCourses = $nbrCourses;
573
574
        return $this;
575
    }
576
577
    public function getNbrCourses(): int
578
    {
579
        return $this->nbrCourses;
580
    }
581
582
    public function setNbrUsers(int $nbrUsers): self
583
    {
584
        $this->nbrUsers = $nbrUsers;
585
586
        return $this;
587
    }
588
589
    public function getNbrUsers(): int
590
    {
591
        return $this->nbrUsers;
592
    }
593
594
    public function setNbrClasses(int $nbrClasses): self
595
    {
596
        $this->nbrClasses = $nbrClasses;
597
598
        return $this;
599
    }
600
601
    public function getNbrClasses(): int
602
    {
603
        return $this->nbrClasses;
604
    }
605
606
    public function setVisibility(int $visibility): self
607
    {
608
        $this->visibility = $visibility;
609
610
        return $this;
611
    }
612
613
    public function getVisibility(): int
614
    {
615
        return $this->visibility;
616
    }
617
618
    public function getPromotion(): ?Promotion
619
    {
620
        return $this->promotion;
621
    }
622
623
    public function setPromotion(?Promotion $promotion): self
624
    {
625
        $this->promotion = $promotion;
626
627
        return $this;
628
    }
629
630
    public function setDisplayStartDate(?DateTime $displayStartDate): self
631
    {
632
        $this->displayStartDate = $displayStartDate;
633
634
        return $this;
635
    }
636
637
    public function getDisplayStartDate(): ?DateTime
638
    {
639
        return $this->displayStartDate;
640
    }
641
642
    public function setDisplayEndDate(?DateTime $displayEndDate): self
643
    {
644
        $this->displayEndDate = $displayEndDate;
645
646
        return $this;
647
    }
648
649
    public function getDisplayEndDate(): ?DateTime
650
    {
651
        return $this->displayEndDate;
652
    }
653
654
    public function setAccessStartDate(?DateTime $accessStartDate): self
655
    {
656
        $this->accessStartDate = $accessStartDate;
657
658
        return $this;
659
    }
660
661
    public function getAccessStartDate(): ?DateTime
662
    {
663
        return $this->accessStartDate;
664
    }
665
666
    public function setAccessEndDate(?DateTime $accessEndDate): self
667
    {
668
        $this->accessEndDate = $accessEndDate;
669
670
        return $this;
671
    }
672
673
    public function getAccessEndDate(): ?DateTime
674
    {
675
        return $this->accessEndDate;
676
    }
677
678
    public function setCoachAccessStartDate(?DateTime $coachAccessStartDate): self
679
    {
680
        $this->coachAccessStartDate = $coachAccessStartDate;
681
682
        return $this;
683
    }
684
685
    public function getCoachAccessStartDate(): ?DateTime
686
    {
687
        return $this->coachAccessStartDate;
688
    }
689
690
    public function setCoachAccessEndDate(?DateTime $coachAccessEndDate): self
691
    {
692
        $this->coachAccessEndDate = $coachAccessEndDate;
693
694
        return $this;
695
    }
696
697
    public function getCoachAccessEndDate(): ?DateTime
698
    {
699
        return $this->coachAccessEndDate;
700
    }
701
702
    public function getGeneralCoaches(): Collection
703
    {
704
        return $this
705
            ->getGeneralCoachesSubscriptions()
706
            ->map(function (SessionRelUser $subscription) {
707
                return $subscription->getUser();
708
            })
709
        ;
710
    }
711
712
    public function getGeneralCoachesSubscriptions(): Collection
713
    {
714
        $criteria = Criteria::create()
715
            ->where(
716
                Criteria::expr()->eq('relationType', self::GENERAL_COACH)
717
            )
718
        ;
719
720
        return $this->users->matching($criteria);
721
    }
722
723
    public function hasUserAsGeneralCoach(User $user): bool
724
    {
725
        $criteria = Criteria::create()
726
            ->where(
727
                Criteria::expr()->eq('relationType', self::GENERAL_COACH)
728
            )
729
            ->andWhere(
730
                Criteria::expr()->eq('user', $user)
731
            )
732
        ;
733
734
        return $this->users->matching($criteria)->count() > 0;
735
    }
736
737
    public function addGeneralCoach(User $coach): self
738
    {
739
        return $this->addUserInSession(self::GENERAL_COACH, $coach);
740
    }
741
742
    public function getCategory(): ?SessionCategory
743
    {
744
        return $this->category;
745
    }
746
747
    public function setCategory(?SessionCategory $category): self
748
    {
749
        $this->category = $category;
750
751
        return $this;
752
    }
753
754
    public static function getStatusList(): array
755
    {
756
        return [
757
            self::VISIBLE => 'status_visible',
758
            self::READ_ONLY => 'status_read_only',
759
            self::INVISIBLE => 'status_invisible',
760
            self::AVAILABLE => 'status_available',
761
        ];
762
    }
763
764
    /**
765
     * Check if session is visible.
766
     */
767
    public function isActive(): bool
768
    {
769
        $now = new Datetime('now');
770
771
        return $now > $this->getAccessStartDate();
772
    }
773
774
    public function isActiveForStudent(): bool
775
    {
776
        $start = $this->getAccessStartDate();
777
        $end = $this->getAccessEndDate();
778
779
        return $this->compareDates($start, $end);
780
    }
781
782
    public function isActiveForCoach(): bool
783
    {
784
        $start = $this->getCoachAccessStartDate();
785
        $end = $this->getCoachAccessEndDate();
786
787
        return $this->compareDates($start, $end);
788
    }
789
790
    /**
791
     * Compare the current date with start and end access dates.
792
     * Either missing date is interpreted as no limit.
793
     *
794
     * @return bool whether now is between the session access start and end dates
795
     */
796
    public function isCurrentlyAccessible(): bool
797
    {
798
        $now = new Datetime();
799
800
        return
801
            (null === $this->accessStartDate || $this->accessStartDate < $now) &&
802
            (null === $this->accessEndDate || $now < $this->accessEndDate);
803
    }
804
805
    public function addCourse(Course $course): self
806
    {
807
        $sessionRelCourse = (new SessionRelCourse())
808
            ->setCourse($course)
809
        ;
810
        $this->addCourses($sessionRelCourse);
811
812
        return $this;
813
    }
814
815
    /**
816
     * Removes a course from this session.
817
     *
818
     * @param Course $course the course to remove from this session
819
     *
820
     * @return bool whether the course was actually found in this session and removed from it
821
     */
822
    public function removeCourse(Course $course): bool
823
    {
824
        $relCourse = $this->getCourseSubscription($course);
825
        if (null !== $relCourse) {
826
            $this->courses->removeElement($relCourse);
827
            $this->setNbrCourses(\count($this->courses));
828
829
            return true;
830
        }
831
832
        return false;
833
    }
834
835
    /**
836
     * @return SessionRelCourseRelUser[]|ArrayCollection|Collection
837
     */
838
    public function getSessionRelCourseRelUsers()
839
    {
840
        return $this->sessionRelCourseRelUsers;
841
    }
842
843
    public function setSessionRelCourseRelUsers(Collection $sessionRelCourseRelUsers): self
844
    {
845
        $this->sessionRelCourseRelUsers = new ArrayCollection();
846
        foreach ($sessionRelCourseRelUsers as $item) {
847
            $this->addSessionRelCourseRelUser($item);
848
        }
849
850
        return $this;
851
    }
852
853
    public function addSessionRelCourseRelUser(SessionRelCourseRelUser $sessionRelCourseRelUser): void
854
    {
855
        $sessionRelCourseRelUser->setSession($this);
856
        if (!$this->hasUserCourseSubscription($sessionRelCourseRelUser)) {
857
            $this->sessionRelCourseRelUsers->add($sessionRelCourseRelUser);
858
        }
859
    }
860
861
    /**
862
     * @return null|SessionRelCourse
863
     */
864
    public function getCourseSubscription(Course $course)
865
    {
866
        $criteria = Criteria::create()->where(
867
            Criteria::expr()->eq('course', $course)
868
        );
869
870
        return $this->courses->matching($criteria)->current();
871
    }
872
873
    /**
874
     * Add a user course subscription.
875
     * If user status in session is student, then increase number of course users.
876
     * Status example: Session::STUDENT.
877
     */
878
    public function addUserInCourse(int $status, User $user, Course $course): SessionRelCourseRelUser
879
    {
880
        $userRelCourseRelSession =
881
            (new SessionRelCourseRelUser())
882
                ->setCourse($course)
883
                ->setUser($user)
884
                ->setSession($this)
885
                ->setStatus($status)
886
        ;
887
        $this->addSessionRelCourseRelUser($userRelCourseRelSession);
888
889
        if (self::STUDENT === $status) {
890
            $sessionCourse = $this->getCourseSubscription($course);
891
            $sessionCourse->setNbrUsers($sessionCourse->getNbrUsers() + 1);
892
        }
893
894
        return $userRelCourseRelSession;
895
    }
896
897
    public function hasUserCourseSubscription(SessionRelCourseRelUser $subscription): bool
898
    {
899
        if (0 !== $this->getSessionRelCourseRelUsers()->count()) {
900
            $criteria = Criteria::create()->where(
901
                Criteria::expr()->eq('user', $subscription->getUser())
902
            )->andWhere(
903
                Criteria::expr()->eq('course', $subscription->getCourse())
904
            )->andWhere(
905
                Criteria::expr()->eq('session', $subscription->getSession())
906
            );
907
            $relation = $this->getSessionRelCourseRelUsers()->matching($criteria);
908
909
            return $relation->count() > 0;
910
        }
911
912
        return false;
913
    }
914
915
    /**
916
     * currentCourse is set in CourseListener.
917
     */
918
    public function getCurrentCourse(): ?Course
919
    {
920
        return $this->currentCourse;
921
    }
922
923
    /**
924
     * currentCourse is set in CourseListener.
925
     */
926
    public function setCurrentCourse(Course $course): self
927
    {
928
        // If the session is registered in the course session list.
929
        $exists = $this->getCourses()->exists(
930
            function ($key, $element) use ($course) {
931
                /** @var SessionRelCourse $element */
932
                return $course->getId() === $element->getCourse()->getId();
933
            }
934
        );
935
936
        if ($exists) {
937
            $this->currentCourse = $course;
938
        }
939
940
        return $this;
941
    }
942
943
    public function setSendSubscriptionNotification(bool $sendNotification): self
944
    {
945
        $this->sendSubscriptionNotification = $sendNotification;
946
947
        return $this;
948
    }
949
950
    public function getSendSubscriptionNotification(): bool
951
    {
952
        return $this->sendSubscriptionNotification;
953
    }
954
955
    /**
956
     * Get user from course by status.
957
     *
958
     * @return ArrayCollection|Collection
959
     */
960
    public function getSessionRelCourseRelUsersByStatus(Course $course, int $status)
961
    {
962
        $criteria = Criteria::create()
963
            ->where(
964
                Criteria::expr()->eq('course', $course)
965
            )
966
            ->andWhere(
967
                Criteria::expr()->eq('status', $status)
968
            )
969
        ;
970
971
        return $this->sessionRelCourseRelUsers->matching($criteria);
972
    }
973
974
    public function getIssuedSkills(): Collection
975
    {
976
        return $this->issuedSkills;
977
    }
978
979
    public function setCurrentUrl(AccessUrl $url): self
980
    {
981
        $urlList = $this->getUrls();
982
        foreach ($urlList as $item) {
983
            if ($item->getUrl()->getId() === $url->getId()) {
984
                $this->currentUrl = $url;
985
986
                break;
987
            }
988
        }
989
990
        return $this;
991
    }
992
993
    /**
994
     * @return AccessUrl
995
     */
996
    public function getCurrentUrl()
997
    {
998
        return $this->currentUrl;
999
    }
1000
1001
    /**
1002
     * @return Collection
1003
     */
1004
    public function getUrls()
1005
    {
1006
        return $this->urls;
1007
    }
1008
1009
    public function setUrls(Collection $urls): self
1010
    {
1011
        $this->urls = new ArrayCollection();
1012
1013
        foreach ($urls as $url) {
1014
            $this->addUrls($url);
1015
        }
1016
1017
        return $this;
1018
    }
1019
1020
    public function addAccessUrl(AccessUrl $url): self
1021
    {
1022
        $accessUrlRelSession = new AccessUrlRelSession();
1023
        $accessUrlRelSession->setUrl($url);
1024
        $accessUrlRelSession->setSession($this);
1025
1026
        $this->addUrls($accessUrlRelSession);
1027
1028
        return $this;
1029
    }
1030
1031
    public function addUrls(AccessUrlRelSession $url): self
1032
    {
1033
        $url->setSession($this);
1034
        $this->urls->add($url);
1035
1036
        return $this;
1037
    }
1038
1039
    /**
1040
     * @return int
1041
     */
1042
    public function getPosition()
1043
    {
1044
        return $this->position;
1045
    }
1046
1047
    public function setPosition(int $position): self
1048
    {
1049
        $this->position = $position;
1050
1051
        return $this;
1052
    }
1053
1054
    public function getStatus(): int
1055
    {
1056
        return $this->status;
1057
    }
1058
1059
    public function setStatus(int $status): self
1060
    {
1061
        $this->status = $status;
1062
1063
        return $this;
1064
    }
1065
1066
    public function getSessionAdmins(): Collection
1067
    {
1068
        return $this
1069
            ->getGeneralAdminsSubscriptions()
1070
            ->map(function (SessionRelUser $subscription) {
1071
                return $subscription->getUser();
1072
            })
1073
        ;
1074
    }
1075
1076
    public function getGeneralAdminsSubscriptions(): Collection
1077
    {
1078
        $criteria = Criteria::create()
1079
            ->where(
1080
                Criteria::expr()->eq('relationType', self::SESSION_ADMIN)
1081
            )
1082
        ;
1083
1084
        return $this->users->matching($criteria);
1085
    }
1086
1087
    public function hasUserAsSessionAdmin(User $user): bool
1088
    {
1089
        $criteria = Criteria::create()
1090
            ->where(
1091
                Criteria::expr()->eq('relationType', self::SESSION_ADMIN)
1092
            )
1093
            ->andWhere(
1094
                Criteria::expr()->eq('user', $user)
1095
            )
1096
        ;
1097
1098
        return $this->users->matching($criteria)->count() > 0;
1099
    }
1100
1101
    public function addSessionAdmin(User $sessionAdmin): self
1102
    {
1103
        return $this->addUserInSession(self::SESSION_ADMIN, $sessionAdmin);
1104
    }
1105
1106
    /**
1107
     * @return SkillRelCourse[]|Collection
1108
     */
1109
    public function getSkills()
1110
    {
1111
        return $this->skills;
1112
    }
1113
1114
    /**
1115
     * @return ResourceLink[]|Collection
1116
     */
1117
    public function getResourceLinks()
1118
    {
1119
        return $this->resourceLinks;
1120
    }
1121
1122
    /**
1123
     * Check if $user is course coach in any course.
1124
     */
1125
    public function hasCoachInCourseList(User $user): bool
1126
    {
1127
        foreach ($this->courses as $sessionCourse) {
1128
            if ($this->hasCourseCoachInCourse($user, $sessionCourse->getCourse())) {
1129
                return true;
1130
            }
1131
        }
1132
1133
        return false;
1134
    }
1135
1136
    /**
1137
     * Check if $user is student in any course.
1138
     */
1139
    public function hasStudentInCourseList(User $user): bool
1140
    {
1141
        foreach ($this->courses as $sessionCourse) {
1142
            if ($this->hasStudentInCourse($user, $sessionCourse->getCourse())) {
1143
                return true;
1144
            }
1145
        }
1146
1147
        return false;
1148
    }
1149
1150
    protected function compareDates(DateTime $start, DateTime $end = null): bool
1151
    {
1152
        $now = new Datetime('now');
1153
1154
        if (!empty($start) && !empty($end) && ($now >= $start && $now <= $end)) {
1155
            return true;
1156
        }
1157
1158
        if (!empty($start) && $now >= $start) {
1159
            return true;
1160
        }
1161
1162
        return !empty($end) && $now <= $end;
1163
    }
1164
}
1165