Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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\form\accept_policy} class.
  19   *
  20   * @package     tool_policy
  21   * @copyright   2018 Marina Glancy
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_policy\form;
  26  
  27  use tool_policy\api;
  28  use tool_policy\policy_version;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  require_once($CFG->dirroot.'/lib/formslib.php');
  33  
  34  /**
  35   * Represents the form for accepting or revoking a policy.
  36   *
  37   * @package     tool_policy
  38   * @copyright   2018 Marina Glancy
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class accept_policy extends \moodleform {
  42  
  43      /**
  44       * Defines the form fields.
  45       */
  46      public function definition() {
  47          global $PAGE, $USER;
  48          $mform = $this->_form;
  49  
  50          if (empty($this->_customdata['userids']) || !is_array($this->_customdata['userids'])) {
  51              throw new \moodle_exception('missingparam', 'error', '', 'userids');
  52          }
  53          if (empty($this->_customdata['versionids']) || !is_array($this->_customdata['versionids'])) {
  54              throw new \moodle_exception('missingparam', '', '', 'versionids');
  55          }
  56          $action = $this->_customdata['action'];
  57          $userids = clean_param_array($this->_customdata['userids'], PARAM_INT);
  58          $versionids = clean_param_array($this->_customdata['versionids'], PARAM_INT);
  59          $usernames = $this->validate_and_get_users($versionids, $userids, $action);
  60          $versionnames = $this->validate_and_get_versions($versionids);
  61  
  62          foreach ($usernames as $userid => $name) {
  63              $mform->addElement('hidden', 'userids['.$userid.']', $userid);
  64              $mform->setType('userids['.$userid.']', PARAM_INT);
  65          }
  66  
  67          foreach ($versionnames as $versionid => $name) {
  68              $mform->addElement('hidden', 'versionids['.$versionid.']', $versionid);
  69              $mform->setType('versionids['.$versionid.']', PARAM_INT);
  70          }
  71  
  72          $mform->addElement('hidden', 'returnurl');
  73          $mform->setType('returnurl', PARAM_LOCALURL);
  74          $useracceptancelabel = (count($usernames) > 1) ? get_string('acceptanceusers', 'tool_policy') :
  75                  get_string('user');
  76          $mform->addElement('static', 'user', $useracceptancelabel, join(', ', $usernames));
  77          $policyacceptancelabel = (count($versionnames) > 1) ? get_string('acceptancepolicies', 'tool_policy') :
  78                  get_string('policydochdrpolicy', 'tool_policy');
  79          $mform->addElement('static', 'policy', $policyacceptancelabel, join(', ', $versionnames));
  80  
  81          if ($action === 'revoke') {
  82              $mform->addElement('static', 'ack', '', get_string('revokeacknowledgement', 'tool_policy'));
  83              $mform->addElement('hidden', 'action', 'revoke');
  84          } else if ($action === 'accept') {
  85              $mform->addElement('static', 'ack', '', get_string('acceptanceacknowledgement', 'tool_policy'));
  86              $mform->addElement('hidden', 'action', 'accept');
  87          } else if ($action === 'decline') {
  88              $mform->addElement('static', 'ack', '', get_string('declineacknowledgement', 'tool_policy'));
  89              $mform->addElement('hidden', 'action', 'decline');
  90          } else {
  91              throw new \moodle_exception('invalidaccessparameter');
  92          }
  93  
  94          $mform->setType('action', PARAM_ALPHA);
  95  
  96          if (count($usernames) == 1 && isset($usernames[$USER->id])) {
  97              // No need to display the acknowledgement if the users are giving/revoking acceptance on their own.
  98              $mform->removeElement('ack');
  99          }
 100  
 101          $mform->addElement('textarea', 'note', get_string('acceptancenote', 'tool_policy'));
 102          $mform->setType('note', PARAM_NOTAGS);
 103  
 104          if (!empty($this->_customdata['showbuttons'])) {
 105              if ($action === 'revoke') {
 106                  $this->add_action_buttons(true, get_string('irevokethepolicy', 'tool_policy'));
 107              } else if ($action === 'accept') {
 108                  $this->add_action_buttons(true, get_string('iagreetothepolicy', 'tool_policy'));
 109              } else if ($action === 'decline') {
 110                  $this->add_action_buttons(true, get_string('declinethepolicy', 'tool_policy'));
 111              }
 112          }
 113  
 114          $PAGE->requires->js_call_amd('tool_policy/policyactions', 'init', ['[data-action="view"]']);
 115      }
 116  
 117      /**
 118       * Validate userids and return usernames
 119       *
 120       * @param array $versionids int[] List of policy version ids to process.
 121       * @param array $userids
 122       * @param string $action accept|decline|revoke
 123       * @return array (userid=>username)
 124       */
 125      protected function validate_and_get_users($versionids, $userids, $action) {
 126          global $DB;
 127  
 128          $usernames = [];
 129          list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
 130          $params['usercontextlevel'] = CONTEXT_USER;
 131          $users = $DB->get_records_sql("SELECT u.id, " . get_all_user_name_fields(true, 'u') . ", " .
 132                  \context_helper::get_preload_record_columns_sql('ctx') .
 133              " FROM {user} u JOIN {context} ctx ON ctx.contextlevel=:usercontextlevel AND ctx.instanceid = u.id
 134              WHERE u.id " . $sql, $params);
 135  
 136          foreach ($userids as $userid) {
 137              if (!isset($users[$userid])) {
 138                  throw new \dml_missing_record_exception('user', 'id=?', [$userid]);
 139              }
 140              $user = $users[$userid];
 141              if (isguestuser($user)) {
 142                  throw new \moodle_exception('noguest');
 143              }
 144              \context_helper::preload_from_record($user);
 145              if ($action === 'revoke') {
 146                  api::can_revoke_policies($versionids, $userid, true);
 147              } else if ($action === 'accept') {
 148                  api::can_accept_policies($versionids, $userid, true);
 149              } else if ($action === 'decline') {
 150                  api::can_decline_policies($versionids, $userid, true);
 151              }
 152              $usernames[$userid] = fullname($user);
 153          }
 154          return $usernames;
 155      }
 156  
 157      /**
 158       * Validate versionids and return their names
 159       *
 160       * @param array $versionids
 161       * @return array (versionid=>name)
 162       */
 163      protected function validate_and_get_versions($versionids) {
 164          $versionnames = [];
 165          $policies = api::list_policies();
 166          foreach ($versionids as $versionid) {
 167              $version = api::get_policy_version($versionid, $policies);
 168              if ($version->audience == policy_version::AUDIENCE_GUESTS) {
 169                  throw new \moodle_exception('errorpolicyversionnotfound', 'tool_policy');
 170              }
 171              $url = new \moodle_url('/admin/tool/policy/view.php', ['versionid' => $version->id]);
 172              $policyname = $version->name;
 173              if ($version->status != policy_version::STATUS_ACTIVE) {
 174                  $policyname .= ' ' . $version->revision;
 175              }
 176              $versionnames[$version->id] = \html_writer::link($url, $policyname,
 177                  ['data-action' => 'view', 'data-versionid' => $version->id]);
 178          }
 179          return $versionnames;
 180      }
 181  
 182      /**
 183       * Process form submission
 184       */
 185      public function process() {
 186          if ($data = $this->get_data()) {
 187              foreach ($data->userids as $userid) {
 188                  if ($data->action === 'revoke') {
 189                      foreach ($data->versionids as $versionid) {
 190                          \tool_policy\api::revoke_acceptance($versionid, $userid, $data->note);
 191                      }
 192                  } else if ($data->action === 'accept') {
 193                      \tool_policy\api::accept_policies($data->versionids, $userid, $data->note);
 194                  } else if ($data->action === 'decline') {
 195                      \tool_policy\api::decline_policies($data->versionids, $userid, $data->note);
 196                  }
 197              }
 198          }
 199      }
 200  }