Passed
Push — master ( 3be753...7af7b8 )
by Julito
10:23
created

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

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