Commit 67df96f0 authored by Richard Mansfield's avatar Richard Mansfield

Change the name of the blogfiles folder when a user changes language setting

parent a4abcab7
......@@ -192,7 +192,11 @@ 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) {
$USER->set_account_preference($pref, $values[$pref]);
if ($pref == 'lang') {
$USER->set_language($values[$pref]);
} else {
$USER->set_account_preference($pref, $values[$pref]);
}
}
db_commit();
......
......@@ -724,11 +724,11 @@ class ArtefactTypeBlogPost extends ArtefactType {
return $fileid;
}
public static function blogfiles_folder_id() {
public static function blogfiles_folder_id($create = true) {
$name = get_string('blogfilesdirname', 'artefact.blog');
$description = get_string('blogfilesdirdescription', 'artefact.blog');
safe_require('artefact', 'file');
return ArtefactTypeFolder::get_folder_id($name, $description);
return ArtefactTypeFolder::get_folder_id($name, $description, null, null, $create);
}
/**
......
......@@ -813,12 +813,15 @@ class ArtefactTypeFolder extends ArtefactTypeFileBase {
}
// Get the id of a folder, creating the folder if necessary
public static function get_folder_id($name, $description, $parentfolderid=null, $userid=null) {
public static function get_folder_id($name, $description, $parentfolderid=null, $userid=null, $create=true) {
if (empty($userid)) {
global $USER;
$userid = $USER->id;
}
if (!$record = self::get_folder_by_name($name, $parentfolderid, $userid)) {
if (!$create) {
return false;
}
$data = new StdClass;
$data->title = $name;
$data->description = $description;
......
......@@ -290,6 +290,46 @@ class User {
$this->set('accountprefs', $accountprefs);
}
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
*
......
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