2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Provides {@link tool_policy\form\accept_policy} class.
20 * @package tool_policy
21 * @copyright 2018 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace tool_policy\form;
28 use tool_policy\policy_version;
30 defined('MOODLE_INTERNAL') || die();
32 require_once($CFG->dirroot.'/lib/formslib.php');
35 * Represents the form for accepting or revoking a policy.
37 * @package tool_policy
38 * @copyright 2018 Marina Glancy
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class accept_policy extends \moodleform {
44 * Defines the form fields.
46 public function definition() {
48 $mform = $this->_form;
50 if (empty($this->_customdata['userids']) || !is_array($this->_customdata['userids'])) {
51 throw new \moodle_exception('missingparam', 'error', '', 'userids');
53 if (empty($this->_customdata['versionids']) || !is_array($this->_customdata['versionids'])) {
54 throw new \moodle_exception('missingparam', '', '', 'versionids');
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);
62 foreach ($usernames as $userid => $name) {
63 $mform->addElement('hidden', 'userids['.$userid.']', $userid);
64 $mform->setType('userids['.$userid.']', PARAM_INT);
67 foreach ($versionnames as $versionid => $name) {
68 $mform->addElement('hidden', 'versionids['.$versionid.']', $versionid);
69 $mform->setType('versionids['.$versionid.']', PARAM_INT);
72 $mform->addElement('hidden', 'returnurl');
73 $mform->setType('returnurl', PARAM_LOCALURL);
74 $useracceptancelabel = (count($usernames) > 1) ? get_string('acceptanceusers', 'tool_policy') :
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));
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');
91 throw new \moodle_exception('invalidaccessparameter');
94 $mform->setType('action', PARAM_ALPHA);
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');
101 $mform->addElement('textarea', 'note', get_string('acceptancenote', 'tool_policy'));
102 $mform->setType('note', PARAM_NOTAGS);
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'));
114 $PAGE->requires->js_call_amd('tool_policy/policyactions', 'init', ['[data-action="view"]']);
118 * Validate userids and return usernames
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)
125 protected function validate_and_get_users($versionids, $userids, $action) {
129 list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
130 $params['usercontextlevel'] = CONTEXT_USER;
131 $userfieldsapi = \core_user\fields::for_name();
132 $users = $DB->get_records_sql("SELECT u.id" . $userfieldsapi->get_sql('u')->selects . ", " .
133 \context_helper::get_preload_record_columns_sql('ctx') .
134 " FROM {user} u JOIN {context} ctx ON ctx.contextlevel=:usercontextlevel AND ctx.instanceid = u.id
135 WHERE u.id " . $sql, $params);
137 foreach ($userids as $userid) {
138 if (!isset($users[$userid])) {
139 throw new \dml_missing_record_exception('user', 'id=?', [$userid]);
141 $user = $users[$userid];
142 if (isguestuser($user)) {
143 throw new \moodle_exception('noguest');
145 \context_helper::preload_from_record($user);
146 if ($action === 'revoke') {
147 api::can_revoke_policies($versionids, $userid, true);
148 } else if ($action === 'accept') {
149 api::can_accept_policies($versionids, $userid, true);
150 } else if ($action === 'decline') {
151 api::can_decline_policies($versionids, $userid, true);
153 $usernames[$userid] = fullname($user);
159 * Validate versionids and return their names
161 * @param array $versionids
162 * @return array (versionid=>name)
164 protected function validate_and_get_versions($versionids) {
166 $policies = api::list_policies();
167 foreach ($versionids as $versionid) {
168 $version = api::get_policy_version($versionid, $policies);
169 if ($version->audience == policy_version::AUDIENCE_GUESTS) {
170 throw new \moodle_exception('errorpolicyversionnotfound', 'tool_policy');
172 $url = new \moodle_url('/admin/tool/policy/view.php', ['versionid' => $version->id]);
173 $policyname = $version->name;
174 if ($version->status != policy_version::STATUS_ACTIVE) {
175 $policyname .= ' ' . $version->revision;
177 $versionnames[$version->id] = \html_writer::link($url, $policyname,
178 ['data-action' => 'view', 'data-versionid' => $version->id]);
180 return $versionnames;
184 * Process form submission
186 public function process() {
187 if ($data = $this->get_data()) {
188 foreach ($data->userids as $userid) {
189 if ($data->action === 'revoke') {
190 foreach ($data->versionids as $versionid) {
191 \tool_policy\api::revoke_acceptance($versionid, $userid, $data->note);
193 } else if ($data->action === 'accept') {
194 \tool_policy\api::accept_policies($data->versionids, $userid, $data->note);
195 } else if ($data->action === 'decline') {
196 \tool_policy\api::decline_policies($data->versionids, $userid, $data->note);