From b8c6de6b57c528e9240faf580bc707695ee578be Mon Sep 17 00:00:00 2001 From: Prasanna LMSACE Date: Fri, 15 Sep 2023 14:19:15 +0530 Subject: [PATCH] Improved menus and items language restriction based on current language. - Fix #358 (UN-235) --- classes/smartmenu.php | 22 ++++++++++++++++++++++ smartmenus/menulib.php | 21 ++++++++++----------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/classes/smartmenu.php b/classes/smartmenu.php index f88137791ef..ff338a22c75 100644 --- a/classes/smartmenu.php +++ b/classes/smartmenu.php @@ -939,6 +939,9 @@ public static function build_smartmenu() { $nodes = []; + // Verify the language changes in user session, if changed than purge the menus and items cache for the user session. + self::verify_lang_session_changes(); + $cache = cache::make('theme_boost_union', 'smartmenus'); // Fetch the list of menus from cache. $topmenus = $cache->get(self::CACHE_MENUSLIST); @@ -978,4 +981,23 @@ public static function build_smartmenu() { return $nodes; } + + /** + * Verifies and handles changes in the session language. + * Clears cached smart menus and items when the user changes the language using the language menu. + * + * @return void + */ + protected static function verify_lang_session_changes() { + global $SESSION, $USER; + // Make sure the lang is updated for the session. + if ($lang = optional_param('lang', '', PARAM_SAFEDIR)) { + // Confirm the cache is not already purged for this language change. To avoid multiple purge. + if (!isset($SESSION->prevlang) || $SESSION->prevlang != $lang) { + // Set the purge cache preference for this session user. Cache will purged in the build_smartmenu method. + \smartmenu_helper::set_user_purgecache($USER->id); + $SESSION->prevlang = $lang; // Save this lang for verification. + } + } + } } diff --git a/smartmenus/menulib.php b/smartmenus/menulib.php index fc926d2a30e..62d59b2fb39 100644 --- a/smartmenus/menulib.php +++ b/smartmenus/menulib.php @@ -98,8 +98,10 @@ public function verify_access_restrictions() { // Restriction by cohorts. $this->restriction_bycohorts($query); - // REstriction by lanuages. - $this->restriction_bylanguage($query); + // Restriction by lanuages. + if (!$this->restriction_bylanguage()) { + return false; + } // Restriction by date. Menu configured date is not started or already ended then hide the menu. if (!$this->restriction_bydate()) { @@ -188,24 +190,21 @@ public function restriction_bycohorts(&$query) { } /** - * Generate the queries and params to verify the user has the language which is contained in the current data. + * Verify the menu has restricted based on the current language. * - * @param stdclass $query Array which contains elements for DB conditions, selectors and params. - * @return void + * @return bool True if the menu is avialble for this lanauge, otherwise false menu will not shown to current user. */ - public function restriction_bylanguage(&$query) { + public function restriction_bylanguage() { global $DB; $languages = $this->data->languages; if (empty($languages)) { return true; } - list($insql, $inparam) = $DB->get_in_or_equal($languages, SQL_PARAMS_NAMED, 'la'); + // Current language selected for this session. + $lang = current_language(); - if (!empty($inparam)) { - $query->where[] = "u.lang $insql"; - $query->params += $inparam; - } + return in_array($lang, $languages); // This item is available for the current language. } /**