From 5f4565b2717d6c07421d5f3ab6711a0feea44644 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 30 Sep 2024 10:07:46 +0100 Subject: [PATCH] MDL-79124 user: final removal of deprecated user preference methods. --- .upgradenotes/MDL-79124-2024100909171769.yml | 14 ++++++ lang/en/deprecated.txt | 2 + lang/en/error.php | 4 +- lib/ajax/ajaxlib.php | 19 ++----- lib/ajax/setuserpref.php | 53 -------------------- lib/javascript-static.js | 18 +------ user/externallib.php | 12 +---- 7 files changed, 24 insertions(+), 98 deletions(-) create mode 100644 .upgradenotes/MDL-79124-2024100909171769.yml delete mode 100644 lib/ajax/setuserpref.php diff --git a/.upgradenotes/MDL-79124-2024100909171769.yml b/.upgradenotes/MDL-79124-2024100909171769.yml new file mode 100644 index 00000000000..02121932cc6 --- /dev/null +++ b/.upgradenotes/MDL-79124-2024100909171769.yml @@ -0,0 +1,14 @@ +issueNumber: MDL-79124 +notes: + core_user: + - message: >- + Final removal of the following user preference helpers, please use the + `core_user/repository` module instead: + + + - `user_preference_allow_ajax_update` + + - `M.util.set_user_preference` + + - `lib/ajax/setuserpref.php` + type: removed diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index 636b5145612..99f6c80e452 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -142,6 +142,8 @@ noreplybouncesubject,core configgeneralquestionbank,core_backup generalquestionbank,core_backup cannotdeletecategoryquestions,core_error +errorsettinguserpref,core_error +notallowedtoupdateprefremotely,core_error errordeletingquestionsfromcategory,core_question importantupdates_content,core_admin importantupdates_title,core_admin diff --git a/lang/en/error.php b/lang/en/error.php index f45ab1d83a1..4ed389287cc 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -260,7 +260,6 @@ $string['erroroutput'] = 'Error output, so disabling automatic redirect.'; $string['errorparsingxml'] = 'Error parsing XML: {$a->errorstring} at line {$a->errorline}, char {$a->errorchar}'; $string['errorreadingfile'] = 'Error reading file "{$a}"'; $string['errorsavingrequest'] = 'An error occurred when trying to save your request.'; -$string['errorsettinguserpref'] = 'Error setting user preference'; $string['errorunzippingfiles'] = 'Error unzipping files'; $string['expiredkey'] = 'Expired key'; $string['externalauthpassworderror'] = 'Non-empty password for external authentication'; @@ -484,7 +483,6 @@ $string['nosite'] = 'Could not find a top-level course!'; $string['nositeid'] = 'No site ID'; $string['nostartdatenoenddate'] = 'A course end date can only be set if a start date is also set.'; $string['nostatstodisplay'] = 'Sorry, there is no available data to display'; -$string['notallowedtoupdateprefremotely'] = 'You are not allowed to update this user preference remotely'; $string['notavailable'] = 'That is not currently available'; $string['notfound'] = 'Not found'; $string['notlocalisederrormessage'] = '{$a}'; @@ -668,3 +666,5 @@ $string['myisamproblem'] = 'Database tables are using MyISAM database engine, it // Deprecated since Moodle 5.0. $string['cannotdeletecategoryquestions'] = 'Could not delete questions from category \'{$a}\''; +$string['errorsettinguserpref'] = 'Error setting user preference'; +$string['notallowedtoupdateprefremotely'] = 'You are not allowed to update this user preference remotely'; diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index d4f8eff2584..9be88035629 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -24,24 +24,11 @@ */ /** - * You need to call this function if you wish to use the set_user_preference method in javascript_static.php, to white-list the - * preference you want to update from JavaScript, and to specify the type of cleaning you expect to be done on values. - * - * @package core - * @category preference - * @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; +#[\core\attribute\deprecated('\'core_user/repository\' module', since: '4.3', mdl: 'MDL-76974', final: true)] +function user_preference_allow_ajax_update() { + \core\deprecation::emit_deprecation_if_present(__FUNCTION__); } /** diff --git a/lib/ajax/setuserpref.php b/lib/ajax/setuserpref.php deleted file mode 100644 index b40e103480e..00000000000 --- a/lib/ajax/setuserpref.php +++ /dev/null @@ -1,53 +0,0 @@ -. - -/** - * Code to update a user preference in response to an ajax call. - * - * You should not send requests to this script directly. Instead use the set_user_preference - * function in javascript_static.js. - * - * @package core - * @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'); - -// Check access. -if (!confirm_sesskey()) { - throw new \moodle_exception('invalidsesskey'); -} - -// Get the name of the preference to update, and check it is allowed. -$name = required_param('pref', PARAM_RAW); -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]); - -// Update -if (!set_user_preference($name, $value)) { - throw new \moodle_exception('errorsettinguserpref'); -} - -echo 'OK'; diff --git a/lib/javascript-static.js b/lib/javascript-static.js index 28a63bd349f..c581376d533 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -194,24 +194,10 @@ M.util.CollapsibleRegion.prototype.div = null; M.util.CollapsibleRegion.prototype.icon = null; /** - * Makes a best effort to connect back to Moodle to update a user preference, - * however, there is no mechanism for finding out if the update succeeded. - * - * Before you can use this function in your JavsScript, you must have called - * 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 update. - * @param {String} value the value to set it to. - * * @deprecated since Moodle 4.3. */ -M.util.set_user_preference = function(name, value) { - Y.log('M.util.set_user_preference is deprecated. Please use the "core_user/repository" module instead.', 'warn'); - - require(['core_user/repository'], function(UserRepository) { - UserRepository.setUserPreference(name, value); - }); +M.util.set_user_preference = function() { + throw new Error('M.util.set_user_preference is deprecated. Please use the "core_user/repository" module instead.'); }; /** diff --git a/user/externallib.php b/user/externallib.php index 6713b6324ee..29175a02d2a 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -1846,17 +1846,7 @@ class core_user_external extends \core_external\external_api { try { - // 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)) { + 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(