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   * Base class for site policy handlers.
  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  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Base class for site policy handlers.
  31   *
  32   * If a plugin wants to act as a site policy handler it has to define class
  33   * PLUGINNAME\privacy\sitepolicy\handler that extends \core_privacy\sitepolicy\handler
  34   *
  35   * @package    core_privacy
  36   * @copyright  2018 Marina Glancy
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  abstract class handler {
  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          $url = static::get_redirect_url($forguests);
  48          return !empty($url);
  49      }
  50  
  51      /**
  52       * Returns URL to redirect user to when user needs to agree to site policy
  53       *
  54       * This is a regular interactive page for web users. It should have normal Moodle header/footers, it should
  55       * allow user to view policies and accept them.
  56       *
  57       * @param bool $forguests
  58       * @return moodle_url|null (returns null if site policy is not defined)
  59       */
  60      abstract public static function get_redirect_url($forguests = false);
  61  
  62      /**
  63       * 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)
  64       *
  65       * This page should not have any header/footer, it does not also have any buttons/checkboxes. The caller needs to implement
  66       * the "Accept" button and call {@link self::accept()} on completion.
  67       *
  68       * @param bool $forguests
  69       * @return moodle_url|null
  70       */
  71      abstract public static function get_embed_url($forguests = false);
  72  
  73      /**
  74       * Accept site policy for the current user
  75       *
  76       * @return bool - false if sitepolicy not defined, user is not logged in or user has already agreed to site policy;
  77       *     true - if we have successfully marked the user as agreed to the site policy
  78       */
  79      public static function accept() {
  80          global $USER, $DB;
  81          if (!isloggedin()) {
  82              return false;
  83          }
  84          if ($USER->policyagreed || !static::is_defined(isguestuser())) {
  85              return false;
  86          }
  87  
  88          if (!isguestuser()) {
  89              // For the guests agreement in stored in session only, for other users - in DB.
  90              $DB->set_field('user', 'policyagreed', 1, array('id' => $USER->id));
  91          }
  92          $USER->policyagreed = 1;
  93          return true;
  94      }
  95  
  96      /**
  97       * Adds "Agree to site policy" checkbox to the signup form.
  98       *
  99       * Sitepolicy handlers can override the simple checkbox with their own controls.
 100       *
 101       * @param \MoodleQuickForm $mform
 102       */
 103      public static function signup_form($mform) {
 104          if ($url = static::get_embed_url()) {
 105              $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
 106              $mform->setExpanded('policyagreement');
 107              $mform->addElement('static', 'policylink', '', '<a href="' . $url .
 108                  '" onclick="this.target=\'_blank\'">' . get_string('policyagreementclick') . '</a>');
 109              $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
 110              $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'client');
 111          }
 112      }
 113  }