PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ajax/ajaxlib.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 82 lines | 23 code | 8 blank | 51 comment | 3 complexity | 44f993486f9dac73413c2b1eade52bf8 MD5 | raw file
  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. * Library functions to facilitate the use of ajax JavaScript in Moodle.
  18. *
  19. * @package core
  20. * @copyright 2009 Tim Hunt
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. /**
  24. * You need to call this function if you wish to use the set_user_preference method in javascript_static.php, to white-list the
  25. * preference you want to update from JavaScript, and to specify the type of cleaning you expect to be done on values.
  26. *
  27. * @package core
  28. * @category preference
  29. * @param string $name the name of the user_perference we should allow to be updated by remote calls.
  30. * @param integer $paramtype one of the PARAM_{TYPE} constants, user to clean submitted values before set_user_preference is called.
  31. * @return null
  32. */
  33. function user_preference_allow_ajax_update($name, $paramtype) {
  34. global $USER, $PAGE;
  35. // Record in the session that this user_preference is allowed to updated remotely.
  36. $USER->ajax_updatable_user_prefs[$name] = $paramtype;
  37. }
  38. /**
  39. * Starts capturing output whilst processing an AJAX request.
  40. *
  41. * This should be used in combination with ajax_check_captured_output to
  42. * report any captured output to the user.
  43. *
  44. * @return Boolean Returns true on success or false on failure.
  45. */
  46. function ajax_capture_output() {
  47. // Start capturing output in case of broken plugins.
  48. return ob_start();
  49. }
  50. /**
  51. * Check captured output for content. If the site has a debug level of
  52. * debugdeveloper set, and the content is non-empty, then throw a coding
  53. * exception which can be captured by the Y.IO request and displayed to the
  54. * user.
  55. *
  56. * @return Any output that was captured.
  57. */
  58. function ajax_check_captured_output() {
  59. global $CFG;
  60. // Retrieve the output - there should be none.
  61. $output = ob_get_contents();
  62. ob_end_clean();
  63. if (!empty($output)) {
  64. $message = 'Unexpected output whilst processing AJAX request. ' .
  65. 'This could be caused by trailing whitespace. Output received: ' .
  66. var_export($output, true);
  67. if ($CFG->debugdeveloper && !empty($output)) {
  68. // Only throw an error if the site is in debugdeveloper.
  69. throw new coding_exception($message);
  70. }
  71. error_log('Potential coding error: ' . $message);
  72. }
  73. return $output;
  74. }