diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index 8c7336789ba..d4f8eff2584 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -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; } diff --git a/lib/ajax/setuserpref.php b/lib/ajax/setuserpref.php index a960a288f78..b40e103480e 100644 --- a/lib/ajax/setuserpref.php +++ b/lib/ajax/setuserpref.php @@ -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]); diff --git a/lib/db/services.php b/lib/db/services.php index 354b2da1197..e0a3148472e 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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( diff --git a/lib/javascript-static.js b/lib/javascript-static.js index 02c9f333181..cf790b6dbfb 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -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); }); }; diff --git a/user/externallib.php b/user/externallib.php index cd256926605..0213d9425d5 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -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( diff --git a/user/upgrade.txt b/user/upgrade.txt index 13d978c65b5..292ca7d6426 100644 --- a/user/upgrade.txt +++ b/user/upgrade.txt @@ -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: