Commit 3cf4b34d authored by Richard Mansfield's avatar Richard Mansfield

Rename blogfiles folder when a user's lang pref is changed by anyone

parent 67df96f0
......@@ -192,11 +192,7 @@ function accountprefs_submit(Pieform $form, $values) {
// use this as looping through values is not safe.
$expectedprefs = expected_account_preferences();
foreach (array_keys($expectedprefs) as $pref) {
if ($pref == 'lang') {
$USER->set_language($values[$pref]);
} else {
$USER->set_account_preference($pref, $values[$pref]);
}
$USER->set_account_preference($pref, $values[$pref]);
}
db_commit();
......
......@@ -731,6 +731,25 @@ class ArtefactTypeBlogPost extends ArtefactType {
return ArtefactTypeFolder::get_folder_id($name, $description, null, null, $create);
}
// Change the name & description of a user's blogfiles folder when the user changes language pref
public static function change_language($userid, $oldlang, $newlang) {
$oldname = get_string_from_language($oldlang, 'blogfilesdirname', 'artefact.blog');
safe_require('artefact', 'file');
$blogfiles = ArtefactTypeFolder::get_folder_by_name($oldname, null, $userid);
if (empty($blogfiles)) {
return;
}
$name = get_string_from_language($newlang, 'blogfilesdirname', 'artefact.blog');
$description = get_string_from_language($newlang, 'blogfilesdirdescription', 'artefact.blog');
if (!empty($name)) {
$blogfiles = artefact_instance_from_id($blogfiles->id);
$blogfiles->set('title', $name);
$blogfiles->set('description', $description);
$blogfiles->commit();
}
}
/**
* This function publishes the blog post.
*
......
......@@ -291,45 +291,6 @@ class User {
}
public function set_language($value) {
$oldlang = $this->get_account_preference('lang');
if (empty($oldlang) || $oldlang == 'default') {
$oldlang = get_config('lang');
}
if (empty($value) || $value == 'default') {
$newlang = get_config('lang');
} else {
$newlang = $value;
}
// Update the name of the user's blogfiles folder.
// @todo: This is nasty because we can't currently specify a
// language when calling get_string(), so we will get the
// folder here, using the old language, and then rename it
// below after the language has been reset.
if ($newlang != $oldlang && get_field('artefact_installed', 'active', 'name', 'blog')) {
safe_require('artefact', 'blog');
$blogfilesid = ArtefactTypeBlogPost::blogfiles_folder_id(false);
}
$this->set_account_preference('lang', $value);
$blogfiles = null;
if ($newlang != $oldlang && !empty($blogfilesid)) {
$name = get_string('blogfilesdirname', 'artefact.blog');
$description = get_string('blogfilesdirdescription', 'artefact.blog');
if (!empty($name)) {
$blogfiles = artefact_instance_from_id($blogfilesid);
$blogfiles->set('title', $name);
$blogfiles->set('description', $description);
$blogfiles->commit();
}
}
}
/**
* Determines if the user is currently logged in
*
......
File mode changed from 100644 to 100755
......@@ -181,6 +181,21 @@ function get_string($identifier, $section='mahara') {
return get_string_location($identifier, $section, $variables);
}
function get_string_from_language($lang, $identifier, $section='mahara') {
$variables = func_get_args();
if (count($variables) > 3) { // we have some stuff we need to sprintf
array_shift($variables);
array_shift($variables);
array_shift($variables); //shift off the first three.
}
else {
$variables = array();
}
return get_string_location($identifier, $section, $variables, 'format_langstring', $lang);
}
function get_helpfile($plugintype, $pluginname, $form, $element, $page=null, $section=null) {
if ($langfile = get_helpfile_location($plugintype, $pluginname, $form, $element, $page, $section)) {
return file_get_contents($langfile);
......@@ -281,7 +296,7 @@ function get_raw_string($identifier, $section='mahara') {
* @param function $replacefunc
* @return string
*/
function get_string_location($identifier, $section, $variables, $replacefunc='format_langstring') {
function get_string_location($identifier, $section, $variables, $replacefunc='format_langstring', $lang='') {
$langconfigstrs = array('parentlanguage', 'strftimedate', 'strftimedateshort', 'strftimedatetime',
'strftimedaydate', 'strftimedaydatetime', 'strftimedayshort', 'strftimedaytime',
......@@ -292,7 +307,9 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo
$section = 'langconfig';
}
$lang = current_language();
if (empty($lang)) {
$lang = current_language();
}
// Define the locations of language strings for this section
$docroot = get_config('docroot');
......
......@@ -84,6 +84,16 @@ function load_account_preferences($userid) {
* @param string $value preference value to set.
*/
function set_account_preference($userid, $field, $value) {
if ($field == 'lang') {
$oldlang = get_field('usr_account_preference', 'value', 'usr', $userid, 'field', 'lang');
if (empty($oldlang) || $oldlang == 'default') {
$oldlang = get_config('lang');
}
$newlang = (empty($value) || $value == 'default') ? get_config('lang') : $value;
if ($newlang != $oldlang) {
change_language($userid, $oldlang, $newlang);
}
}
if (record_exists('usr_account_preference', 'usr', $userid, 'field', $field)) {
set_field('usr_account_preference', 'value', $value, 'usr', $userid, 'field', $field);
}
......@@ -102,6 +112,23 @@ function set_account_preference($userid, $field, $value) {
}
}
/**
* Change language-specific stuff in the db for a user. Currently
* just changes the name of the 'blogfiles' folder in the user's files
* area.
*
* @param int $userid user id to set preference for
* @param string $oldlang old language
* @param string $newlang new language
*/
function change_language($userid, $oldlang, $newlang) {
if (get_field('artefact_installed', 'active', 'name', 'blog')) {
safe_require('artefact', 'blog');
ArtefactTypeBlogPost::change_language($userid, $oldlang, $newlang);
}
}
/**
* sets an activity preference in the database
* if you want to set it in the session as well
......
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