Skip to content

Commit

Permalink
ajax: MDL-17084 provide a way for JavaScript to update user preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhunt committed Oct 31, 2008
1 parent bc0b96f commit bd1884f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lang/en_utf8/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
$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';
Expand Down Expand Up @@ -354,6 +355,7 @@
$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';
Expand Down
60 changes: 60 additions & 0 deletions lib/ajax/setuserpref.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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';
?>
35 changes: 35 additions & 0 deletions lib/javascript-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,39 @@ emoticons_help = {
}
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);
}
}
17 changes: 16 additions & 1 deletion lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,6 @@ function unset_user_preference($name, $otheruserid=NULL) {
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
Expand Down Expand Up @@ -1103,6 +1102,22 @@ function get_user_preferences($name=NULL, $default=NULL, $otheruserid=NULL) {
}
}

/**
* 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 ////////////////////////////////////////////

Expand Down
1 change: 1 addition & 0 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2811,6 +2811,7 @@ function standard_js_config() {
'wwwroot' => $CFG->httpswwwroot, // Yes, really.
'pixpath' => $CFG->pixpath,
'modpixpath' => $CFG->modpixpath,
'sesskey' => sesskey(),
);
if (debugging('', DEBUG_DEVELOPER)) {
$config['developerdebug'] = true;
Expand Down

0 comments on commit bd1884f

Please sign in to comment.