2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Library functions to facilitate the use of ajax JavaScript in Moodle.
22 * @copyright 2009 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * You need to call this function if you wish to use the set_user_preference method in javascript_static.php, to white-list the
28 * preference you want to update from JavaScript, and to specify the type of cleaning you expect to be done on values.
31 * @category preference
32 * @param string $name the name of the user_perference we should allow to be updated by remote calls.
33 * @param integer $paramtype one of the PARAM_{TYPE} constants, user to clean submitted values before set_user_preference is called.
36 function user_preference_allow_ajax_update($name, $paramtype) {
39 // Record in the session that this user_preference is allowed to updated remotely.
40 $USER->ajax_updatable_user_prefs
[$name] = $paramtype;
44 * Starts capturing output whilst processing an AJAX request.
46 * This should be used in combination with ajax_check_captured_output to
47 * report any captured output to the user.
49 * @return Boolean Returns true on success or false on failure.
51 function ajax_capture_output() {
52 // Start capturing output in case of broken plugins.
57 * Check captured output for content. If the site has a debug level of
58 * debugdeveloper set, and the content is non-empty, then throw a coding
59 * exception which can be captured by the Y.IO request and displayed to the
62 * @return Any output that was captured.
64 function ajax_check_captured_output() {
67 // Retrieve the output - there should be none.
68 $output = ob_get_contents();
71 if (!empty($output)) {
72 $message = 'Unexpected output whilst processing AJAX request. ' .
73 'This could be caused by trailing whitespace. Output received: ' .
74 var_export($output, true);
75 if ($CFG->debugdeveloper
&& !empty($output)) {
76 // Only throw an error if the site is in debugdeveloper.
77 throw new coding_exception($message);
79 error_log('Potential coding error: ' . $message);