From b6a5df0b8b7a41d6f4fdc1ddd419d99b6b6d1946 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) --- CHANGES.md | 1 + classes/smartmenu.php | 22 ++++++++++++++++++++++ smartmenus/menulib.php | 21 ++++++++++----------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 63fa1832738..dcb2012e4ac 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ Changes ### Unreleased +* 2023-09-22 - Bugfix: Smart menu language restriction did not respect switching language with the language switcher, solves #358. * 2023-09-26 - Bugfix: The smart menu third level arrow was broken, solves #402. * 2023-09-22 - Make codechecker happier * 2023-09-24 - Test: Behat scenario 'Show hint for self enrolment without an enrolment key' was broken, solves #398. diff --git a/classes/smartmenu.php b/classes/smartmenu.php index c077c0cd3db..d135bbf37a5 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 ffec97fd009..e790ea5c119 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 languages. + 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 available for this lanauage, otherwise false. */ - 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. } /**