Passed
Pull Request — 1.11.x (#4160)
by Angel Fernando Quiroz
16:08 queued 03:42
created

Webinar::initializeDisplayableProperties()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 29
rs 9.6333
c 1
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\Zoom;
6
7
use Chamilo\PluginBundle\Zoom\API\WebinarSchema;
8
use Chamilo\PluginBundle\Zoom\API\WebinarSettings;
9
use DateInterval;
10
use DateTime;
11
use Doctrine\ORM\Mapping as ORM;
12
use Exception;
13
use ZoomPlugin;
14
15
/**
16
 * @ORM\Entity()
17
 * @ORM\HasLifecycleCallbacks
18
 */
19
class Webinar extends Meeting
20
{
21
    /**
22
     * @var string
23
     *
24
     * @ORM\Column(name="webinar_schema_json", type="text", nullable=true)
25
     */
26
    protected $webinarSchemaJson;
27
28
    /**
29
     * @var WebinarSchema
30
     */
31
    protected $webinarSchema;
32
33
    public function preFlush()
34
    {
35
        if (null !== $this->webinarSchema) {
36
            $this->webinarSchemaJson = json_encode($this->webinarSchema);
37
        }
38
    }
39
40
    public function postLoad()
41
    {
42
        if (null !== $this->webinarSchemaJson) {
43
            $this->webinarSchema = WebinarSchema::fromJson($this->webinarSchemaJson);
44
        }
45
46
        $this->initializeDisplayableProperties();
47
    }
48
49
    /**
50
     * @throws Exception
51
     */
52
    public function setWebinarSchema(WebinarSchema $webinarSchema): Webinar
53
    {
54
        if (null === $this->meetingId) {
55
            $this->meetingId = $webinarSchema->id;
56
        } elseif ($this->meetingId != $webinarSchema->id) {
57
            throw new Exception('the Meeting identifier differs from the MeetingInfoGet identifier');
58
        }
59
60
        $this->webinarSchema = $webinarSchema;
61
62
        $this->initializeDisplayableProperties();
63
64
        return $this;
65
    }
66
67
    public function getWebinarSchema(): WebinarSchema
68
    {
69
        return $this->webinarSchema;
70
    }
71
72
    protected function initializeDisplayableProperties()
73
    {
74
        $zoomPlugin = ZoomPlugin::create();
75
76
        $namedTypes = [
77
            WebinarSchema::TYPE_WEBINAR => $zoomPlugin->get_lang('Webinar'),
78
            WebinarSchema::TYPE_RECURRING_NO_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithNoFixedTime'),
79
            WebinarSchema::TYPE_RECURRING_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithFixedTime'),
80
        ];
81
82
        $this->typeName = $namedTypes[$this->webinarSchema->type];
83
84
        if ($this->webinarSchema->start_time) {
85
            $this->startDateTime = new DateTime(
86
                $this->webinarSchema->start_time,
87
                new \DateTimeZone(api_get_timezone())
88
            );
89
            $this->formattedStartTime = $this->startDateTime->format('Y-m-d H:i');
90
        }
91
92
        if ($this->webinarSchema->duration) {
93
            $now = new DateTime();
94
            $later = new DateTime();
95
            $later->add(
96
                new DateInterval('PT'.$this->webinarSchema->duration.'M')
97
            );
98
            $this->durationInterval = $later->diff($now);
99
            $this->formattedDuration = $this->durationInterval->format(
100
                $zoomPlugin->get_lang('DurationFormat')
101
            );
102
        }
103
    }
104
105
    public function hasCloudAutoRecordingEnabled(): bool
106
    {
107
        return $this->webinarSchema->settings->auto_recording !== ZoomPlugin::RECORDING_TYPE_NONE;
108
    }
109
110
    public function requiresDateAndDuration(): bool
111
    {
112
        return WebinarSchema::TYPE_WEBINAR == $this->webinarSchema->type;
113
    }
114
115
    public function requiresRegistration(): bool
116
    {
117
        return in_array(
118
            $this->webinarSchema->settings->approval_type,
119
            [
120
                WebinarSettings::APPROVAL_TYPE_AUTOMATICALLY_APPROVE,
121
                WebinarSettings::APPROVAL_TYPE_MANUALLY_APPROVE,
122
            ]
123
        );
124
    }
125
126
    public function getTopic(): string
127
    {
128
        return $this->webinarSchema->topic;
129
    }
130
131
    public function getAgenda(): ?string
132
    {
133
        return $this->webinarSchema->agenda;
134
    }
135
}
136