Passed
Pull Request — 1.11.x (#4160)
by Angel Fernando Quiroz
10:16
created

Meeting::getId()   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\BaseMeetingTrait;
13
use Chamilo\PluginBundle\Zoom\API\MeetingInfoGet;
14
use Chamilo\PluginBundle\Zoom\API\MeetingListItem;
15
use Chamilo\PluginBundle\Zoom\API\MeetingSettings;
16
use Chamilo\UserBundle\Entity\User;
17
use Database;
18
use DateInterval;
19
use DateTime;
20
use DateTimeZone;
21
use Doctrine\Common\Collections\ArrayCollection;
22
use Doctrine\Common\Collections\Criteria;
23
use Doctrine\ORM\Mapping as ORM;
24
use Exception;
25
26
/**
27
 * Class Meeting.
28
 *
29
 * @ORM\Entity(repositoryClass="Chamilo\PluginBundle\Zoom\MeetingRepository")
30
 * @ORM\Table(
31
 *     name="plugin_zoom_meeting",
32
 *     indexes={
33
 *         @ORM\Index(name="user_id_index", columns={"user_id"}),
34
 *         @ORM\Index(name="course_id_index", columns={"course_id"}),
35
 *         @ORM\Index(name="session_id_index", columns={"session_id"})
36
 *     }
37
 * )
38
 * @ORM\HasLifecycleCallbacks
39
 * @ORM\InheritanceType("SINGLE_TABLE")
40
 * @ORM\DiscriminatorColumn(name="type", type="string")
41
 * @ORM\DiscriminatorMap({"meeting" = "Chamilo\PluginBundle\Zoom\Meeting", "webinar" = "Chamilo\PluginBundle\Zoom\Webinar"})
42
 */
43
class Meeting
44
{
45
    /** @var string meeting type name */
46
    public $typeName;
47
48
    /** @var DateTime meeting start time as a DateTime instance */
49
    public $startDateTime;
50
51
    /** @var string meeting formatted start time */
52
    public $formattedStartTime;
53
54
    /** @var DateInterval meeting duration as a DateInterval instance */
55
    public $durationInterval;
56
57
    /** @var string meeting formatted duration */
58
    public $formattedDuration;
59
60
    /** @var string */
61
    public $statusName;
62
63
    /**
64
     * @var int
65
     * @ORM\Column(type="integer")
66
     * @ORM\Id
67
     * @ORM\GeneratedValue()
68
     */
69
    protected $id;
70
71
    /**
72
     * @var int the remote zoom meeting identifier
73
     * @ORM\Column(name="meeting_id", type="string")
74
     */
75
    protected $meetingId;
76
77
    /**
78
     * @var User
79
     * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User")
80
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
81
     */
82
    protected $user;
83
84
    /**
85
     * @var Course
86
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
87
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable=true)
88
     */
89
    protected $course;
90
91
    /**
92
     * @var CGroupInfo
93
     * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroupInfo")
94
     * @ORM\JoinColumn(name="group_id", referencedColumnName="iid", nullable=true)
95
     */
96
    protected $group;
97
98
    /**
99
     * @var Session
100
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session")
101
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=true)
102
     */
103
    protected $session;
104
105
    /**
106
     * @var string
107
     * @ORM\Column(type="text", name="meeting_list_item_json", nullable=true)
108
     */
109
    protected $meetingListItemJson;
110
111
    /**
112
     * @var string
113
     * @ORM\Column(type="text", name="meeting_info_get_json", nullable=true)
114
     */
115
    protected $meetingInfoGetJson;
116
117
    /** @var MeetingListItem */
118
    protected $meetingListItem;
119
120
    /** @var MeetingInfoGet */
121
    protected $meetingInfoGet;
122
123
    /**
124
     * @var MeetingActivity[]|ArrayCollection
125
     * @ORM\OrderBy({"createdAt" = "DESC"})
126
     * @ORM\OneToMany(targetEntity="MeetingActivity", mappedBy="meeting", cascade={"persist", "remove"})
127
     */
128
    protected $activities;
129
130
    /**
131
     * @var Registrant[]|ArrayCollection
132
     *
133
     * @ORM\OneToMany(targetEntity="Registrant", mappedBy="meeting", cascade={"persist", "remove"})
134
     */
135
    protected $registrants;
136
137
    /**
138
     * @var Recording[]|ArrayCollection
139
     *
140
     * @ORM\OneToMany(targetEntity="Recording", mappedBy="meeting", cascade={"persist"}, orphanRemoval=true)
141
     */
142
    protected $recordings;
143
144
    /**
145
     * @var string|null
146
     *
147
     * @ORM\Column(type="string", name="account_email", nullable=true)
148
     */
149
    protected $accountEmail;
150
151
    public function __construct()
152
    {
153
        $this->registrants = new ArrayCollection();
154
        $this->recordings = new ArrayCollection();
155
        $this->activities = new ArrayCollection();
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function __toString()
162
    {
163
        return sprintf('Meeting %d', $this->id);
164
    }
165
166
    /**
167
     * @return int
168
     */
169
    public function getId()
170
    {
171
        return $this->id;
172
    }
173
174
    /**
175
     * @return int
176
     */
177
    public function getMeetingId()
178
    {
179
        return $this->meetingId;
180
    }
181
182
    /**
183
     * @param int $meetingId
184
     *
185
     * @return Meeting
186
     */
187
    public function setMeetingId($meetingId)
188
    {
189
        $this->meetingId = $meetingId;
190
191
        return $this;
192
    }
193
194
    /**
195
     * @return User
196
     */
197
    public function getUser()
198
    {
199
        return $this->user;
200
    }
201
202
    /**
203
     * @return Course
204
     */
205
    public function getCourse()
206
    {
207
        return $this->course;
208
    }
209
210
    /**
211
     * @return Session
212
     */
213
    public function getSession()
214
    {
215
        return $this->session;
216
    }
217
218
    /**
219
     * @return Registrant[]|ArrayCollection
220
     */
221
    public function getRegistrants()
222
    {
223
        return $this->registrants;
224
    }
225
226
    /**
227
     * @return Recording[]|ArrayCollection
228
     */
229
    public function getRecordings()
230
    {
231
        return $this->recordings;
232
    }
233
234
    /**
235
     * @return MeetingActivity[]|ArrayCollection
236
     */
237
    public function getActivities()
238
    {
239
        return $this->activities;
240
    }
241
242
    public function addActivity(MeetingActivity $activity)
243
    {
244
        $activity->setMeeting($this);
245
        $this->activities[] = $activity;
246
    }
247
248
    /**
249
     * @param MeetingActivity[]|ArrayCollection $activities
250
     *
251
     * @return Meeting
252
     */
253
    public function setActivities($activities)
254
    {
255
        $this->activities = $activities;
256
257
        return $this;
258
    }
259
260
    /**
261
     * @ORM\PostLoad
262
     *
263
     * @throws Exception
264
     */
265
    public function postLoad()
266
    {
267
        if (null !== $this->meetingListItemJson) {
268
            $this->meetingListItem = MeetingListItem::fromJson($this->meetingListItemJson);
269
        }
270
        if (null !== $this->meetingInfoGetJson) {
271
            $this->meetingInfoGet = MeetingInfoGet::fromJson($this->meetingInfoGetJson);
272
        }
273
        $this->initializeDisplayableProperties();
274
    }
275
276
    /**
277
     * @ORM\PostUpdate
278
     *
279
     * @throws Exception
280
     */
281
    public function postUpdate()
282
    {
283
        $this->initializeDisplayableProperties();
284
    }
285
286
    /**
287
     * @ORM\PreFlush
288
     */
289
    public function preFlush()
290
    {
291
        if (null !== $this->meetingListItem) {
292
            $this->meetingListItemJson = json_encode($this->meetingListItem);
293
        }
294
        if (null !== $this->meetingInfoGet) {
295
            $this->meetingInfoGetJson = json_encode($this->meetingInfoGet);
296
        }
297
    }
298
299
    /**
300
     * @return MeetingListItem
301
     */
302
    public function getMeetingListItem()
303
    {
304
        return $this->meetingListItem;
305
    }
306
307
    /**
308
     * @return MeetingInfoGet
309
     */
310
    public function getMeetingInfoGet()
311
    {
312
        return $this->meetingInfoGet;
313
    }
314
315
    /**
316
     * @param User $user
317
     *
318
     * @return $this
319
     */
320
    public function setUser($user)
321
    {
322
        $this->user = $user;
323
324
        return $this;
325
    }
326
327
    /**
328
     * @param Course $course
329
     *
330
     * @return $this
331
     */
332
    public function setCourse($course)
333
    {
334
        $this->course = $course;
335
336
        return $this;
337
    }
338
339
    /**
340
     * @param Session $session
341
     *
342
     * @return $this
343
     */
344
    public function setSession($session)
345
    {
346
        $this->session = $session;
347
348
        return $this;
349
    }
350
351
    /**
352
     * @return CGroupInfo
353
     */
354
    public function getGroup()
355
    {
356
        return $this->group;
357
    }
358
359
    /**
360
     * @param CGroupInfo $group
361
     *
362
     * @return Meeting
363
     */
364
    public function setGroup($group)
365
    {
366
        $this->group = $group;
367
368
        return $this;
369
    }
370
371
    /**
372
     * @param MeetingListItem $meetingListItem
373
     *
374
     * @throws Exception
375
     *
376
     * @return Meeting
377
     */
378
    public function setMeetingListItem($meetingListItem)
379
    {
380
        if (null === $this->meetingId) {
381
            $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...
382
        } elseif ($this->meetingId != $meetingListItem->id) {
383
            throw new Exception('the Meeting identifier differs from the MeetingListItem identifier');
384
        }
385
        $this->meetingListItem = $meetingListItem;
386
387
        return $this;
388
    }
389
390
    /**
391
     * @param MeetingInfoGet|BaseMeetingTrait $meetingInfoGet
392
     *
393
     * @throws Exception
394
     *
395
     * @return Meeting
396
     */
397
    public function setMeetingInfoGet($meetingInfoGet)
398
    {
399
        if (null === $this->meetingId) {
400
            $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...
401
        } elseif ($this->meetingId != $meetingInfoGet->id) {
402
            throw new Exception('the Meeting identifier differs from the MeetingInfoGet identifier');
403
        }
404
        $this->meetingInfoGet = $meetingInfoGet;
405
        $this->initializeDisplayableProperties();
406
407
        return $this;
408
    }
409
410
    /**
411
     * @return bool
412
     */
413
    public function isCourseMeeting()
414
    {
415
        return null !== $this->course;
416
    }
417
418
    /**
419
     * @return bool
420
     */
421
    public function isCourseGroupMeeting()
422
    {
423
        return null !== $this->course && null !== $this->group;
424
    }
425
426
    /**
427
     * @return bool
428
     */
429
    public function isUserMeeting()
430
    {
431
        return null !== $this->user && null === $this->course;
432
    }
433
434
    /**
435
     * @return bool
436
     */
437
    public function isGlobalMeeting()
438
    {
439
        return null === $this->user && null === $this->course;
440
    }
441
442
    public function setStatus($status)
443
    {
444
        $this->meetingInfoGet->status = $status;
445
    }
446
447
    /**
448
     * Builds the list of users that can register into this meeting.
449
     * Zoom requires an email address, therefore users without an email address are excluded from the list.
450
     *
451
     * @return User[] the list of users
452
     */
453
    public function getRegistrableUsers()
454
    {
455
        $users = [];
456
        if (!$this->isCourseMeeting()) {
457
            $criteria = ['active' => true];
458
            $users = Database::getManager()->getRepository('ChamiloUserBundle:User')->findBy($criteria);
459
        } elseif (null === $this->session) {
460
            if (null !== $this->course) {
461
                /** @var CourseRelUser $courseRelUser */
462
                foreach ($this->course->getUsers() as $courseRelUser) {
463
                    $users[] = $courseRelUser->getUser();
464
                }
465
            }
466
        } else {
467
            if (null !== $this->course) {
468
                $subscriptions = $this->session->getUserCourseSubscriptionsByStatus($this->course, Session::STUDENT);
469
                if ($subscriptions) {
470
                    /** @var SessionRelCourseRelUser $sessionCourseUser */
471
                    foreach ($subscriptions as $sessionCourseUser) {
472
                        $users[] = $sessionCourseUser->getUser();
473
                    }
474
                }
475
            }
476
        }
477
478
        $activeUsersWithEmail = [];
479
        foreach ($users as $user) {
480
            if ($user->isActive() && !empty($user->getEmail())) {
481
                $activeUsersWithEmail[] = $user;
482
            }
483
        }
484
485
        return $activeUsersWithEmail;
486
    }
487
488
    /**
489
     * @return bool
490
     */
491
    public function requiresDateAndDuration()
492
    {
493
        return MeetingInfoGet::TYPE_SCHEDULED === $this->meetingInfoGet->type
494
            || MeetingInfoGet::TYPE_RECURRING_WITH_FIXED_TIME === $this->meetingInfoGet->type;
495
    }
496
497
    public function requiresRegistration(): bool
498
    {
499
        return true; //MeetingSettings::APPROVAL_TYPE_AUTOMATICALLY_APPROVE === $this->meetingInfoGet->settings->approval_type;
500
        /*return
501
            MeetingSettings::APPROVAL_TYPE_NO_REGISTRATION_REQUIRED != $this->meetingInfoGet->settings->approval_type;*/
502
    }
503
504
    /**
505
     * @return bool
506
     */
507
    public function hasCloudAutoRecordingEnabled()
508
    {
509
        return \ZoomPlugin::RECORDING_TYPE_NONE !== $this->meetingInfoGet->settings->auto_recording;
510
    }
511
512
    public function getRegistrantByUser(User $user): ?Registrant
513
    {
514
        $criteria = Criteria::create();
515
        $criteria
516
            ->where(
517
                Criteria::expr()->eq('user', $user)
518
            );
519
520
        $registrant = $this->registrants->matching($criteria)->first();
521
522
        return $registrant ?: null;
523
    }
524
525
    /**
526
     * Generates a short presentation of the meeting for the future participant.
527
     * To be displayed above the "Enter meeting" link.
528
     *
529
     * @return string
530
     */
531
    public function getIntroduction()
532
    {
533
        $introduction = sprintf('<h1>%s</h1>', $this->getTopic());
534
        if (!$this->isGlobalMeeting()) {
535
            if (!empty($this->formattedStartTime)) {
536
                $introduction .= $this->formattedStartTime;
537
                if (!empty($this->formattedDuration)) {
538
                    $introduction .= ' ('.$this->formattedDuration.')';
539
                }
540
            }
541
        }
542
        if ($this->user) {
543
            $introduction .= sprintf('<p>%s</p>', $this->user->getFullname());
544
        } elseif ($this->isCourseMeeting()) {
545
            if (null === $this->session) {
546
                $introduction .= sprintf('<p class="main">%s</p>', $this->course);
547
            } else {
548
                $introduction .= sprintf('<p class="main">%s (%s)</p>', $this->course, $this->session);
549
            }
550
        }
551
        if (!empty($this->getAgenda())) {
552
            $introduction .= sprintf('<p>%s</p>', $this->getAgenda());
553
        }
554
555
        return $introduction;
556
    }
557
558
    public function getAccountEmail(): ?string
559
    {
560
        return $this->accountEmail;
561
    }
562
563
    public function setAccountEmail(?string $accountEmail): self
564
    {
565
        $this->accountEmail = $accountEmail;
566
567
        return $this;
568
    }
569
570
    public function getTopic(): string
571
    {
572
        return $this->meetingInfoGet->topic;
573
    }
574
575
    public function getAgenda(): ?string
576
    {
577
        return $this->meetingInfoGet->agenda;
578
    }
579
580
    /**
581
     * @throws Exception on unexpected start_time or duration
582
     */
583
    protected function initializeDisplayableProperties()
584
    {
585
        $zoomPlugin = new \ZoomPlugin();
586
587
        $typeList = [
588
            API\Meeting::TYPE_INSTANT => $zoomPlugin->get_lang('InstantMeeting'),
589
            API\Meeting::TYPE_SCHEDULED => $zoomPlugin->get_lang('ScheduledMeeting'),
590
            API\Meeting::TYPE_RECURRING_WITH_NO_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithNoFixedTime'),
591
            API\Meeting::TYPE_RECURRING_WITH_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithFixedTime'),
592
        ];
593
        $this->typeName = $typeList[$this->meetingInfoGet->type];
594
595
        if (property_exists($this, 'status')) {
596
            $statusList = [
597
                'waiting' => $zoomPlugin->get_lang('Waiting'),
598
                'started' => $zoomPlugin->get_lang('Started'),
599
                'finished' => $zoomPlugin->get_lang('Finished'),
600
            ];
601
            $this->statusName = $statusList[$this->meetingInfoGet->status];
602
        }
603
        $this->startDateTime = null;
604
        $this->formattedStartTime = '';
605
        $this->durationInterval = null;
606
        $this->formattedDuration = '';
607
        if (!empty($this->meetingInfoGet->start_time)) {
608
            $this->startDateTime = new DateTime($this->meetingInfoGet->start_time);
609
            $this->startDateTime->setTimezone(new DateTimeZone(api_get_timezone()));
610
            $this->formattedStartTime = $this->startDateTime->format('Y-m-d H:i');
611
        }
612
613
        if (!empty($this->meetingInfoGet->duration)) {
614
            $now = new DateTime();
615
            $later = new DateTime();
616
            $later->add(new DateInterval('PT'.$this->meetingInfoGet->duration.'M'));
617
            $this->durationInterval = $later->diff($now);
618
            $this->formattedDuration = $this->durationInterval->format($zoomPlugin->get_lang('DurationFormat'));
619
        }
620
    }
621
}
622