Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

src/Chamilo/CoreBundle/Entity/Session.php (1 issue)

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