Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Site policy management class.
  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   * Site policy management class.
  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 manager {
  39  
  40      /**
  41       * Returns the list of plugins that can work as sitepolicy handlers (have class PLUGINNAME\privacy\sitepolicy\handler)
  42       * @return array
  43       */
  44      public function get_all_handlers() {
  45          $sitepolicyhandlers = [];
  46          foreach (\core_component::get_plugin_types() as $ptype => $unused) {
  47              $plugins = \core_component::get_plugin_list_with_class($ptype, 'privacy\local\sitepolicy\handler') +
  48                  \core_component::get_plugin_list_with_class($ptype, 'privacy_local_sitepolicy_handler');
  49              // Allow plugins to have the class either with namespace or without (useful for unittest).
  50              foreach ($plugins as $pname => $class) {
  51                  $sitepolicyhandlers[$pname] = $class;
  52              }
  53          }
  54          return $sitepolicyhandlers;
  55      }
  56  
  57      /**
  58       * Returns the current site policy handler
  59       *
  60       * @return handler
  61       */
  62      public function get_handler_classname() {
  63          global $CFG;
  64  
  65          if (!empty($CFG->sitepolicyhandler)) {
  66              $sitepolicyhandlers = $this->get_all_handlers();
  67  
  68              if (!isset($sitepolicyhandlers[$CFG->sitepolicyhandler])) {
  69                  return default_handler::class;
  70  
  71              } else {
  72                  return $sitepolicyhandlers[$CFG->sitepolicyhandler];
  73              }
  74  
  75          } else {
  76              return default_handler::class;
  77          }
  78      }
  79  
  80      /**
  81       * Checks if the site has site policy defined
  82       *
  83       * @param bool $forguests
  84       * @return bool
  85       */
  86      public function is_defined($forguests = false) {
  87          return component_class_callback($this->get_handler_classname(), 'is_defined', [$forguests]);
  88      }
  89  
  90      /**
  91       * Returns URL to redirect user to when user needs to agree to site policy
  92       *
  93       * This is a regular interactive page for web users. It should have normal Moodle header/footers, it should
  94       * allow user to view policies and accept them.
  95       *
  96       * @param bool $forguests
  97       * @return moodle_url|null (returns null if site policy is not defined)
  98       */
  99      public function get_redirect_url($forguests = false) {
 100          $url = component_class_callback($this->get_handler_classname(), 'get_redirect_url', [$forguests]);
 101          if ($url && !($url instanceof moodle_url)) {
 102              $url = new moodle_url($url);
 103          }
 104          return $url;
 105      }
 106  
 107      /**
 108       * 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)
 109       *
 110       * This page should not have any header/footer, it does not also have any buttons/checkboxes. The caller needs to implement
 111       * the "Accept" button and call {@link self::accept()} on completion.
 112       *
 113       * @param bool $forguests
 114       * @return moodle_url|null
 115       */
 116      public function get_embed_url($forguests = false) {
 117          $url = component_class_callback($this->get_handler_classname(), 'get_embed_url', [$forguests]);
 118          if ($url && !($url instanceof moodle_url)) {
 119              $url = new moodle_url($url);
 120          }
 121          return $url;
 122      }
 123  
 124      /**
 125       * Accept site policy for the current user
 126       *
 127       * @return bool - false if sitepolicy not defined, user is not logged in or user has already agreed to site policy;
 128       *     true - if we have successfully marked the user as agreed to the site policy
 129       */
 130      public function accept() {
 131          return component_class_callback($this->get_handler_classname(), 'accept', []);
 132      }
 133  
 134      /**
 135       * Adds "Agree to site policy" checkbox to the signup form.
 136       *
 137       * Sitepolicy handlers can override the simple checkbox with their own controls.
 138       *
 139       * @param \MoodleQuickForm $mform
 140       */
 141      public function signup_form($mform) {
 142          component_class_callback($this->get_handler_classname(), 'signup_form', [$mform]);
 143      }
 144  }