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
- Fix #358 (UN-235)
  • Loading branch information
prasanna-lmsace authored and abias committed Oct 7, 2023
1 parent 7efda1b commit fadee49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-22 - Make codechecker happier
* 2023-09-24 - Test: Behat scenario 'Show hint for self enrolment without an enrolment key' was broken, solves #398.
* 2023-09-22 - Improvement: Reuse Moodle core function remove_dir(), solves #369.
Expand Down
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 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()) {
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 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.
}

/**
Expand Down

0 comments on commit fadee49

Please sign in to comment.