on-demand release 4.0dev+
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
f72f94a2 1<?php
b2330db6 2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17
18/**
0bb38e8c 19 * Library functions to facilitate the use of ajax JavaScript in Moodle.
b2330db6 20 *
4a3ecbb2 21 * @package core
b2330db6 22 * @copyright 2009 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
b2330db6 26/**
4d6b40a0
GGC
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.
b2330db6 29 *
56588dfb 30 * @package core
4d6b40a0
GGC
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.
34 * @return null
b2330db6 35 */
0bb38e8c
PS
36function user_preference_allow_ajax_update($name, $paramtype) {
37 global $USER, $PAGE;
2b8c3f8c 38
0bb38e8c
PS
39 // Record in the session that this user_preference is allowed to updated remotely.
40 $USER->ajax_updatable_user_prefs[$name] = $paramtype;
b2330db6 41}
42
af736f90
AN
43/**
44 * Starts capturing output whilst processing an AJAX request.
45 *
46 * This should be used in combination with ajax_check_captured_output to
47 * report any captured output to the user.
48 *
2ef8ff9e 49 * @return Boolean Returns true on success or false on failure.
af736f90
AN
50 */
51function ajax_capture_output() {
52 // Start capturing output in case of broken plugins.
53 return ob_start();
54}
55
56/**
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
60 * user.
61 *
62 * @return Any output that was captured.
63 */
64function ajax_check_captured_output() {
65 global $CFG;
66
67 // Retrieve the output - there should be none.
68 $output = ob_get_contents();
69 ob_end_clean();
70
1fa31fb3
AN
71 if (!empty($output)) {
72 $message = 'Unexpected output whilst processing AJAX request. ' .
af736f90 73 'This could be caused by trailing whitespace. Output received: ' .
1fa31fb3
AN
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);
78 }
79 error_log('Potential coding error: ' . $message);
af736f90
AN
80 }
81 return $output;
82}