Passed
Push — 1.11.x ( 9ec170...aa9893 )
by Julito
10:19 queued 13s
created

BBBPlugin::install()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 80
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 43
nc 1
nop 0
dl 0
loc 80
rs 9.232
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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