Skip to content

Commit

Permalink
MDL-67853 message: Translate user message preference from restore and WS
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Jan 27, 2022
1 parent b4c7598 commit d195956
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
38 changes: 36 additions & 2 deletions backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,41 @@ public static function create_included_users($basepath, $restoreid, $userid,
$preference = (object)$preference;
// Prepare the record and insert it
$preference->userid = $newuserid;
$status = $DB->insert_record('user_preferences', $preference);

// Translate _loggedin / _loggedoff message user preferences to _enabled. (MDL-67853)
// This code cannot be removed.
if (preg_match('/message_provider_.*/', $preference->name)) {
$nameparts = explode('_', $preference->name);
$name = array_pop($nameparts);

if ($name == 'loggedin' || $name == 'loggedoff') {
$preference->name = implode('_', $nameparts).'_enabled';

$existingpreference = $DB->get_record('user_preferences',
['name' => $preference->name , 'userid' => $newuserid]);
// Merge both values.
if ($existingpreference) {
$values = [];

if (!empty($existingpreference->value) && $existingpreference->value != 'none') {
$values = explode(',', $existingpreference->value);
}

if (!empty($preference->value) && $preference->value != 'none') {
$values = array_merge(explode(',', $preference->value), $values);
$values = array_unique($values);
}

$existingpreference->value = empty($values) ? 'none' : implode(',', $values);

$DB->update_record('user_preferences', $existingpreference);
continue;
}
}
}
// End translating loggedin / loggedoff message user preferences.

$DB->insert_record('user_preferences', $preference);
}
}
// Special handling for htmleditor which was converted to a preference.
Expand All @@ -1311,7 +1345,7 @@ public static function create_included_users($basepath, $restoreid, $userid,
$preference->userid = $newuserid;
$preference->name = 'htmleditor';
$preference->value = 'textarea';
$status = $DB->insert_record('user_preferences', $preference);
$DB->insert_record('user_preferences', $preference);
}
}

Expand Down
12 changes: 12 additions & 0 deletions user/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@ public static function update_user_preferences($userid = 0, $emailstop = null, $
if (!empty($preferences)) {
$userpref = ['id' => $userid];
foreach ($preferences as $preference) {

/*
* Rename user message provider preferences to avoid orphan settings on old app versions.
* @todo Remove this "translation" block on MDL-73284.
*/
if (preg_match('/message_provider_.*_loggedin/', $preference['type']) ||
preg_match('/message_provider_.*_loggedoff/', $preference['type'])) {
$nameparts = explode('_', $preference['type']);
array_pop($nameparts);
$preference['type'] = implode('_', $nameparts).'_enabled';
}

$userpref['preference_' . $preference['type']] = $preference['value'];
}
useredit_update_user_preference($userpref);
Expand Down

0 comments on commit d195956

Please sign in to comment.