Completed
Push — master ( 27e209...a08afa )
by Julito
186:04 queued 150:53
created

Session::setCurrentUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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

472
                if ($this->userCourseSubscriptions[$i]->/** @scrutinizer ignore-call */ getStatus() === self::STUDENT) {

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...
473
                    $sessionCourse = $this->getCourseSubscription($course);
474
475
                    $sessionCourse->setNbrUsers(
476
                        $sessionCourse->getNbrUsers() - 1
477
                    );
478
                }
479
480
                unset($this->userCourseSubscriptions[$i]);
481
            }
482
        }
483
    }
484
485
    /**
486
     * @param User $user
487
     * @param Course $course
488
     * @param int $status if not set it will check if the user is registered
489
     * with any status
490
     *
491
     * @return bool
492
     */
493
    public function hasUserInCourse(User $user, Course $course, $status = null)
494
    {
495
        $relation = $this->getUserInCourse($user, $course, $status);
496
497
        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

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