Passed
Pull Request — 1.11.x (#3998)
by Ghazi
11:49
created

BBBPlugin::uninstall()   F

Complexity

Conditions 12
Paths 772

Size

Total Lines 91
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 62
nc 772
nop 0
dl 0
loc 91
rs 3.3224
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Videoconference plugin with BBB
13
 */
14
class BBBPlugin extends Plugin
15
{
16
    const ROOM_OPEN = 0;
17
    const ROOM_CLOSE = 1;
18
    const ROOM_CHECK = 2;
19
20
    public $isCoursePlugin = true;
21
22
    // When creating a new course this settings are added to the course
23
    public $course_settings = [
24
        [
25
            'name' => 'big_blue_button_record_and_store',
26
            'type' => 'checkbox',
27
        ],
28
        [
29
            'name' => 'bbb_enable_conference_in_groups',
30
            'type' => 'checkbox',
31
        ],
32
        [
33
            'name' => 'bbb_force_record_generation',
34
            'type' => 'checkbox',
35
        ],
36
        [
37
            'name' => 'big_blue_button_students_start_conference_in_groups',
38
            'type' => 'checkbox',
39
        ],
40
    ];
41
42
    /**
43
     * BBBPlugin constructor.
44
     */
45
    protected function __construct()
46
    {
47
        parent::__construct(
48
            '2.10',
49
            'Julio Montoya, Yannick Warnier, Angel Fernando Quiroz Campos, Jose Angel Ruiz',
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
                'allow_regenerate_recording' => 'boolean',
71
                // Default course settings, must be the same as $course_settings
72
                'big_blue_button_record_and_store' => 'checkbox',
73
                'bbb_enable_conference_in_groups' => 'checkbox',
74
                'bbb_force_record_generation' => 'checkbox',
75
                'disable_course_settings' => 'boolean',
76
                'meeting_duration' => 'text',
77
            ]
78
        );
79
80
        $this->isAdminPlugin = true;
81
    }
82
83
    /**
84
     * @return BBBPlugin|null
85
     */
86
    public static function create()
87
    {
88
        static $result = null;
89
90
        return $result ? $result : $result = new self();
91
    }
92
93
    /**
94
     * @param string $variable
95
     *
96
     * @return bool
97
     */
98
    public function validateCourseSetting($variable)
99
    {
100
        if ($this->get('disable_course_settings') === 'true') {
101
            return false;
102
        }
103
104
        $result = true;
105
        switch ($variable) {
106
            case 'bbb_enable_conference_in_groups':
107
                $result = $this->get('enable_conference_in_course_groups') === 'true';
108
                break;
109
            case 'bbb_force_record_generation':
110
                $result = $this->get('allow_regenerate_recording') === 'true';
111
                break;
112
            case 'big_blue_button_record_and_store':
113
        }
114
115
        return $result;
116
    }
117
118
    /**
119
     *
120
     * @return array
121
     */
122
    public function getCourseSettings()
123
    {
124
        $settings = [];
125
        if ($this->get('disable_course_settings') !== 'true') {
126
            $settings = parent::getCourseSettings();
127
        }
128
129
        return $settings;
130
    }
131
132
    /**
133
     *
134
     * @return \Plugin
135
     */
136
    public function performActionsAfterConfigure()
137
    {
138
        $result = $this->get('disable_course_settings') === 'true';
139
        if ($result) {
140
            $valueConference = $this->get('bbb_enable_conference_in_groups') === 'true' ? 1 : 0;
141
            self::update_course_field_in_all_courses('bbb_enable_conference_in_groups', $valueConference);
142
143
            $valueForceRecordGeneration = $this->get('bbb_force_record_generation') === 'true' ? 1 : 0;
144
            self::update_course_field_in_all_courses('bbb_force_record_generation', $valueForceRecordGeneration);
145
146
            $valueForceRecordStore = $this->get('big_blue_button_record_and_store') === 'true' ? 1 : 0;
147
            self::update_course_field_in_all_courses('big_blue_button_record_and_store', $valueForceRecordStore);
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * Install
155
     */
156
    public function install()
157
    {
158
        $sql = "CREATE TABLE IF NOT EXISTS plugin_bbb_meeting (
159
                id INT unsigned NOT NULL auto_increment PRIMARY KEY,
160
                c_id INT unsigned NOT NULL DEFAULT 0,
161
                group_id INT unsigned NOT NULL DEFAULT 0,
162
                user_id INT unsigned NOT NULL DEFAULT 0,
163
                meeting_name VARCHAR(255) NOT NULL DEFAULT '',
164
                attendee_pw VARCHAR(255) NOT NULL DEFAULT '',
165
                moderator_pw VARCHAR(255) NOT NULL DEFAULT '',
166
                record INT NOT NULL DEFAULT 0,
167
                status INT NOT NULL DEFAULT 0,
168
                created_at VARCHAR(255) NOT NULL,
169
                closed_at VARCHAR(255) NOT NULL,
170
                calendar_id INT DEFAULT 0,
171
                welcome_msg VARCHAR(255) NOT NULL DEFAULT '',
172
                session_id INT unsigned DEFAULT 0,
173
                remote_id CHAR(30),
174
                internal_meeting_id VARCHAR(255) DEFAULT NULL,
175
                visibility TINYINT NOT NULL DEFAULT 1,
176
                voice_bridge INT NOT NULL DEFAULT 1,
177
                access_url INT NOT NULL DEFAULT 1,
178
                video_url TEXT NULL,
179
                has_video_m4v TINYINT NOT NULL DEFAULT 0
180
                )";
181
        Database::query($sql);
182
183
        Database::query(
184
            "CREATE TABLE IF NOT EXISTS plugin_bbb_room (
185
                id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
186
                meeting_id int NOT NULL,
187
                participant_id int(11) NOT NULL,
188
                in_at datetime,
189
                out_at datetime,
190
                close INT NOT NULL DEFAULT 0
191
            );"
192
        );
193
        $fieldLabel = 'plugin_bbb_course_users_limit';
194
        $fieldType = ExtraField::FIELD_TYPE_INTEGER;
195
        $fieldTitle = $this->get_lang('MaxUsersInConferenceRoom');
196
        $fieldDefault = '0';
197
        $extraField = new ExtraField('course');
198
        $fieldId = CourseManager::create_course_extra_field(
199
            $fieldLabel,
200
            $fieldType,
201
            $fieldTitle,
202
            $fieldDefault
203
        );
204
        $extraField->find($fieldId);
205
        $extraField->update(
206
            [
207
                'id' => $fieldId,
208
                'variable' => 'plugin_bbb_course_users_limit',
209
                'changeable' => 1,
210
                'visible_to_self' => 1,
211
                'visible_to_others' => 0,
212
            ]
213
        );
214
        $fieldLabel = 'plugin_bbb_session_users_limit';
215
        $extraField = new ExtraField('session');
216
        $fieldId = SessionManager::create_session_extra_field(
217
            $fieldLabel,
218
            $fieldType,
219
            $fieldTitle,
220
            $fieldDefault
221
        );
222
        $extraField->find($fieldId);
223
        $extraField->update(
224
            [
225
                'id' => $fieldId,
226
                'variable' => 'plugin_bbb_session_users_limit',
227
                'changeable' => 1,
228
                'visible_to_self' => 1,
229
                'visible_to_others' => 0,
230
            ]
231
        );
232
233
        Database::query(
234
            "CREATE TABLE plugin_bbb_meeting_format (
235
                    id int unsigned not null PRIMARY KEY AUTO_INCREMENT,
236
                    meeting_id int unsigned not null,
237
                    format_type varchar(255) not null,
238
                    resource_url text not null
239
                    );"
240
        );
241
242
        // Copy icons into the main/img/icons folder
243
        $iconName = 'bigbluebutton';
244
        $iconsList = [
245
            '64/'.$iconName.'.png',
246
            '64/'.$iconName.'_na.png',
247
            '32/'.$iconName.'.png',
248
            '32/'.$iconName.'_na.png',
249
            '22/'.$iconName.'.png',
250
            '22/'.$iconName.'_na.png',
251
        ];
252
        $sourceDir = api_get_path(SYS_PLUGIN_PATH).'bbb/resources/img/';
253
        $destinationDir = api_get_path(SYS_CODE_PATH).'img/icons/';
254
        foreach ($iconsList as $icon) {
255
            $src = $sourceDir.$icon;
256
            $dest = $destinationDir.$icon;
257
            copy($src, $dest);
258
        }
259
        // Installing course settings
260
        $this->install_course_fields_in_all_courses(true, 'bigbluebutton.png');
261
    }
262
263
    /**
264
     * Uninstall
265
     */
266
    public function uninstall()
267
    {
268
        $t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
269
        $t_options = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
270
        $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
271
272
        $variables = [
273
            'bbb_salt',
274
            'bbb_host',
275
            'bbb_tool_enable',
276
            'enable_global_conference',
277
            'enable_global_conference_per_user',
278
            'enable_global_conference_link',
279
            'disable_download_conference_link',
280
            'enable_conference_in_course_groups',
281
            'bbb_plugin',
282
            'bbb_plugin_host',
283
            'bbb_plugin_salt',
284
            'max_users_limit',
285
            'global_conference_allow_roles'
286
        ];
287
288
        $urlId = api_get_current_access_url_id();
289
290
        foreach ($variables as $variable) {
291
            $sql = "DELETE FROM $t_settings WHERE variable = '$variable' AND access_url = $urlId";
292
            Database::query($sql);
293
        }
294
295
        $em = Database::getManager();
296
        $sm = $em->getConnection()->getSchemaManager();
297
        if ($sm->tablesExist('plugin_bbb_meeting')) {
298
            Database::query("DELETE FROM plugin_bbb_meeting WHERE access_url = $urlId");
299
        }
300
301
        // Only delete tables if it's uninstalled from main url.
302
        if (1 == $urlId) {
303
            $extraField = new ExtraField('course');
304
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
305
                'plugin_bbb_course_users_limit'
306
            );
307
            if (!empty($extraFieldInfo)) {
308
                $extraField->delete($extraFieldInfo['id']);
309
            }
310
            $extraField = new ExtraField('session');
311
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
312
                'plugin_bbb_session_users_limit'
313
            );
314
            if (!empty($extraFieldInfo)) {
315
                $extraField->delete($extraFieldInfo['id']);
316
            }
317
318
            if ($sm->tablesExist('plugin_bbb_meeting_format')) {
319
                Database::query('DROP TABLE IF EXISTS plugin_bbb_meeting_format');
320
            }
321
322
            $sql = "DELETE FROM $t_options WHERE variable = 'bbb_plugin'";
323
            Database::query($sql);
324
325
            // hack to get rid of Database::query warning (please add c_id...)
326
            $sql = "DELETE FROM $t_tool WHERE name = 'bbb' AND c_id != 0";
327
            Database::query($sql);
328
329
            if ($sm->tablesExist('plugin_bbb_meeting_format')) {
330
                Database::query('DROP TABLE IF EXISTS plugin_bbb_meeting_format');
331
            }
332
            if ($sm->tablesExist('plugin_bbb_room')) {
333
                Database::query('DROP TABLE IF EXISTS plugin_bbb_room');
334
            }
335
            if ($sm->tablesExist('plugin_bbb_meeting')) {
336
                Database::query('DROP TABLE IF EXISTS plugin_bbb_meeting');
337
            }
338
339
            // Deleting course settings
340
            $this->uninstall_course_fields_in_all_courses($this->course_settings);
341
342
            // Remove icons from the main/img/icons folder
343
            $iconName = 'bigbluebutton';
344
            $iconsList = [
345
                '64/'.$iconName.'.png',
346
                '64/'.$iconName.'_na.png',
347
                '32/'.$iconName.'.png',
348
                '32/'.$iconName.'_na.png',
349
                '22/'.$iconName.'.png',
350
                '22/'.$iconName.'_na.png',
351
            ];
352
            $destinationDir = api_get_path(SYS_CODE_PATH).'img/icons/';
353
            foreach ($iconsList as $icon) {
354
                $dest = $destinationDir.$icon;
355
                if (is_file($dest)) {
356
                    @unlink($dest);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

356
                    /** @scrutinizer ignore-unhandled */ @unlink($dest);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
357
                }
358
            }
359
        }
360
    }
361
362
    /**
363
     * Update
364
     */
365
    public function update()
366
    {
367
        $sql = "SHOW COLUMNS FROM plugin_bbb_room WHERE Field = 'close'";
368
        $res = Database::query($sql);
369
370
        if (Database::num_rows($res) === 0) {
371
            $sql = "ALTER TABLE plugin_bbb_room ADD close int unsigned NULL";
372
            $res = Database::query($sql);
373
            if (!$res) {
0 ignored issues
show
introduced by
$res is of type Doctrine\DBAL\Driver\Statement, thus it always evaluated to true.
Loading history...
374
                echo Display::return_message($this->get_lang('ErrorUpdateFieldDB'), 'warning');
375
            }
376
377
            Database::update(
378
                'plugin_bbb_room',
379
                ['close' => BBBPlugin::ROOM_CLOSE]
380
            );
381
        }
382
    }
383
384
    /**
385
     * Set the course setting in all courses
386
     *
387
     * @param bool $variable Course setting to update
388
     * @param bool $value New values of the course setting
389
     */
390
    public function update_course_field_in_all_courses($variable, $value)
391
    {
392
        // Update existing courses to add the new course setting value
393
        $table = Database::get_main_table(TABLE_MAIN_COURSE);
394
        $sql = "SELECT id FROM $table ORDER BY id";
395
        $res = Database::query($sql);
396
        $courseSettingTable = Database::get_course_table(TABLE_COURSE_SETTING);
397
        while ($row = Database::fetch_assoc($res)) {
398
            Database::update(
399
                $courseSettingTable,
400
                ['value' => $value],
401
                ['variable = ? AND c_id = ?' => [$variable, $row['id']]]
402
            );
403
        }
404
    }
405
}
406