$string['erroronline'] = 'Error on line $a';
$string['errorreadingfile'] = 'Error reading file \"$a\"';
$string['errorunzippingfiles'] = 'Error unzipping files';
+$string['errorsettinguserpref'] = 'Error setting user preference';
$string['expiredkey'] = 'Expired key';
$string['failtoloadblocks'] = 'One or more blocks are registered in the database, but they all failed to load!';
$string['fieldrequired'] = '\"$a\" is a required field';
$string['nositeid'] = 'No site ID';
$string['nofolder'] = 'Requested directory does not exist';
$string['nostatstodisplay'] = 'Sorry, there is no available data to display';
+$string['notallowedtoupdateprefremotely'] = 'You are not allowed to udpate this user preference remotely';
$string['notavailable'] = 'That is not currently available';
$string['notmemberofgroup'] = 'You are not a member of this course group';
$string['notownerofkey'] = 'You are not owner of this key';
--- /dev/null
+<?php // $Id$
+
+///////////////////////////////////////////////////////////////////////////
+// //
+// NOTICE OF COPYRIGHT //
+// //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment //
+// http://moodle.org //
+// //
+// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
+// //
+// This program is free software; you can redistribute it and/or modify //
+// it under the terms of the GNU General Public License as published by //
+// the Free Software Foundation; either version 2 of the License, or //
+// (at your option) any later version. //
+// //
+// This program is distributed in the hope that it will be useful, //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
+// GNU General Public License for more details: //
+// //
+// http://www.gnu.org/copyleft/gpl.html //
+// //
+///////////////////////////////////////////////////////////////////////////
+
+/**
+ * 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.
+ *
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package moodlecore
+ */
+
+require_once(dirname(__FILE__) . '/../../config.php');
+
+// Check access.
+if (!isloggedin()) {;
+ print_error('mustbeloggedin');
+}
+if (!confirm_sesskey()) {
+ print_error('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])) {
+ print_error('notallowedtoupdateprefremotely');
+}
+
+// Get and the value.
+$value = required_param('value', $USER->ajax_updatable_user_prefs[$name]);
+
+// Update
+if (!set_user_preference($name, $value)) {
+ print_error('errorsettinguserpref');
+}
+
+echo 'OK';
+?>
\ No newline at end of file
}
emoticons_help.inputarea.focus();
}
+}
+
+/**
+ * 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 || moodle_cfg.developerdebug) {
+ var url = moodle_cfg.wwwroot + '/lib/ajax/setuserpref.php?sesskey=' +
+ moodle_cfg.sesskey + '&pref=' + encodeURI(name) + '&value=' + encodeURI(value);
+
+ // If we are a developer, ensure that failures are reported.
+ var callback = {};
+ if (moodle_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);
+ }
}
\ No newline at end of file
return $DB->delete_records('user_preferences', array('userid'=>$userid, 'name'=>$name));
}
-
/**
* Sets a whole array of preferences for the current user
* @param array $prefarray An array of key/value pairs to be set
}
}
+/**
+ * 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.
+ *
+ * @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.
+ */
+function user_preference_allow_ajax_update($name, $paramtype) {
+ global $USER;
+ require_js(array('yui_yahoo', 'yui_connection'));
+ $USER->ajax_updatable_user_prefs[$name] = $paramtype;
+}
/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
'wwwroot' => $CFG->httpswwwroot, // Yes, really.
'pixpath' => $CFG->pixpath,
'modpixpath' => $CFG->modpixpath,
+ 'sesskey' => sesskey(),
);
if (debugging('', DEBUG_DEVELOPER)) {
$config['developerdebug'] = true;