MDL-76974 user: deprecate old method of setting user preferences.

The `M.util.set_user_preference` method (plus accompanying helpers)
are now deprecated, in favour of the `core_user/repository` module
to achieve the same.

Update current implementation to proxy calls to the new API ensuring
backwards compatibility, while emitting copious developer debugging.
This commit is contained in:
Paul Holden
2023-08-22 14:42:58 +01:00
parent e998f14061
commit d096c60e0c
6 changed files with 33 additions and 21 deletions
+4
View File
@@ -32,10 +32,14 @@
* @param string $name the name of the user_perference we should allow to be updated by remote calls.
* @param integer $paramtype one of the PARAM_{TYPE} constants, user to clean submitted values before set_user_preference is called.
* @return null
*
* @deprecated since Moodle 4.3
*/
function user_preference_allow_ajax_update($name, $paramtype) {
global $USER, $PAGE;
debugging(__FUNCTION__ . '() is deprecated. Please use the "core_user/repository" module instead.', DEBUG_DEVELOPER);
// Record in the session that this user_preference is allowed to updated remotely.
$USER->ajax_updatable_user_prefs[$name] = $paramtype;
}
+3
View File
@@ -24,6 +24,7 @@
* @category preference
* @copyright 2008 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated since Moodle 4.3
*/
require_once(__DIR__ . '/../../config.php');
@@ -39,6 +40,8 @@ if (!isset($USER->ajax_updatable_user_prefs[$name])) {
throw new \moodle_exception('notallowedtoupdateprefremotely');
}
debugging('Use of setuserpref.php is deprecated. Please use the "core_user/repository" module instead.', DEBUG_DEVELOPER);
// Get and the value.
$value = required_param('value', $USER->ajax_updatable_user_prefs[$name]);
+1
View File
@@ -1912,6 +1912,7 @@ $functions = array(
'type' => 'write',
'capabilities' => 'moodle/site:config',
'ajax' => true,
'loginrequired' => false,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_user_agree_site_policy' => array(
+7 -18
View File
@@ -195,27 +195,16 @@ M.util.CollapsibleRegion.prototype.icon = null;
* user_preference_allow_ajax_update from moodlelib.php to tell Moodle that
* the udpate is allowed, and how to safely clean and submitted values.
*
* @param String name the name of the setting to udpate.
* @param String the value to set it to.
* @param {String} name the name of the setting to update.
* @param {String} value the value to set it to.
*
* @deprecated since Moodle 4.3.
*/
M.util.set_user_preference = function(name, value) {
YUI().use('io', function(Y) {
var url = M.cfg.wwwroot + '/lib/ajax/setuserpref.php?sesskey=' +
M.cfg.sesskey + '&pref=' + encodeURI(name) + '&value=' + encodeURI(value);
Y.log('M.util.set_user_preference is deprecated. Please use the "core_user/repository" module instead.', 'warn');
// If we are a developer, ensure that failures are reported.
var cfg = {
method: 'get',
on: {}
};
if (M.cfg.developerdebug) {
cfg.on.failure = function(id, o, args) {
alert("Error updating user preference '" + name + "' using ajax. Clicking this link will repeat the Ajax call that failed so you can see the error: ");
}
}
// Make the request.
Y.io(url, cfg);
require(['core_user/repository'], function(UserRepository) {
UserRepository.setUserPreference(name, value);
});
};
+14 -3
View File
@@ -1822,14 +1822,14 @@ class core_user_external extends \core_external\external_api {
* @throws moodle_exception
*/
public static function set_user_preferences($preferences) {
global $USER;
global $PAGE, $USER;
$params = self::validate_parameters(self::set_user_preferences_parameters(), array('preferences' => $preferences));
$warnings = array();
$saved = array();
$context = context_system::instance();
self::validate_context($context);
$PAGE->set_context($context);
$userscache = array();
foreach ($params['preferences'] as $pref) {
@@ -1855,7 +1855,18 @@ class core_user_external extends \core_external\external_api {
}
try {
if (core_user::can_edit_preference($pref['name'], $user)) {
// Support legacy preferences from the old M.util.set_user_preference API (always using the current user).
if (isset($USER->ajax_updatable_user_prefs[$pref['name']])) {
debugging('Updating preferences via ajax_updatable_user_prefs is deprecated. ' .
'Please use the "core_user/repository" module instead.', DEBUG_DEVELOPER);
set_user_preference($pref['name'], $pref['value']);
$saved[] = array(
'name' => $pref['name'],
'userid' => $USER->id,
);
} else if (core_user::can_edit_preference($pref['name'], $user)) {
$value = core_user::clean_preference($pref['value'], $pref['name']);
set_user_preference($pref['name'], $value, $user->id);
$saved[] = array(
+4
View File
@@ -9,6 +9,10 @@ This files describes API changes for code that uses the user API.
* The `core_user/repository` Javascript module now exports new methods for manipulating user preferences:
- `[get|set]UserPreference`
- `[get|set]UserPreferences`
* The following user preference helpers have been deprecated, please use the `core_user/repository` module instead:
- `user_preference_allow_ajax_update`
- `M.util.set_user_preference`
- `lib/ajax/setuserpref.php`
* The external `core_user_set_user_preferences` method will now default the `userid` property of each preference to
that of the current user, if omitted
* The following previously deprecated methods have been removed and can no longer be used: