Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 and 403]

   1  <?php
   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  /**
  19   * Library functions to facilitate the use of ajax JavaScript in Moodle.
  20   *
  21   * @package   core
  22   * @copyright 2009 Tim Hunt
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  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.
  29   *
  30   * @package  core
  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
  35   */
  36  function user_preference_allow_ajax_update($name, $paramtype) {
  37      global $USER, $PAGE;
  38  
  39      // Record in the session that this user_preference is allowed to updated remotely.
  40      $USER->ajax_updatable_user_prefs[$name] = $paramtype;
  41  }
  42  
  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   *
  49   * @return Boolean Returns true on success or false on failure.
  50   */
  51  function 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   */
  64  function 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  
  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);
  78          }
  79          error_log('Potential coding error: ' . $message);
  80      }
  81      return $output;
  82  }