Completed
Push — master ( f77763...d3c71d )
by Julito
11:18
created

Session::isUserGeneralCoach()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiFilter;
8
use ApiPlatform\Core\Annotation\ApiResource;
9
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
12
use Chamilo\CourseBundle\Entity\CStudentPublication;
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\Common\Collections\Criteria;
16
use Doctrine\ORM\Mapping as ORM;
17
use Symfony\Component\Serializer\Annotation\Groups;
18
use Symfony\Component\Validator\Constraints as Assert;
19
20
/**
21
 * @ApiResource(
22
 *     attributes={"security"="is_granted('ROLE_ADMIN')"},
23
 *     normalizationContext={"groups"={"session:read"}, "swagger_definition_name"="Read"},
24
 *     denormalizationContext={"groups"={"session:write"}},
25
 * )
26
 *
27
 * @ApiFilter(SearchFilter::class, properties={"name": "partial"})
28
 * @ApiFilter(PropertyFilter::class)
29
 * @ApiFilter(OrderFilter::class, properties={"id", "name"})
30
 *
31
 * @ORM\Table(
32
 *      name="session",
33
 *      uniqueConstraints={@ORM\UniqueConstraint(name="name", columns={"name"})},
34
 *      indexes={
35
 *          @ORM\Index(name="idx_id_coach", columns={"id_coach"}),
36
 *          @ORM\Index(name="idx_id_session_admin_id", columns={"session_admin_id"})
37
 *      }
38
 * )
39
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\SessionListener"})
40
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\SessionRepository")
41
 */
42
class Session
43
{
44
    public const VISIBLE = 1;
45
    public const READ_ONLY = 2;
46
    public const INVISIBLE = 3;
47
    public const AVAILABLE = 4;
48
49
    public const STUDENT = 0;
50
    public const DRH = 1;
51
    public const COACH = 2;
52
53
    /**
54
     * @var int
55
     * @Groups({"session:read"})
56
     * @ORM\Column(name="id", type="integer", nullable=false, unique=false)
57
     * @ORM\Id
58
     * @ORM\GeneratedValue(strategy="AUTO")
59
     */
60
    protected $id;
61
62
    /**
63
     * @ORM\OneToMany(
64
     *     targetEntity="SkillRelCourse", mappedBy="session", cascade={"persist", "remove"}
65
     * )
66
     */
67
    protected $skills;
68
69
    /**
70
     * @var ArrayCollection
71
     *
72
     * @ORM\OrderBy({"position" = "ASC"})
73
     * @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
74
     */
75
    protected $courses;
76
77
    /**
78
     * @var ArrayCollection
79
     *
80
     * @ORM\OneToMany(targetEntity="SessionRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
81
     */
82
    protected $users;
83
84
    /**
85
     * @var ArrayCollection
86
     *
87
     * @ORM\OneToMany(
88
     *     targetEntity="SessionRelCourseRelUser",
89
     *     mappedBy="session",
90
     *     cascade={"persist"},
91
     *     orphanRemoval=true
92
     * )
93
     */
94
    protected $userCourseSubscriptions;
95
96
    /**
97
     * @var Course
98
     */
99
    protected $currentCourse;
100
101
    /**
102
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="session", cascade={"persist"})
103
     */
104
    protected $issuedSkills;
105
106
    /**
107
     * @ORM\OneToMany(
108
     *     targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelSession",
109
     *     mappedBy="session",
110
     *     cascade={"persist"}, orphanRemoval=true
111
     * )
112
     */
113
    protected $urls;
114
115
    /**
116
     * @ORM\OneToMany(targetEntity="ResourceLink", mappedBy="session", cascade={"remove"}, orphanRemoval=true)
117
     */
118
    protected $resourceLinks;
119
120
    /**
121
     * @var AccessUrl
122
     */
123
    protected $currentUrl;
124
125
    /**
126
     * @var string
127
     * @Assert\NotBlank()
128
     * @Groups({"session:read", "session:write", "session_rel_course_rel_user:read"})
129
     * @ORM\Column(name="name", type="string", length=150, nullable=false, unique=false)
130
     */
131
    protected $name;
132
133
    /**
134
     * @var string
135
     * @Groups({"session:read", "session:write"})
136
     *
137
     * @ORM\Column(name="description", type="text", nullable=true, unique=false)
138
     */
139
    protected $description;
140
141
    /**
142
     * @var bool
143
     * @Groups({"session:read", "session:write"})
144
     *
145
     * @ORM\Column(name="show_description", type="boolean", nullable=true)
146
     */
147
    protected $showDescription;
148
149
    /**
150
     * @var int
151
     *
152
     * @ORM\Column(name="duration", type="integer", nullable=true)
153
     */
154
    protected $duration;
155
156
    /**
157
     * @var int
158
     * @Groups({"session:read"})
159
     * @ORM\Column(name="nbr_courses", type="smallint", nullable=true, unique=false)
160
     */
161
    protected $nbrCourses;
162
163
    /**
164
     * @var int
165
     * @Groups({"session:read"})
166
     * @ORM\Column(name="nbr_users", type="integer", nullable=true, unique=false)
167
     */
168
    protected $nbrUsers;
169
170
    /**
171
     * @var int
172
     * @Groups({"session:read"})
173
     * @ORM\Column(name="nbr_classes", type="integer", nullable=true, unique=false)
174
     */
175
    protected $nbrClasses;
176
177
    /**
178
     * @var User
179
     *
180
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User")
181
     * @ORM\JoinColumn(name="session_admin_id", referencedColumnName="id", nullable=true)
182
     */
183
    protected $sessionAdmin;
184
185
    /**
186
     * @var int
187
     * @Groups({"session:read"})
188
     * @ORM\Column(name="visibility", type="integer", nullable=false, unique=false)
189
     */
190
    protected $visibility;
191
192
    /**
193
     * @var int
194
     *
195
     * @ORM\Column(name="promotion_id", type="integer", nullable=true, unique=false)
196
     */
197
    protected $promotionId;
198
199
    /**
200
     * @var \DateTime
201
     * @Groups({"session:read"})
202
     * @ORM\Column(name="display_start_date", type="datetime", nullable=true, unique=false)
203
     */
204
    protected $displayStartDate;
205
206
    /**
207
     * @var \DateTime
208
     * @Groups({"session:read"})
209
     * @ORM\Column(name="display_end_date", type="datetime", nullable=true, unique=false)
210
     */
211
    protected $displayEndDate;
212
213
    /**
214
     * @var \DateTime
215
     *
216
     * @ORM\Column(name="access_start_date", type="datetime", nullable=true, unique=false)
217
     */
218
    protected $accessStartDate;
219
220
    /**
221
     * @var \DateTime
222
     *
223
     * @ORM\Column(name="access_end_date", type="datetime", nullable=true, unique=false)
224
     */
225
    protected $accessEndDate;
226
227
    /**
228
     * @var \DateTime
229
     *
230
     * @ORM\Column(name="coach_access_start_date", type="datetime", nullable=true, unique=false)
231
     */
232
    protected $coachAccessStartDate;
233
234
    /**
235
     * @var \DateTime
236
     *
237
     * @ORM\Column(name="coach_access_end_date", type="datetime", nullable=true, unique=false)
238
     */
239
    protected $coachAccessEndDate;
240
241
    /**
242
     * @var int
243
     *
244
     * @ORM\Column(name="position", type="integer", nullable=false, options={"default":0})
245
     */
246
    protected $position;
247
248
    /**
249
     * @var int
250
     * @Groups({"session:read"})
251
     *
252
     * @ORM\Column(name="status", type="integer", nullable=false)
253
     */
254
    protected $status;
255
256
    /**
257
     * @var User
258
     * @Assert\NotBlank
259
     * @Groups({"session:read", "session:write"})
260
     *
261
     * @ORM\ManyToOne(targetEntity="User", inversedBy="sessionAsGeneralCoach")
262
     * @ORM\JoinColumn(name="id_coach", referencedColumnName="id")
263
     */
264
    protected $generalCoach;
265
266
    /**
267
     * @Groups({"session:read", "session:write"})
268
     * @ORM\ManyToOne(targetEntity="SessionCategory", inversedBy="session")
269
     * @ORM\JoinColumn(name="session_category_id", referencedColumnName="id")
270
     */
271
    protected $category;
272
273
    /**
274
     * @var bool
275
     *
276
     * @ORM\Column(name="send_subscription_notification", type="boolean", nullable=false, options={"default":false})
277
     */
278
    protected $sendSubscriptionNotification;
279
280
    /**
281
     * @var ArrayCollection
282
     *
283
     * @ORM\OneToMany(
284
     *     targetEntity="Chamilo\CourseBundle\Entity\CStudentPublication",
285
     *     mappedBy="session",
286
     *     cascade={"persist"},
287
     *     orphanRemoval=true
288
     * )
289
     */
290
    protected $studentPublications;
291
292
    /**
293
     * Constructor.
294
     */
295
    public function __construct()
296
    {
297
        $this->items = new ArrayCollection();
298
        $this->urls = new ArrayCollection();
299
300
        $this->nbrClasses = 0;
301
        $this->nbrUsers = 0;
302
        $this->sendSubscriptionNotification = false;
303
        $this->displayStartDate = new \DateTime();
304
        $this->displayEndDate = new \DateTime();
305
        $this->accessStartDate = new \DateTime();
306
        $this->accessEndDate = new \DateTime();
307
        $this->coachAccessStartDate = new \DateTime();
308
        $this->coachAccessEndDate = new \DateTime();
309
        $this->visibility = 1;
310
311
        $this->courses = new ArrayCollection();
312
        $this->users = new ArrayCollection();
313
        $this->userCourseSubscriptions = new ArrayCollection();
314
        $this->showDescription = false;
315
        $this->category = null;
316
        $this->status = 0;
317
        $this->position = 0;
318
        $this->studentPublications = new ArrayCollection();
319
    }
320
321
    /**
322
     * @return string
323
     */
324
    public function __toString()
325
    {
326
        return (string) $this->getName();
327
    }
328
329
    /**
330
     * @return int
331
     */
332
    public function getDuration()
333
    {
334
        return $this->duration;
335
    }
336
337
    /**
338
     * @param int $duration
339
     *
340
     * @return $this
341
     */
342
    public function setDuration($duration)
343
    {
344
        $this->duration = $duration;
345
346
        return $this;
347
    }
348
349
    /**
350
     * @return string
351
     */
352
    public function getShowDescription()
353
    {
354
        return $this->showDescription;
355
    }
356
357
    /**
358
     * @param string $showDescription
359
     *
360
     * @return $this
361
     */
362
    public function setShowDescription($showDescription)
363
    {
364
        $this->showDescription = $showDescription;
0 ignored issues
show
Documentation Bug introduced by
The property $showDescription was declared of type boolean, but $showDescription is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
365
366
        return $this;
367
    }
368
369
    /**
370
     * Get id.
371
     *
372
     * @return int
373
     */
374
    public function getId()
375
    {
376
        return $this->id;
377
    }
378
379
    /**
380
     * @param int $id
381
     */
382
    public function setId($id)
383
    {
384
        $this->id = $id;
385
    }
386
387
    /**
388
     * @return ArrayCollection
389
     */
390
    public function getUsers()
391
    {
392
        return $this->users;
393
    }
394
395
    /**
396
     * @param $users
397
     *
398
     * @return $this
399
     */
400
    public function setUsers($users)
401
    {
402
        $this->users = new ArrayCollection();
403
404
        foreach ($users as $user) {
405
            $this->addUser($user);
406
        }
407
408
        return $this;
409
    }
410
411
    public function addUser(SessionRelUser $user)
412
    {
413
        $user->setSession($this);
414
415
        if (!$this->hasUser($user)) {
416
            $this->users[] = $user;
417
        }
418
    }
419
420
    /**
421
     * @param int $status
422
     */
423
    public function addUserInSession($status, User $user)
424
    {
425
        $sessionRelUser = new SessionRelUser();
426
        $sessionRelUser->setSession($this);
427
        $sessionRelUser->setUser($user);
428
        $sessionRelUser->setRelationType($status);
429
430
        $this->addUser($sessionRelUser);
431
    }
432
433
    /**
434
     * @return bool
435
     */
436
    public function hasUser(SessionRelUser $subscription)
437
    {
438
        if ($this->getUsers()->count()) {
439
            $criteria = Criteria::create()->where(
440
                Criteria::expr()->eq('user', $subscription->getUser())
441
            )->andWhere(
442
                Criteria::expr()->eq('session', $subscription->getSession())
443
            )->andWhere(
444
                Criteria::expr()->eq('relationType', $subscription->getRelationType())
445
            );
446
447
            $relation = $this->getUsers()->matching($criteria);
448
449
            return $relation->count() > 0;
450
        }
451
452
        return false;
453
    }
454
455
    /**
456
     * @return ArrayCollection
457
     */
458
    public function getCourses()
459
    {
460
        return $this->courses;
461
    }
462
463
    /**
464
     * @param $courses
465
     */
466
    public function setCourses(ArrayCollection $courses)
467
    {
468
        $this->courses = new ArrayCollection();
469
470
        foreach ($courses as $course) {
471
            $this->addCourses($course);
472
        }
473
    }
474
475
    public function addCourses(SessionRelCourse $course)
476
    {
477
        $course->setSession($this);
478
        $this->courses[] = $course;
479
    }
480
481
    /**
482
     * @return bool
483
     */
484
    public function hasCourse(Course $course)
485
    {
486
        if ($this->getCourses()->count()) {
487
            $criteria = Criteria::create()->where(
488
                Criteria::expr()->eq('course', $course)
489
            );
490
            $relation = $this->getCourses()->matching($criteria);
491
492
            return $relation->count() > 0;
493
        }
494
495
        return false;
496
    }
497
498
    /**
499
     * Check for existence of a relation (SessionRelCourse) between a course and this session.
500
     *
501
     * @return bool whether the course is related to this session
502
     */
503
    public function isRelatedToCourse(Course $course): bool
504
    {
505
        return !is_null(
506
            \Database::getManager()->getRepository(SessionRelCourse::class)->findOneBy([
507
                'session' => $this,
508
                'course' => $course,
509
            ])
510
        );
511
    }
512
513
    /**
514
     * Remove $course.
515
     *
516
     * @param SessionRelCourse $course
517
     */
518
    public function removeCourses($course)
519
    {
520
        foreach ($this->courses as $key => $value) {
521
            if ($value->getId() == $course->getId()) {
522
                unset($this->courses[$key]);
523
            }
524
        }
525
    }
526
527
    /**
528
     * Remove course subscription for a user.
529
     * If user status in session is student, then decrease number of course users.
530
     */
531
    public function removeUserCourseSubscription(User $user, Course $course)
532
    {
533
        /** @var SessionRelCourseRelUser $courseSubscription */
534
        foreach ($this->userCourseSubscriptions as $i => $courseSubscription) {
535
            if ($courseSubscription->getCourse()->getId() === $course->getId() &&
536
                $courseSubscription->getUser()->getId() === $user->getId()) {
537
                if (self::STUDENT === $this->userCourseSubscriptions[$i]->getStatus()) {
538
                    $sessionCourse = $this->getCourseSubscription($course);
539
540
                    $sessionCourse->setNbrUsers(
541
                        $sessionCourse->getNbrUsers() - 1
542
                    );
543
                }
544
545
                unset($this->userCourseSubscriptions[$i]);
546
            }
547
        }
548
    }
549
550
    /**
551
     * @param int $status if not set it will check if the user is registered
552
     *                    with any status
553
     */
554
    public function hasUserInCourse(User $user, Course $course, $status = null): bool
555
    {
556
        $relation = $this->getUserInCourse($user, $course, $status);
557
558
        return $relation->count() > 0;
0 ignored issues
show
Bug introduced by
The method count() does not exist on Chamilo\CoreBundle\Entity\Session. ( Ignorable by Annotation )

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

558
        return $relation->/** @scrutinizer ignore-call */ count() > 0;

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...
559
    }
560
561
    /**
562
     * @return bool
563
     */
564
    public function hasStudentInCourse(User $user, Course $course)
565
    {
566
        return $this->hasUserInCourse($user, $course, self::STUDENT);
567
    }
568
569
    /**
570
     * @param Course $course
571
     */
572
    public function hasCoachInCourseWithStatus(User $user, Course $course = null): bool
573
    {
574
        if (empty($course)) {
575
            return false;
576
        }
577
578
        return $this->hasUserInCourse($user, $course, self::COACH);
579
    }
580
581
    /**
582
     * @param string $status
583
     *
584
     * @return \Doctrine\Common\Collections\Collection|static
585
     */
586
    public function getUserInCourse(User $user, Course $course, $status = null)
587
    {
588
        $criteria = Criteria::create()->where(
589
            Criteria::expr()->eq('course', $course)
590
        )->andWhere(
591
            Criteria::expr()->eq('user', $user)
592
        );
593
594
        if (null !== $status) {
595
            $criteria->andWhere(
596
                Criteria::expr()->eq('status', $status)
597
            );
598
        }
599
600
        return $this->getUserCourseSubscriptions()->matching($criteria);
601
    }
602
603
    /**
604
     * Set name.
605
     *
606
     * @param string $name
607
     *
608
     * @return $this
609
     */
610
    public function setName($name)
611
    {
612
        $this->name = $name;
613
614
        return $this;
615
    }
616
617
    /**
618
     * Get name.
619
     *
620
     * @return string
621
     */
622
    public function getName()
623
    {
624
        return $this->name;
625
    }
626
627
    /**
628
     * Set description.
629
     *
630
     * @param string $description
631
     *
632
     * @return $this
633
     */
634
    public function setDescription($description)
635
    {
636
        $this->description = $description;
637
638
        return $this;
639
    }
640
641
    /**
642
     * Get description.
643
     *
644
     * @return string
645
     */
646
    public function getDescription()
647
    {
648
        return $this->description;
649
    }
650
651
    /**
652
     * Set nbrCourses.
653
     *
654
     * @param int $nbrCourses
655
     *
656
     * @return Session
657
     */
658
    public function setNbrCourses($nbrCourses)
659
    {
660
        $this->nbrCourses = $nbrCourses;
661
662
        return $this;
663
    }
664
665
    /**
666
     * Get nbrCourses.
667
     *
668
     * @return int
669
     */
670
    public function getNbrCourses()
671
    {
672
        return $this->nbrCourses;
673
    }
674
675
    /**
676
     * Set nbrUsers.
677
     *
678
     * @param int $nbrUsers
679
     *
680
     * @return Session
681
     */
682
    public function setNbrUsers($nbrUsers)
683
    {
684
        $this->nbrUsers = $nbrUsers;
685
686
        return $this;
687
    }
688
689
    /**
690
     * Get nbrUsers.
691
     *
692
     * @return int
693
     */
694
    public function getNbrUsers()
695
    {
696
        return $this->nbrUsers;
697
    }
698
699
    /**
700
     * Set nbrClasses.
701
     *
702
     * @param int $nbrClasses
703
     *
704
     * @return Session
705
     */
706
    public function setNbrClasses($nbrClasses)
707
    {
708
        $this->nbrClasses = $nbrClasses;
709
710
        return $this;
711
    }
712
713
    /**
714
     * Get nbrClasses.
715
     *
716
     * @return int
717
     */
718
    public function getNbrClasses()
719
    {
720
        return $this->nbrClasses;
721
    }
722
723
    /**
724
     * Set visibility.
725
     *
726
     * @param int $visibility
727
     *
728
     * @return Session
729
     */
730
    public function setVisibility($visibility)
731
    {
732
        $this->visibility = $visibility;
733
734
        return $this;
735
    }
736
737
    /**
738
     * Get visibility.
739
     *
740
     * @return int
741
     */
742
    public function getVisibility()
743
    {
744
        return $this->visibility;
745
    }
746
747
    /**
748
     * Set promotionId.
749
     *
750
     * @param int $promotionId
751
     *
752
     * @return Session
753
     */
754
    public function setPromotionId($promotionId)
755
    {
756
        $this->promotionId = $promotionId;
757
758
        return $this;
759
    }
760
761
    /**
762
     * Get promotionId.
763
     *
764
     * @return int
765
     */
766
    public function getPromotionId()
767
    {
768
        return $this->promotionId;
769
    }
770
771
    /**
772
     * Set displayStartDate.
773
     *
774
     * @param \DateTime $displayStartDate
775
     *
776
     * @return Session
777
     */
778
    public function setDisplayStartDate($displayStartDate)
779
    {
780
        $this->displayStartDate = $displayStartDate;
781
782
        return $this;
783
    }
784
785
    /**
786
     * Get displayStartDate.
787
     *
788
     * @return \DateTime
789
     */
790
    public function getDisplayStartDate()
791
    {
792
        return $this->displayStartDate;
793
    }
794
795
    /**
796
     * Set displayEndDate.
797
     *
798
     * @param \DateTime $displayEndDate
799
     *
800
     * @return Session
801
     */
802
    public function setDisplayEndDate($displayEndDate)
803
    {
804
        $this->displayEndDate = $displayEndDate;
805
806
        return $this;
807
    }
808
809
    /**
810
     * Get displayEndDate.
811
     *
812
     * @return \DateTime
813
     */
814
    public function getDisplayEndDate()
815
    {
816
        return $this->displayEndDate;
817
    }
818
819
    /**
820
     * Set accessStartDate.
821
     *
822
     * @param \DateTime $accessStartDate
823
     *
824
     * @return Session
825
     */
826
    public function setAccessStartDate($accessStartDate)
827
    {
828
        $this->accessStartDate = $accessStartDate;
829
830
        return $this;
831
    }
832
833
    /**
834
     * Get accessStartDate.
835
     *
836
     * @return \DateTime
837
     */
838
    public function getAccessStartDate()
839
    {
840
        return $this->accessStartDate;
841
    }
842
843
    /**
844
     * Set accessEndDate.
845
     *
846
     * @param \DateTime $accessEndDate
847
     *
848
     * @return Session
849
     */
850
    public function setAccessEndDate($accessEndDate)
851
    {
852
        $this->accessEndDate = $accessEndDate;
853
854
        return $this;
855
    }
856
857
    /**
858
     * Get accessEndDate.
859
     *
860
     * @return \DateTime
861
     */
862
    public function getAccessEndDate()
863
    {
864
        return $this->accessEndDate;
865
    }
866
867
    /**
868
     * Set coachAccessStartDate.
869
     *
870
     * @param \DateTime $coachAccessStartDate
871
     *
872
     * @return Session
873
     */
874
    public function setCoachAccessStartDate($coachAccessStartDate)
875
    {
876
        $this->coachAccessStartDate = $coachAccessStartDate;
877
878
        return $this;
879
    }
880
881
    /**
882
     * Get coachAccessStartDate.
883
     *
884
     * @return \DateTime
885
     */
886
    public function getCoachAccessStartDate()
887
    {
888
        return $this->coachAccessStartDate;
889
    }
890
891
    /**
892
     * Set coachAccessEndDate.
893
     *
894
     * @param \DateTime $coachAccessEndDate
895
     *
896
     * @return Session
897
     */
898
    public function setCoachAccessEndDate($coachAccessEndDate)
899
    {
900
        $this->coachAccessEndDate = $coachAccessEndDate;
901
902
        return $this;
903
    }
904
905
    /**
906
     * Get coachAccessEndDate.
907
     *
908
     * @return \DateTime
909
     */
910
    public function getCoachAccessEndDate()
911
    {
912
        return $this->coachAccessEndDate;
913
    }
914
915
    /**
916
     * @return User
917
     */
918
    public function getGeneralCoach()
919
    {
920
        return $this->generalCoach;
921
    }
922
923
    /**
924
     * @param $coach
925
     *
926
     * @return $this
927
     */
928
    public function setGeneralCoach($coach)
929
    {
930
        $this->generalCoach = $coach;
931
932
        return $this;
933
    }
934
935
    /**
936
     * @return SessionCategory
937
     */
938
    public function getCategory()
939
    {
940
        return $this->category;
941
    }
942
943
    /**
944
     * @param $category
945
     *
946
     * @return $this
947
     */
948
    public function setCategory($category)
949
    {
950
        $this->category = $category;
951
952
        return $this;
953
    }
954
955
    public static function getStatusList(): array
956
    {
957
        return [
958
            self::VISIBLE => 'status_visible',
959
            self::READ_ONLY => 'status_read_only',
960
            self::INVISIBLE => 'status_invisible',
961
            self::AVAILABLE => 'status_available',
962
        ];
963
    }
964
965
    /**
966
     * Check if session is visible.
967
     */
968
    public function isActive(): bool
969
    {
970
        $now = new \Datetime('now');
971
972
        if ($now > $this->getAccessStartDate()) {
973
            return true;
974
        }
975
976
        return false;
977
    }
978
979
    public function isActiveForStudent(): bool
980
    {
981
        $start = $this->getAccessStartDate();
982
        $end = $this->getAccessEndDate();
983
984
        return $this->compareDates($start, $end);
985
    }
986
987
    /**
988
     * @return bool
989
     */
990
    public function isActiveForCoach()
991
    {
992
        $start = $this->getCoachAccessStartDate();
993
        $end = $this->getCoachAccessEndDate();
994
995
        return $this->compareDates($start, $end);
996
    }
997
998
    /**
999
     * Compare the current date with start and end access dates.
1000
     * Either missing date is interpreted as no limit.
1001
     *
1002
     * @return bool whether now is between the session access start and end dates
1003
     */
1004
    public function isCurrentlyAccessible()
1005
    {
1006
        try {
1007
            $now = new \Datetime();
1008
        } catch (\Exception $exception) {
1009
            return false;
1010
        }
1011
1012
        return (is_null($this->accessStartDate) || $this->accessStartDate < $now)
1013
            && (is_null($this->accessEndDate) || $now < $this->accessEndDate);
1014
    }
1015
1016
    public function addCourse(Course $course)
1017
    {
1018
        $entity = new SessionRelCourse();
1019
        $entity->setCourse($course);
1020
        $this->addCourses($entity);
1021
    }
1022
1023
    /**
1024
     * Removes a course from this session.
1025
     *
1026
     * @param Course $course the course to remove from this session
1027
     *
1028
     * @return bool whether the course was actually found in this session and removed from it
1029
     */
1030
    public function removeCourse(Course $course)
1031
    {
1032
        $relCourse = $this->getCourseSubscription($course);
1033
        if ($relCourse) {
0 ignored issues
show
introduced by
$relCourse is of type Chamilo\CoreBundle\Entity\SessionRelCourse, thus it always evaluated to true.
Loading history...
1034
            $this->courses->removeElement($relCourse);
1035
            $this->setNbrCourses(count($this->courses));
1036
1037
            return true;
1038
        }
1039
1040
        return false;
1041
    }
1042
1043
    /**
1044
     * @return ArrayCollection
1045
     */
1046
    public function getUserCourseSubscriptions()
1047
    {
1048
        return $this->userCourseSubscriptions;
1049
    }
1050
1051
    /**
1052
     * @param ArrayCollection $userCourseSubscriptions
1053
     *
1054
     * @return $this
1055
     */
1056
    public function setUserCourseSubscriptions($userCourseSubscriptions)
1057
    {
1058
        $this->userCourseSubscriptions = new ArrayCollection();
1059
1060
        foreach ($userCourseSubscriptions as $item) {
1061
            $this->addUserCourseSubscription($item);
1062
        }
1063
1064
        return $this;
1065
    }
1066
1067
    public function addUserCourseSubscription(SessionRelCourseRelUser $subscription)
1068
    {
1069
        $subscription->setSession($this);
1070
        if (!$this->hasUserCourseSubscription($subscription)) {
1071
            $this->userCourseSubscriptions[] = $subscription;
1072
        }
1073
    }
1074
1075
    /**
1076
     * @return SessionRelCourse
1077
     */
1078
    public function getCourseSubscription(Course $course)
1079
    {
1080
        $criteria = Criteria::create()->where(
1081
            Criteria::expr()->eq('course', $course)
1082
        );
1083
1084
        /** @var SessionRelCourse $sessionCourse */
1085
        return $this->courses
1086
            ->matching($criteria)
1087
            ->current();
1088
    }
1089
1090
    /**
1091
     * Add a user course subscription.
1092
     * If user status in session is student, then increase number of course users.
1093
     *
1094
     * @param int $status
1095
     */
1096
    public function addUserInCourse($status, User $user, Course $course)
1097
    {
1098
        $userRelCourseRelSession = new SessionRelCourseRelUser();
1099
        $userRelCourseRelSession->setCourse($course);
1100
        $userRelCourseRelSession->setUser($user);
1101
        $userRelCourseRelSession->setSession($this);
1102
        $userRelCourseRelSession->setStatus($status);
1103
        $this->addUserCourseSubscription($userRelCourseRelSession);
1104
1105
        if (self::STUDENT === $status) {
1106
            $sessionCourse = $this->getCourseSubscription($course);
1107
1108
            $sessionCourse->setNbrUsers(
1109
                $sessionCourse->getNbrUsers() + 1
1110
            );
1111
        }
1112
    }
1113
1114
    /**
1115
     * @return bool
1116
     */
1117
    public function hasUserCourseSubscription(SessionRelCourseRelUser $subscription)
1118
    {
1119
        if ($this->getUserCourseSubscriptions()->count()) {
1120
            $criteria = Criteria::create()->where(
1121
                Criteria::expr()->eq('user', $subscription->getUser())
1122
            )->andWhere(
1123
                Criteria::expr()->eq('course', $subscription->getCourse())
1124
            )->andWhere(
1125
                Criteria::expr()->eq('session', $subscription->getSession())
1126
            );
1127
            $relation = $this->getUserCourseSubscriptions()->matching($criteria);
1128
1129
            return $relation->count() > 0;
1130
        }
1131
1132
        return false;
1133
    }
1134
1135
    /**
1136
     * currentCourse is set in CourseListener.
1137
     *
1138
     * @return Course
1139
     */
1140
    public function getCurrentCourse()
1141
    {
1142
        return $this->currentCourse;
1143
    }
1144
1145
    /**
1146
     * currentCourse is set in CourseListener.
1147
     *
1148
     * @return $this
1149
     */
1150
    public function setCurrentCourse(Course $course)
1151
    {
1152
        // If the session is registered in the course session list.
1153
        $exists = $this->getCourses()->exists(
1154
            function ($key, $element) use ($course) {
1155
                /** @var SessionRelCourse $element */
1156
                return $course->getId() == $element->getCourse()->getId();
1157
            }
1158
        );
1159
1160
        if ($exists) {
1161
            $this->currentCourse = $course;
1162
        }
1163
1164
        return $this;
1165
    }
1166
1167
    /**
1168
     * Set $sendSubscriptionNotification.
1169
     *
1170
     * @param bool $sendNotification
1171
     *
1172
     * @return Session
1173
     */
1174
    public function setSendSubscriptionNotification($sendNotification)
1175
    {
1176
        $this->sendSubscriptionNotification = $sendNotification;
1177
1178
        return $this;
1179
    }
1180
1181
    /**
1182
     * Get $sendSubscriptionNotification.
1183
     *
1184
     * @return bool
1185
     */
1186
    public function getSendSubscriptionNotification()
1187
    {
1188
        return $this->sendSubscriptionNotification;
1189
    }
1190
1191
    /**
1192
     * Get user from course by status.
1193
     *
1194
     * @param int $status
1195
     *
1196
     * @return \Doctrine\Common\Collections\ArrayCollection|\Doctrine\Common\Collections\Collection
1197
     */
1198
    public function getUserCourseSubscriptionsByStatus(Course $course, $status)
1199
    {
1200
        $criteria = Criteria::create()
1201
            ->where(
1202
                Criteria::expr()->eq('course', $course)
1203
            )
1204
            ->andWhere(
1205
                Criteria::expr()->eq('status', $status)
1206
            );
1207
1208
        return $this->userCourseSubscriptions->matching($criteria);
1209
    }
1210
1211
    /**
1212
     * @return Session
1213
     */
1214
    public function setStudentPublications(Collection $studentPublications)
1215
    {
1216
        $this->studentPublications = new ArrayCollection();
1217
1218
        foreach ($studentPublications as $studentPublication) {
1219
            $this->addStudentPublication($studentPublication);
1220
        }
1221
1222
        return $this;
1223
    }
1224
1225
    /**
1226
     * @return Session
1227
     */
1228
    public function addStudentPublication(CStudentPublication $studentPublication)
1229
    {
1230
        $this->studentPublications[] = $studentPublication;
1231
1232
        return $this;
1233
    }
1234
1235
    /**
1236
     * Get studentPublications.
1237
     *
1238
     * @return ArrayCollection
1239
     */
1240
    public function getStudentPublications()
1241
    {
1242
        return $this->studentPublications;
1243
    }
1244
1245
    /**
1246
     * Get issuedSkills.
1247
     *
1248
     * @return ArrayCollection
1249
     */
1250
    public function getIssuedSkills()
1251
    {
1252
        return $this->issuedSkills;
1253
    }
1254
1255
    /**
1256
     * @return $this
1257
     */
1258
    public function setCurrentUrl(AccessUrl $url)
1259
    {
1260
        $urlList = $this->getUrls();
1261
        /** @var AccessUrlRelCourse $item */
1262
        foreach ($urlList as $item) {
1263
            if ($item->getUrl()->getId() == $url->getId()) {
1264
                $this->currentUrl = $url;
1265
1266
                break;
1267
            }
1268
        }
1269
1270
        return $this;
1271
    }
1272
1273
    /**
1274
     * @return AccessUrl
1275
     */
1276
    public function getCurrentUrl()
1277
    {
1278
        return $this->currentUrl;
1279
    }
1280
1281
    /**
1282
     * @return ArrayCollection
1283
     */
1284
    public function getUrls()
1285
    {
1286
        return $this->urls;
1287
    }
1288
1289
    /**
1290
     * @param $urls
1291
     */
1292
    public function setUrls($urls)
1293
    {
1294
        $this->urls = new ArrayCollection();
1295
1296
        foreach ($urls as $url) {
1297
            $this->addUrls($url);
1298
        }
1299
    }
1300
1301
    public function addUrl(AccessUrl $url)
1302
    {
1303
        $accessUrlRelSession = new AccessUrlRelSession();
1304
        $accessUrlRelSession->setUrl($url);
1305
        $accessUrlRelSession->setSession($this);
1306
1307
        $this->addUrls($accessUrlRelSession);
1308
    }
1309
1310
    public function addUrls(AccessUrlRelSession $url)
1311
    {
1312
        $url->setSession($this);
1313
        $this->urls[] = $url;
1314
    }
1315
1316
    /**
1317
     * @return int
1318
     */
1319
    public function getPosition()
1320
    {
1321
        return $this->position;
1322
    }
1323
1324
    /**
1325
     * @param int $position
1326
     *
1327
     * @return Session
1328
     */
1329
    public function setPosition($position)
1330
    {
1331
        $this->position = $position;
1332
1333
        return $this;
1334
    }
1335
1336
    public function getStatus(): int
1337
    {
1338
        return $this->status;
1339
    }
1340
1341
    public function setStatus(int $status): self
1342
    {
1343
        $this->status = $status;
1344
1345
        return $this;
1346
    }
1347
1348
    public function getSessionAdmin(): User
1349
    {
1350
        return $this->sessionAdmin;
1351
    }
1352
1353
    public function setSessionAdmin(User $sessionAdmin): self
1354
    {
1355
        $this->sessionAdmin = $sessionAdmin;
1356
1357
        return $this;
1358
    }
1359
1360
    public function isUserGeneralCoach(User $user): bool
1361
    {
1362
        $generalCoach = $this->getGeneralCoach();
1363
1364
        if (!$generalCoach) {
0 ignored issues
show
introduced by
$generalCoach is of type Chamilo\CoreBundle\Entity\User, thus it always evaluated to true.
Loading history...
1365
            return false;
1366
        }
1367
1368
        if ($user->getId() === $generalCoach->getId()) {
1369
            return true;
1370
        }
1371
1372
        return false;
1373
    }
1374
1375
    /**
1376
     * Check if $user is course coach in any course.
1377
     *
1378
     * @return bool
1379
     */
1380
    public function hasCoachInCourseList(User $user)
1381
    {
1382
        /** @var SessionRelCourse $sessionCourse */
1383
        foreach ($this->courses as $sessionCourse) {
1384
            if ($this->hasCoachInCourseWithStatus($user, $sessionCourse->getCourse())) {
1385
                return true;
1386
            }
1387
        }
1388
1389
        return false;
1390
    }
1391
1392
    /**
1393
     * Check if $user is student in any course.
1394
     *
1395
     * @return bool
1396
     */
1397
    public function hasStudentInCourseList(User $user)
1398
    {
1399
        /** @var SessionRelCourse $sessionCourse */
1400
        foreach ($this->courses as $sessionCourse) {
1401
            if ($this->hasStudentInCourse($user, $sessionCourse->getCourse())) {
1402
                return true;
1403
            }
1404
        }
1405
1406
        return false;
1407
    }
1408
1409
    /**
1410
     * @param \DateTime $start
1411
     * @param \DateTime $end
1412
     */
1413
    protected function compareDates($start, $end): bool
1414
    {
1415
        $now = new \Datetime('now');
1416
1417
        if (!empty($start) && !empty($end)) {
1418
            if ($now >= $start && $now <= $end) {
1419
                return true;
1420
            }
1421
        }
1422
1423
        if (!empty($start)) {
1424
            if ($now >= $start) {
1425
                return true;
1426
            }
1427
        }
1428
1429
        if (!empty($end)) {
1430
            if ($now <= $end) {
1431
                return true;
1432
            }
1433
        }
1434
1435
        return false;
1436
    }
1437
}
1438