| Total Complexity | 55 |
| Total Lines | 399 |
| 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 |
||
| 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 |
||
| 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 |
||
| 274 | } |
||
| 275 | |||
| 276 | // Sends a deleteRecordings API request to BigBlueButton |
||
| 277 | private function deleteRecording(string $recordId): void |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Installs the plugin |
||
| 292 | */ |
||
| 293 | public function install(): void |
||
| 294 | { |
||
| 295 | $em = Database::getManager(); |
||
| 296 | |||
| 297 | $pluginRepo = Container::getPluginRepository(); |
||
| 298 | $plugin = $pluginRepo->findOneByTitle($this->get_name()); |
||
| 299 | |||
| 300 | if (!$plugin) { |
||
| 301 | $plugin = new \Chamilo\CoreBundle\Entity\Plugin(); |
||
| 302 | $plugin->setTitle($this->get_name()); |
||
| 303 | } else { |
||
| 304 | $plugin = $em->merge($plugin); |
||
| 305 | } |
||
| 306 | |||
| 307 | $plugin->setInstalled(true); |
||
| 308 | $plugin->setInstalledVersion($this->get_version()); |
||
| 309 | $plugin->setSource(\Chamilo\CoreBundle\Entity\Plugin::SOURCE_OFFICIAL); |
||
| 310 | $em->persist($plugin); |
||
| 311 | |||
| 312 | $accessUrlRepo = Container::getAccessUrlRepository(); |
||
| 313 | $accessUrlRelPluginRepo = Container::getAccessUrlRelPluginRepository(); |
||
| 314 | |||
| 315 | $accessUrls = $accessUrlRepo->findAll(); |
||
| 316 | |||
| 317 | foreach ($accessUrls as $accessUrl) { |
||
| 318 | $rel = $accessUrlRelPluginRepo->findOneBy([ |
||
| 319 | 'plugin' => $plugin, |
||
| 320 | 'url' => $accessUrl, |
||
| 321 | ]); |
||
| 322 | |||
| 323 | if (!$rel) { |
||
| 324 | $rel = new AccessUrlRelPlugin(); |
||
| 325 | $rel->setPlugin($plugin); |
||
| 326 | $rel->setUrl($accessUrl); |
||
| 327 | $rel->setActive(true); |
||
| 328 | |||
| 329 | $configuration = []; |
||
| 330 | foreach ($this->fields as $name => $type) { |
||
| 331 | if (is_array($type)) { |
||
| 332 | $configuration[$name] = $type['type'] === 'boolean' ? 'false' : ''; |
||
| 333 | } else { |
||
| 334 | $configuration[$name] = in_array($type, ['boolean','checkbox'], true) ? 'false' : ''; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | $rel->setConfiguration($configuration); |
||
| 338 | |||
| 339 | $em->persist($rel); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | $em->flush(); |
||
| 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 |
||
| 353 | { |
||
| 354 | $em = Database::getManager(); |
||
| 355 | |||
| 356 | $pluginRepo = Container::getPluginRepository(); |
||
| 357 | $relRepo = Container::getAccessUrlRelPluginRepository(); |
||
| 358 | |||
| 359 | $plugin = $pluginRepo->findOneByTitle($this->get_name()); |
||
| 360 | |||
| 361 | if (!$plugin) { |
||
| 362 | return; |
||
| 363 | } |
||
| 364 | |||
| 365 | $rels = $relRepo->findBy(['plugin' => $plugin]); |
||
| 366 | foreach ($rels as $rel) { |
||
| 367 | $em->remove($rel); |
||
| 368 | } |
||
| 369 | |||
| 370 | $plugin->setInstalled(false); |
||
| 371 | |||
| 372 | $em->persist($plugin); |
||
| 373 | $em->flush(); |
||
| 374 | } |
||
| 375 | |||
| 376 | public function canCurrentUserSeeGlobalConferenceLink(): bool |
||
| 377 | { |
||
| 378 | $allowedStatuses = $this->get('global_conference_allow_roles') ?? []; |
||
| 379 | |||
| 380 | if (empty($allowedStatuses)) { |
||
| 381 | return api_is_platform_admin(); |
||
| 382 | } |
||
| 383 | |||
| 384 | foreach ($allowedStatuses as $status) { |
||
| 385 | switch ((int) $status) { |
||
| 386 | case PLATFORM_ADMIN: |
||
| 387 | if (api_is_platform_admin()) { |
||
| 388 | return true; |
||
| 389 | } |
||
| 390 | break; |
||
| 391 | case COURSEMANAGER: |
||
| 392 | if (api_is_teacher()) { |
||
| 393 | return true; |
||
| 394 | } |
||
| 395 | break; |
||
| 396 | case STUDENT: |
||
| 397 | if (api_is_student()) { |
||
| 398 | return true; |
||
| 399 | } |
||
| 400 | break; |
||
| 401 | case STUDENT_BOSS: |
||
| 402 | if (api_is_student_boss()) { |
||
| 403 | return true; |
||
| 404 | } |
||
| 405 | break; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | return false; |
||
| 410 | } |
||
| 411 | |||
| 412 | public function get_name(): string |
||
| 415 | } |
||
| 416 | } |
||
| 417 |
If you suppress an error, we recommend checking for the error condition explicitly: