Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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