Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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 $OUTPUT;
 130  
 131          $data = (object) [
 132              'pluginbaseurl' => (new moodle_url('/admin/tool/policy'))->out(false),
 133              'haspermissionagreedocs' => $this->haspermissionagreedocs,
 134              'supportemail' => $OUTPUT->supportemail(['class' => 'font-weight-bold'])
 135          ];
 136  
 137          // Get the messages to display.
 138          $messagetitle = null;
 139          $messagedesc = null;
 140          if (!$this->haspermissionagreedocs) {
 141              if (!empty($this->behalfuser)) {
 142                  // If viewing docs in behalf of other user, get his/her full name and profile link.
 143                  $userfullname = fullname($this->behalfuser, has_capability('moodle/site:viewfullnames', \context_system::instance())
 144                      || has_capability('moodle/site:viewfullnames', \context_user::instance($this->behalfid)));
 145                  $data->behalfuser = html_writer::link(\context_user::instance($this->behalfid)->get_url(), $userfullname);
 146  
 147                  $messagetitle = get_string('nopermissiontoagreedocsbehalf', 'tool_policy');
 148                  $messagedesc = get_string('nopermissiontoagreedocsbehalf_desc', 'tool_policy', $data->behalfuser);
 149              } else {
 150                  $messagetitle = get_string('nopermissiontoagreedocs', 'tool_policy');
 151                  $messagedesc = get_string('nopermissiontoagreedocs_desc', 'tool_policy');
 152              }
 153          }
 154          $data->messagetitle = $messagetitle;
 155          $data->messagedesc = $messagedesc;
 156  
 157          // Add policies list.
 158          $policieslinks = array();
 159          foreach ($this->policies as $policyversion) {
 160              // Get a link to display the full policy document.
 161              $policyurl = new moodle_url('/admin/tool/policy/view.php',
 162                  array('policyid' => $policyversion->policyid, 'returnurl' => qualified_me()));
 163              $policyattributes = array('data-action' => 'view',
 164                                        'data-versionid' => $policyversion->id,
 165                                        'data-behalfid' => $this->behalfid);
 166              $policieslinks[] = html_writer::link($policyurl, $policyversion->name, $policyattributes);
 167          }
 168          $data->policies = $policieslinks;
 169  
 170          return $data;
 171      }
 172  }