Completed
Push — master ( 94fc66...075f50 )
by Julito
397:03 queued 333:37
created

Session::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
rs 8.9713
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Entity;
5
6
use Chamilo\UserBundle\Entity\User;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Criteria;
9
use Doctrine\ORM\Mapping as ORM;
10
11
//use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
12
//use Gedmo\Mapping\Annotation as Gedmo;
13
14
/**
15
 * Session
16
 * UniqueEntity("name")
17
 * @ORM\Table(
18
 *      name="session",
19
 *      uniqueConstraints={@ORM\UniqueConstraint(name="name", columns={"name"})},
20
 *      indexes={
21
 *          @ORM\Index(name="idx_id_coach", columns={"id_coach"}),
22
 *          @ORM\Index(name="idx_id_session_admin_id", columns={"session_admin_id"})
23
 *      }
24
 * )
25
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\SessionListener"})
26
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Entity\Repository\SessionRepository")
27
 */
28
class Session
29
{
30
    const VISIBLE = 1;
31
    const READ_ONLY = 2;
32
    const INVISIBLE = 3;
33
    const AVAILABLE = 4;
34
35
    const STUDENT = 0;
36
    const DRH = 1;
37
    const COACH = 2;
38
39
    /**
40
     * @var integer
41
     *
42
     * @ORM\Column(name="id", type="integer", nullable=false, unique=false)
43
     * @ORM\Id
44
     * @ORM\GeneratedValue(strategy="AUTO")
45
     */
46
    private $id;
47
48
    /**
49
     * @var string
50
     *
51
     * @ORM\Column(name="name", type="string", length=150, nullable=false, unique=false)
52
     */
53
    private $name;
54
55
    /**
56
     * @var string
57
     *
58
     * @ORM\Column(name="description", type="text", nullable=true, unique=false)
59
     */
60
    private $description;
61
62
    /**
63
     * @var string
64
     *
65
     * @ORM\Column(name="show_description", type="boolean", nullable=true)
66
     */
67
    private $showDescription;
68
69
    /**
70
     * @var integer
71
     *
72
     * @ORM\Column(name="duration", type="integer", nullable=true)
73
     */
74
    private $duration;
75
76
    /**
77
     * @var integer
78
     *
79
     * @ORM\Column(name="nbr_courses", type="smallint", nullable=true, unique=false)
80
     */
81
    private $nbrCourses;
82
83
    /**
84
     * @var integer
85
     *
86
     * @ORM\Column(name="nbr_users", type="integer", nullable=true, unique=false)
87
     */
88
    private $nbrUsers;
89
90
    /**
91
     * @var integer
92
     *
93
     * @ORM\Column(name="nbr_classes", type="integer", nullable=true, unique=false)
94
     */
95
    private $nbrClasses;
96
97
    /**
98
     * @var integer
99
     *
100
     * @ORM\Column(name="session_admin_id", type="integer", nullable=true, unique=false)
101
     */
102
    private $sessionAdminId;
103
104
    /**
105
     * @var integer
106
     *
107
     * @ORM\Column(name="visibility", type="integer", nullable=false, unique=false)
108
     */
109
    private $visibility;
110
111
    /**
112
     * @var integer
113
     *
114
     * @ORM\Column(name="promotion_id", type="integer", nullable=true, unique=false)
115
     */
116
    private $promotionId;
117
118
    /**
119
     * @var \DateTime
120
     *
121
     * @ORM\Column(name="display_start_date", type="datetime", nullable=true, unique=false)
122
     */
123
    private $displayStartDate;
124
125
    /**
126
     * @var \DateTime
127
     *
128
     * @ORM\Column(name="display_end_date", type="datetime", nullable=true, unique=false)
129
     */
130
    private $displayEndDate;
131
132
    /**
133
     * @var \DateTime
134
     *
135
     * @ORM\Column(name="access_start_date", type="datetime", nullable=true, unique=false)
136
     */
137
    private $accessStartDate;
138
139
    /**
140
     * @var \DateTime
141
     *
142
     * @ORM\Column(name="access_end_date", type="datetime", nullable=true, unique=false)
143
     */
144
    private $accessEndDate;
145
146
    /**
147
     * @var \DateTime
148
     *
149
     * @ORM\Column(name="coach_access_start_date", type="datetime", nullable=true, unique=false)
150
     */
151
    private $coachAccessStartDate;
152
153
    /**
154
     * @var \DateTime
155
     *
156
     * @ORM\Column(name="coach_access_end_date", type="datetime", nullable=true, unique=false)
157
     */
158
    private $coachAccessEndDate;
159
160
    /**
161
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CItemProperty", mappedBy="session")
162
     **/
163
    //private $items;
164
165
    /**
166
     * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="sessionAsGeneralCoach")
167
     * @ORM\JoinColumn(name="id_coach", referencedColumnName="id")
168
     **/
169
    private $generalCoach;
170
171
    /**
172
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\SessionCategory", inversedBy="session")
173
     * @ORM\JoinColumn(name="session_category_id", referencedColumnName="id")
174
     **/
175
    private $category;
176
177
    /**
178
     * @var ArrayCollection
179
     * @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
180
     **/
181
    protected $courses;
182
183
    /**
184
     * @var ArrayCollection
185
     * @ORM\OneToMany(targetEntity="SessionRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
186
     **/
187
    protected $users;
188
189
    /**
190
     * @var ArrayCollection
191
     * @ORM\OneToMany(targetEntity="SessionRelCourseRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
192
     **/
193
    protected $userCourseSubscriptions;
194
195
    /**
196
     * @var Course
197
     **/
198
    protected $currentCourse;
199
200
    /**
201
     * @var boolean
202
     * @ORM\Column(name="send_subscription_notification", type="boolean", nullable=false, options={"default":false})
203
     */
204
    private $sendSubscriptionNotification;
205
206
    /**
207
     * @var ArrayCollection
208
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CStudentPublication", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
209
     */
210
    private $studentPublications;
211
212
    /**
213
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="session", cascade={"persist"})
214
     */
215
    protected $issuedSkills;
216
217
    /**
218
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelSession", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
219
     **/
220
    protected $urls;
221
222
    /**
223
     * @var AccessUrl
224
     **/
225
    protected $currentUrl;
226
227
    /**
228
     * Constructor
229
     */
230
    public function __construct()
231
    {
232
        $this->items = new ArrayCollection();
233
        $this->urls = new ArrayCollection();
234
235
        $this->nbrClasses = 0;
236
        $this->nbrUsers = 0;
237
        $this->nbrUsers = 0;
238
239
        $this->displayStartDate = new \DateTime();
240
        $this->displayEndDate = new \DateTime();
241
        $this->accessStartDate = new \DateTime();
242
        $this->accessEndDate = new \DateTime();
243
        $this->coachAccessStartDate = new \DateTime();
244
        $this->coachAccessEndDate = new \DateTime();
245
        $this->visibility = 1;
246
247
        $this->courses = new ArrayCollection();
248
        $this->users = new ArrayCollection();
249
        $this->userCourseSubscriptions = new ArrayCollection();
250
        $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...
251
        $this->category = null;
252
        $this->studentPublications = new ArrayCollection();
253
    }
254
255
    /**
256
     * @return int
257
     */
258
    public function getDuration()
259
    {
260
        return $this->duration;
261
    }
262
263
    /**
264
     * @param int $duration
265
     */
266
    public function setDuration($duration)
267
    {
268
        $this->duration = $duration;
269
    }
270
271
    /**
272
     * @return string
273
     */
274
    public function getShowDescription()
275
    {
276
        return $this->showDescription;
277
    }
278
279
    /**
280
     * @param string $showDescription
281
     * @return $this
282
     */
283
    public function setShowDescription($showDescription)
284
    {
285
        $this->showDescription = $showDescription;
286
287
        return $this;
288
    }
289
290
    /**
291
     * @return string
292
     */
293
    public function __toString()
294
    {
295
        return (string) $this->getName();
296
    }
297
298
    /**
299
     * Get id
300
     *
301
     * @return integer
302
     */
303
    public function getId()
304
    {
305
        return $this->id;
306
    }
307
308
    /**
309
     * @param int $id
310
     */
311
    public function setId($id)
312
    {
313
        $this->id = $id;
314
    }
315
316
    /**
317
     * @return ArrayCollection
318
     */
319
    public function getUsers()
320
    {
321
        return $this->users;
322
    }
323
324
    /**
325
     * @param $users
326
     * @return $this
327
     */
328
    public function setUsers($users)
329
    {
330
        $this->users = new ArrayCollection();
331
332
        foreach ($users as $user) {
333
            $this->addUser($user);
334
        }
335
336
        return $this;
337
    }
338
339
    /**
340
     * @param SessionRelUser $user
341
     */
342
    public function addUser(SessionRelUser $user)
343
    {
344
        $user->setSession($this);
345
346
        if (!$this->hasUser($user)) {
347
            $this->users[] = $user;
348
        }
349
    }
350
351
    /**
352
     * @param int $status
353
     * @param User $user
354
     */
355
    public function addUserInSession($status, User $user)
356
    {
357
        $sessionRelUser = new SessionRelUser();
358
        $sessionRelUser->setSession($this);
359
        $sessionRelUser->setUser($user);
360
        $sessionRelUser->setRelationType($status);
361
362
        $this->addUser($sessionRelUser);
363
    }
364
365
    /**
366
     * @param SessionRelUser $subscription
367
     * @return bool
368
     */
369 View Code Duplication
    public function hasUser(SessionRelUser $subscription)
370
    {
371
        if ($this->getUsers()->count()) {
372
            $criteria = Criteria::create()->where(
373
                Criteria::expr()->eq("user", $subscription->getUser())
374
            )->andWhere(
375
                Criteria::expr()->eq("session", $subscription->getSession())
376
            )->andWhere(
377
                Criteria::expr()->eq("relationType", $subscription->getRelationType())
378
            );
379
380
            $relation = $this->getUsers()->matching($criteria);
381
382
            return $relation->count() > 0;
383
        }
384
385
        return false;
386
    }
387
388
    /**
389
     * @return ArrayCollection
390
     */
391
    public function getCourses()
392
    {
393
        return $this->courses;
394
    }
395
396
    /**
397
     * @param $courses
398
     */
399
    public function setCourses($courses)
400
    {
401
        $this->courses = new ArrayCollection();
402
403
        foreach ($courses as $course) {
404
            $this->addCourses($course);
405
        }
406
    }
407
408
    /**
409
     * @param SessionRelCourse $course
410
     */
411
    public function addCourses(SessionRelCourse $course)
412
    {
413
        $course->setSession($this);
414
        $this->courses[] = $course;
415
    }
416
417
    /**
418
     * @param Course $course
419
     *
420
     * @return bool
421
     */
422
    public function hasCourse(Course $course)
423
    {
424
        if ($this->getCourses()->count()) {
425
            $criteria = Criteria::create()->where(
426
                Criteria::expr()->eq("course", $course)
427
            );
428
            $relation = $this->getCourses()->matching($criteria);
429
430
            return $relation->count() > 0;
431
        }
432
433
        return false;
434
    }
435
436
437
    /**
438
     * Remove $course
439
     *
440
     * @param SessionRelCourse $course
441
     */
442
    public function removeCourses($course)
443
    {
444
        foreach ($this->courses as $key => $value) {
445
            if ($value->getId() == $course->getId()) {
446
                unset($this->courses[$key]);
447
            }
448
        }
449
    }
450
451
    /**
452
     * @param User $user
453
     * @param Course $course
454
     * @param int $status if not set it will check if the user is registered
455
     * with any status
456
     *
457
     * @return bool
458
     */
459
    public function hasUserInCourse(User $user, Course $course, $status = null)
460
    {
461
        $relation = $this->getUserInCourse($user, $course, $status);
462
463
        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...
464
    }
465
466
    /**
467
     * @param User $user
468
     * @param Course $course
469
     *
470
     * @return bool
471
     */
472
    public function hasStudentInCourse(User $user, Course $course)
473
    {
474
        return $this->hasUserInCourse($user, $course, self::STUDENT);
475
    }
476
477
    /**
478
     * @param User $user
479
     * @param Course $course
480
     *
481
     * @return bool
482
     */
483
    public function hasCoachInCourseWithStatus(User $user, Course $course)
484
    {
485
        return $this->hasUserInCourse($user, $course, self::COACH);
486
    }
487
488
    /**
489
     * @param User $user
490
     * @param Course $course
491
     * @param string $status
492
     *
493
     * @return \Doctrine\Common\Collections\Collection|static
494
     */
495
    public function getUserInCourse(User $user, Course $course, $status = null)
496
    {
497
        $criteria = Criteria::create()->where(
498
            Criteria::expr()->eq("course", $course)
499
        )->andWhere(
500
            Criteria::expr()->eq("user", $user)
501
        );
502
503
        if (!is_null($status))  {
504
            $criteria->andWhere(
505
                Criteria::expr()->eq("status", $status)
506
            );
507
        }
508
509
        return $this->getUserCourseSubscriptions()->matching($criteria);
510
    }
511
512
    /**
513
     * Set name
514
     *
515
     * @param string $name
516
     * @return Session
517
     */
518
    public function setName($name)
519
    {
520
        $this->name = $name;
521
522
        return $this;
523
    }
524
525
    /**
526
     * Get name
527
     *
528
     * @return string
529
     */
530
    public function getName()
531
    {
532
        return $this->name;
533
    }
534
535
    /**
536
     * Set description
537
     *
538
     * @param string $description
539
     * @return Session
540
     */
541
    public function setDescription($description)
542
    {
543
        $this->description = $description;
544
545
        return $this;
546
    }
547
548
    /**
549
     * Get description
550
     *
551
     * @return string
552
     */
553
    public function getDescription()
554
    {
555
        return $this->description;
556
    }
557
558
    /**
559
     * Set nbrCourses
560
     *
561
     * @param integer $nbrCourses
562
     * @return Session
563
     */
564
    public function setNbrCourses($nbrCourses)
565
    {
566
        $this->nbrCourses = $nbrCourses;
567
568
        return $this;
569
    }
570
571
    /**
572
     * Get nbrCourses
573
     *
574
     * @return integer
575
     */
576
    public function getNbrCourses()
577
    {
578
        return $this->nbrCourses;
579
    }
580
581
    /**
582
     * Set nbrUsers
583
     *
584
     * @param integer $nbrUsers
585
     * @return Session
586
     */
587
    public function setNbrUsers($nbrUsers)
588
    {
589
        $this->nbrUsers = $nbrUsers;
590
591
        return $this;
592
    }
593
594
    /**
595
     * Get nbrUsers
596
     *
597
     * @return integer
598
     */
599
    public function getNbrUsers()
600
    {
601
        return $this->nbrUsers;
602
    }
603
604
    /**
605
     * Set nbrClasses
606
     *
607
     * @param integer $nbrClasses
608
     * @return Session
609
     */
610
    public function setNbrClasses($nbrClasses)
611
    {
612
        $this->nbrClasses = $nbrClasses;
613
614
        return $this;
615
    }
616
617
    /**
618
     * Get nbrClasses
619
     *
620
     * @return integer
621
     */
622
    public function getNbrClasses()
623
    {
624
        return $this->nbrClasses;
625
    }
626
627
    /**
628
     * Set sessionAdminId
629
     *
630
     * @param integer $sessionAdminId
631
     * @return Session
632
     */
633
    public function setSessionAdminId($sessionAdminId)
634
    {
635
        $this->sessionAdminId = $sessionAdminId;
636
637
        return $this;
638
    }
639
640
    /**
641
     * Get sessionAdminId
642
     *
643
     * @return integer
644
     */
645
    public function getSessionAdminId()
646
    {
647
        return $this->sessionAdminId;
648
    }
649
650
    /**
651
     * Set visibility
652
     *
653
     * @param integer $visibility
654
     * @return Session
655
     */
656
    public function setVisibility($visibility)
657
    {
658
        $this->visibility = $visibility;
659
660
        return $this;
661
    }
662
663
    /**
664
     * Get visibility
665
     *
666
     * @return integer
667
     */
668
    public function getVisibility()
669
    {
670
        return $this->visibility;
671
    }
672
673
    /**
674
     * Set promotionId
675
     *
676
     * @param integer $promotionId
677
     * @return Session
678
     */
679
    public function setPromotionId($promotionId)
680
    {
681
        $this->promotionId = $promotionId;
682
683
        return $this;
684
    }
685
686
    /**
687
     * Get promotionId
688
     *
689
     * @return integer
690
     */
691
    public function getPromotionId()
692
    {
693
        return $this->promotionId;
694
    }
695
696
    /**
697
     * Set displayStartDate
698
     *
699
     * @param \DateTime $displayStartDate
700
     * @return Session
701
     */
702
    public function setDisplayStartDate($displayStartDate)
703
    {
704
        $this->displayStartDate = $displayStartDate;
705
706
        return $this;
707
    }
708
709
    /**
710
     * Get displayStartDate
711
     *
712
     * @return \DateTime
713
     */
714
    public function getDisplayStartDate()
715
    {
716
        return $this->displayStartDate;
717
    }
718
719
    /**
720
     * Set displayEndDate
721
     *
722
     * @param \DateTime $displayEndDate
723
     * @return Session
724
     */
725
    public function setDisplayEndDate($displayEndDate)
726
    {
727
        $this->displayEndDate = $displayEndDate;
728
729
        return $this;
730
    }
731
732
    /**
733
     * Get displayEndDate
734
     *
735
     * @return \DateTime
736
     */
737
    public function getDisplayEndDate()
738
    {
739
        return $this->displayEndDate;
740
    }
741
742
    /**
743
     * Set accessStartDate
744
     *
745
     * @param \DateTime $accessStartDate
746
     * @return Session
747
     */
748
    public function setAccessStartDate($accessStartDate)
749
    {
750
        $this->accessStartDate = $accessStartDate;
751
752
        return $this;
753
    }
754
755
    /**
756
     * Get accessStartDate
757
     *
758
     * @return \DateTime
759
     */
760
    public function getAccessStartDate()
761
    {
762
        return $this->accessStartDate;
763
    }
764
765
    /**
766
     * Set accessEndDate
767
     *
768
     * @param \DateTime $accessEndDate
769
     * @return Session
770
     */
771
    public function setAccessEndDate($accessEndDate)
772
    {
773
        $this->accessEndDate = $accessEndDate;
774
775
        return $this;
776
    }
777
778
    /**
779
     * Get accessEndDate
780
     *
781
     * @return \DateTime
782
     */
783
    public function getAccessEndDate()
784
    {
785
        return $this->accessEndDate;
786
    }
787
788
    /**
789
     * Set coachAccessStartDate
790
     *
791
     * @param \DateTime $coachAccessStartDate
792
     * @return Session
793
     */
794
    public function setCoachAccessStartDate($coachAccessStartDate)
795
    {
796
        $this->coachAccessStartDate = $coachAccessStartDate;
797
798
        return $this;
799
    }
800
801
    /**
802
     * Get coachAccessStartDate
803
     *
804
     * @return \DateTime
805
     */
806
    public function getCoachAccessStartDate()
807
    {
808
        return $this->coachAccessStartDate;
809
    }
810
811
    /**
812
     * Set coachAccessEndDate
813
     *
814
     * @param \DateTime $coachAccessEndDate
815
     * @return Session
816
     */
817
    public function setCoachAccessEndDate($coachAccessEndDate)
818
    {
819
        $this->coachAccessEndDate = $coachAccessEndDate;
820
821
        return $this;
822
    }
823
824
    /**
825
     * Get coachAccessEndDate
826
     *
827
     * @return \DateTime
828
     */
829
    public function getCoachAccessEndDate()
830
    {
831
        return $this->coachAccessEndDate;
832
    }
833
834
    /**
835
     * Get id
836
     *
837
     * @return User
838
     */
839
    public function getGeneralCoach()
840
    {
841
        return $this->generalCoach;
842
    }
843
844
    /**
845
     * @param $coach
846
     * @return $this
847
     */
848
    public function setGeneralCoach($coach)
849
    {
850
        $this->generalCoach = $coach;
851
852
        return $this;
853
    }
854
855
    /**
856
     * @return mixed
857
     */
858
    public function getCategory()
859
    {
860
        return $this->category;
861
    }
862
863
    /**
864
     * @param $category
865
     * @return $this
866
     */
867
    public function setCategory($category)
868
    {
869
        $this->category = $category;
870
871
        return $this;
872
    }
873
874
    /**
875
     * @return array
876
     */
877
    public static function getStatusList()
878
    {
879
        return array(
880
            self::VISIBLE => 'status_visible',
881
            self::READ_ONLY => 'status_read_only',
882
            self::INVISIBLE => 'status_invisible',
883
            self::AVAILABLE => 'status_available',
884
        );
885
    }
886
887
    /**
888
     * Check if session is visible
889
     * @return bool
890
     */
891
    public function isActive()
892
    {
893
        $now = new \Datetime('now');
894
895
        if ($now > $this->getAccessStartDate()) {
896
897
            return true;
898
        }
899
900
        return false;
901
    }
902
903
    /**
904
     * @param Course $course
905
     */
906
    public function addCourse(Course $course)
907
    {
908
        $entity = new SessionRelCourse();
909
        $entity->setCourse($course);
910
        $this->addCourses($entity);
911
    }
912
913
    /**
914
     * @return ArrayCollection
915
     */
916
    public function getUserCourseSubscriptions()
917
    {
918
        return $this->userCourseSubscriptions;
919
    }
920
921
    /**
922
     * @param ArrayCollection $userCourseSubscriptions
923
     */
924
    public function setUserCourseSubscriptions($userCourseSubscriptions)
925
    {
926
        $this->userCourseSubscriptions = new ArrayCollection();
927
928
        foreach ($userCourseSubscriptions as $item) {
929
            $this->addUserCourseSubscription($item);
930
        }
931
    }
932
933
    /**
934
     * @param SessionRelCourseRelUser $subscription
935
     */
936
    public function addUserCourseSubscription(SessionRelCourseRelUser $subscription)
937
    {
938
        $subscription->setSession($this);
939
        if (!$this->hasUserCourseSubscription($subscription)) {
940
            $this->userCourseSubscriptions[] = $subscription;
941
        }
942
    }
943
944
    /**
945
     * @param int $status
946
     * @param User $user
947
     * @param Course $course
948
     */
949
    public function addUserInCourse($status, User $user, Course $course)
950
    {
951
        $userRelCourseRelSession = new SessionRelCourseRelUser();
952
        $userRelCourseRelSession->setCourse($course);
953
        $userRelCourseRelSession->setUser($user);
954
        $userRelCourseRelSession->setSession($this);
955
        $userRelCourseRelSession->setStatus($status);
956
        $this->addUserCourseSubscription($userRelCourseRelSession);
957
    }
958
959
    /**
960
     * @param SessionRelCourseRelUser $subscription
961
     * @return bool
962
     */
963 View Code Duplication
    public function hasUserCourseSubscription(SessionRelCourseRelUser $subscription)
964
    {
965
        if ($this->getUserCourseSubscriptions()->count()) {
966
            $criteria = Criteria::create()->where(
967
                Criteria::expr()->eq("user", $subscription->getUser())
968
            )->andWhere(
969
                Criteria::expr()->eq("course", $subscription->getCourse())
970
            )->andWhere(
971
                Criteria::expr()->eq("session", $subscription->getSession())
972
            );
973
            $relation = $this->getUserCourseSubscriptions()->matching($criteria);
974
975
            return $relation->count() > 0;
976
        }
977
978
        return false;
979
    }
980
981
    /**
982
     * @return Course
983
     */
984
    public function getCurrentCourse()
985
    {
986
        return $this->currentCourse;
987
    }
988
989
    /**
990
     * @param Course $course
991
     * @return $this
992
     */
993
    public function setCurrentCourse(Course $course)
994
    {
995
        // If the session is registered in the course session list.
996
        if ($this->hasCourse($course)) {
997
            $this->currentCourse = $course;
998
        }
999
1000
        return $this;
1001
    }
1002
1003
    /**
1004
     * Set $sendSubscriptionNotification
1005
     * @param boolean $sendNotification
1006
     * @return Session
1007
     */
1008
    public function setSendSubscriptionNotification($sendNotification)
1009
    {
1010
        $this->sendSubscriptionNotification = $sendNotification;
1011
1012
        return $this;
1013
    }
1014
1015
    /**
1016
     * Get $sendSubscriptionNotification
1017
     * @return boolean
1018
     */
1019
    public function getSendSubscriptionNotification()
1020
    {
1021
        return $this->sendSubscriptionNotification;
1022
    }
1023
1024
    /**
1025
     * Get user from course by status
1026
     * @param \Chamilo\CoreBundle\Entity\Course $course
1027
     * @param string $status
1028
     * @return \Doctrine\Common\Collections\Collection|static
1029
     */
1030
    public function getUserCourseSubscriptionsByStatus(Course $course, $status)
1031
    {
1032
        $criteria = Criteria::create()
1033
            ->where(
1034
                Criteria::expr()->eq("course", $course)
1035
            )
1036
            ->andWhere(
1037
                Criteria::expr()->eq("status", $status)
1038
            );
1039
1040
        return $this->userCourseSubscriptions->matching($criteria);
1041
    }
1042
1043
    public function getBuyCoursePluginPrice()
1044
    {
1045
        // start buycourse validation
1046
        // display the course price and buy button if the buycourses plugin is enabled and this course is configured
1047
        $plugin = \BuyCoursesPlugin::create();
1048
        $isThisCourseInSale = $plugin->buyCoursesForGridCatalogVerificator($this->id, \BuyCoursesPlugin::PRODUCT_TYPE_SESSION);
1049
        $return = [];
1050
1051
        if ($isThisCourseInSale) {
1052
            // set the Price label
1053
            $return['html'] = $isThisCourseInSale['html'];
1054
            // set the Buy button instead register.
1055
            if ($isThisCourseInSale['verificator']) {
1056
                $return['buy_button'] = $plugin->returnBuyCourseButton($this->id, \BuyCoursesPlugin::PRODUCT_TYPE_SESSION);
1057
            }
1058
        }
1059
        // end buycourse validation
1060
        return $return;
1061
    }
1062
1063
    /**
1064
     * @param ArrayCollection $studentPublications
1065
     * @return Session
1066
     */
1067
    public function setStudentPublications(ArrayCollection $studentPublications)
1068
    {
1069
        $this->studentPublications = new ArrayCollection();
1070
1071
        foreach ($studentPublications as $studentPublication) {
1072
            $this->addStudentPublication($studentPublication);
1073
        }
1074
1075
        return $this;
1076
    }
1077
1078
    /**
1079
     * @param CStudentPublication $studentPublication
1080
     * @return Session
1081
     */
1082
    public function addStudentPublication(CStudentPublication $studentPublication)
1083
    {
1084
        $this->studentPublications[] = $studentPublication;
1085
1086
        return $this;
1087
    }
1088
1089
    /**
1090
     * Get studentPublications
1091
     * @return ArrayCollection
1092
     */
1093
    public function getStudentPublications()
1094
    {
1095
        return $this->studentPublications;
1096
    }
1097
1098
    /**
1099
     * Get issuedSkills
1100
     * @return ArrayCollection
1101
     */
1102
    public function getIssuedSkills()
1103
    {
1104
        return $this->issuedSkills;
1105
    }
1106
1107
    /**
1108
     * @param AccessUrl $url
1109
     *
1110
     * @return $this
1111
     */
1112 View Code Duplication
    public function setCurrentUrl(AccessUrl $url)
1113
    {
1114
        $urlList = $this->getUrls();
1115
        /** @var AccessUrlRelCourse $item */
1116
        foreach ($urlList as $item) {
1117
            if ($item->getUrl()->getId() == $url->getId()) {
1118
                $this->currentUrl = $url;
1119
                break;
1120
            }
1121
        }
1122
1123
        return $this;
1124
    }
1125
1126
    /**
1127
     * @return AccessUrl
1128
     */
1129
    public function getCurrentUrl()
1130
    {
1131
        return $this->currentUrl;
1132
    }
1133
1134
    /**
1135
     * @return ArrayCollection
1136
     */
1137
    public function getUrls()
1138
    {
1139
        return $this->urls;
1140
    }
1141
1142
    /**
1143
     * @param $urls
1144
     */
1145
    public function setUrls($urls)
1146
    {
1147
        $this->urls = new ArrayCollection();
1148
1149
        foreach ($urls as $url) {
1150
            $this->addUrls($url);
1151
        }
1152
    }
1153
1154
    /**
1155
     * @param AccessUrlRelSession $url
1156
     */
1157
    public function addUrls(AccessUrlRelSession $url)
1158
    {
1159
        $url->setSession($this);
1160
        $this->urls[] = $url;
1161
    }
1162
}
1163