| Total Complexity | 59 |
| Total Lines | 422 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 16 | class BbbPlugin extends Plugin |
||
| 17 | { |
||
| 18 | const ROOM_OPEN = 0; |
||
| 19 | const ROOM_CLOSE = 1; |
||
| 20 | const ROOM_CHECK = 2; |
||
| 21 | |||
| 22 | public $isCoursePlugin = true; |
||
| 23 | |||
| 24 | // Default course settings when creating a new course |
||
| 25 | public $course_settings = [ |
||
| 26 | ['name' => 'big_blue_button_record_and_store', 'type' => 'checkbox'], |
||
| 27 | ['name' => 'bbb_enable_conference_in_groups', 'type' => 'checkbox'], |
||
| 28 | ['name' => 'bbb_force_record_generation', 'type' => 'checkbox'], |
||
| 29 | ['name' => 'big_blue_button_students_start_conference_in_groups', 'type' => 'checkbox'], |
||
| 30 | ]; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * BBBPlugin constructor. |
||
| 34 | * Defines all available plugin settings. |
||
| 35 | */ |
||
| 36 | protected function __construct() |
||
| 37 | { |
||
| 38 | parent::__construct( |
||
| 39 | '2.9', |
||
| 40 | 'Julio Montoya, Yannick Warnier, Angel Fernando Quiroz Campos, Jose Angel Ruiz', |
||
| 41 | [ |
||
| 42 | 'tool_enable' => 'boolean', |
||
| 43 | 'host' => 'text', |
||
| 44 | 'salt' => 'text', |
||
| 45 | 'enable_global_conference' => 'boolean', |
||
| 46 | 'enable_global_conference_per_user' => 'boolean', |
||
| 47 | 'enable_conference_in_course_groups' => 'boolean', |
||
| 48 | 'enable_global_conference_link' => 'boolean', |
||
| 49 | 'disable_download_conference_link' => 'boolean', |
||
| 50 | 'max_users_limit' => 'text', |
||
| 51 | 'global_conference_allow_roles' => [ |
||
| 52 | 'type' => 'select', |
||
| 53 | 'options' => [ |
||
| 54 | PLATFORM_ADMIN => get_lang('Administrator'), |
||
| 55 | COURSEMANAGER => get_lang('Teacher'), |
||
| 56 | STUDENT => get_lang('Learner'), |
||
| 57 | STUDENT_BOSS => get_lang('Superior (n+1)'), |
||
| 58 | ], |
||
| 59 | 'attributes' => ['multiple' => 'multiple'], |
||
| 60 | ], |
||
| 61 | 'allow_regenerate_recording' => 'boolean', |
||
| 62 | 'big_blue_button_record_and_store' => 'checkbox', |
||
| 63 | 'bbb_enable_conference_in_groups' => 'checkbox', |
||
| 64 | 'bbb_force_record_generation' => 'checkbox', |
||
| 65 | 'disable_course_settings' => 'boolean', |
||
| 66 | 'meeting_duration' => 'text', |
||
| 67 | 'delete_recordings_on_course_delete' => 'boolean', |
||
| 68 | 'hide_conference_link' => 'boolean', |
||
| 69 | ] |
||
| 70 | ); |
||
| 71 | |||
| 72 | $this->isAdminPlugin = true; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns a singleton instance of the plugin. |
||
| 77 | */ |
||
| 78 | public static function create(): self |
||
| 79 | { |
||
| 80 | static $result = null; |
||
| 81 | return $result ??= new self(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Validates if a course setting is enabled depending on global plugin configuration. |
||
| 86 | */ |
||
| 87 | public function validateCourseSetting($variable): bool |
||
| 88 | { |
||
| 89 | if ($this->get('disable_course_settings') === 'true') { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | switch ($variable) { |
||
| 94 | case 'bbb_enable_conference_in_groups': |
||
| 95 | return $this->get('enable_conference_in_course_groups') === 'true'; |
||
| 96 | case 'bbb_force_record_generation': |
||
| 97 | return $this->get('allow_regenerate_recording') === 'true'; |
||
| 98 | default: |
||
| 99 | return true; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns course-level plugin settings if not disabled globally. |
||
| 105 | */ |
||
| 106 | public function getCourseSettings(): array |
||
| 107 | { |
||
| 108 | if ($this->get('disable_course_settings') === 'true') { |
||
| 109 | return []; |
||
| 110 | } |
||
| 111 | |||
| 112 | return parent::getCourseSettings(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Performs automatic updates to all course settings after configuration changes. |
||
| 117 | */ |
||
| 118 | public function performActionsAfterConfigure(): self |
||
| 119 | { |
||
| 120 | if ($this->get('disable_course_settings') === 'true') { |
||
| 121 | self::updateCourseFieldInAllCourses( |
||
| 122 | 'bbb_enable_conference_in_groups', |
||
| 123 | $this->get('enable_conference_in_course_groups') === 'true' ? 1 : 0 |
||
| 124 | ); |
||
| 125 | self::updateCourseFieldInAllCourses( |
||
| 126 | 'bbb_force_record_generation', |
||
| 127 | $this->get('allow_regenerate_recording') === 'true' ? 1 : 0 |
||
| 128 | ); |
||
| 129 | self::updateCourseFieldInAllCourses( |
||
| 130 | 'big_blue_button_record_and_store', |
||
| 131 | $this->get('big_blue_button_record_and_store') === 'true' ? 1 : 0 |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Updates a course setting value across all existing courses. |
||
| 140 | */ |
||
| 141 | public static function updateCourseFieldInAllCourses(string $variable, string $value): void |
||
| 166 | } |
||
| 167 | |||
| 168 | // Hook called when a course is deleted |
||
| 169 | public function doWhenDeletingCourse($courseId): void |
||
| 170 | { |
||
| 171 | // Check if the setting is enabled |
||
| 172 | if ($this->get('delete_recordings_on_course_delete') !== 'true') { |
||
| 173 | return; |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->removeBbbRecordingsForCourse($courseId); |
||
| 177 | } |
||
| 178 | |||
| 179 | // Hook called when a session is deleted |
||
| 180 | public function doWhenDeletingSession($sessionId): void |
||
| 181 | { |
||
| 182 | // Check if the setting is enabled |
||
| 183 | if ($this->get('delete_recordings_on_course_delete') !== 'true') { |
||
| 184 | return; |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->removeBbbRecordingsForSession($sessionId); |
||
| 188 | } |
||
| 189 | |||
| 190 | // Remove BBB recordings linked to a specific course |
||
| 191 | private function removeBbbRecordingsForCourse(int $courseId): void |
||
| 192 | { |
||
| 193 | $em = Database::getManager(); |
||
| 194 | $meetingRepo = $em->getRepository(ConferenceMeeting::class); |
||
| 195 | $recordingRepo = $em->getRepository(ConferenceRecording::class); |
||
| 196 | |||
| 197 | // Get all BBB meetings for this course |
||
| 198 | $meetings = $meetingRepo->createQueryBuilder('m') |
||
| 199 | ->where('m.course = :cid') |
||
| 200 | ->andWhere('m.serviceProvider = :sp') |
||
| 201 | ->setParameters(['cid' => $courseId, 'sp' => 'bbb']) |
||
| 202 | ->getQuery() |
||
| 203 | ->getResult(); |
||
| 204 | |||
| 205 | foreach ($meetings as $meeting) { |
||
| 206 | // Get all recordings of this meeting |
||
| 207 | $recordings = $recordingRepo->findBy([ |
||
| 208 | 'meeting' => $meeting, |
||
| 209 | 'formatType' => 'bbb', |
||
| 210 | ]); |
||
| 211 | |||
| 212 | foreach ($recordings as $rec) { |
||
| 213 | // Try to extract the record ID from the URL |
||
| 214 | if ($recordId = $this->extractRecordId($rec->getResourceUrl())) { |
||
| 215 | $this->deleteRecording($recordId); // Call BBB API to delete |
||
| 216 | } |
||
| 217 | |||
| 218 | $em->remove($rec); // Remove local record |
||
| 219 | } |
||
| 220 | |||
| 221 | $em->remove($meeting); // Optionally remove the meeting entity |
||
| 222 | } |
||
| 223 | |||
| 224 | $em->flush(); // Save all removals |
||
| 225 | } |
||
| 226 | |||
| 227 | // Remove BBB recordings linked to a specific session |
||
| 228 | private function removeBbbRecordingsForSession(int $sessionId): void |
||
| 229 | { |
||
| 230 | $em = Database::getManager(); |
||
| 231 | $meetingRepo = $em->getRepository(ConferenceMeeting::class); |
||
| 232 | $recordingRepo = $em->getRepository(ConferenceRecording::class); |
||
| 233 | |||
| 234 | // Get all BBB meetings for this session |
||
| 235 | $meetings = $meetingRepo->findBy([ |
||
| 236 | 'session' => $sessionId, |
||
| 237 | 'serviceProvider' => 'bbb', |
||
| 238 | ]); |
||
| 239 | |||
| 240 | foreach ($meetings as $meeting) { |
||
| 241 | $recordings = $recordingRepo->findBy([ |
||
| 242 | 'meeting' => $meeting, |
||
| 243 | 'formatType' => 'bbb', |
||
| 244 | ]); |
||
| 245 | |||
| 246 | foreach ($recordings as $rec) { |
||
| 247 | if ($recordId = $this->extractRecordId($rec->getResourceUrl())) { |
||
| 248 | $this->deleteRecording($recordId); |
||
| 249 | } |
||
| 250 | |||
| 251 | $em->remove($rec); |
||
| 252 | } |
||
| 253 | |||
| 254 | $em->remove($meeting); |
||
| 255 | } |
||
| 256 | |||
| 257 | $em->flush(); |
||
| 258 | } |
||
| 259 | |||
| 260 | // Extracts the recordID from the BBB recording URL |
||
| 261 | private function extractRecordId(string $url): ?string |
||
| 262 | { |
||
| 263 | // Match parameter ?recordID=xxx |
||
| 264 | if (preg_match('/[?&]recordID=([\w-]+)/', $url, $matches)) { |
||
| 265 | return $matches[1]; |
||
| 266 | } |
||
| 267 | |||
| 268 | // Optional: match paths like .../recordingID-123456 |
||
| 269 | if (preg_match('/recordingID[-=](\d+)/', $url, $matches)) { |
||
| 270 | return $matches[1]; |
||
| 271 | } |
||
| 272 | |||
| 273 | return null; |
||
| 274 | } |
||
| 275 | |||
| 276 | // Sends a deleteRecordings API request to BigBlueButton |
||
| 277 | private function deleteRecording(string $recordId): void |
||
| 278 | { |
||
| 279 | $host = rtrim($this->get('host'), '/'); |
||
| 280 | $salt = $this->get('salt'); |
||
| 281 | |||
| 282 | $query = "recordID={$recordId}"; |
||
| 283 | $checksum = sha1('deleteRecordings' . $query . $salt); |
||
| 284 | $url = "{$host}/bigbluebutton/api/deleteRecordings?{$query}&checksum={$checksum}"; |
||
| 285 | |||
| 286 | // Send the request (silently) |
||
| 287 | @file_get_contents($url); |
||
|
|
|||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Installs the plugin |
||
| 292 | */ |
||
| 293 | public function install(): void |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Uninstalls the plugin. |
||
| 348 | * - Removes AccessUrl relations |
||
| 349 | * - Marks the plugin as not installed |
||
| 350 | * - Keeps course-level settings/data intact (safer for future re-installs) |
||
| 351 | */ |
||
| 352 | public function uninstall(): void |
||
| 374 | } |
||
| 375 | |||
| 376 | public function canCurrentUserSeeGlobalConferenceLink(): bool |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Make sure the current user can see the "share" link under the join |
||
| 414 | * button in the course tool page. |
||
| 415 | * The method is called "show" while the setting is "hide" because we favor |
||
| 416 | * minimal disruption in the introduction of new settings (so by default, |
||
| 417 | * the link is shown to respect older versions' behavior). |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | public function showShareLink(): bool |
||
| 433 | } |
||
| 434 | |||
| 435 | public function get_name(): string |
||
| 440 |
If you suppress an error, we recommend checking for the error condition explicitly: