]> git.mjollnir.org Git - moodle.git/commitdiff
ajax: MDL-17084 provide a way for JavaScript to update user preferences.
authortjhunt <tjhunt>
Fri, 31 Oct 2008 08:25:19 +0000 (08:25 +0000)
committertjhunt <tjhunt>
Fri, 31 Oct 2008 08:25:19 +0000 (08:25 +0000)
lang/en_utf8/error.php
lib/ajax/setuserpref.php [new file with mode: 0644]
lib/javascript-static.js
lib/moodlelib.php
lib/weblib.php

index cf47e555cf45b9e4e0e8e3b4ad16dd36ca4b0d43..ae68bbcaa1e884688df3187068afed4714bda4ea 100644 (file)
@@ -205,6 +205,7 @@ $string['errorcreatingfile'] = 'Error creating file \"$a\"';
 $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';
@@ -354,6 +355,7 @@ $string['nosite'] = 'No sites';
 $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';
diff --git a/lib/ajax/setuserpref.php b/lib/ajax/setuserpref.php
new file mode 100644 (file)
index 0000000..e112aef
--- /dev/null
@@ -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';
+?>
\ No newline at end of file
index 8959784abd4fc05d50293e29d8242c1cd69482ff..88546bc4359f63e25e91aeb3f603ce76e9099997 100644 (file)
@@ -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); 
+    }
 }
\ No newline at end of file
index 9f41bcf6b8160b20a28a1d9b710498c7420be616..6fc69f94e0b06840723ce2eb8984999a2edcd29e 100644 (file)
@@ -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
@@ -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 ////////////////////////////////////////////
 
index eeb0c69c4c4e5cbd40f451e9597f952c052aa069..74362475e669127f8cfa6fe5b2201c7b168e3708 100644 (file)
@@ -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;