Completed
Push — master ( 0dfc19...c58ed8 )
by Julito
10:02
created

Session::removeCourses()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
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
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\SearchFilter;
10
use Chamilo\CourseBundle\Entity\CStudentPublication;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Collection;
13
use Doctrine\Common\Collections\Criteria;
14
use Doctrine\ORM\Mapping as ORM;
15
use Symfony\Component\Serializer\Annotation\Groups;
16
use Symfony\Component\Validator\Constraints as Assert;
17
18
/**
19
 * @ApiResource(
20
 *     normalizationContext={"groups"={"session:read"}, "swagger_definition_name"="Read"},
21
 *     denormalizationContext={"groups"={"session:write"}},
22
 * )
23
 *
24
 * @ApiFilter(SearchFilter::class, properties={"name": "partial"})
25
 * @ORM\Table(
26
 *      name="session",
27
 *      uniqueConstraints={@ORM\UniqueConstraint(name="name", columns={"name"})},
28
 *      indexes={
29
 *          @ORM\Index(name="idx_id_coach", columns={"id_coach"}),
30
 *          @ORM\Index(name="idx_id_session_admin_id", columns={"session_admin_id"})
31
 *      }
32
 * )
33
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\SessionListener"})
34
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\SessionRepository")
35
 */
36
class Session
37
{
38
    public const VISIBLE = 1;
39
    public const READ_ONLY = 2;
40
    public const INVISIBLE = 3;
41
    public const AVAILABLE = 4;
42
43
    public const STUDENT = 0;
44
    public const DRH = 1;
45
    public const COACH = 2;
46
47
    /**
48
     * @var int
49
     * @Groups({"session:read"})
50
     * @ORM\Column(name="id", type="integer", nullable=false, unique=false)
51
     * @ORM\Id
52
     * @ORM\GeneratedValue(strategy="AUTO")
53
     */
54
    protected $id;
55
56
    /**
57
     * @ORM\OneToMany(
58
     *     targetEntity="SkillRelCourse", mappedBy="session", cascade={"persist", "remove"}
59
     * )
60
     */
61
    protected $skills;
62
63
    /**
64
     * @var ArrayCollection
65
     *
66
     * @ORM\OrderBy({"position" = "ASC"})
67
     * @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
68
     */
69
    protected $courses;
70
71
    /**
72
     * @var ArrayCollection
73
     *
74
     * @ORM\OneToMany(targetEntity="SessionRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
75
     */
76
    protected $users;
77
78
    /**
79
     * @var ArrayCollection
80
     *
81
     * @ORM\OneToMany(
82
     *     targetEntity="SessionRelCourseRelUser",
83
     *     mappedBy="session",
84
     *     cascade={"persist"},
85
     *     orphanRemoval=true
86
     * )
87
     */
88
    protected $userCourseSubscriptions;
89
90
    /**
91
     * @var Course
92
     */
93
    protected $currentCourse;
94
95
    /**
96
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="session", cascade={"persist"})
97
     */
98
    protected $issuedSkills;
99
100
    /**
101
     * @ORM\OneToMany(
102
     *     targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelSession",
103
     *     mappedBy="session",
104
     *     cascade={"persist"}, orphanRemoval=true
105
     * )
106
     */
107
    protected $urls;
108
109
    /**
110
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="session", cascade={"remove"}, orphanRemoval=true)
111
     */
112
    protected $resourceLinks;
113
114
    /**
115
     * @var AccessUrl
116
     */
117
    protected $currentUrl;
118
119
    /**
120
     * @var string
121
     * @Assert\NotBlank()
122
     * @Groups({"session:read", "session:write"})
123
     * @ORM\Column(name="name", type="string", length=150, nullable=false, unique=false)
124
     */
125
    protected $name;
126
127
    /**
128
     * @var string
129
     * @Groups({"session:read", "session:write"})
130
     *
131
     * @ORM\Column(name="description", type="text", nullable=true, unique=false)
132
     */
133
    protected $description;
134
135
    /**
136
     * @var bool
137
     * @Groups({"session:read", "session:write"})
138
     *
139
     * @ORM\Column(name="show_description", type="boolean", nullable=true)
140
     */
141
    protected $showDescription;
142
143
    /**
144
     * @var int
145
     *
146
     * @ORM\Column(name="duration", type="integer", nullable=true)
147
     */
148
    protected $duration;
149
150
    /**
151
     * @var int
152
     * @Groups({"session:read"})
153
     * @ORM\Column(name="nbr_courses", type="smallint", nullable=true, unique=false)
154
     */
155
    protected $nbrCourses;
156
157
    /**
158
     * @var int
159
     * @Groups({"session:read"})
160
     * @ORM\Column(name="nbr_users", type="integer", nullable=true, unique=false)
161
     */
162
    protected $nbrUsers;
163
164
    /**
165
     * @var int
166
     * @Groups({"session:read"})
167
     * @ORM\Column(name="nbr_classes", type="integer", nullable=true, unique=false)
168
     */
169
    protected $nbrClasses;
170
171
    /**
172
     * @var User
173
     *
174
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="receivedMessages")
175
     * @ORM\JoinColumn(name="session_admin_id", referencedColumnName="id", nullable=true)
176
     */
177
    protected $sessionAdmin;
178
179
    /**
180
     * @var int
181
     * @Groups({"session:read"})
182
     * @ORM\Column(name="visibility", type="integer", nullable=false, unique=false)
183
     */
184
    protected $visibility;
185
186
    /**
187
     * @var int
188
     *
189
     * @ORM\Column(name="promotion_id", type="integer", nullable=true, unique=false)
190
     */
191
    protected $promotionId;
192
193
    /**
194
     * @var \DateTime
195
     * @Groups({"session:read"})
196
     * @ORM\Column(name="display_start_date", type="datetime", nullable=true, unique=false)
197
     */
198
    protected $displayStartDate;
199
200
    /**
201
     * @var \DateTime
202
     * @Groups({"session:read"})
203
     * @ORM\Column(name="display_end_date", type="datetime", nullable=true, unique=false)
204
     */
205
    protected $displayEndDate;
206
207
    /**
208
     * @var \DateTime
209
     *
210
     * @ORM\Column(name="access_start_date", type="datetime", nullable=true, unique=false)
211
     */
212
    protected $accessStartDate;
213
214
    /**
215
     * @var \DateTime
216
     *
217
     * @ORM\Column(name="access_end_date", type="datetime", nullable=true, unique=false)
218
     */
219
    protected $accessEndDate;
220
221
    /**
222
     * @var \DateTime
223
     *
224
     * @ORM\Column(name="coach_access_start_date", type="datetime", nullable=true, unique=false)
225
     */
226
    protected $coachAccessStartDate;
227
228
    /**
229
     * @var \DateTime
230
     *
231
     * @ORM\Column(name="coach_access_end_date", type="datetime", nullable=true, unique=false)
232
     */
233
    protected $coachAccessEndDate;
234
235
    /**
236
     * @var int
237
     *
238
     * @ORM\Column(name="position", type="integer", nullable=false, options={"default":0})
239
     */
240
    protected $position;
241
242
    /**
243
     * @var int
244
     * @Groups({"session:read"})
245
     *
246
     * @ORM\Column(name="status", type="integer", nullable=false)
247
     */
248
    protected $status;
249
250
    /**
251
     * @var User
252
     * @Groups({"session:read", "session:write"})
253
     *
254
     * @ORM\ManyToOne(targetEntity="User", inversedBy="sessionAsGeneralCoach")
255
     * @ORM\JoinColumn(name="id_coach", referencedColumnName="id")
256
     */
257
    protected $generalCoach;
258
259
    /**
260
     * @Groups({"session:read", "session:write"})
261
     * @ORM\ManyToOne(targetEntity="SessionCategory", inversedBy="session")
262
     * @ORM\JoinColumn(name="session_category_id", referencedColumnName="id")
263
     */
264
    protected $category;
265
266
    /**
267
     * @var bool
268
     *
269
     * @ORM\Column(name="send_subscription_notification", type="boolean", nullable=false, options={"default":false})
270
     */
271
    protected $sendSubscriptionNotification;
272
273
    /**
274
     * @var ArrayCollection
275
     *
276
     * @ORM\OneToMany(
277
     *     targetEntity="Chamilo\CourseBundle\Entity\CStudentPublication",
278
     *     mappedBy="session",
279
     *     cascade={"persist"},
280
     *     orphanRemoval=true
281
     * )
282
     */
283
    protected $studentPublications;
284
285
    /**
286
     * Constructor.
287
     */
288
    public function __construct()
289
    {
290
        $this->items = new ArrayCollection();
0 ignored issues
show
Bug Best Practice introduced by
The property items does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
291
        $this->urls = new ArrayCollection();
292
293
        $this->nbrClasses = 0;
294
        $this->nbrUsers = 0;
295
296
        $this->displayStartDate = new \DateTime();
297
        $this->displayEndDate = new \DateTime();
298
        $this->accessStartDate = new \DateTime();
299
        $this->accessEndDate = new \DateTime();
300
        $this->coachAccessStartDate = new \DateTime();
301
        $this->coachAccessEndDate = new \DateTime();
302
        $this->visibility = 1;
303
304
        $this->courses = new ArrayCollection();
305
        $this->users = new ArrayCollection();
306
        $this->userCourseSubscriptions = new ArrayCollection();
307
        $this->showDescription = false;
308
        $this->category = null;
309
        $this->status = 0;
310
        $this->studentPublications = new ArrayCollection();
311
    }
312
313
    /**
314
     * @return string
315
     */
316
    public function __toString()
317
    {
318
        return (string) $this->getName();
319
    }
320
321
    /**
322
     * @return int
323
     */
324
    public function getDuration()
325
    {
326
        return $this->duration;
327
    }
328
329
    /**
330
     * @param int $duration
331
     *
332
     * @return $this
333
     */
334
    public function setDuration($duration)
335
    {
336
        $this->duration = $duration;
337
338
        return $this;
339
    }
340
341
    /**
342
     * @return string
343
     */
344
    public function getShowDescription()
345
    {
346
        return $this->showDescription;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->showDescription returns the type boolean which is incompatible with the documented return type string.
Loading history...
347
    }
348
349
    /**
350
     * @param string $showDescription
351
     *
352
     * @return $this
353
     */
354
    public function setShowDescription($showDescription)
355
    {
356
        $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...
357
358
        return $this;
359
    }
360
361
    /**
362
     * Get id.
363
     *
364
     * @return int
365
     */
366
    public function getId()
367
    {
368
        return $this->id;
369
    }
370
371
    /**
372
     * @param int $id
373
     */
374
    public function setId($id)
375
    {
376
        $this->id = $id;
377
    }
378
379
    /**
380
     * @return ArrayCollection
381
     */
382
    public function getUsers()
383
    {
384
        return $this->users;
385
    }
386
387
    /**
388
     * @param $users
389
     *
390
     * @return $this
391
     */
392
    public function setUsers($users)
393
    {
394
        $this->users = new ArrayCollection();
395
396
        foreach ($users as $user) {
397
            $this->addUser($user);
398
        }
399
400
        return $this;
401
    }
402
403
    public function addUser(SessionRelUser $user)
404
    {
405
        $user->setSession($this);
406
407
        if (!$this->hasUser($user)) {
408
            $this->users[] = $user;
409
        }
410
    }
411
412
    /**
413
     * @param int $status
414
     */
415
    public function addUserInSession($status, User $user)
416
    {
417
        $sessionRelUser = new SessionRelUser();
418
        $sessionRelUser->setSession($this);
419
        $sessionRelUser->setUser($user);
420
        $sessionRelUser->setRelationType($status);
421
422
        $this->addUser($sessionRelUser);
423
    }
424
425
    /**
426
     * @return bool
427
     */
428
    public function hasUser(SessionRelUser $subscription)
429
    {
430
        if ($this->getUsers()->count()) {
431
            $criteria = Criteria::create()->where(
432
                Criteria::expr()->eq('user', $subscription->getUser())
433
            )->andWhere(
434
                Criteria::expr()->eq('session', $subscription->getSession())
435
            )->andWhere(
436
                Criteria::expr()->eq('relationType', $subscription->getRelationType())
437
            );
438
439
            $relation = $this->getUsers()->matching($criteria);
440
441
            return $relation->count() > 0;
442
        }
443
444
        return false;
445
    }
446
447
    /**
448
     * @return ArrayCollection
449
     */
450
    public function getCourses()
451
    {
452
        return $this->courses;
453
    }
454
455
    /**
456
     * @param $courses
457
     */
458
    public function setCourses(ArrayCollection $courses)
459
    {
460
        $this->courses = new ArrayCollection();
461
462
        foreach ($courses as $course) {
463
            $this->addCourses($course);
464
        }
465
    }
466
467
    public function addCourses(SessionRelCourse $course)
468
    {
469
        $course->setSession($this);
470
        $this->courses[] = $course;
471
    }
472
473
    /**
474
     * @return bool
475
     */
476
    public function hasCourse(Course $course)
477
    {
478
        if ($this->getCourses()->count()) {
479
            $criteria = Criteria::create()->where(
480
                Criteria::expr()->eq('course', $course)
481
            );
482
            $relation = $this->getCourses()->matching($criteria);
483
484
            return $relation->count() > 0;
485
        }
486
487
        return false;
488
    }
489
490
    /**
491
     * Remove $course.
492
     *
493
     * @param SessionRelCourse $course
494
     */
495
    public function removeCourses($course)
496
    {
497
        foreach ($this->courses as $key => $value) {
498
            if ($value->getId() == $course->getId()) {
499
                unset($this->courses[$key]);
500
            }
501
        }
502
    }
503
504
    /**
505
     * Remove course subscription for a user.
506
     * If user status in session is student, then decrease number of course users.
507
     */
508
    public function removeUserCourseSubscription(User $user, Course $course)
509
    {
510
        /** @var SessionRelCourseRelUser $courseSubscription */
511
        foreach ($this->userCourseSubscriptions as $i => $courseSubscription) {
512
            if ($courseSubscription->getCourse()->getId() === $course->getId() &&
513
                $courseSubscription->getUser()->getId() === $user->getId()) {
514
                if (self::STUDENT === $this->userCourseSubscriptions[$i]->getStatus()) {
515
                    $sessionCourse = $this->getCourseSubscription($course);
516
517
                    $sessionCourse->setNbrUsers(
518
                        $sessionCourse->getNbrUsers() - 1
519
                    );
520
                }
521
522
                unset($this->userCourseSubscriptions[$i]);
523
            }
524
        }
525
    }
526
527
    /**
528
     * @param int $status if not set it will check if the user is registered
529
     *                    with any status
530
     */
531
    public function hasUserInCourse(User $user, Course $course, $status = null): bool
532
    {
533
        $relation = $this->getUserInCourse($user, $course, $status);
534
535
        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

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