Passed
Pull Request — master (#6336)
by
unknown
08:35
created

BbbPlugin::canCurrentUserSeeGlobalConferenceLink()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 11
nop 0
dl 0
loc 34
rs 7.3166
c 0
b 0
f 0

How to fix   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
use Chamilo\CourseBundle\Entity\CCourseSetting;
6
use Chamilo\CoreBundle\Entity\Course;
7
8
/**
9
 * BigBlueButton plugin configuration class.
10
 * Handles plugin options and course settings.
11
 */
12
class BbbPlugin extends Plugin
13
{
14
    const ROOM_OPEN = 0;
15
    const ROOM_CLOSE = 1;
16
    const ROOM_CHECK = 2;
17
18
    public $isCoursePlugin = true;
19
20
    // Default course settings when creating a new course
21
    public $course_settings = [
22
        ['name' => 'big_blue_button_record_and_store', 'type' => 'checkbox'],
23
        ['name' => 'bbb_enable_conference_in_groups', 'type' => 'checkbox'],
24
        ['name' => 'bbb_force_record_generation', 'type' => 'checkbox'],
25
        ['name' => 'big_blue_button_students_start_conference_in_groups', 'type' => 'checkbox'],
26
    ];
27
28
    /**
29
     * BBBPlugin constructor.
30
     * Defines all available plugin settings.
31
     */
32
    protected function __construct()
33
    {
34
        parent::__construct(
35
            '2.9',
36
            'Julio Montoya, Yannick Warnier, Angel Fernando Quiroz Campos, Jose Angel Ruiz',
37
            [
38
                'tool_enable' => 'boolean',
39
                'host' => 'text',
40
                'salt' => 'text',
41
                'enable_global_conference' => 'boolean',
42
                'enable_global_conference_per_user' => 'boolean',
43
                'enable_conference_in_course_groups' => 'boolean',
44
                'enable_global_conference_link' => 'boolean',
45
                'disable_download_conference_link' => 'boolean',
46
                'max_users_limit' => 'text',
47
                'global_conference_allow_roles' => [
48
                    'type' => 'select',
49
                    'options' => [
50
                        PLATFORM_ADMIN => get_lang('Administrator'),
51
                        COURSEMANAGER => get_lang('Teacher'),
52
                        STUDENT => get_lang('Student'),
53
                        STUDENT_BOSS => get_lang('StudentBoss'),
54
                    ],
55
                    'attributes' => ['multiple' => 'multiple'],
56
                ],
57
                'allow_regenerate_recording' => 'boolean',
58
                'big_blue_button_record_and_store' => 'checkbox',
59
                'bbb_enable_conference_in_groups' => 'checkbox',
60
                'bbb_force_record_generation' => 'checkbox',
61
                'disable_course_settings' => 'boolean',
62
                'meeting_duration' => 'text',
63
            ]
64
        );
65
66
        $this->isAdminPlugin = true;
67
    }
68
69
    /**
70
     * Returns a singleton instance of the plugin.
71
     */
72
    public static function create(): self
73
    {
74
        static $result = null;
75
        return $result ??= new self();
76
    }
77
78
    /**
79
     * Validates if a course setting is enabled depending on global plugin configuration.
80
     */
81
    public function validateCourseSetting($variable): bool
82
    {
83
        if ($this->get('disable_course_settings') === 'true') {
84
            return false;
85
        }
86
87
        switch ($variable) {
88
            case 'bbb_enable_conference_in_groups':
89
                return $this->get('enable_conference_in_course_groups') === 'true';
90
            case 'bbb_force_record_generation':
91
                return $this->get('allow_regenerate_recording') === 'true';
92
            default:
93
                return true;
94
        }
95
    }
96
97
    /**
98
     * Returns course-level plugin settings if not disabled globally.
99
     */
100
    public function getCourseSettings(): array
101
    {
102
        if ($this->get('disable_course_settings') === 'true') {
103
            return [];
104
        }
105
106
        return parent::getCourseSettings();
107
    }
108
109
    /**
110
     * Performs automatic updates to all course settings after configuration changes.
111
     */
112
    public function performActionsAfterConfigure(): self
113
    {
114
        if ($this->get('disable_course_settings') === 'true') {
115
            self::updateCourseFieldInAllCourses(
116
                'bbb_enable_conference_in_groups',
117
                $this->get('enable_conference_in_course_groups') === 'true' ? 1 : 0
118
            );
119
            self::updateCourseFieldInAllCourses(
120
                'bbb_force_record_generation',
121
                $this->get('allow_regenerate_recording') === 'true' ? 1 : 0
122
            );
123
            self::updateCourseFieldInAllCourses(
124
                'big_blue_button_record_and_store',
125
                $this->get('big_blue_button_record_and_store') === 'true' ? 1 : 0
126
            );
127
        }
128
129
        return $this;
130
    }
131
132
    /**
133
     * Updates a course setting value across all existing courses.
134
     */
135
    public static function updateCourseFieldInAllCourses(string $variable, string $value): void
136
    {
137
        $entityManager = Database::getManager();
138
        $courseRepo = $entityManager->getRepository(Course::class);
139
        $settingRepo = $entityManager->getRepository(CCourseSetting::class);
140
141
        $courses = $courseRepo->createQueryBuilder('c')
142
            ->select('c.id')
143
            ->orderBy('c.id', 'ASC')
144
            ->getQuery()
145
            ->getArrayResult();
146
147
        foreach ($courses as $course) {
148
            $setting = $settingRepo->findOneBy([
149
                'variable' => $variable,
150
                'cId' => $course['id'],
151
            ]);
152
153
            if ($setting) {
154
                $setting->setValue($value);
155
                $entityManager->persist($setting);
156
            }
157
        }
158
159
        $entityManager->flush();
160
    }
161
162
    public function canCurrentUserSeeGlobalConferenceLink(): bool
163
    {
164
        $allowedStatuses = $this->get('global_conference_allow_roles') ?? [];
165
166
        if (empty($allowedStatuses)) {
167
            return api_is_platform_admin();
168
        }
169
170
        foreach ($allowedStatuses as $status) {
171
            switch ((int) $status) {
172
                case PLATFORM_ADMIN:
173
                    if (api_is_platform_admin()) {
174
                        return true;
175
                    }
176
                    break;
177
                case COURSEMANAGER:
178
                    if (api_is_teacher()) {
179
                        return true;
180
                    }
181
                    break;
182
                case STUDENT:
183
                    if (api_is_student()) {
184
                        return true;
185
                    }
186
                    break;
187
                case STUDENT_BOSS:
188
                    if (api_is_student_boss()) {
189
                        return true;
190
                    }
191
                    break;
192
            }
193
        }
194
195
        return false;
196
    }
197
198
    public function get_name(): string
199
    {
200
        return 'Bbb';
201
    }
202
}
203