Completed
Push — master ( 3fdf06...e648ff )
by Julito
33:51
created

Session::isActiveForStudent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
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 string
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
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CItemProperty", mappedBy="session")
164
     **/
165
    //private $items;
166
167
    /**
168
     * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="sessionAsGeneralCoach")
169
     * @ORM\JoinColumn(name="id_coach", referencedColumnName="id")
170
     **/
171
    private $generalCoach;
172
173
    /**
174
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\SessionCategory", inversedBy="session")
175
     * @ORM\JoinColumn(name="session_category_id", referencedColumnName="id")
176
     **/
177
    private $category;
178
179
    /**
180
     * @var ArrayCollection
181
     * @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
182
     **/
183
    protected $courses;
184
185
    /**
186
     * @var ArrayCollection
187
     * @ORM\OneToMany(targetEntity="SessionRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
188
     **/
189
    protected $users;
190
191
    /**
192
     * @var ArrayCollection
193
     * @ORM\OneToMany(targetEntity="SessionRelCourseRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
194
     **/
195
    protected $userCourseSubscriptions;
196
197
    /**
198
     * @var Course
199
     **/
200
    protected $currentCourse;
201
202
    /**
203
     * @var boolean
204
     * @ORM\Column(name="send_subscription_notification", type="boolean", nullable=false, options={"default":false})
205
     */
206
    private $sendSubscriptionNotification;
207
208
    /**
209
     * @var ArrayCollection
210
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CStudentPublication", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
211
     */
212
    private $studentPublications;
213
214
    /**
215
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="session", cascade={"persist"})
216
     */
217
    protected $issuedSkills;
218
219
    /**
220
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelSession", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
221
     **/
222
    protected $urls;
223
224
    /**
225
     * @var AccessUrl
226
     **/
227
    protected $currentUrl;
228
229
    /**
230
     * Constructor
231
     */
232
    public function __construct()
233
    {
234
        $this->items = new ArrayCollection();
235
        $this->urls = new ArrayCollection();
236
237
        $this->nbrClasses = 0;
238
        $this->nbrUsers = 0;
239
        $this->nbrUsers = 0;
240
241
        $this->displayStartDate = new \DateTime();
242
        $this->displayEndDate = new \DateTime();
243
        $this->accessStartDate = new \DateTime();
244
        $this->accessEndDate = new \DateTime();
245
        $this->coachAccessStartDate = new \DateTime();
246
        $this->coachAccessEndDate = new \DateTime();
247
        $this->visibility = 1;
248
249
        $this->courses = new ArrayCollection();
250
        $this->users = new ArrayCollection();
251
        $this->userCourseSubscriptions = new ArrayCollection();
252
        $this->showDescription = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $showDescription was declared of type string, but 0 is of type integer. 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...
253
        $this->category = null;
254
        $this->studentPublications = new ArrayCollection();
255
    }
256
257
    /**
258
     * @return int
259
     */
260
    public function getDuration()
261
    {
262
        return $this->duration;
263
    }
264
265
    /**
266
     * @param int $duration
267
     */
268
    public function setDuration($duration)
269
    {
270
        $this->duration = $duration;
271
    }
272
273
    /**
274
     * @return string
275
     */
276
    public function getShowDescription()
277
    {
278
        return $this->showDescription;
279
    }
280
281
    /**
282
     * @param string $showDescription
283
     * @return $this
284
     */
285
    public function setShowDescription($showDescription)
286
    {
287
        $this->showDescription = $showDescription;
288
289
        return $this;
290
    }
291
292
    /**
293
     * @return string
294
     */
295
    public function __toString()
296
    {
297
        return (string) $this->getName();
298
    }
299
300
    /**
301
     * Get id
302
     *
303
     * @return integer
304
     */
305
    public function getId()
306
    {
307
        return $this->id;
308
    }
309
310
    /**
311
     * @param int $id
312
     */
313
    public function setId($id)
314
    {
315
        $this->id = $id;
316
    }
317
318
    /**
319
     * @return ArrayCollection
320
     */
321
    public function getUsers()
322
    {
323
        return $this->users;
324
    }
325
326
    /**
327
     * @param $users
328
     * @return $this
329
     */
330
    public function setUsers($users)
331
    {
332
        $this->users = new ArrayCollection();
333
334
        foreach ($users as $user) {
335
            $this->addUser($user);
336
        }
337
338
        return $this;
339
    }
340
341
    /**
342
     * @param SessionRelUser $user
343
     */
344
    public function addUser(SessionRelUser $user)
345
    {
346
        $user->setSession($this);
347
348
        if (!$this->hasUser($user)) {
349
            $this->users[] = $user;
350
        }
351
    }
352
353
    /**
354
     * @param int $status
355
     * @param User $user
356
     */
357
    public function addUserInSession($status, User $user)
358
    {
359
        $sessionRelUser = new SessionRelUser();
360
        $sessionRelUser->setSession($this);
361
        $sessionRelUser->setUser($user);
362
        $sessionRelUser->setRelationType($status);
363
364
        $this->addUser($sessionRelUser);
365
    }
366
367
    /**
368
     * @param SessionRelUser $subscription
369
     * @return bool
370
     */
371 View Code Duplication
    public function hasUser(SessionRelUser $subscription)
372
    {
373
        if ($this->getUsers()->count()) {
374
            $criteria = Criteria::create()->where(
375
                Criteria::expr()->eq("user", $subscription->getUser())
376
            )->andWhere(
377
                Criteria::expr()->eq("session", $subscription->getSession())
378
            )->andWhere(
379
                Criteria::expr()->eq("relationType", $subscription->getRelationType())
380
            );
381
382
            $relation = $this->getUsers()->matching($criteria);
383
384
            return $relation->count() > 0;
385
        }
386
387
        return false;
388
    }
389
390
    /**
391
     * @return ArrayCollection
392
     */
393
    public function getCourses()
394
    {
395
        return $this->courses;
396
    }
397
398
    /**
399
     * @param $courses
400
     */
401
    public function setCourses($courses)
402
    {
403
        $this->courses = new ArrayCollection();
404
405
        foreach ($courses as $course) {
406
            $this->addCourses($course);
407
        }
408
    }
409
410
    /**
411
     * @param SessionRelCourse $course
412
     */
413
    public function addCourses(SessionRelCourse $course)
414
    {
415
        $course->setSession($this);
416
        $this->courses[] = $course;
417
    }
418
419
    /**
420
     * @param Course $course
421
     *
422
     * @return bool
423
     */
424
    public function hasCourse(Course $course)
425
    {
426
        if ($this->getCourses()->count()) {
427
            $criteria = Criteria::create()->where(
428
                Criteria::expr()->eq("course", $course)
429
            );
430
            $relation = $this->getCourses()->matching($criteria);
431
432
            return $relation->count() > 0;
433
        }
434
435
        return false;
436
    }
437
438
439
    /**
440
     * Remove $course
441
     *
442
     * @param SessionRelCourse $course
443
     */
444
    public function removeCourses($course)
445
    {
446
        foreach ($this->courses as $key => $value) {
447
            if ($value->getId() == $course->getId()) {
448
                unset($this->courses[$key]);
449
            }
450
        }
451
    }
452
453
    /**
454
     * @param User $user
455
     * @param Course $course
456
     * @param int $status if not set it will check if the user is registered
457
     * with any status
458
     *
459
     * @return bool
460
     */
461
    public function hasUserInCourse(User $user, Course $course, $status = null)
462
    {
463
        $relation = $this->getUserInCourse($user, $course, $status);
464
465
        return $relation->count() > 0;
0 ignored issues
show
Bug introduced by
The method count does only exist in Doctrine\Common\Collections\Collection, but not in Chamilo\CoreBundle\Entity\Session.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

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