Commit f291642c authored by Aaron Wells's avatar Aaron Wells
Browse files

Allow institutions to specify their users' default language

Bug 1266320

Change-Id: I24f72049f13e145f46145df2d248a02e2a3af8ba
parent 5be3b920
......@@ -344,6 +344,26 @@ if ($institution || $add) {
'defaultvalue' => $data->defaultmembershipperiod,
'help' => true,
);
$languages = get_languages();
// Get the default language. If the institution has one stored, use that. Otherwise, use 'sitedefault'
$defaultlang = false;
if (!empty($data->name)) {
$defaultlang = get_config_institution($data->name, 'lang');
}
// If the defaultlang they provided is no longer valid, use "site default"
if (!$defaultlang || !array_key_exists($defaultlang, $languages)) {
$defaultlang = 'sitedefault';
}
$elements['lang'] = array(
'type' => 'select',
'defaultvalue' => $defaultlang,
'title' => get_string('institutionlanguage', 'admin'),
'description' => get_string('institutionlanguagedescription', 'admin'),
'options' => array_merge(array('sitedefault' => get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')'), $languages),
'ignore' => (count($languages) < 2),
);
$elements['logo'] = array(
'type' => 'file',
'title' => get_string('Logo', 'admin'),
......@@ -617,6 +637,10 @@ function institution_validate(Pieform $form, $values) {
$form->set_error('logo', get_string('profileiconimagetoobig', 'artefact.file', $width, $height, $imagemaxwidth, $imagemaxheight));
}
}
if (!empty($values['lang']) && $values['lang'] != 'sitedefault' && !array_key_exists($values['lang'], get_languages())) {
$form->set_error('lang', get_string('institutionlanginvalid', 'admin'));
}
}
function institution_submit(Pieform $form, $values) {
......@@ -646,6 +670,16 @@ function institution_submit(Pieform $form, $values) {
$newinstitution->registerallowed = ($values['registerallowed']) ? 1 : 0;
$newinstitution->registerconfirm = ($values['registerconfirm']) ? 1 : 0;
}
if (!empty($values['lang'])) {
if ($values['lang'] == 'sitedefault') {
$newinstitution->lang = null;
}
else {
$newinstitution->lang = $values['lang'];
}
}
$newinstitution->theme = (empty($values['theme']) || $values['theme'] == 'sitedefault') ? null : $values['theme'];
$newinstitution->dropdownmenu = (!empty($values['dropdownmenu'])) ? 1 : 0;
$newinstitution->skins = (!empty($values['skins'])) ? 1 : 0;
......
......@@ -794,6 +794,9 @@ $string['institutionnamealreadytaken'] = 'This institution name is already taken
$string['institutiondisplayname'] = 'Institution display name';
$string['institutionexpiry'] = 'Institution expiry date';
$string['institutionexpirydescription'] = 'The date at which this institutions membership of %s will be suspended.';
$string['institutionlanguage'] = 'Language';
$string['institutionlanguagedescription'] = 'The default language for users in this institution.';
$string['defaultlangforinstitution'] = '%s default';
$string['institutionupdatedsuccessfully'] = 'Institution updated successfully.';
$string['registrationallowed'] = 'Registration allowed';
$string['registrationalloweddescription3'] = 'If checked, people can register for this institution on your site using the registration form. If registration is off, non-members cannot request membership of the institution and members cannot leave the institution or delete their user accounts themselves.';
......
......@@ -287,13 +287,11 @@ class Institution {
if (is_numeric($user)) {
$user = get_record('usr', 'id', $user);
}
if ($user instanceof User) {
$lang = $user->get_account_preference('lang');
if (empty($lang) || $lang == 'default') {
$lang = get_config('lang');
}
// The user hasn't been added yet, so we have to manually use this institution's lang
if ($this->lang != 'default') {
$lang = $this->lang;
}
else { // stdclass object
else {
$lang = get_user_language($user->id);
}
$userinst = new StdClass;
......
......@@ -1185,19 +1185,21 @@ function set_locale_for_language($lang) {
}
/**
* This function returns the current
* language to use, either for a given user
* or sitewide, or the default
* This function returns the current language to use, either for a given user
* or sitewide, or the default.
*
* This method is invoked in every call to get_string(), so make it performant!
*
* @return string
*/
function current_language() {
global $USER, $CFG, $SESSION;
static $userlang, $lastlang;
static $userlang, $lastlang, $instlang;
$loggedin = $USER instanceof User && $USER->is_logged_in();
// Retrieve & cache the user lang pref (if the user is logged in)
if (!isset($userlang) && $loggedin) {
$userlang = $USER->get_account_preference('lang');
if ($userlang !== null && $userlang != 'default') {
......@@ -1208,16 +1210,36 @@ function current_language() {
}
}
// Retrieve & cache the institution language (if the user is logged in)
if (!isset($instlang) && $loggedin) {
$instlang = get_user_institution_language();
}
// Retrieve the $SESSION lang (from the user selecting a language while logged-out)
// Note that if the user selected a language while logged out, and then logs in,
// we will have set their user account pref to match that lang, over in
// LiveUser->authenticate().
if (!$loggedin && is_a($SESSION, 'Session')) {
$sesslang = $SESSION->get('lang');
}
else {
$sesslang = null;
}
// Logged-in user's language preference
if (!empty($userlang) && $userlang != 'default') {
$lang = $userlang;
}
else if (!$loggedin && is_a($SESSION, 'Session')) {
$sesslang = $SESSION->get('lang');
if (!empty($sesslang) && $sesslang != 'default') {
$lang = $sesslang;
}
// Logged-out user's language menu selection
else if (!empty($sesslang) && $sesslang != 'default') {
$lang = $sesslang;
}
// Logged-in user's institution language setting
else if (!empty($instlang) && $instlang != 'default') {
$lang = $instlang;
}
// If there's no language from the user pref or the logged-out lang menu...
if (empty($lang)) {
$lang = !empty($CFG->lang) ? $CFG->lang : 'en.utf8';
}
......@@ -1231,6 +1253,41 @@ function current_language() {
return $lastlang = $lang;
}
/**
* Find out a user's institution language. If they belong to one institution that has specified
* a language, then this will be that institution's language. If they belong to multiple
* institutions that have specified languages, it will be the arbitrarily "first" institution.
* If they belong to no institution that has specified a language, it will return null.
*
* @param int $userid Which user to check (defaults to $USER)
* @param string $sourceinst If provided, the source institution for the language will be
* returned here by reference
* @return string A language, or 'default'
*/
function get_user_institution_language($userid = null, &$sourceinst = null) {
global $USER;
if ($userid == null) {
$userid = $USER->id;
}
$instlangs = get_configs_user_institutions('lang', $userid);
// Every user belongs to at least one institution
foreach ($instlangs as $name => $lang) {
$sourceinst = $name;
$instlang = $lang;
// If the user belongs to multiple institutions, arbitrarily use the language
// from the first one that has specified a language.
if (!empty($instlang) && $instlang != 'default' && language_installed($instlang)) {
break;
}
}
if (!$instlang) {
$instlang = 'default';
}
return $instlang;
}
/**
* Helper function to sprintf language strings
* with a variable number of arguments
......
......@@ -176,6 +176,16 @@ function get_account_preference($userid, $field) {
function get_user_language($userid) {
$langpref = get_account_preference($userid, 'lang');
if (empty($langpref) || $langpref == 'default') {
// Check for an institution language
$langlist = get_configs_user_institutions('lang', $userid);
foreach($langlist as $l) {
if (!empty($l) && $l != 'default') {
return $l;
}
}
// Use the site language
return get_config('lang');
}
return $langpref;
......@@ -260,11 +270,19 @@ function general_account_prefs_form_elements($prefs) {
'help' => true,
);
$languages = get_languages();
// Determine default language.
$instlang = get_user_institution_language($USER->id, $instlanginstname);
if (!empty($instlang) && $instlang != 'default') {
$sitedefaultlabel = get_string('defaultlangforinstitution', 'admin', get_config_institution($instlanginstname, 'displayname')) . ' (' . $languages[$instlang] . ')';
}
else {
$sitedefaultlabel = get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')';
}
$elements['lang'] = array(
'type' => 'select',
'defaultvalue' => $prefs->lang,
'title' => get_string('language', 'account'),
'options' => array_merge(array('default' => get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')'), $languages),
'options' => array_merge(array('default' => $sitedefaultlabel), $languages),
'help' => true,
'ignore' => count($languages) < 2,
);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment