| Total Complexity | 44 |
| Total Lines | 312 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BbbPlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BbbPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 | // Default course settings when creating a new course |
||
| 23 | public $course_settings = [ |
||
| 24 | ['name' => 'big_blue_button_record_and_store', 'type' => 'checkbox'], |
||
| 25 | ['name' => 'bbb_enable_conference_in_groups', 'type' => 'checkbox'], |
||
| 26 | ['name' => 'bbb_force_record_generation', 'type' => 'checkbox'], |
||
| 27 | ['name' => 'big_blue_button_students_start_conference_in_groups', 'type' => 'checkbox'], |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * BBBPlugin constructor. |
||
| 32 | * Defines all available plugin settings. |
||
| 33 | */ |
||
| 34 | protected function __construct() |
||
| 35 | { |
||
| 36 | parent::__construct( |
||
| 37 | '2.9', |
||
| 38 | 'Julio Montoya, Yannick Warnier, Angel Fernando Quiroz Campos, Jose Angel Ruiz', |
||
| 39 | [ |
||
| 40 | 'tool_enable' => 'boolean', |
||
| 41 | 'host' => 'text', |
||
| 42 | 'salt' => 'text', |
||
| 43 | 'enable_global_conference' => 'boolean', |
||
| 44 | 'enable_global_conference_per_user' => 'boolean', |
||
| 45 | 'enable_conference_in_course_groups' => 'boolean', |
||
| 46 | 'enable_global_conference_link' => 'boolean', |
||
| 47 | 'disable_download_conference_link' => 'boolean', |
||
| 48 | 'max_users_limit' => 'text', |
||
| 49 | 'global_conference_allow_roles' => [ |
||
| 50 | 'type' => 'select', |
||
| 51 | 'options' => [ |
||
| 52 | PLATFORM_ADMIN => get_lang('Administrator'), |
||
| 53 | COURSEMANAGER => get_lang('Teacher'), |
||
| 54 | STUDENT => get_lang('Student'), |
||
| 55 | STUDENT_BOSS => get_lang('StudentBoss'), |
||
| 56 | ], |
||
| 57 | 'attributes' => ['multiple' => 'multiple'], |
||
| 58 | ], |
||
| 59 | 'allow_regenerate_recording' => 'boolean', |
||
| 60 | 'big_blue_button_record_and_store' => 'checkbox', |
||
| 61 | 'bbb_enable_conference_in_groups' => 'checkbox', |
||
| 62 | 'bbb_force_record_generation' => 'checkbox', |
||
| 63 | 'disable_course_settings' => 'boolean', |
||
| 64 | 'meeting_duration' => 'text', |
||
| 65 | 'delete_recordings_on_course_delete' => 'boolean', |
||
| 66 | ] |
||
| 67 | ); |
||
| 68 | |||
| 69 | $this->isAdminPlugin = true; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Returns a singleton instance of the plugin. |
||
| 74 | */ |
||
| 75 | public static function create(): self |
||
| 76 | { |
||
| 77 | static $result = null; |
||
| 78 | return $result ??= new self(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Validates if a course setting is enabled depending on global plugin configuration. |
||
| 83 | */ |
||
| 84 | public function validateCourseSetting($variable): bool |
||
| 85 | { |
||
| 86 | if ($this->get('disable_course_settings') === 'true') { |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | |||
| 90 | switch ($variable) { |
||
| 91 | case 'bbb_enable_conference_in_groups': |
||
| 92 | return $this->get('enable_conference_in_course_groups') === 'true'; |
||
| 93 | case 'bbb_force_record_generation': |
||
| 94 | return $this->get('allow_regenerate_recording') === 'true'; |
||
| 95 | default: |
||
| 96 | return true; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns course-level plugin settings if not disabled globally. |
||
| 102 | */ |
||
| 103 | public function getCourseSettings(): array |
||
| 104 | { |
||
| 105 | if ($this->get('disable_course_settings') === 'true') { |
||
| 106 | return []; |
||
| 107 | } |
||
| 108 | |||
| 109 | return parent::getCourseSettings(); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Performs automatic updates to all course settings after configuration changes. |
||
| 114 | */ |
||
| 115 | public function performActionsAfterConfigure(): self |
||
| 116 | { |
||
| 117 | if ($this->get('disable_course_settings') === 'true') { |
||
| 118 | self::updateCourseFieldInAllCourses( |
||
| 119 | 'bbb_enable_conference_in_groups', |
||
| 120 | $this->get('enable_conference_in_course_groups') === 'true' ? 1 : 0 |
||
| 121 | ); |
||
| 122 | self::updateCourseFieldInAllCourses( |
||
| 123 | 'bbb_force_record_generation', |
||
| 124 | $this->get('allow_regenerate_recording') === 'true' ? 1 : 0 |
||
| 125 | ); |
||
| 126 | self::updateCourseFieldInAllCourses( |
||
| 127 | 'big_blue_button_record_and_store', |
||
| 128 | $this->get('big_blue_button_record_and_store') === 'true' ? 1 : 0 |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Updates a course setting value across all existing courses. |
||
| 137 | */ |
||
| 138 | public static function updateCourseFieldInAllCourses(string $variable, string $value): void |
||
| 163 | } |
||
| 164 | |||
| 165 | // Hook called when a course is deleted |
||
| 166 | public function doWhenDeletingCourse($courseId): void |
||
| 167 | { |
||
| 168 | // Check if the setting is enabled |
||
| 169 | if ($this->get('delete_recordings_on_course_delete') !== 'true') { |
||
| 170 | return; |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->removeBbbRecordingsForCourse($courseId); |
||
| 174 | } |
||
| 175 | |||
| 176 | // Hook called when a session is deleted |
||
| 177 | public function doWhenDeletingSession($sessionId): void |
||
| 178 | { |
||
| 179 | // Check if the setting is enabled |
||
| 180 | if ($this->get('delete_recordings_on_course_delete') !== 'true') { |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | $this->removeBbbRecordingsForSession($sessionId); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Remove BBB recordings linked to a specific course |
||
| 188 | private function removeBbbRecordingsForCourse(int $courseId): void |
||
| 222 | } |
||
| 223 | |||
| 224 | // Remove BBB recordings linked to a specific session |
||
| 225 | private function removeBbbRecordingsForSession(int $sessionId): void |
||
| 226 | { |
||
| 227 | $em = Database::getManager(); |
||
| 228 | $meetingRepo = $em->getRepository(ConferenceMeeting::class); |
||
| 229 | $recordingRepo = $em->getRepository(ConferenceRecording::class); |
||
| 230 | |||
| 231 | // Get all BBB meetings for this session |
||
| 232 | $meetings = $meetingRepo->findBy([ |
||
| 233 | 'session' => $sessionId, |
||
| 234 | 'serviceProvider' => 'bbb', |
||
| 235 | ]); |
||
| 236 | |||
| 237 | foreach ($meetings as $meeting) { |
||
| 238 | $recordings = $recordingRepo->findBy([ |
||
| 239 | 'meeting' => $meeting, |
||
| 240 | 'formatType' => 'bbb', |
||
| 241 | ]); |
||
| 242 | |||
| 243 | foreach ($recordings as $rec) { |
||
| 244 | if ($recordId = $this->extractRecordId($rec->getResourceUrl())) { |
||
| 245 | $this->deleteRecording($recordId); |
||
| 246 | } |
||
| 247 | |||
| 248 | $em->remove($rec); |
||
| 249 | } |
||
| 250 | |||
| 251 | $em->remove($meeting); |
||
| 252 | } |
||
| 253 | |||
| 254 | $em->flush(); |
||
| 255 | } |
||
| 256 | |||
| 257 | // Extracts the recordID from the BBB recording URL |
||
| 258 | private function extractRecordId(string $url): ?string |
||
| 271 | } |
||
| 272 | |||
| 273 | // Sends a deleteRecordings API request to BigBlueButton |
||
| 274 | private function deleteRecording(string $recordId): void |
||
| 285 | } |
||
| 286 | |||
| 287 | public function canCurrentUserSeeGlobalConferenceLink(): bool |
||
| 288 | { |
||
| 289 | $allowedStatuses = $this->get('global_conference_allow_roles') ?? []; |
||
| 290 | |||
| 291 | if (empty($allowedStatuses)) { |
||
| 292 | return api_is_platform_admin(); |
||
| 293 | } |
||
| 294 | |||
| 295 | foreach ($allowedStatuses as $status) { |
||
| 296 | switch ((int) $status) { |
||
| 297 | case PLATFORM_ADMIN: |
||
| 298 | if (api_is_platform_admin()) { |
||
| 299 | return true; |
||
| 300 | } |
||
| 301 | break; |
||
| 302 | case COURSEMANAGER: |
||
| 303 | if (api_is_teacher()) { |
||
| 304 | return true; |
||
| 305 | } |
||
| 306 | break; |
||
| 307 | case STUDENT: |
||
| 308 | if (api_is_student()) { |
||
| 309 | return true; |
||
| 310 | } |
||
| 311 | break; |
||
| 312 | case STUDENT_BOSS: |
||
| 313 | if (api_is_student_boss()) { |
||
| 314 | return true; |
||
| 315 | } |
||
| 316 | break; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | return false; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function get_name(): string |
||
| 326 | } |
||
| 327 | } |
||
| 328 |
If you suppress an error, we recommend checking for the error condition explicitly: