Passed
Pull Request — master (#6161)
by
unknown
08:14
created

BBBPlugin::install()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 79
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

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

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
/**
6
 * BigBlueButton plugin configuration class.
7
 * Handles plugin options and course settings.
8
 */
9
class BBBPlugin extends Plugin
10
{
11
    const ROOM_OPEN = 0;
12
    const ROOM_CLOSE = 1;
13
    const ROOM_CHECK = 2;
14
15
    public $isCoursePlugin = true;
16
17
    // Default course settings when creating a new course
18
    public $course_settings = [
19
        ['name' => 'big_blue_button_record_and_store', 'type' => 'checkbox'],
20
        ['name' => 'bbb_enable_conference_in_groups', 'type' => 'checkbox'],
21
        ['name' => 'bbb_force_record_generation', 'type' => 'checkbox'],
22
        ['name' => 'big_blue_button_students_start_conference_in_groups', 'type' => 'checkbox'],
23
    ];
24
25
    /**
26
     * BBBPlugin constructor.
27
     * Defines all available plugin settings.
28
     */
29
    protected function __construct()
30
    {
31
        parent::__construct(
32
            '2.9',
33
            'Julio Montoya, Yannick Warnier, Angel Fernando Quiroz Campos, Jose Angel Ruiz',
34
            [
35
                'tool_enable' => 'boolean',
36
                'host' => 'text',
37
                'salt' => 'text',
38
                'enable_global_conference' => 'boolean',
39
                'enable_global_conference_per_user' => 'boolean',
40
                'enable_conference_in_course_groups' => 'boolean',
41
                'enable_global_conference_link' => 'boolean',
42
                'disable_download_conference_link' => 'boolean',
43
                'max_users_limit' => 'text',
44
                'global_conference_allow_roles' => [
45
                    'type' => 'select',
46
                    'options' => [
47
                        PLATFORM_ADMIN => get_lang('Administrator'),
48
                        COURSEMANAGER => get_lang('Teacher'),
49
                        STUDENT => get_lang('Student'),
50
                        STUDENT_BOSS => get_lang('StudentBoss'),
51
                    ],
52
                    'attributes' => ['multiple' => 'multiple'],
53
                ],
54
                'allow_regenerate_recording' => 'boolean',
55
                'big_blue_button_record_and_store' => 'checkbox',
56
                'bbb_enable_conference_in_groups' => 'checkbox',
57
                'bbb_force_record_generation' => 'checkbox',
58
                'disable_course_settings' => 'boolean',
59
                'meeting_duration' => 'text',
60
            ]
61
        );
62
63
        $this->isAdminPlugin = true;
64
    }
65
66
    /**
67
     * Returns a singleton instance of the plugin.
68
     */
69
    public static function create(): self
70
    {
71
        static $result = null;
72
        return $result ??= new self();
73
    }
74
75
    /**
76
     * Validates if a course setting is enabled depending on global plugin configuration.
77
     */
78
    public function validateCourseSetting($variable): bool
79
    {
80
        if ($this->get('disable_course_settings') === 'true') {
81
            return false;
82
        }
83
84
        switch ($variable) {
85
            case 'bbb_enable_conference_in_groups':
86
                return $this->get('enable_conference_in_course_groups') === 'true';
87
            case 'bbb_force_record_generation':
88
                return $this->get('allow_regenerate_recording') === 'true';
89
            default:
90
                return true;
91
        }
92
    }
93
94
    /**
95
     * Returns course-level plugin settings if not disabled globally.
96
     */
97
    public function getCourseSettings(): array
98
    {
99
        if ($this->get('disable_course_settings') === 'true') {
100
            return [];
101
        }
102
103
        return parent::getCourseSettings();
104
    }
105
106
    /**
107
     * Performs automatic updates to all course settings after configuration changes.
108
     */
109
    public function performActionsAfterConfigure(): self
110
    {
111
        if ($this->get('disable_course_settings') === 'true') {
112
            self::update_course_field_in_all_courses(
113
                'bbb_enable_conference_in_groups',
114
                $this->get('enable_conference_in_course_groups') === 'true' ? 1 : 0
115
            );
116
            self::update_course_field_in_all_courses(
117
                'bbb_force_record_generation',
118
                $this->get('allow_regenerate_recording') === 'true' ? 1 : 0
119
            );
120
            self::update_course_field_in_all_courses(
121
                'big_blue_button_record_and_store',
122
                $this->get('big_blue_button_record_and_store') === 'true' ? 1 : 0
123
            );
124
        }
125
126
        return $this;
127
    }
128
129
    /**
130
     * Updates a course setting value across all existing courses.
131
     */
132
    public function update_course_field_in_all_courses($variable, $value): void
133
    {
134
        // Update existing courses to add the new course setting value
135
        $table = Database::get_main_table(TABLE_MAIN_COURSE);
136
        $sql = "SELECT id FROM $table ORDER BY id";
137
        $res = Database::query($sql);
138
        $courseSettingTable = Database::get_course_table(TABLE_COURSE_SETTING);
139
        while ($row = Database::fetch_assoc($res)) {
140
            Database::update(
141
                $courseSettingTable,
142
                ['value' => $value],
143
                ['variable = ? AND c_id = ?' => [$variable, $row['id']]]
144
            );
145
        }
146
    }
147
}
148