From 0443e4bd7c20227e56870b8bddc09f053298a234 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 27 Apr 2022 15:38:44 -0500 Subject: [PATCH] Plugin: BBB: Improve code readability --- plugin/bbb/cron_close_meeting.php | 168 ++++++++++++++++-------------- plugin/bbb/lib/bbb.lib.php | 6 +- 2 files changed, 91 insertions(+), 83 deletions(-) diff --git a/plugin/bbb/cron_close_meeting.php b/plugin/bbb/cron_close_meeting.php index 36cc3f40df..030841660c 100644 --- a/plugin/bbb/cron_close_meeting.php +++ b/plugin/bbb/cron_close_meeting.php @@ -13,91 +13,101 @@ $roomTable = Database::get_main_table('plugin_bbb_room'); $applyAllUrls = 'true' === $plugin->get('plugin_bbb_multiple_urls_cron_apply_to_all'); $bbb = new bbb(); -if ($bbb->pluginEnabled) { - $activeSessions = $bbb->getActiveSessions($applyAllUrls); - - if (!empty($activeSessions)) { - foreach ($activeSessions as $value) { - $meetingId = $value['id']; - $courseCode = null; - $courseInfo = api_get_course_info_by_id($value['c_id']); - if (!empty($courseInfo)) { - $courseCode = $courseInfo['code']; - } - $meetingBBB = $bbb->getMeetingInfo( - [ - 'meetingId' => $value['remote_id'], - 'password' => $value['moderator_pw'], - ] +if (!$bbb->pluginEnabled) { + return; +} + +$activeSessions = $bbb->getActiveSessions($applyAllUrls); + +if (empty($activeSessions)) { + return; +} + +foreach ($activeSessions as $value) { + $meetingId = $value['id']; + $courseCode = null; + $courseInfo = api_get_course_info_by_id($value['c_id']); + if (!empty($courseInfo)) { + $courseCode = $courseInfo['code']; + } + + $meetingBBB = $bbb->getMeetingInfo( + [ + 'meetingId' => $value['remote_id'], + 'password' => $value['moderator_pw'], + ] + ); + + if ($meetingBBB === false) { + //checking with the remote_id didn't work, so just in case and + // to provide backwards support, check with the id + $params = [ + 'meetingId' => $value['id'], + 'password' => $value['moderator_pw'], + ]; + $meetingBBB = $bbb->getMeetingInfo($params); + } + + if (empty($meetingBBB)) { + continue; + } + + if (!isset($meetingBBB['returncode'])) { + continue; + } + + $action = (string) $meetingBBB['returncode']; + + switch ($action) { + case 'FAILED': + $bbb->endMeeting($value['id'], $courseCode); + break; + case 'SUCCESS': + Database::update( + $roomTable, + ['close' => BBBPlugin::ROOM_CHECK], + ['meeting_id = ? AND close= ?' => [$meetingId, BBBPlugin::ROOM_OPEN]] ); - if ($meetingBBB === false) { - //checking with the remote_id didn't work, so just in case and - // to provide backwards support, check with the id - $params = [ - 'meetingId' => $value['id'], - 'password' => $value['moderator_pw'], - ]; - $meetingBBB = $bbb->getMeetingInfo($params); - } + $i = 0; + while ($i < $meetingBBB['participantCount']) { + $participantId = $meetingBBB[$i]['userId']; + $roomData = Database::select( + '*', + $roomTable, + [ + 'where' => [ + 'meeting_id = ? AND participant_id = ? AND close = ?' => [ + $meetingId, + $participantId, + BBBPlugin::ROOM_CHECK, + ], + ], + 'order' => 'id DESC', + ], + 'first' + ); - if (!empty($meetingBBB)) { - if (isset($meetingBBB['returncode'])) { - $action = (string) $meetingBBB['returncode']; - switch ($action) { - case 'FAILED': - $bbb->endMeeting($value['id'], $courseCode); - break; - case 'SUCCESS': - Database::update( - $roomTable, - ['close' => BBBPlugin::ROOM_CHECK], - ['meeting_id = ? AND close= ?' => [$meetingId, BBBPlugin::ROOM_OPEN]] - ); - - $i = 0; - while ($i < $meetingBBB['participantCount']) { - $participantId = $meetingBBB[$i]['userId']; - $roomData = Database::select( - '*', - $roomTable, - [ - 'where' => [ - 'meeting_id = ? AND participant_id = ? AND close = ?' => [ - $meetingId, - $participantId, - BBBPlugin::ROOM_CHECK, - ], - ], - 'order' => 'id DESC', - ], - 'first' - ); - - if (!empty($roomData)) { - $roomId = $roomData['id']; - if (!empty($roomId)) { - Database::update( - $roomTable, - ['out_at' => api_get_utc_datetime(), 'close' => BBBPlugin::ROOM_OPEN], - ['id = ? ' => $roomId] - ); - } - } - $i++; - } - - Database::update( - $roomTable, - ['out_at' => api_get_utc_datetime(), 'close' => BBBPlugin::ROOM_CLOSE], - ['meeting_id = ? AND close= ?' => [$meetingId, BBBPlugin::ROOM_CHECK]] - ); - - break; + if (!empty($roomData)) { + $roomId = $roomData['id']; + if (!empty($roomId)) { + Database::update( + $roomTable, + ['out_at' => api_get_utc_datetime(), 'close' => BBBPlugin::ROOM_OPEN], + ['id = ? ' => $roomId] + ); } } + $i++; } - } + + Database::update( + $roomTable, + ['out_at' => api_get_utc_datetime(), 'close' => BBBPlugin::ROOM_CLOSE], + ['meeting_id = ? AND close= ?' => [$meetingId, BBBPlugin::ROOM_CHECK]] + ); + + break; } } diff --git a/plugin/bbb/lib/bbb.lib.php b/plugin/bbb/lib/bbb.lib.php index cf1e19bcc1..94b07ef709 100755 --- a/plugin/bbb/lib/bbb.lib.php +++ b/plugin/bbb/lib/bbb.lib.php @@ -1880,7 +1880,7 @@ class bbb * * @return array */ - public function getActiveSessions(bool $allSites = false) + public function getActiveSessions(bool $allSites = false): array { $where = ['where' => ['status = ?' => 1]]; @@ -1888,13 +1888,11 @@ class bbb $where['where'][' AND access_url = ?'] = $this->accessUrl; } - $meetingList = Database::select( + return Database::select( '*', $this->table, $where ); - - return $meetingList; } /**