Passed
Pull Request — 1.11.x (#4151)
by Angel Fernando Quiroz
15:35 queued 07:02
created

Meeting::getSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\Zoom;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\CourseRelUser;
9
use Chamilo\CoreBundle\Entity\Session;
10
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
11
use Chamilo\CourseBundle\Entity\CGroupInfo;
12
use Chamilo\PluginBundle\Zoom\API\MeetingInfoGet;
13
use Chamilo\PluginBundle\Zoom\API\MeetingListItem;
14
use Chamilo\PluginBundle\Zoom\API\MeetingSettings;
15
use Chamilo\UserBundle\Entity\User;
16
use Database;
17
use DateInterval;
18
use DateTime;
19
use DateTimeZone;
20
use Doctrine\Common\Collections\ArrayCollection;
21
use Doctrine\ORM\Mapping as ORM;
22
use Exception;
23
24
/**
25
 * Class Meeting.
26
 *
27
 * @ORM\Entity(repositoryClass="Chamilo\PluginBundle\Zoom\MeetingRepository")
28
 * @ORM\Table(
29
 *     name="plugin_zoom_meeting",
30
 *     indexes={
31
 *         @ORM\Index(name="user_id_index", columns={"user_id"}),
32
 *         @ORM\Index(name="course_id_index", columns={"course_id"}),
33
 *         @ORM\Index(name="session_id_index", columns={"session_id"})
34
 *     }
35
 * )
36
 * @ORM\HasLifecycleCallbacks
37
 */
38
class Meeting
39
{
40
    /** @var string meeting type name */
41
    public $typeName;
42
43
    /** @var DateTime meeting start time as a DateTime instance */
44
    public $startDateTime;
45
46
    /** @var string meeting formatted start time */
47
    public $formattedStartTime;
48
49
    /** @var DateInterval meeting duration as a DateInterval instance */
50
    public $durationInterval;
51
52
    /** @var string meeting formatted duration */
53
    public $formattedDuration;
54
55
    /** @var string */
56
    public $statusName;
57
58
    /**
59
     * @var int
60
     * @ORM\Column(type="integer", name="id")
61
     * @ORM\Id
62
     * @ORM\GeneratedValue()
63
     */
64
    protected $id;
65
66
    /**
67
     * @var int the remote zoom meeting identifier
68
     * @ORM\Column(name="meeting_id", type="string")
69
     */
70
    protected $meetingId;
71
72
    /**
73
     * @var User
74
     * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User")
75
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
76
     */
77
    protected $user;
78
79
    /**
80
     * @var Course
81
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
82
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable=true)
83
     */
84
    protected $course;
85
86
    /**
87
     * @var CGroupInfo
88
     * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroupInfo")
89
     * @ORM\JoinColumn(name="group_id", referencedColumnName="iid", nullable=true)
90
     */
91
    protected $group;
92
93
    /**
94
     * @var Session
95
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session")
96
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=true)
97
     */
98
    protected $session;
99
100
    /**
101
     * @var string
102
     * @ORM\Column(type="text", name="meeting_list_item_json", nullable=true)
103
     */
104
    protected $meetingListItemJson;
105
106
    /**
107
     * @var string
108
     * @ORM\Column(type="text", name="meeting_info_get_json", nullable=true)
109
     */
110
    protected $meetingInfoGetJson;
111
112
    /**
113
     * @var bool
114
     *
115
     * @ORM\Column(type="boolean", name="sign_attendance")
116
     */
117
    protected $signAttendance;
118
119
    /**
120
     * @var string|null
121
     *
122
     * @ORM\Column(type="text", name="reason_to_sign_attendance", nullable=true)
123
     */
124
    protected $reasonToSignAttendance;
125
126
    /** @var MeetingListItem */
127
    protected $meetingListItem;
128
129
    /** @var MeetingInfoGet */
130
    protected $meetingInfoGet;
131
132
    /**
133
     * @var MeetingActivity[]|ArrayCollection
134
     * @ORM\OrderBy({"createdAt" = "DESC"})
135
     * @ORM\OneToMany(targetEntity="MeetingActivity", mappedBy="meeting", cascade={"persist", "remove"})
136
     */
137
    protected $activities;
138
139
    /**
140
     * @var Registrant[]|ArrayCollection
141
     *
142
     * @ORM\OneToMany(targetEntity="Registrant", mappedBy="meeting", cascade={"persist", "remove"})
143
     */
144
    protected $registrants;
145
146
    /**
147
     * @var Recording[]|ArrayCollection
148
     *
149
     * @ORM\OneToMany(targetEntity="Recording", mappedBy="meeting", cascade={"persist"}, orphanRemoval=true)
150
     */
151
    protected $recordings;
152
153
    /**
154
     * @var ArrayCollection<int, Signature>
155
     *
156
     * @ORM\OneToMany(targetEntity="Signature", mappedBy="meeting")
157
     */
158
    protected $signatures;
159
160
    public function __construct()
161
    {
162
        $this->registrants = new ArrayCollection();
163
        $this->recordings = new ArrayCollection();
164
        $this->activities = new ArrayCollection();
165
        $this->signAttendance = false;
166
        $this->signatures = new ArrayCollection();
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function __toString()
173
    {
174
        return sprintf('Meeting %d', $this->id);
175
    }
176
177
    /**
178
     * @return int
179
     */
180
    public function getId()
181
    {
182
        return $this->id;
183
    }
184
185
    /**
186
     * @return int
187
     */
188
    public function getMeetingId()
189
    {
190
        return $this->meetingId;
191
    }
192
193
    /**
194
     * @param int $meetingId
195
     *
196
     * @return Meeting
197
     */
198
    public function setMeetingId($meetingId)
199
    {
200
        $this->meetingId = $meetingId;
201
202
        return $this;
203
    }
204
205
    /**
206
     * @return User
207
     */
208
    public function getUser()
209
    {
210
        return $this->user;
211
    }
212
213
    /**
214
     * @return Course
215
     */
216
    public function getCourse()
217
    {
218
        return $this->course;
219
    }
220
221
    /**
222
     * @return Session
223
     */
224
    public function getSession()
225
    {
226
        return $this->session;
227
    }
228
229
    /**
230
     * @return Registrant[]|ArrayCollection
231
     */
232
    public function getRegistrants()
233
    {
234
        return $this->registrants;
235
    }
236
237
    /**
238
     * @return Recording[]|ArrayCollection
239
     */
240
    public function getRecordings()
241
    {
242
        return $this->recordings;
243
    }
244
245
    /**
246
     * @return MeetingActivity[]|ArrayCollection
247
     */
248
    public function getActivities()
249
    {
250
        return $this->activities;
251
    }
252
253
    public function addActivity(MeetingActivity $activity)
254
    {
255
        $activity->setMeeting($this);
256
        $this->activities[] = $activity;
257
    }
258
259
    /**
260
     * @param MeetingActivity[]|ArrayCollection $activities
261
     *
262
     * @return Meeting
263
     */
264
    public function setActivities($activities)
265
    {
266
        $this->activities = $activities;
267
268
        return $this;
269
    }
270
271
    /**
272
     * @ORM\PostLoad
273
     *
274
     * @throws Exception
275
     */
276
    public function postLoad()
277
    {
278
        if (null !== $this->meetingListItemJson) {
279
            $this->meetingListItem = MeetingListItem::fromJson($this->meetingListItemJson);
280
        }
281
        if (null !== $this->meetingInfoGetJson) {
282
            $this->meetingInfoGet = MeetingInfoGet::fromJson($this->meetingInfoGetJson);
283
        }
284
        $this->initializeDisplayableProperties();
285
    }
286
287
    /**
288
     * @ORM\PostUpdate
289
     *
290
     * @throws Exception
291
     */
292
    public function postUpdate()
293
    {
294
        $this->initializeDisplayableProperties();
295
    }
296
297
    /**
298
     * @ORM\PreFlush
299
     */
300
    public function preFlush()
301
    {
302
        if (null !== $this->meetingListItem) {
303
            $this->meetingListItemJson = json_encode($this->meetingListItem);
304
        }
305
        if (null !== $this->meetingInfoGet) {
306
            $this->meetingInfoGetJson = json_encode($this->meetingInfoGet);
307
        }
308
    }
309
310
    /**
311
     * @return MeetingListItem
312
     */
313
    public function getMeetingListItem()
314
    {
315
        return $this->meetingListItem;
316
    }
317
318
    /**
319
     * @return MeetingInfoGet
320
     */
321
    public function getMeetingInfoGet()
322
    {
323
        return $this->meetingInfoGet;
324
    }
325
326
    /**
327
     * @param User $user
328
     *
329
     * @return $this
330
     */
331
    public function setUser($user)
332
    {
333
        $this->user = $user;
334
335
        return $this;
336
    }
337
338
    /**
339
     * @param Course $course
340
     *
341
     * @return $this
342
     */
343
    public function setCourse($course)
344
    {
345
        $this->course = $course;
346
347
        return $this;
348
    }
349
350
    /**
351
     * @param Session $session
352
     *
353
     * @return $this
354
     */
355
    public function setSession($session)
356
    {
357
        $this->session = $session;
358
359
        return $this;
360
    }
361
362
    /**
363
     * @return CGroupInfo
364
     */
365
    public function getGroup()
366
    {
367
        return $this->group;
368
    }
369
370
    /**
371
     * @param CGroupInfo $group
372
     *
373
     * @return Meeting
374
     */
375
    public function setGroup($group)
376
    {
377
        $this->group = $group;
378
379
        return $this;
380
    }
381
382
    /**
383
     * @param MeetingListItem $meetingListItem
384
     *
385
     * @throws Exception
386
     *
387
     * @return Meeting
388
     */
389
    public function setMeetingListItem($meetingListItem)
390
    {
391
        if (null === $this->meetingId) {
392
            $this->meetingId = $meetingListItem->id;
0 ignored issues
show
Documentation Bug introduced by
The property $meetingId was declared of type integer, but $meetingListItem->id is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
393
        } elseif ($this->meetingId != $meetingListItem->id) {
394
            throw new Exception('the Meeting identifier differs from the MeetingListItem identifier');
395
        }
396
        $this->meetingListItem = $meetingListItem;
397
398
        return $this;
399
    }
400
401
    /**
402
     * @param MeetingInfoGet $meetingInfoGet
403
     *
404
     * @throws Exception
405
     *
406
     * @return Meeting
407
     */
408
    public function setMeetingInfoGet($meetingInfoGet)
409
    {
410
        if (null === $this->meetingId) {
411
            $this->meetingId = $meetingInfoGet->id;
0 ignored issues
show
Documentation Bug introduced by
The property $meetingId was declared of type integer, but $meetingInfoGet->id is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
412
        } elseif ($this->meetingId != $meetingInfoGet->id) {
413
            throw new Exception('the Meeting identifier differs from the MeetingInfoGet identifier');
414
        }
415
        $this->meetingInfoGet = $meetingInfoGet;
416
        $this->initializeDisplayableProperties();
417
418
        return $this;
419
    }
420
421
    /**
422
     * @return bool
423
     */
424
    public function isCourseMeeting()
425
    {
426
        return null !== $this->course;
427
    }
428
429
    /**
430
     * @return bool
431
     */
432
    public function isCourseGroupMeeting()
433
    {
434
        return null !== $this->course && null !== $this->group;
435
    }
436
437
    /**
438
     * @return bool
439
     */
440
    public function isUserMeeting()
441
    {
442
        return null !== $this->user && null === $this->course;
443
    }
444
445
    /**
446
     * @return bool
447
     */
448
    public function isGlobalMeeting()
449
    {
450
        return null === $this->user && null === $this->course;
451
    }
452
453
    public function setStatus($status)
454
    {
455
        $this->meetingInfoGet->status = $status;
456
    }
457
458
    /**
459
     * Builds the list of users that can register into this meeting.
460
     * Zoom requires an email address, therefore users without an email address are excluded from the list.
461
     *
462
     * @return User[] the list of users
463
     */
464
    public function getRegistrableUsers()
465
    {
466
        $users = [];
467
        if (!$this->isCourseMeeting()) {
468
            $criteria = ['active' => true];
469
            $users = Database::getManager()->getRepository('ChamiloUserBundle:User')->findBy($criteria);
470
        } elseif (null === $this->session) {
471
            if (null !== $this->course) {
472
                /** @var CourseRelUser $courseRelUser */
473
                foreach ($this->course->getUsers() as $courseRelUser) {
474
                    $users[] = $courseRelUser->getUser();
475
                }
476
            }
477
        } else {
478
            if (null !== $this->course) {
479
                $subscriptions = $this->session->getUserCourseSubscriptionsByStatus($this->course, Session::STUDENT);
480
                if ($subscriptions) {
481
                    /** @var SessionRelCourseRelUser $sessionCourseUser */
482
                    foreach ($subscriptions as $sessionCourseUser) {
483
                        $users[] = $sessionCourseUser->getUser();
484
                    }
485
                }
486
            }
487
        }
488
489
        $activeUsersWithEmail = [];
490
        foreach ($users as $user) {
491
            if ($user->isActive() && !empty($user->getEmail())) {
492
                $activeUsersWithEmail[] = $user;
493
            }
494
        }
495
496
        return $activeUsersWithEmail;
497
    }
498
499
    /**
500
     * @return bool
501
     */
502
    public function requiresDateAndDuration()
503
    {
504
        return MeetingInfoGet::TYPE_SCHEDULED === $this->meetingInfoGet->type
505
            || MeetingInfoGet::TYPE_RECURRING_WITH_FIXED_TIME === $this->meetingInfoGet->type;
506
    }
507
508
    /**
509
     * @return bool
510
     */
511
    public function requiresRegistration()
512
    {
513
        return MeetingSettings::APPROVAL_TYPE_AUTOMATICALLY_APPROVE === $this->meetingInfoGet->settings->approval_type;
514
        /*return
515
            MeetingSettings::APPROVAL_TYPE_NO_REGISTRATION_REQUIRED != $this->meetingInfoGet->settings->approval_type;*/
516
    }
517
518
    /**
519
     * @return bool
520
     */
521
    public function hasCloudAutoRecordingEnabled()
522
    {
523
        return \ZoomPlugin::RECORDING_TYPE_NONE !== $this->meetingInfoGet->settings->auto_recording;
524
    }
525
526
    /**
527
     * @param User $user
528
     *
529
     * @return bool
530
     */
531
    public function hasRegisteredUser($user)
532
    {
533
        return $this->getRegistrants()->exists(
534
            function (Registrant $registrantEntity) use (&$user) {
535
                return $registrantEntity->getUser() === $user;
536
            }
537
        );
538
    }
539
540
    /**
541
     * @param User $user
542
     *
543
     * @return Registrant|null
544
     */
545
    public function getRegistrant($user)
546
    {
547
        foreach ($this->getRegistrants() as $registrant) {
548
            if ($registrant->getUser() === $user) {
549
                return $registrant;
550
            }
551
        }
552
553
        return null;
554
    }
555
556
    /**
557
     * Generates a short presentation of the meeting for the future participant.
558
     * To be displayed above the "Enter meeting" link.
559
     *
560
     * @return string
561
     */
562
    public function getIntroduction()
563
    {
564
        $introduction = sprintf('<h1>%s</h1>', $this->meetingInfoGet->topic).PHP_EOL;
565
        if (!$this->isGlobalMeeting()) {
566
            if (!empty($this->formattedStartTime)) {
567
                $introduction .= $this->formattedStartTime;
568
                if (!empty($this->formattedDuration)) {
569
                    $introduction .= ' ('.$this->formattedDuration.')';
570
                }
571
                $introduction .= PHP_EOL;
572
            }
573
        }
574
        if ($this->user) {
575
            $introduction .= sprintf('<p>%s</p>', $this->user->getFullname()).PHP_EOL;
576
        } elseif ($this->isCourseMeeting()) {
577
            if (null === $this->session) {
578
                $introduction .= sprintf('<p class="main">%s</p>', $this->course).PHP_EOL;
579
            } else {
580
                $introduction .= sprintf('<p class="main">%s (%s)</p>', $this->course, $this->session).PHP_EOL;
581
            }
582
        }
583
        if (!empty($this->meetingInfoGet->agenda)) {
584
            $introduction .= sprintf('<p>%s</p>', $this->meetingInfoGet->agenda).PHP_EOL;
585
        }
586
587
        return $introduction;
588
    }
589
590
    public function isSignAttendance(): bool
591
    {
592
        return $this->signAttendance;
593
    }
594
595
    public function setSignAttendance(bool $signAttendance): Meeting
596
    {
597
        $this->signAttendance = $signAttendance;
598
599
        return $this;
600
    }
601
602
    public function getReasonToSignAttendance(): ?string
603
    {
604
        return $this->reasonToSignAttendance;
605
    }
606
607
    public function setReasonToSignAttendance(string $reasonToSignAttendance): Meeting
608
    {
609
        $this->reasonToSignAttendance = $reasonToSignAttendance;
610
611
        return $this;
612
    }
613
614
    /**
615
     * @return ArrayCollection<int, Signature>
616
     */
617
    public function getSignatures(): ArrayCollection
618
    {
619
        return $this->signatures;
620
    }
621
622
    /**
623
     * @throws Exception on unexpected start_time or duration
624
     */
625
    private function initializeDisplayableProperties()
626
    {
627
        $zoomPlugin = new \ZoomPlugin();
628
629
        $typeList = [
630
            API\Meeting::TYPE_INSTANT => $zoomPlugin->get_lang('InstantMeeting'),
631
            API\Meeting::TYPE_SCHEDULED => $zoomPlugin->get_lang('ScheduledMeeting'),
632
            API\Meeting::TYPE_RECURRING_WITH_NO_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithNoFixedTime'),
633
            API\Meeting::TYPE_RECURRING_WITH_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithFixedTime'),
634
        ];
635
        $this->typeName = $typeList[$this->meetingInfoGet->type];
636
637
        if (property_exists($this, 'status')) {
638
            $statusList = [
639
                'waiting' => $zoomPlugin->get_lang('Waiting'),
640
                'started' => $zoomPlugin->get_lang('Started'),
641
                'finished' => $zoomPlugin->get_lang('Finished'),
642
            ];
643
            $this->statusName = $statusList[$this->meetingInfoGet->status];
644
        }
645
        $this->startDateTime = null;
646
        $this->formattedStartTime = '';
647
        $this->durationInterval = null;
648
        $this->formattedDuration = '';
649
        if (!empty($this->meetingInfoGet->start_time)) {
650
            $this->startDateTime = new DateTime($this->meetingInfoGet->start_time);
651
            $this->startDateTime->setTimezone(new DateTimeZone(api_get_timezone()));
652
            $this->formattedStartTime = $this->startDateTime->format('Y-m-d H:i');
653
        }
654
655
        if (!empty($this->meetingInfoGet->duration)) {
656
            $now = new DateTime();
657
            $later = new DateTime();
658
            $later->add(new DateInterval('PT'.$this->meetingInfoGet->duration.'M'));
659
            $this->durationInterval = $later->diff($now);
660
            $this->formattedDuration = $this->durationInterval->format($zoomPlugin->get_lang('DurationFormat'));
661
        }
662
    }
663
}
664