Passed
Push — 1.11.x ( 1aa6fe...d6a61e )
by Julito
09:41
created

BBBPlugin::getFlashUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/* To show the plugin course icons you need to add these icons:
6
 * main/img/icons/22/plugin_name.png
7
 * main/img/icons/64/plugin_name.png
8
 * main/img/icons/64/plugin_name_na.png
9
*/
10
11
/**
12
 * Class BBBPlugin
13
 * Videoconference plugin with BBB
14
 */
15
class BBBPlugin extends Plugin
16
{
17
    const INTERFACE_FLASH = 0;
18
    const INTERFACE_HTML5 = 1;
19
20
    const LAUNCH_TYPE_DEFAULT = 0;
21
    const LAUNCH_TYPE_SET_BY_TEACHER = 1;
22
    const LAUNCH_TYPE_SET_BY_STUDENT = 2;
23
24
    public $isCoursePlugin = true;
25
26
    // When creating a new course this settings are added to the course
27
    public $course_settings = [
28
        [
29
            'name' => 'big_blue_button_record_and_store',
30
            'type' => 'checkbox',
31
        ],
32
        [
33
            'name' => 'bbb_enable_conference_in_groups',
34
            'type' => 'checkbox',
35
        ],
36
        [
37
            'name' => 'bbb_force_record_generation',
38
            'type' => 'checkbox',
39
        ],
40
    ];
41
42
    /**
43
     * BBBPlugin constructor.
44
     */
45
    protected function __construct()
46
    {
47
        parent::__construct(
48
            '2.8.1',
49
            'Julio Montoya, Yannick Warnier, Angel Fernando Quiroz Campos',
50
            [
51
                'tool_enable' => 'boolean',
52
                'host' => 'text',
53
                'salt' => 'text',
54
                'enable_global_conference' => 'boolean',
55
                'enable_global_conference_per_user' => 'boolean',
56
                'enable_conference_in_course_groups' => 'boolean',
57
                'enable_global_conference_link' => 'boolean',
58
                'disable_download_conference_link' => 'boolean',
59
                'max_users_limit' => 'text',
60
                'global_conference_allow_roles' => [
61
                    'type' => 'select',
62
                    'options' => [
63
                        PLATFORM_ADMIN => get_lang('Administrator'),
64
                        COURSEMANAGER => get_lang('Teacher'),
65
                        STUDENT => get_lang('Student'),
66
                        STUDENT_BOSS => get_lang('StudentBoss'),
67
                    ],
68
                    'attributes' => ['multiple' => 'multiple'],
69
                ],
70
                'interface' => [
71
                    'type' => 'select',
72
                    'options' => [
73
                        self::INTERFACE_FLASH => 'Flash',
74
                        self::INTERFACE_HTML5 => 'HTML5',
75
                    ],
76
                ],
77
                'launch_type' => [
78
                    'type' => 'select',
79
                    'options' => [
80
                        self::LAUNCH_TYPE_DEFAULT => 'SetByDefault',
81
                        self::LAUNCH_TYPE_SET_BY_TEACHER => 'SetByTeacher',
82
                        self::LAUNCH_TYPE_SET_BY_STUDENT => 'SetByStudent',
83
                    ],
84
                    'translate_options' => true, // variables will be translated using the plugin->get_lang
85
                ],
86
                'allow_regenerate_recording' => 'boolean',
87
                // Default course settings, must be the same as $course_settings
88
                'big_blue_button_record_and_store' => 'checkbox',
89
                'bbb_enable_conference_in_groups' => 'checkbox',
90
                'bbb_force_record_generation' => 'checkbox',
91
                'disable_course_settings' => 'boolean',
92
            ]
93
        );
94
95
        $this->isAdminPlugin = true;
0 ignored issues
show
Bug Best Practice introduced by
The property isAdminPlugin does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
    }
97
98
    /**
99
     * @return BBBPlugin|null
100
     */
101
    public static function create()
102
    {
103
        static $result = null;
104
105
        return $result ? $result : $result = new self();
106
    }
107
108
    /**
109
     * @param string $variable
110
     *
111
     * @return bool
112
     */
113
    public function validateCourseSetting($variable)
114
    {
115
        if ($this->get('disable_course_settings') === 'true') {
116
            return false;
117
        }
118
119
        $result = true;
120
        switch ($variable) {
121
            case 'bbb_enable_conference_in_groups':
122
                $result = $this->get('enable_conference_in_course_groups') === 'true';
123
                break;
124
            case 'bbb_force_record_generation':
125
                $result = $this->get('allow_regenerate_recording') === 'true';
126
                break;
127
            case 'big_blue_button_record_and_store':
128
        }
129
130
        return $result;
131
    }
132
133
    /**
134
     *
135
     * @return array
136
     */
137
    public function getCourseSettings()
138
    {
139
        $settings = [];
140
        if ($this->get('disable_course_settings') !== 'true') {
141
            $settings = parent::getCourseSettings();
142
        }
143
144
        return $settings;
145
    }
146
147
    /**
148
     *
149
     * @return \Plugin
150
     */
151
    public function performActionsAfterConfigure()
152
    {
153
        $result = $this->get('disable_course_settings') === 'true';
154
        if ($result) {
155
            $valueConference = $this->get('bbb_enable_conference_in_groups') === 'true' ? 1 : 0;
156
            self::update_course_field_in_all_courses('bbb_enable_conference_in_groups', $valueConference);
157
158
            $valueForceRecordGeneration = $this->get('bbb_force_record_generation') === 'true' ? 1 : 0;
159
            self::update_course_field_in_all_courses('bbb_force_record_generation', $valueForceRecordGeneration);
160
161
            $valueForceRecordStore = $this->get('big_blue_button_record_and_store') === 'true' ? 1 : 0;
162
            self::update_course_field_in_all_courses('big_blue_button_record_and_store', $valueForceRecordStore);
163
        }
164
165
        return $this;
166
    }
167
168
    /**
169
     * Install
170
     */
171
    public function install()
172
    {
173
        $sql = "CREATE TABLE IF NOT EXISTS plugin_bbb_meeting (
174
                id INT unsigned NOT NULL auto_increment PRIMARY KEY,
175
                c_id INT unsigned NOT NULL DEFAULT 0,
176
                group_id INT unsigned NOT NULL DEFAULT 0,
177
                user_id INT unsigned NOT NULL DEFAULT 0,
178
                meeting_name VARCHAR(255) NOT NULL DEFAULT '',
179
                attendee_pw VARCHAR(255) NOT NULL DEFAULT '',
180
                moderator_pw VARCHAR(255) NOT NULL DEFAULT '',
181
                record INT NOT NULL DEFAULT 0,
182
                status INT NOT NULL DEFAULT 0,
183
                created_at VARCHAR(255) NOT NULL,
184
                closed_at VARCHAR(255) NOT NULL,
185
                calendar_id INT DEFAULT 0,
186
                welcome_msg VARCHAR(255) NOT NULL DEFAULT '',
187
                session_id INT unsigned DEFAULT 0,
188
                remote_id CHAR(30),
189
                internal_meeting_id VARCHAR(255) DEFAULT NULL,
190
                visibility TINYINT NOT NULL DEFAULT 1,
191
                voice_bridge INT NOT NULL DEFAULT 1,
192
                access_url INT NOT NULL DEFAULT 1,
193
                video_url TEXT NULL,
194
                has_video_m4v TINYINT NOT NULL DEFAULT 0,
195
                interface INT NOT NULL DEFAULT 0
196
                )";
197
        Database::query($sql);
198
199
        Database::query(
200
            "CREATE TABLE IF NOT EXISTS plugin_bbb_room (
201
                id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
202
                meeting_id int NOT NULL,
203
                participant_id int(11) NOT NULL,
204
                in_at datetime,
205
                out_at datetime,
206
                interface int NOT NULL DEFAULT 0
207
            );"
208
        );
209
        $fieldLabel = 'plugin_bbb_course_users_limit';
210
        $fieldType = ExtraField::FIELD_TYPE_INTEGER;
211
        $fieldTitle = 'MaxUsersInConferenceRoom';
212
        $fieldDefault = '0';
213
        $extraField = new ExtraField('course');
214
        $fieldId = CourseManager::create_course_extra_field(
215
            $fieldLabel,
216
            $fieldType,
217
            $fieldTitle,
218
            $fieldDefault
219
        );
220
        $extraField->find($fieldId);
221
        $extraField->update(
222
            [
223
                'id' => $fieldId,
224
                'variable' => 'plugin_bbb_course_users_limit',
225
                'changeable' => 1,
226
                'visible_to_self' => 1,
227
                'visible_to_others' => 0,
228
            ]
229
        );
230
        $fieldLabel = 'plugin_bbb_session_users_limit';
231
        $extraField = new ExtraField('session');
232
        $fieldId = SessionManager::create_session_extra_field(
233
            $fieldLabel,
234
            $fieldType,
235
            $fieldTitle,
236
            $fieldDefault
237
        );
238
        $extraField->find($fieldId);
239
        $extraField->update(
240
            [
241
                'id' => $fieldId,
242
                'variable' => 'plugin_bbb_session_users_limit',
243
                'changeable' => 1,
244
                'visible_to_self' => 1,
245
                'visible_to_others' => 0,
246
            ]
247
        );
248
249
        // Installing course settings
250
        $this->install_course_fields_in_all_courses();
251
    }
252
253
    /**
254
     * Uninstall
255
     */
256
    public function uninstall()
257
    {
258
        $t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
259
        $t_options = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
260
        $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
261
        $urlId = api_get_current_access_url_id();
262
        $variables = [
263
            'bbb_salt',
264
            'bbb_host',
265
            'bbb_tool_enable',
266
            'enable_global_conference',
267
            'enable_global_conference_per_user',
268
            'enable_global_conference_link',
269
            'disable_download_conference_link',
270
            'enable_conference_in_course_groups',
271
            'bbb_plugin',
272
            'bbb_plugin_host',
273
            'bbb_plugin_salt',
274
            'max_users_limit',
275
            'global_conference_allow_roles',
276
            'interface',
277
            'launch_type',
278
        ];
279
280
        foreach ($variables as $variable) {
281
            $sql = "DELETE FROM $t_settings WHERE variable = '$variable' AND access_url = $urlId";
282
            Database::query($sql);
283
        }
284
285
        // Only delete if it's uninstalled from main url.
286
        if (1 == $urlId) {
287
            $extraField = new ExtraField('course');
288
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
289
                'plugin_bbb_course_users_limit'
290
            );
291
            if (!empty($extraFieldInfo)) {
292
                $extraField->delete($extraFieldInfo['id']);
293
            }
294
            $extraField = new ExtraField('session');
295
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
296
                'plugin_bbb_session_users_limit'
297
            );
298
            if (!empty($extraFieldInfo)) {
299
                $extraField->delete($extraFieldInfo['id']);
300
            }
301
302
            $sql = "DELETE FROM $t_options WHERE variable = 'bbb_plugin'";
303
            Database::query($sql);
304
305
            // hack to get rid of Database::query warning (please add c_id...)
306
            $sql = "DELETE FROM $t_tool WHERE name = 'bbb' AND c_id != 0";
307
            Database::query($sql);
308
309
            Database::query('DROP TABLE IF EXISTS plugin_bbb_room');
310
            Database::query('DROP TABLE IF EXISTS plugin_bbb_meeting');
311
312
            // Deleting course settings
313
            $this->uninstall_course_fields_in_all_courses($this->course_settings);
314
        }
315
    }
316
317
    /**
318
     * Return an array with URL
319
     *
320
     * @param string $conferenceUrl
321
     *
322
     * @return array
323
     */
324
    public function getUrlInterfaceLinks($conferenceUrl)
325
    {
326
        $urlList[] = $this->getFlashUrl($conferenceUrl);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$urlList was never initialized. Although not strictly required by PHP, it is generally a good practice to add $urlList = array(); before regardless.
Loading history...
327
        $urlList[] = $this->getHtmlUrl($conferenceUrl);
328
329
        return $urlList;
330
    }
331
332
    /**
333
     * @param string $conferenceUrl
334
     *
335
     * @return array
336
     */
337
    public function getFlashUrl($conferenceUrl)
338
    {
339
        $data = [
340
            'text' => $this->get_lang('EnterConferenceFlash'),
341
            'url' => $conferenceUrl.'&interface='.self::INTERFACE_FLASH,
342
            'icon' => 'resources/img/64/videoconference_flash.png',
343
        ];
344
345
        return $data;
346
    }
347
348
    /**
349
     * @param string $conferenceUrl
350
     *
351
     * @return array
352
     */
353
    public function getHtmlUrl($conferenceUrl)
354
    {
355
        $data = [
356
            'text' => $this->get_lang('EnterConferenceHTML5'),
357
            'url' => $conferenceUrl.'&interface='.self::INTERFACE_HTML5,
358
            'icon' => 'resources/img/64/videoconference_html5.png',
359
        ];
360
361
        return $data;
362
    }
363
364
    /**
365
     * Set the course setting in all courses
366
     *
367
     * @param bool $variable Course setting to update
368
     * @param bool $value New values of the course setting
369
     */
370
    public function update_course_field_in_all_courses($variable, $value)
371
    {
372
        // Update existing courses to add the new course setting value
373
        $table = Database::get_main_table(TABLE_MAIN_COURSE);
374
        $sql = "SELECT id FROM $table ORDER BY id";
375
        $res = Database::query($sql);
376
        $courseSettingTable = Database::get_course_table(TABLE_COURSE_SETTING);
377
        while ($row = Database::fetch_assoc($res)) {
378
            Database::update(
379
                $courseSettingTable,
380
                ['value' => $value],
381
                ['variable = ? AND c_id = ?' => [$variable, $row['id']]]
382
            );
383
        }
384
    }
385
}
386