Skip to content

Commit

Permalink
Improved menus and items language restriction based on current language.
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanna-lmsace committed Sep 15, 2023
1 parent 7785fbd commit b8c6de6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
22 changes: 22 additions & 0 deletions classes/smartmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
}
}
}
}
21 changes: 10 additions & 11 deletions smartmenus/menulib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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.
}

/**
Expand Down

0 comments on commit b8c6de6

Please sign in to comment.