Passed
Pull Request — 1.11.x (#4363)
by Angel Fernando Quiroz
07:54
created

Meeting::getReasonToSignAttendance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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