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.
   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 handler class.
  19   *
  20   * @package    tool_policy
  21   * @copyright  2018 Sara Arjona <sara@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_policy\privacy\local\sitepolicy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use tool_policy\api;
  30  use tool_policy\policy_version;
  31  
  32  /**
  33   * Class implementation for a site policy handler.
  34   *
  35   * @package    tool_policy
  36   * @copyright  2018 Sara Arjona <sara@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class handler extends \core_privacy\local\sitepolicy\handler {
  40  
  41      /**
  42       * Returns URL to redirect user to when user needs to agree to site policy
  43       *
  44       * This is a regular interactive page for web users. It should have normal Moodle header/footers, it should
  45       * allow user to view policies and accept them.
  46       *
  47       * @param bool $forguests
  48       * @return moodle_url|null (returns null if site policy is not defined)
  49       */
  50      public static function get_redirect_url($forguests = false) {
  51          // There is no redirect for guests, policies are shown in the popup, only return redirect url for the logged in users.
  52          if (!$forguests && api::get_current_versions_ids(policy_version::AUDIENCE_LOGGEDIN)) {
  53              return new \moodle_url('/admin/tool/policy/index.php');
  54          }
  55          return null;
  56      }
  57  
  58      /**
  59       * 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)
  60       *
  61       * This page should not have any header/footer, it does not also have any buttons/checkboxes. The caller needs to implement
  62       * the "Accept" button and call {@link self::accept()} on completion.
  63       *
  64       * @param bool $forguests
  65       * @return moodle_url|null
  66       */
  67      public static function get_embed_url($forguests = false) {
  68          if (api::get_current_versions_ids($forguests ? policy_version::AUDIENCE_GUESTS : policy_version::AUDIENCE_LOGGEDIN)) {
  69              return new \moodle_url('/admin/tool/policy/viewall.php');
  70          }
  71          return null;
  72      }
  73  
  74      /**
  75       * Accept site policy for the current user
  76       *
  77       * @return bool - false if sitepolicy not defined, user is not logged in or user has already agreed to site policy;
  78       *     true - if we have successfully marked the user as agreed to the site policy
  79       */
  80      public static function accept() {
  81          global $USER, $DB;
  82  
  83          if (!isloggedin()) {
  84              return false;
  85          }
  86  
  87          if ($USER->policyagreed) {
  88              return false;
  89          }
  90  
  91          if (isguestuser()) {
  92              // For guests, agreement is stored in the session only.
  93              $USER->policyagreed = 1;
  94              return true;
  95          }
  96  
  97          // Find all compulsory policies and mark them as accepted.
  98          $compulsory = [];
  99          foreach (api::list_current_versions(policy_version::AUDIENCE_LOGGEDIN) as $policyversion) {
 100              if ($policyversion->optional == policy_version::AGREEMENT_COMPULSORY) {
 101                  $compulsory[] = $policyversion->id;
 102              }
 103          }
 104  
 105          if ($compulsory) {
 106              api::accept_policies($compulsory);
 107          }
 108  
 109          // Mark policies as agreed.
 110          $DB->set_field('user', 'policyagreed', 1, array('id' => $USER->id));
 111          $USER->policyagreed = 1;
 112  
 113          return true;
 114      }
 115  
 116      /**
 117       * Adds "Agree to site policy" checkbox to the signup form.
 118       *
 119       * @param \MoodleQuickForm $mform
 120       */
 121      public static function signup_form($mform) {
 122          if (static::is_defined()) {
 123              // This plugin displays policies to the user who is signing up before the signup form is shown.
 124              // By the time user has access to signup form they have already agreed to all compulsory policies.
 125              $mform->addElement('hidden', 'policyagreed', 1);
 126              $mform->setType('policyagreed', PARAM_INT);
 127          }
 128      }
 129  }