Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [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   * @deprecated since Moodle 4.3
  37   */
  38  function user_preference_allow_ajax_update($name, $paramtype) {
  39      global $USER, $PAGE;
  40  
  41      debugging(__FUNCTION__ . '() is deprecated. Please use the "core_user/repository" module instead.', DEBUG_DEVELOPER);
  42  
  43      // Record in the session that this user_preference is allowed to updated remotely.
  44      $USER->ajax_updatable_user_prefs[$name] = $paramtype;
  45  }
  46  
  47  /**
  48   * Starts capturing output whilst processing an AJAX request.
  49   *
  50   * This should be used in combination with ajax_check_captured_output to
  51   * report any captured output to the user.
  52   *
  53   * @return Boolean Returns true on success or false on failure.
  54   */
  55  function ajax_capture_output() {
  56      // Start capturing output in case of broken plugins.
  57      return ob_start();
  58  }
  59  
  60  /**
  61   * Check captured output for content. If the site has a debug level of
  62   * debugdeveloper set, and the content is non-empty, then throw a coding
  63   * exception which can be captured by the Y.IO request and displayed to the
  64   * user.
  65   *
  66   * @return Any output that was captured.
  67   */
  68  function ajax_check_captured_output() {
  69      global $CFG;
  70  
  71      // Retrieve the output - there should be none.
  72      $output = ob_get_contents();
  73      ob_end_clean();
  74  
  75      if (!empty($output)) {
  76          $message = 'Unexpected output whilst processing AJAX request. ' .
  77                  'This could be caused by trailing whitespace. Output received: ' .
  78                  var_export($output, true);
  79          if ($CFG->debugdeveloper && !empty($output)) {
  80              // Only throw an error if the site is in debugdeveloper.
  81              throw new coding_exception($message);
  82          }
  83          error_log('Potential coding error: ' . $message);
  84      }
  85      return $output;
  86  }