Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   * Default (core) handler for site policies.
  19   *
  20   * @package    core_privacy
  21   * @copyright  2018 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_privacy\local\sitepolicy;
  26  
  27  use moodle_url;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Default (core) handler for site policies.
  33   *
  34   * @package    core_privacy
  35   * @copyright  2018 Marina Glancy
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class default_handler extends handler {
  39  
  40      /**
  41       * Checks if the site has site policy defined
  42       *
  43       * @param bool $forguests
  44       * @return bool
  45       */
  46      public static function is_defined($forguests = false) {
  47          global $CFG;
  48          if (!empty($CFG->sitepolicyhandler)) {
  49              // This handler can also be used as a fallback in case of invalid $CFG->sitepolicyhandler,
  50              // in this case assume that no site policy is set.
  51              return false;
  52          }
  53          if (!$forguests) {
  54              return !empty($CFG->sitepolicy);
  55          } else {
  56              return !empty($CFG->sitepolicyguest);
  57          }
  58      }
  59  
  60      /**
  61       * Returns URL to redirect user to when user needs to agree to site policy
  62       *
  63       * This is a regular interactive page for web users. It should have normal Moodle header/footers, it should
  64       * allow user to view policies and accept them.
  65       *
  66       * @param bool $forguests
  67       * @return moodle_url|null (returns null if site policy is not defined)
  68       */
  69      public static function get_redirect_url($forguests = false) {
  70          return static::is_defined($forguests) ? new moodle_url('/user/policy.php') : null;
  71      }
  72  
  73      /**
  74       * Returns URL of the site policy that needs to be displayed to the user (inside iframe or to use in WS such as mobile app)
  75       *
  76       * This page should not have any header/footer, it does not also have any buttons/checkboxes. The caller needs to implement
  77       * the "Accept" button and call {@link self::accept()} on completion.
  78       *
  79       * @param bool $forguests
  80       * @return moodle_url|null
  81       */
  82      public static function get_embed_url($forguests = false) {
  83          global $CFG;
  84          if (!empty($CFG->sitepolicyhandler)) {
  85              // This handler can also be used as a fallback in case of invalid $CFG->sitepolicyhandler,
  86              // in this case assume that no site policy is set.
  87              return null;
  88          }
  89          if ($forguests && !empty($CFG->sitepolicyguest)) {
  90              return new moodle_url($CFG->sitepolicyguest);
  91          } else if (!$forguests && !empty($CFG->sitepolicy)) {
  92              return new moodle_url($CFG->sitepolicy);
  93          }
  94          return null;
  95      }
  96  }