From a7801515fc8cf0e0dd26cd72738da6404f1c2093 Mon Sep 17 00:00:00 2001 From: Lai Wei Date: Mon, 14 Oct 2024 10:02:48 +0100 Subject: [PATCH] Read custom course sync status from DB to avoid caching issue --- .../o365/classes/feature/coursesync/utils.php | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/local/o365/classes/feature/coursesync/utils.php b/local/o365/classes/feature/coursesync/utils.php index 6887286e1..51502aac5 100644 --- a/local/o365/classes/feature/coursesync/utils.php +++ b/local/o365/classes/feature/coursesync/utils.php @@ -69,7 +69,12 @@ public static function get_enabled_courses(bool $returnallids = false) { return true; } } else if ($coursesyncsetting === 'oncustom') { - $coursesenabled = get_config('local_o365', 'coursesynccustom'); + // Get the list of enabled courses from database rather than get_config() to avoid cache issues. + $coursesenabled = $DB->get_field('config_plugins', 'value', ['plugin' => 'local_o365', 'name' => 'coursesynccustom']); + if (!$coursesenabled) { + $coursesenabled = ''; + } + $originalcoursesenabled = $coursesenabled; $coursesenabled = @json_decode($coursesenabled, true); if (!empty($coursesenabled) && is_array($coursesenabled)) { $changed = false; @@ -81,7 +86,10 @@ public static function get_enabled_courses(bool $returnallids = false) { } } if ($changed) { - set_config('coursesynccustom', json_encode($coursesenabled), 'local_o365'); + if ($originalcoursesenabled !== json_encode($coursesenabled)) { + add_to_config_log('coursesynccustom', $originalcoursesenabled, json_encode($coursesenabled), 'local_o365'); + set_config('coursesynccustom', json_encode($coursesenabled), 'local_o365'); + } } return array_keys($coursesenabled); } @@ -222,7 +230,15 @@ public static function delete_microsoft_365_group(int $courseid) { * @param bool $enabled Whether to enable or disable. */ public static function set_course_sync_enabled(int $courseid, bool $enabled = true) { - $customcoursesyncsetting = get_config('local_o365', 'coursesynccustom'); + global $DB; + + // Get the list of enabled courses from database rather than get_config() to avoid cache issues. + $customcoursesyncsetting = $DB->get_field('config_plugins', 'value', + ['plugin' => 'local_o365', 'name' => 'coursesynccustom']); + if ($customcoursesyncsetting === false) { + $customcoursesyncsetting = ''; + } + $originalcustomcoursesyncsetting = $customcoursesyncsetting; $customcoursesyncsetting = @json_decode($customcoursesyncsetting, true); if (empty($customcoursesyncsetting) || !is_array($customcoursesyncsetting)) { $customcoursesyncsetting = []; @@ -239,7 +255,11 @@ public static function set_course_sync_enabled(int $courseid, bool $enabled = tr } } - set_config('coursesynccustom', json_encode($customcoursesyncsetting), 'local_o365'); + if ($originalcustomcoursesyncsetting != json_encode($customcoursesyncsetting)) { + add_to_config_log('coursesynccustom', $originalcustomcoursesyncsetting, json_encode($customcoursesyncsetting), + 'local_o365'); + set_config('coursesynccustom', json_encode($customcoursesyncsetting), 'local_o365'); + } } /**