diff --git a/blocks/dock.js b/blocks/dock.js index 4f7578cb2a7..19e4573fddf 100644 --- a/blocks/dock.js +++ b/blocks/dock.js @@ -427,7 +427,7 @@ M.core_dock = { if (!this.skipsetposition) { // save the users preference - set_user_preference('docked_block_instance_'+this.id, 1); + M.util.set_user_preference('docked_block_instance_'+this.id, 1); } else { this.skipsetposition = false; } @@ -504,7 +504,7 @@ M.core_dock = { } this.cachedcontentnode = null; - set_user_preference('docked_block_instance_'+this.id, 0); + M.util.set_user_preference('docked_block_instance_'+this.id, 0); return true; } }, diff --git a/lib/javascript-static.js b/lib/javascript-static.js index b6a007f5bec..2144e7f237f 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -128,7 +128,7 @@ M.util.CollapsibleRegion = function(Y, id, userpref, strtooltip) { animation.set('reverse', this.div.hasClass('collapsed')); // Update the user preference. if (this.userpref) { - set_user_preference(this.userpref, !this.div.hasClass('collapsed')); + M.util.set_user_preference(this.userpref, !this.div.hasClass('collapsed')); } animation.run(); }, this, animation); @@ -215,6 +215,39 @@ M.util.init_help_icons = function(Y) { }); }; + +/** + * 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 udpate. + * @param String the value to set it to. + */ +M.util.set_user_preference = function(name, value) { + YUI(M.yui.loader).use('io', function(Y) { + var url = M.cfg.wwwroot + '/lib/ajax/setuserpref.php?sesskey=' + + M.cfg.sesskey + '&pref=' + encodeURI(name) + '&value=' + encodeURI(value); + + // 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); + }); +}; + //=== old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code === function popupchecker(msg) { @@ -1086,42 +1119,6 @@ emoticons_help = { } } -/** - * 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 udpate. - * @param String the value to set it to. - */ -function set_user_preference(name, value) { - // Don't generate a script error if the library has not been loaded, - // unless we are a Developer, in which case we want the error. - if (YAHOO && YAHOO.util && YAHOO.util.Connect || M.cfg.developerdebug) { - var url = M.cfg.wwwroot + '/lib/ajax/setuserpref.php?sesskey=' + - M.cfg.sesskey + '&pref=' + encodeURI(name) + '&value=' + encodeURI(value); - - // If we are a developer, ensure that failures are reported. - var callback = {}; - if (M.cfg.developerdebug) { - callback.failure = function() { - var a = document.createElement('a'); - a.href = url; - a.classname = 'error'; - a.appendChild(document.createTextNode("Error updating user preference '" + name + "' using ajax. Clicking this link will repeat the Ajax call that failed so you can see the error.")); - document.body.insertBefore(a, document.body.firstChild); - } - } - - // Make the request. - YAHOO.util.Connect.asyncRequest('GET', url, callback); - } -} - - /** * Oject to handle expanding and collapsing blocks when an icon is clicked on. * @constructor @@ -1165,7 +1162,7 @@ block_hider.prototype.handle_click = function(e) { YAHOO.util.Event.stopEvent(e); this.ishidden = !this.ishidden; this.update_state(); - set_user_preference(this.userpref, this.ishidden); + M.util.set_user_preference(this.userpref, this.ishidden); } /** Set the state of the block show/hide icon to this.ishidden. */ diff --git a/user/selector/script.js b/user/selector/script.js index 0058ea010b7..c890b7ad94b 100644 --- a/user/selector/script.js +++ b/user/selector/script.js @@ -516,5 +516,5 @@ function user_selector_options_tracker() { } user_selector_options_tracker.prototype.handle_option_change = function(option) { - set_user_preference(option, document.getElementById(option + 'id').checked); + M.util.set_user_preference(option, document.getElementById(option + 'id').checked); } \ No newline at end of file