weekly release 4.1dev
[moodle.git] / admin / tool / policy / classes / form / accept_policy.php
CommitLineData
cf398020
MG
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
25namespace tool_policy\form;
26
27use tool_policy\api;
28use tool_policy\policy_version;
29
30defined('MOODLE_INTERNAL') || die();
31
32require_once($CFG->dirroot.'/lib/formslib.php');
33
34/**
461b1931 35 * Represents the form for accepting or revoking a policy.
cf398020
MG
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 */
41class accept_policy extends \moodleform {
42
43 /**
44 * Defines the form fields.
45 */
46 public function definition() {
4a742e4f 47 global $PAGE, $USER;
cf398020
MG
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 }
4a742e4f 56 $action = $this->_customdata['action'];
cf398020
MG
57 $userids = clean_param_array($this->_customdata['userids'], PARAM_INT);
58 $versionids = clean_param_array($this->_customdata['versionids'], PARAM_INT);
ad5e2135 59 $usernames = $this->validate_and_get_users($versionids, $userids, $action);
cf398020
MG
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);
c106d489
MG
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));
cf398020 80
4a742e4f 81 if ($action === 'revoke') {
461b1931
SA
82 $mform->addElement('static', 'ack', '', get_string('revokeacknowledgement', 'tool_policy'));
83 $mform->addElement('hidden', 'action', 'revoke');
4a742e4f 84 } else if ($action === 'accept') {
461b1931 85 $mform->addElement('static', 'ack', '', get_string('acceptanceacknowledgement', 'tool_policy'));
4a742e4f
DM
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');
461b1931 99 }
cf398020
MG
100
101 $mform->addElement('textarea', 'note', get_string('acceptancenote', 'tool_policy'));
102 $mform->setType('note', PARAM_NOTAGS);
103
104 if (!empty($this->_customdata['showbuttons'])) {
4a742e4f 105 if ($action === 'revoke') {
461b1931 106 $this->add_action_buttons(true, get_string('irevokethepolicy', 'tool_policy'));
4a742e4f 107 } else if ($action === 'accept') {
461b1931 108 $this->add_action_buttons(true, get_string('iagreetothepolicy', 'tool_policy'));
4a742e4f
DM
109 } else if ($action === 'decline') {
110 $this->add_action_buttons(true, get_string('declinethepolicy', 'tool_policy'));
461b1931 111 }
cf398020
MG
112 }
113
90117cef 114 $PAGE->requires->js_call_amd('tool_policy/policyactions', 'init', ['[data-action="view"]']);
cf398020
MG
115 }
116
117 /**
118 * Validate userids and return usernames
119 *
ad5e2135 120 * @param array $versionids int[] List of policy version ids to process.
cf398020 121 * @param array $userids
4a742e4f 122 * @param string $action accept|decline|revoke
cf398020
MG
123 * @return array (userid=>username)
124 */
ad5e2135 125 protected function validate_and_get_users($versionids, $userids, $action) {
e8cf2ffd 126 global $DB;
4a742e4f 127
cf398020
MG
128 $usernames = [];
129 list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
130 $params['usercontextlevel'] = CONTEXT_USER;
5e72715e 131 $userfieldsapi = \core_user\fields::for_name();
558cc1b8 132 $users = $DB->get_records_sql("SELECT u.id" . $userfieldsapi->get_sql('u')->selects . ", " .
cf398020
MG
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);
136
cf398020
MG
137 foreach ($userids as $userid) {
138 if (!isset($users[$userid])) {
139 throw new \dml_missing_record_exception('user', 'id=?', [$userid]);
140 }
141 $user = $users[$userid];
142 if (isguestuser($user)) {
143 throw new \moodle_exception('noguest');
144 }
e8cf2ffd 145 \context_helper::preload_from_record($user);
4a742e4f 146 if ($action === 'revoke') {
ad5e2135 147 api::can_revoke_policies($versionids, $userid, true);
4a742e4f 148 } else if ($action === 'accept') {
ad5e2135 149 api::can_accept_policies($versionids, $userid, true);
4a742e4f 150 } else if ($action === 'decline') {
ad5e2135 151 api::can_decline_policies($versionids, $userid, true);
461b1931 152 }
cf398020
MG
153 $usernames[$userid] = fullname($user);
154 }
155 return $usernames;
156 }
157
158 /**
159 * Validate versionids and return their names
160 *
161 * @param array $versionids
162 * @return array (versionid=>name)
163 */
164 protected function validate_and_get_versions($versionids) {
165 $versionnames = [];
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');
171 }
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;
176 }
177 $versionnames[$version->id] = \html_writer::link($url, $policyname,
178 ['data-action' => 'view', 'data-versionid' => $version->id]);
179 }
180 return $versionnames;
181 }
182
183 /**
184 * Process form submission
185 */
186 public function process() {
187 if ($data = $this->get_data()) {
188 foreach ($data->userids as $userid) {
4a742e4f 189 if ($data->action === 'revoke') {
461b1931
SA
190 foreach ($data->versionids as $versionid) {
191 \tool_policy\api::revoke_acceptance($versionid, $userid, $data->note);
192 }
4a742e4f 193 } else if ($data->action === 'accept') {
461b1931 194 \tool_policy\api::accept_policies($data->versionids, $userid, $data->note);
4a742e4f
DM
195 } else if ($data->action === 'decline') {
196 \tool_policy\api::decline_policies($data->versionids, $userid, $data->note);
461b1931 197 }
cf398020
MG
198 }
199 }
200 }
25d31d43 201}