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.
   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\acceptances} class.
  19   *
  20   * @package     tool_policy
  21   * @category    output
  22   * @copyright   2018 Marina Glancy
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_policy\output;
  27  
  28  use tool_policy\api;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  use moodle_url;
  33  use renderable;
  34  use renderer_base;
  35  use single_button;
  36  use templatable;
  37  use tool_policy\policy_version;
  38  
  39  /**
  40   * List of users and their acceptances
  41   *
  42   * @copyright 2018 Marina Glancy
  43   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class acceptances implements renderable, templatable {
  46  
  47      /** @var id */
  48      protected $userid;
  49  
  50      /** @var moodle_url */
  51      protected $returnurl;
  52  
  53      /**
  54       * Contructor.
  55       *
  56       * @param int $userid
  57       * @param string|moodle_url $returnurl
  58       */
  59      public function __construct($userid, $returnurl = null) {
  60          $this->userid = $userid;
  61          $this->returnurl = $returnurl ? (new moodle_url($returnurl))->out(false) : null;
  62      }
  63  
  64      /**
  65       * Export the page data for the mustache template.
  66       *
  67       * @param renderer_base $output renderer to be used to render the page elements.
  68       * @return stdClass
  69       */
  70      public function export_for_template(renderer_base $output) {
  71          $data = (object)[];
  72          $data->hasonbehalfagreements = false;
  73          $data->pluginbaseurl = (new moodle_url('/admin/tool/policy'))->out(false);
  74          $data->returnurl = $this->returnurl;
  75  
  76          // Get the list of policies and versions that current user is able to see
  77          // and the respective acceptance records for the selected user.
  78          $policies = api::get_policies_with_acceptances($this->userid);
  79          $versionids = [];
  80  
  81          $canviewfullnames = has_capability('moodle/site:viewfullnames', \context_system::instance());
  82          foreach ($policies as $policy) {
  83              foreach ($policy->versions as $version) {
  84                  $versionids[$version->id] = $version->id;
  85                  unset($version->summary);
  86                  unset($version->content);
  87                  $version->iscurrent = ($version->status == policy_version::STATUS_ACTIVE);
  88                  $version->isoptional = ($version->optional == policy_version::AGREEMENT_OPTIONAL);
  89                  $version->name = $version->name;
  90                  $version->revision = $version->revision;
  91                  $returnurl = new moodle_url('/admin/tool/policy/user.php', ['userid' => $this->userid]);
  92                  $version->viewurl = (new moodle_url('/admin/tool/policy/view.php', [
  93                      'policyid' => $policy->id,
  94                      'versionid' => $version->id,
  95                      'returnurl' => $returnurl->out(false),
  96                  ]))->out(false);
  97  
  98                  if ($version->acceptance !== null) {
  99                      $acceptance = $version->acceptance;
 100                      $version->timeaccepted = userdate($acceptance->timemodified, get_string('strftimedatetime'));
 101                      $onbehalf = $acceptance->usermodified && $acceptance->usermodified != $this->userid;
 102                      if ($version->acceptance->status == 1) {
 103                          $version->agreement = new user_agreement($this->userid, [$version->id], [], $returnurl,
 104                              [$version->id => $version->name], $onbehalf);
 105                      } else {
 106                          $version->agreement = new user_agreement($this->userid, [], [$version->id], $returnurl,
 107                              [$version->id => $version->name], $onbehalf);
 108                      }
 109                      if ($onbehalf) {
 110                          $usermodified = (object)['id' => $acceptance->usermodified];
 111                          username_load_fields_from_object($usermodified, $acceptance, 'mod');
 112                          $profileurl = new \moodle_url('/user/profile.php', array('id' => $usermodified->id));
 113                          $version->acceptedby = \html_writer::link($profileurl, fullname($usermodified, $canviewfullnames ||
 114                              has_capability('moodle/site:viewfullnames', \context_user::instance($acceptance->usermodified))));
 115                          $data->hasonbehalfagreements = true;
 116                      }
 117                      $version->note = format_text($acceptance->note);
 118                  } else if ($version->iscurrent) {
 119                      $version->agreement = new user_agreement($this->userid, [], [], $returnurl, [$version->id => $version->name]);
 120                  }
 121                  if (isset($version->agreement)) {
 122                      $version->agreement = $version->agreement->export_for_template($output);
 123                  }
 124              }
 125  
 126              if ($policy->versions[0]->status != policy_version::STATUS_ACTIVE) {
 127                  // Add an empty "currentversion" on top.
 128                  $policy->versions = [0 => (object)[]] + $policy->versions;
 129              }
 130  
 131              $policy->versioncount = count($policy->versions);
 132              $policy->versions = array_values($policy->versions);
 133              $policy->versions[0]->isfirst = 1;
 134              $policy->versions[0]->hasarchived = (count($policy->versions) > 1);
 135          }
 136  
 137          $data->policies = array_values($policies);
 138          $data->canrevoke = \tool_policy\api::can_revoke_policies(array_keys($versionids), $this->userid);
 139  
 140          return $data;
 141      }
 142  }