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

Webinar::setWebinarSchema()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 1
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 DateInterval;
9
use DateTime;
10
use Doctrine\ORM\Mapping as ORM;
11
use Exception;
12
use ZoomPlugin;
13
14
/**
15
 * @ORM\Entity()
16
 * @ORM\HasLifecycleCallbacks
17
 */
18
class Webinar extends Meeting
19
{
20
    /**
21
     * @var string
22
     *
23
     * @ORM\Column(name="webinar_schema_json", type="text", nullable=true)
24
     */
25
    protected $webinarSchemaJson;
26
27
    /**
28
     * @var WebinarSchema
29
     */
30
    protected $webinarSchema;
31
32
    public function preFlush()
33
    {
34
        if (null !== $this->webinarSchema) {
35
            $this->webinarSchemaJson = json_encode($this->webinarSchema);
36
        }
37
    }
38
39
    public function postLoad()
40
    {
41
        if (null !== $this->webinarSchemaJson) {
42
            $this->webinarSchema = WebinarSchema::fromJson($this->webinarSchemaJson);
43
        }
44
45
        $this->initializeDisplayableProperties();
46
    }
47
48
    /**
49
     * @throws Exception
50
     */
51
    public function setWebinarSchema(WebinarSchema $webinarSchema): Webinar
52
    {
53
        if (null === $this->meetingId) {
54
            $this->meetingId = $webinarSchema->id;
55
        } elseif ($this->meetingId != $webinarSchema->id) {
56
            throw new Exception('the Meeting identifier differs from the MeetingInfoGet identifier');
57
        }
58
59
        $this->webinarSchema = $webinarSchema;
60
61
        $this->initializeDisplayableProperties();
62
63
        return $this;
64
    }
65
66
    public function getWebinarSchema(): WebinarSchema
67
    {
68
        return $this->webinarSchema;
69
    }
70
71
    protected function initializeDisplayableProperties()
72
    {
73
        $zoomPlugin = ZoomPlugin::create();
74
75
        $namedTypes = [
76
            WebinarSchema::TYPE_WEBINAR => $zoomPlugin->get_lang('Webinar'),
77
            WebinarSchema::TYPE_RECURRING_NO_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithNoFixedTime'),
78
            WebinarSchema::TYPE_RECURRING_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithFixedTime'),
79
        ];
80
81
        $this->typeName = $namedTypes[$this->webinarSchema->type];
82
83
        if ($this->webinarSchema->start_time) {
84
            $this->startDateTime = new DateTime(
85
                $this->webinarSchema->start_time,
86
                new \DateTimeZone(api_get_timezone())
87
            );
88
            $this->formattedStartTime = $this->startDateTime->format('Y-m-d H:i');
89
        }
90
91
        if ($this->webinarSchema->duration) {
92
            $now = new DateTime();
93
            $later = new DateTime();
94
            $later->add(
95
                new DateInterval('PT'.$this->webinarSchema->duration.'M')
96
            );
97
            $this->durationInterval = $later->diff($now);
98
            $this->formattedDuration = $this->durationInterval->format(
99
                $zoomPlugin->get_lang('DurationFormat')
100
            );
101
        }
102
    }
103
104
    public function hasCloudAutoRecordingEnabled(): bool
105
    {
106
        return $this->webinarSchema->settings->auto_recording !== ZoomPlugin::RECORDING_TYPE_NONE;
107
    }
108
}
109