Completed
Push — master ( a73e85...13dd2e )
by Julito
08:28
created

BBBPlugin::uninstall()   B

Complexity

Conditions 8
Paths 68

Size

Total Lines 70
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 47
c 0
b 0
f 0
nc 68
nop 0
dl 0
loc 70
rs 7.9119

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_HTML5 => 'HTML5',
78
                        self::INTERFACE_FLASH => 'Flash',
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
                close INT NOT NULL DEFAULT 0
212
            );"
213
        );
214
        $fieldLabel = 'plugin_bbb_course_users_limit';
215
        $fieldType = ExtraField::FIELD_TYPE_INTEGER;
216
        $fieldTitle = 'MaxUsersInConferenceRoom';
217
        $fieldDefault = '0';
218
        $extraField = new ExtraField('course');
219
        $fieldId = CourseManager::create_course_extra_field(
220
            $fieldLabel,
221
            $fieldType,
222
            $fieldTitle,
223
            $fieldDefault
224
        );
225
        $extraField->find($fieldId);
226
        $extraField->update(
227
            [
228
                'id' => $fieldId,
229
                'variable' => 'plugin_bbb_course_users_limit',
230
                'changeable' => 1,
231
                'visible_to_self' => 1,
232
                'visible_to_others' => 0,
233
            ]
234
        );
235
        $fieldLabel = 'plugin_bbb_session_users_limit';
236
        $extraField = new ExtraField('session');
237
        $fieldId = SessionManager::create_session_extra_field(
238
            $fieldLabel,
239
            $fieldType,
240
            $fieldTitle,
241
            $fieldDefault
242
        );
243
        $extraField->find($fieldId);
244
        $extraField->update(
245
            [
246
                'id' => $fieldId,
247
                'variable' => 'plugin_bbb_session_users_limit',
248
                'changeable' => 1,
249
                'visible_to_self' => 1,
250
                'visible_to_others' => 0,
251
            ]
252
        );
253
254
        // Installing course settings
255
        $this->install_course_fields_in_all_courses();
256
    }
257
258
    /**
259
     * Uninstall
260
     */
261
    public function uninstall()
262
    {
263
        $t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
264
        $t_options = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
265
        $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
266
267
        $variables = [
268
            'bbb_salt',
269
            'bbb_host',
270
            'bbb_tool_enable',
271
            'enable_global_conference',
272
            'enable_global_conference_per_user',
273
            'enable_global_conference_link',
274
            'disable_download_conference_link',
275
            'enable_conference_in_course_groups',
276
            'bbb_plugin',
277
            'bbb_plugin_host',
278
            'bbb_plugin_salt',
279
            'max_users_limit',
280
            'global_conference_allow_roles',
281
            'interface',
282
            'launch_type',
283
        ];
284
285
        $urlId = api_get_current_access_url_id();
286
287
        foreach ($variables as $variable) {
288
            $sql = "DELETE FROM $t_settings WHERE variable = '$variable' AND access_url = $urlId";
289
            Database::query($sql);
290
        }
291
292
        $em = Database::getManager();
293
        $sm = $em->getConnection()->getSchemaManager();
294
        if ($sm->tablesExist('plugin_bbb_meeting')) {
295
            Database::query("DELETE FROM plugin_bbb_meeting WHERE access_url = $urlId");
296
        }
297
298
        // Only delete tables if it's uninstalled from main url.
299
        if (1 == $urlId) {
300
            $extraField = new ExtraField('course');
301
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
302
                'plugin_bbb_course_users_limit'
303
            );
304
            if (!empty($extraFieldInfo)) {
305
                $extraField->delete($extraFieldInfo['id']);
306
            }
307
            $extraField = new ExtraField('session');
308
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
309
                'plugin_bbb_session_users_limit'
310
            );
311
            if (!empty($extraFieldInfo)) {
312
                $extraField->delete($extraFieldInfo['id']);
313
            }
314
315
            $sql = "DELETE FROM $t_options WHERE variable = 'bbb_plugin'";
316
            Database::query($sql);
317
318
            // hack to get rid of Database::query warning (please add c_id...)
319
            $sql = "DELETE FROM $t_tool WHERE name = 'bbb' AND c_id != 0";
320
            Database::query($sql);
321
322
            if ($sm->tablesExist('plugin_bbb_room')) {
323
                Database::query('DROP TABLE IF EXISTS plugin_bbb_room');
324
            }
325
            if ($sm->tablesExist('plugin_bbb_meeting')) {
326
                Database::query('DROP TABLE IF EXISTS plugin_bbb_meeting');
327
            }
328
329
            // Deleting course settings
330
            $this->uninstall_course_fields_in_all_courses($this->course_settings);
331
        }
332
    }
333
334
    /**
335
     * Update
336
     */
337
    public function update()
338
    {
339
        $sql = "SHOW COLUMNS FROM plugin_bbb_room WHERE Field = 'close'";
340
        $res = Database::query($sql);
341
342
        if (Database::num_rows($res) === 0) {
343
            $sql = "ALTER TABLE plugin_bbb_room ADD close int unsigned NULL";
344
            $res = Database::query($sql);
345
            if (!$res) {
0 ignored issues
show
introduced by
$res is of type Doctrine\DBAL\Driver\Statement, thus it always evaluated to true.
Loading history...
346
                echo Display::return_message($this->get_lang('ErrorUpdateFieldDB'), 'warning');
347
            }
348
349
            Database::update(
350
                'plugin_bbb_room',
351
                ['close' => BBBPlugin::ROOM_CLOSE]
352
            );
353
        }
354
    }
355
356
    /**
357
     * Return an array with URL
358
     *
359
     * @param string $conferenceUrl
360
     *
361
     * @return array
362
     */
363
    public function getUrlInterfaceLinks($conferenceUrl)
364
    {
365
        $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...
366
        $urlList[] = $this->getHtmlUrl($conferenceUrl);
367
368
        return $urlList;
369
    }
370
371
    /**
372
     * @param string $conferenceUrl
373
     *
374
     * @return array
375
     */
376
    public function getFlashUrl($conferenceUrl)
377
    {
378
        $data = [
379
            'text' => $this->get_lang('EnterConferenceFlash'),
380
            'url' => $conferenceUrl.'&interface='.self::INTERFACE_FLASH,
381
            'icon' => 'resources/img/64/videoconference_flash.png',
382
        ];
383
384
        return $data;
385
    }
386
387
    /**
388
     * @param string $conferenceUrl
389
     *
390
     * @return array
391
     */
392
    public function getHtmlUrl($conferenceUrl)
393
    {
394
        $data = [
395
            'text' => $this->get_lang('EnterConferenceHTML5'),
396
            'url' => $conferenceUrl.'&interface='.self::INTERFACE_HTML5,
397
            'icon' => 'resources/img/64/videoconference_html5.png',
398
        ];
399
400
        return $data;
401
    }
402
403
    /**
404
     * Set the course setting in all courses
405
     *
406
     * @param bool $variable Course setting to update
407
     * @param bool $value New values of the course setting
408
     */
409
    public function update_course_field_in_all_courses($variable, $value)
410
    {
411
        // Update existing courses to add the new course setting value
412
        $table = Database::get_main_table(TABLE_MAIN_COURSE);
413
        $sql = "SELECT id FROM $table ORDER BY id";
414
        $res = Database::query($sql);
415
        $courseSettingTable = Database::get_course_table(TABLE_COURSE_SETTING);
416
        while ($row = Database::fetch_assoc($res)) {
417
            Database::update(
418
                $courseSettingTable,
419
                ['value' => $value],
420
                ['variable = ? AND c_id = ?' => [$variable, $row['id']]]
421
            );
422
        }
423
    }
424
}
425