Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Provides {@link tool_policy\output\renderer} class.
  19   *
  20   * @package     tool_policy
  21   * @category    output
  22   * @copyright   2018 Sara Arjona <sara@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_policy\output;
  27  
  28  use core\session\manager;
  29  use moodle_exception;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  use context_system;
  34  use core_user;
  35  use html_writer;
  36  use moodle_url;
  37  use renderable;
  38  use renderer_base;
  39  use templatable;
  40  use tool_policy\api;
  41  use tool_policy\policy_version;
  42  
  43  /**
  44   * Represents a page for showing the error messages.
  45   *
  46   * This is used when a user has no permission to agree to policies or accept policies on behalf of defined behalfid.
  47   *
  48   * @copyright 2018 Sara Arjona <sara@moodle.com>
  49   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  50   */
  51  class page_nopermission implements renderable, templatable {
  52  
  53      /** @var int User id who wants to view this page. */
  54      protected $behalfid = null;
  55  
  56      /** @var object User who wants to accept this page. */
  57      protected $behalfuser = null;
  58  
  59      /** @var bool True if user has permission to accept policy documents; false otherwise. */
  60      protected $haspermissionagreedocs = true;
  61  
  62      /** @var array $policies List of public policies objects. */
  63      protected $policies = null;
  64  
  65      /**
  66       * Prepare the page for rendering.
  67       *
  68       * @param array $versionids int[] List of policy version ids that were checked.
  69       * @param int $behalfid The userid to consent policies as (such as child's id).
  70       */
  71      public function __construct(array $versionids, $behalfid) {
  72          global $USER;
  73  
  74          $behalfid = $behalfid ?: $USER->id;
  75          $realuser = manager::get_realuser();
  76          if ($realuser->id != $behalfid) {
  77              $this->behalfuser = core_user::get_user($behalfid, '*', MUST_EXIST);
  78              $this->behalfid = $this->behalfuser->id;
  79          }
  80  
  81          if (!empty($USER->id)) {
  82              // For existing users, it's needed to check if they have the capability for accepting policies.
  83              $this->haspermissionagreedocs = api::can_accept_policies($versionids, $this->behalfid);
  84          }
  85  
  86          $this->policies = api::list_current_versions(policy_version::AUDIENCE_LOGGEDIN);
  87  
  88          if (empty($this->policies) && !empty($USER->id)) {
  89              // Existing user without policies to agree to.
  90              $currentuser = (!empty($this->behalfuser)) ? $this->behalfuser : $USER;
  91              if (!$currentuser->policyagreed) {
  92                  // If there are no policies to agreed, change $user->policyagreed to true.
  93                  api::update_policyagreed($currentuser);
  94              }
  95          }
  96  
  97          $this->prepare_global_page_access();
  98      }
  99  
 100      /**
 101       * Sets up the global $PAGE and performs the access checks.
 102       */
 103      protected function prepare_global_page_access() {
 104          global $PAGE, $SITE, $USER;
 105  
 106          $myurl = new moodle_url('/admin/tool/policy/index.php', [
 107              'behalfid' => $this->behalfid,
 108          ]);
 109  
 110          if (isguestuser() || empty($USER->id) || !$USER->policyagreed) {
 111              // Disable notifications for new users, guests or users who haven't agreed to the policies.
 112              $PAGE->set_popup_notification_allowed(false);
 113          }
 114          $PAGE->set_context(context_system::instance());
 115          $PAGE->set_pagelayout('standard');
 116          $PAGE->set_url($myurl);
 117          $PAGE->set_heading($SITE->fullname);
 118          $PAGE->set_title(get_string('policiesagreements', 'tool_policy'));
 119          $PAGE->navbar->add(get_string('policiesagreements', 'tool_policy'), new moodle_url('/admin/tool/policy/index.php'));
 120      }
 121  
 122      /**
 123       * Export the page data for the mustache template.
 124       *
 125       * @param renderer_base $output renderer to be used to render the page elements.
 126       * @return \stdClass
 127       */
 128      public function export_for_template(renderer_base $output) {
 129          global $CFG;
 130  
 131          $data = (object) [
 132              'pluginbaseurl' => (new moodle_url('/admin/tool/policy'))->out(false),
 133              'haspermissionagreedocs' => $this->haspermissionagreedocs,
 134              'supportname' => $CFG->supportname,
 135              'supportemail' => $CFG->supportemail,
 136          ];
 137  
 138          // Get the messages to display.
 139          $messagetitle = null;
 140          $messagedesc = null;
 141          if (!$this->haspermissionagreedocs) {
 142              if (!empty($this->behalfuser)) {
 143                  // If viewing docs in behalf of other user, get his/her full name and profile link.
 144                  $userfullname = fullname($this->behalfuser, has_capability('moodle/site:viewfullnames', \context_system::instance())
 145                      || has_capability('moodle/site:viewfullnames', \context_user::instance($this->behalfid)));
 146                  $data->behalfuser = html_writer::link(\context_user::instance($this->behalfid)->get_url(), $userfullname);
 147  
 148                  $messagetitle = get_string('nopermissiontoagreedocsbehalf', 'tool_policy');
 149                  $messagedesc = get_string('nopermissiontoagreedocsbehalf_desc', 'tool_policy', $data->behalfuser);
 150              } else {
 151                  $messagetitle = get_string('nopermissiontoagreedocs', 'tool_policy');
 152                  $messagedesc = get_string('nopermissiontoagreedocs_desc', 'tool_policy');
 153              }
 154          }
 155          $data->messagetitle = $messagetitle;
 156          $data->messagedesc = $messagedesc;
 157  
 158          // Add policies list.
 159          $policieslinks = array();
 160          foreach ($this->policies as $policyversion) {
 161              // Get a link to display the full policy document.
 162              $policyurl = new moodle_url('/admin/tool/policy/view.php',
 163                  array('policyid' => $policyversion->policyid, 'returnurl' => qualified_me()));
 164              $policyattributes = array('data-action' => 'view',
 165                                        'data-versionid' => $policyversion->id,
 166                                        'data-behalfid' => $this->behalfid);
 167              $policieslinks[] = html_writer::link($policyurl, $policyversion->name, $policyattributes);
 168          }
 169          $data->policies = $policieslinks;
 170  
 171          return $data;
 172      }
 173  }