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\user_agreement} 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  defined('MOODLE_INTERNAL') || die();
  29  
  30  use moodle_url;
  31  use renderable;
  32  use renderer_base;
  33  use single_button;
  34  use templatable;
  35  
  36  /**
  37   * List of users and their acceptances
  38   *
  39   * @copyright 2018 Marina Glancy
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class user_agreement implements \templatable, \renderable {
  43  
  44      /** @var int */
  45      protected $userid;
  46  
  47      /** @var bool */
  48      protected $onbehalf;
  49  
  50      /** @var moodle_url */
  51      protected $pageurl;
  52  
  53      /** @var array */
  54      protected $versions;
  55  
  56      /** @var array */
  57      protected $accepted;
  58  
  59      /** @var array */
  60      protected $declined;
  61  
  62      /** @var bool */
  63      protected $canaccept;
  64  
  65      /** @var bool */
  66      protected $canrevoke;
  67  
  68      /**
  69       * user_agreement constructor
  70       *
  71       * @param int $userid
  72       * @param array $accepted list of ids of accepted versions
  73       * @param array $declined list of ids of declined versions
  74       * @param moodle_url $pageurl
  75       * @param array $versions list of versions (id=>name)
  76       * @param bool $onbehalf whether at least one version was accepted by somebody else on behalf of the user
  77       * @param bool $canaccept does the current user have permission to accept/decline the policy on behalf of user $userid
  78       * @param bool $canrevoke does the current user have permission to revoke the policy on behalf of user $userid
  79       */
  80      public function __construct($userid, array $accepted, array $declined, moodle_url $pageurl, $versions, $onbehalf = false,
  81                                  $canaccept = null, $canrevoke = null) {
  82  
  83          // Make sure that all ids in $accepted and $declined are present in $versions.
  84          if (array_diff(array_merge($accepted, $declined), array_keys($versions))) {
  85              throw new \coding_exception('Policy version ids mismatch');
  86          }
  87  
  88          $this->userid = $userid;
  89          $this->onbehalf = $onbehalf;
  90          $this->pageurl = $pageurl;
  91          $this->versions = $versions;
  92          $this->accepted = $accepted;
  93          $this->declined = $declined;
  94          $this->canaccept = $canaccept;
  95  
  96          if (count($this->accepted) < count($this->versions) && $canaccept === null) {
  97              $this->canaccept = \tool_policy\api::can_accept_policies(array_keys($this->versions), $this->userid);
  98          }
  99  
 100          if (count($this->accepted) > 0 && $canrevoke === null) {
 101              $this->canrevoke = \tool_policy\api::can_revoke_policies(array_keys($this->versions), $this->userid);
 102          }
 103      }
 104  
 105      /**
 106       * Export data to be rendered.
 107       *
 108       * @param renderer_base $output
 109       * @return stdClass
 110       */
 111      public function export_for_template(\renderer_base $output) {
 112  
 113          $data = (object)[
 114              'statusicon' => '',
 115              'statustext' => '',
 116              'statuslink' => '',
 117              'actions' => [],
 118          ];
 119  
 120          if (count($this->versions) == 1) {
 121              // We represent one particular policy's agreement status.
 122              $versionname = reset($this->versions);
 123              $versionid = key($this->versions);
 124  
 125              $actionaccept = (object)[
 126                  'text' => get_string('useracceptanceactionaccept', 'tool_policy'),
 127                  'title' => get_string('useracceptanceactionacceptone', 'tool_policy', $versionname),
 128                  'data' => 'acceptmodal',
 129                  'url' => (new \moodle_url('/admin/tool/policy/accept.php', [
 130                      'userids[]' => $this->userid,
 131                      'versionids[]' => $versionid,
 132                      'action' => 'accept',
 133                      'returnurl' => $this->pageurl->out_as_local_url(false),
 134                  ]))->out(false),
 135              ];
 136  
 137              $actionrevoke = (object)[
 138                  'text' => get_string('useracceptanceactionrevoke', 'tool_policy'),
 139                  'title' => get_string('useracceptanceactionrevokeone', 'tool_policy', $versionname),
 140                  'data' => 'acceptmodal',
 141                  'url' => (new \moodle_url('/admin/tool/policy/accept.php', [
 142                      'userids[]' => $this->userid,
 143                      'versionids[]' => $versionid,
 144                      'action' => 'revoke',
 145                      'returnurl' => $this->pageurl->out_as_local_url(false),
 146                  ]))->out(false),
 147              ];
 148  
 149              $actiondecline = (object)[
 150                  'text' => get_string('useracceptanceactiondecline', 'tool_policy'),
 151                  'title' => get_string('useracceptanceactiondeclineone', 'tool_policy', $versionname),
 152                  'data' => 'acceptmodal',
 153                  'url' => (new \moodle_url('/admin/tool/policy/accept.php', [
 154                      'userids[]' => $this->userid,
 155                      'versionids[]' => $versionid,
 156                      'action' => 'decline',
 157                      'returnurl' => $this->pageurl->out_as_local_url(false),
 158                  ]))->out(false),
 159              ];
 160  
 161              if ($this->accepted) {
 162                  $data->statusicon = 'agreed';
 163  
 164                  if ($this->onbehalf) {
 165                      $data->statustext = get_string('acceptancestatusacceptedbehalf', 'tool_policy');
 166                  } else {
 167                      $data->statustext = get_string('acceptancestatusaccepted', 'tool_policy');
 168                  }
 169  
 170                  if ($this->canrevoke) {
 171                      $data->actions[] = $actionrevoke;
 172                  }
 173  
 174              } else if ($this->declined) {
 175                  $data->statusicon = 'declined';
 176  
 177                  if ($this->onbehalf) {
 178                      $data->statustext = get_string('acceptancestatusdeclinedbehalf', 'tool_policy');
 179                  } else {
 180                      $data->statustext = get_string('acceptancestatusdeclined', 'tool_policy');
 181                  }
 182  
 183                  if ($this->canaccept) {
 184                      $data->actions[] = $actionaccept;
 185                  }
 186  
 187              } else {
 188                  $data->statusicon = 'pending';
 189                  $data->statustext = get_string('acceptancestatuspending', 'tool_policy');
 190  
 191                  if ($this->canaccept) {
 192                      $data->actions[] = $actionaccept;
 193                      $data->actions[] = $actiondecline;
 194                  }
 195              }
 196  
 197          } else if (count($this->versions) > 1) {
 198              // We represent the summary status for multiple policies.
 199  
 200              $data->actions[] = (object)[
 201                  'text' => get_string('useracceptanceactiondetails', 'tool_policy'),
 202                  'url' => (new \moodle_url('/admin/tool/policy/user.php', [
 203                      'userid' => $this->userid,
 204                      'returnurl' => $this->pageurl->out_as_local_url(false),
 205                  ]))->out(false),
 206              ];
 207  
 208              // Prepare the action link to accept all pending policies.
 209              $accepturl = new \moodle_url('/admin/tool/policy/accept.php', [
 210                  'userids[]' => $this->userid,
 211                  'action' => 'accept',
 212                  'returnurl' => $this->pageurl->out_as_local_url(false),
 213              ]);
 214  
 215              foreach (array_diff(array_keys($this->versions), $this->accepted, $this->declined) as $ix => $versionid) {
 216                  $accepturl->param('versionids['.$ix.']', $versionid);
 217              }
 218  
 219              $actionaccept = (object)[
 220                  'text' => get_string('useracceptanceactionaccept', 'tool_policy'),
 221                  'title' => get_string('useracceptanceactionacceptpending', 'tool_policy'),
 222                  'data' => 'acceptmodal',
 223                  'url' => $accepturl->out(false),
 224              ];
 225  
 226              // Prepare the action link to revoke all agreed policies.
 227              $revokeurl = new \moodle_url('/admin/tool/policy/accept.php', [
 228                  'userids[]' => $this->userid,
 229                  'action' => 'revoke',
 230                  'returnurl' => $this->pageurl->out_as_local_url(false),
 231              ]);
 232  
 233              foreach ($this->accepted as $ix => $versionid) {
 234                  $revokeurl->param('versionids['.$ix.']', $versionid);
 235              }
 236  
 237              $actionrevoke = (object)[
 238                  'text' => get_string('useracceptanceactionrevoke', 'tool_policy'),
 239                  'title' => get_string('useracceptanceactionrevokeall', 'tool_policy'),
 240                  'data' => 'acceptmodal',
 241                  'url' => $revokeurl->out(false),
 242              ];
 243  
 244              // Prepare the action link to decline all pending policies.
 245              $declineurl = new \moodle_url('/admin/tool/policy/accept.php', [
 246                  'userids[]' => $this->userid,
 247                  'action' => 'decline',
 248                  'returnurl' => $this->pageurl->out_as_local_url(false),
 249              ]);
 250  
 251              foreach (array_diff(array_keys($this->versions), $this->accepted, $this->declined) as $ix => $versionid) {
 252                  $declineurl->param('versionids['.$ix.']', $versionid);
 253              }
 254  
 255              $actiondecline = (object)[
 256                  'text' => get_string('useracceptanceactiondecline', 'tool_policy'),
 257                  'title' => get_string('useracceptanceactiondeclinepending', 'tool_policy'),
 258                  'data' => 'acceptmodal',
 259                  'url' => $declineurl->out(false),
 260              ];
 261  
 262              $countversions = count($this->versions);
 263              $countaccepted = count($this->accepted);
 264              $countdeclined = count($this->declined);
 265  
 266              if ($countaccepted == $countversions) {
 267                  // All policies accepted.
 268                  $data->statusicon = 'agreed';
 269                  $data->statustext = get_string('acceptancestatusaccepted', 'tool_policy');
 270  
 271                  if ($this->canrevoke) {
 272                      $data->actions[] = $actionrevoke;
 273                  }
 274  
 275              } else if ($countdeclined == $countversions) {
 276                  // All policies declined.
 277                  $data->statusicon = 'declined';
 278                  $data->statustext = get_string('acceptancestatusdeclined', 'tool_policy');
 279  
 280              } else if ($countaccepted + $countdeclined == $countversions) {
 281                  // All policies responded, only some of them accepted.
 282                  $data->statusicon = 'partial';
 283                  $data->statustext = get_string('acceptancestatuspartial', 'tool_policy');
 284  
 285                  if ($this->accepted && $this->canrevoke) {
 286                      $data->actions[] = $actionrevoke;
 287                  }
 288  
 289              } else {
 290                  // Some policies are pending.
 291                  $data->statusicon = 'pending';
 292                  $data->statustext = get_string('acceptancestatuspending', 'tool_policy');
 293  
 294                  if ($this->canaccept) {
 295                      $data->actions[] = $actionaccept;
 296                      $data->actions[] = $actiondecline;
 297                  }
 298              }
 299          }
 300  
 301          return $data;
 302      }
 303  
 304      /**
 305       * Describe the status with a plain text for downloading purposes.
 306       *
 307       * @return string
 308       */
 309      public function export_for_download() {
 310  
 311          if (count($this->versions) == 1) {
 312              if ($this->accepted) {
 313                  if ($this->onbehalf) {
 314                      return get_string('acceptancestatusacceptedbehalf', 'tool_policy');
 315                  } else {
 316                      return get_string('acceptancestatusaccepted', 'tool_policy');
 317                  }
 318  
 319              } else if ($this->declined) {
 320                  if ($this->onbehalf) {
 321                      return get_string('acceptancestatusdeclinedbehalf', 'tool_policy');
 322                  } else {
 323                      return get_string('acceptancestatusdeclined', 'tool_policy');
 324                  }
 325  
 326              } else {
 327                  return get_string('acceptancestatuspending', 'tool_policy');
 328              }
 329  
 330          } else if (count($this->versions) > 1) {
 331              if (count($this->accepted) == count($this->versions)) {
 332                  return get_string('acceptancestatusaccepted', 'tool_policy');
 333  
 334              } else if (count($this->declined) == count($this->versions)) {
 335                  return get_string('acceptancestatusdeclined', 'tool_policy');
 336  
 337              } else if (count($this->accepted) > 0 || count($this->declined) > 0) {
 338                  return get_string('acceptancestatuspartial', 'tool_policy');
 339  
 340              } else {
 341                  return get_string('acceptancestatuspending', 'tool_policy');
 342              }
 343          }
 344      }
 345  }