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] [Versions 401 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 moodle_exception;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  use context_system;
  33  use moodle_url;
  34  use renderable;
  35  use renderer_base;
  36  use single_button;
  37  use templatable;
  38  use tool_policy\api;
  39  use tool_policy\policy_version;
  40  
  41  /**
  42   * Represents a page for showing the given policy document version.
  43   *
  44   * @copyright 2018 Sara Arjona <sara@moodle.com>
  45   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class page_viewdoc implements renderable, templatable {
  48  
  49      /** @var stdClass Exported {@link \tool_policy\policy_version_exporter} to display on this page. */
  50      protected $policy;
  51  
  52      /** @var string Return URL. */
  53      protected $returnurl = null;
  54  
  55      /** @var int User id who wants to view this page. */
  56      protected $behalfid = null;
  57  
  58      /** @var bool View the policy as a part of the management UI. */
  59      protected $manage;
  60  
  61      /** @var int Position of the current policy with respect to the total of policy docs to display. */
  62      protected $numpolicy = 0;
  63  
  64      /** @var int Total number of policy documents which the user has to agree to. */
  65      protected $totalpolicies = 0;
  66  
  67      /**
  68       * Prepare the page for rendering.
  69       *
  70       * @param int $policyid The policy id for this page.
  71       * @param int $versionid The version id to show. Empty tries to load the current one.
  72       * @param string $returnurl URL of a page to continue after reading the policy text.
  73       * @param int $behalfid The userid to view this policy version as (such as child's id).
  74       * @param bool $manage View the policy as a part of the management UI.
  75       * @param int $numpolicy Position of the current policy with respect to the total of policy docs to display.
  76       * @param int $totalpolicies Total number of policy documents which the user has to agree to.
  77       */
  78      public function __construct($policyid, $versionid, $returnurl, $behalfid, $manage, $numpolicy = 0, $totalpolicies = 0) {
  79  
  80          $this->returnurl = $returnurl;
  81          $this->behalfid = $behalfid;
  82          $this->manage = $manage;
  83          $this->numpolicy = $numpolicy;
  84          $this->totalpolicies = $totalpolicies;
  85  
  86          $this->prepare_policy($policyid, $versionid);
  87          $this->prepare_global_page_access();
  88      }
  89  
  90      /**
  91       * Loads the policy version to display on the page.
  92       *
  93       * @param int $policyid The policy id for this page.
  94       * @param int $versionid The version id to show. Empty tries to load the current one.
  95       */
  96      protected function prepare_policy($policyid, $versionid) {
  97  
  98          if ($versionid) {
  99              $this->policy = api::get_policy_version($versionid);
 100  
 101          } else {
 102              $this->policy = array_reduce(api::list_current_versions(), function ($carry, $current) use ($policyid) {
 103                  if ($current->policyid == $policyid) {
 104                      return $current;
 105                  }
 106                  return $carry;
 107              });
 108          }
 109  
 110          if (empty($this->policy)) {
 111              throw new \moodle_exception('errorpolicyversionnotfound', 'tool_policy');
 112          }
 113      }
 114  
 115      /**
 116       * Sets up the global $PAGE and performs the access checks.
 117       */
 118      protected function prepare_global_page_access() {
 119          global $CFG, $PAGE, $SITE, $USER;
 120  
 121          $myurl = new moodle_url('/admin/tool/policy/view.php', [
 122              'policyid' => $this->policy->policyid,
 123              'versionid' => $this->policy->id,
 124              'returnurl' => $this->returnurl,
 125              'behalfid' => $this->behalfid,
 126              'manage' => $this->manage,
 127              'numpolicy' => $this->numpolicy,
 128              'totalpolicies' => $this->totalpolicies,
 129          ]);
 130  
 131          if ($this->manage) {
 132              require_once($CFG->libdir.'/adminlib.php');
 133              admin_externalpage_setup('tool_policy_managedocs', '', null, $myurl);
 134              require_capability('tool/policy:managedocs', context_system::instance());
 135              $PAGE->navbar->add(format_string($this->policy->name),
 136                  new moodle_url('/admin/tool/policy/managedocs.php', ['id' => $this->policy->policyid]));
 137          } else {
 138              if ($this->policy->status != policy_version::STATUS_ACTIVE) {
 139                  require_login();
 140              } else if (isguestuser() || empty($USER->id) || !$USER->policyagreed) {
 141                  // Disable notifications for new users, guests or users who haven't agreed to the policies.
 142                  $PAGE->set_popup_notification_allowed(false);
 143              }
 144              $PAGE->set_url($myurl);
 145              $PAGE->set_heading($SITE->fullname);
 146              $PAGE->set_title(get_string('policiesagreements', 'tool_policy'));
 147              $PAGE->navbar->add(get_string('policiesagreements', 'tool_policy'), new moodle_url('/admin/tool/policy/index.php'));
 148              $PAGE->navbar->add(format_string($this->policy->name));
 149          }
 150  
 151          if (!api::can_user_view_policy_version($this->policy, $this->behalfid)) {
 152              throw new moodle_exception('accessdenied', 'tool_policy');
 153          }
 154      }
 155  
 156      /**
 157       * Export the page data for the mustache template.
 158       *
 159       * @param renderer_base $output renderer to be used to render the page elements.
 160       * @return stdClass
 161       */
 162      public function export_for_template(renderer_base $output) {
 163          global $USER;
 164  
 165          $data = (object) [
 166              'pluginbaseurl' => (new moodle_url('/admin/tool/policy'))->out(false),
 167              'returnurl' => $this->returnurl ? (new moodle_url($this->returnurl))->out(false) : null,
 168              'numpolicy' => $this->numpolicy ? : null,
 169              'totalpolicies' => $this->totalpolicies ? : null,
 170          ];
 171          if ($this->manage && $this->policy->status != policy_version::STATUS_ARCHIVED) {
 172              $paramsurl = ['policyid' => $this->policy->policyid, 'versionid' => $this->policy->id];
 173              $data->editurl = (new moodle_url('/admin/tool/policy/editpolicydoc.php', $paramsurl))->out(false);
 174          }
 175  
 176          if ($this->policy->agreementstyle == policy_version::AGREEMENTSTYLE_OWNPAGE) {
 177              if (!api::is_user_version_accepted($USER->id, $this->policy->id)) {
 178                  unset($data->returnurl);
 179                  $data->accepturl = (new moodle_url('/admin/tool/policy/index.php', [
 180                      'listdoc[]' => $this->policy->id,
 181                      'status'.$this->policy->id => 1,
 182                      'submit' => 'accept',
 183                      'sesskey' => sesskey(),
 184                  ]))->out(false);
 185                  if ($this->policy->optional == policy_version::AGREEMENT_OPTIONAL) {
 186                      $data->declineurl = (new moodle_url('/admin/tool/policy/index.php', [
 187                          'listdoc[]' => $this->policy->id,
 188                          'status'.$this->policy->id => 0,
 189                          'submit' => 'decline',
 190                          'sesskey' => sesskey(),
 191                      ]))->out(false);
 192                  }
 193              }
 194          }
 195  
 196          $data->policy = clone($this->policy);
 197  
 198          return $data;
 199      }
 200  }