MDL-62342 privacy: Use singular/plural form in labels
[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() {
47 global $PAGE;
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 }
461b1931 56 $revoke = (!empty($this->_customdata['action']) && $this->_customdata['action'] == 'revoke');
cf398020
MG
57 $userids = clean_param_array($this->_customdata['userids'], PARAM_INT);
58 $versionids = clean_param_array($this->_customdata['versionids'], PARAM_INT);
461b1931 59 $usernames = $this->validate_and_get_users($userids, $revoke);
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
461b1931
SA
81 if ($revoke) {
82 $mform->addElement('static', 'ack', '', get_string('revokeacknowledgement', 'tool_policy'));
83 $mform->addElement('hidden', 'action', 'revoke');
84 $mform->setType('action', PARAM_ALPHA);
85 } else {
86 $mform->addElement('static', 'ack', '', get_string('acceptanceacknowledgement', 'tool_policy'));
87 }
cf398020
MG
88
89 $mform->addElement('textarea', 'note', get_string('acceptancenote', 'tool_policy'));
90 $mform->setType('note', PARAM_NOTAGS);
91
92 if (!empty($this->_customdata['showbuttons'])) {
461b1931
SA
93 if ($revoke) {
94 $this->add_action_buttons(true, get_string('irevokethepolicy', 'tool_policy'));
95 } else {
96 $this->add_action_buttons(true, get_string('iagreetothepolicy', 'tool_policy'));
97 }
cf398020
MG
98 }
99
100 $PAGE->requires->js_call_amd('tool_policy/policyactions', 'init');
101 }
102
103 /**
104 * Validate userids and return usernames
105 *
106 * @param array $userids
461b1931 107 * @param boolean $revoke True if policies will be revoked; false when policies will be accepted.
cf398020
MG
108 * @return array (userid=>username)
109 */
461b1931 110 protected function validate_and_get_users($userids, $revoke = false) {
e8cf2ffd 111 global $DB;
cf398020
MG
112 $usernames = [];
113 list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
114 $params['usercontextlevel'] = CONTEXT_USER;
115 $users = $DB->get_records_sql("SELECT u.id, " . get_all_user_name_fields(true, 'u') . ", " .
116 \context_helper::get_preload_record_columns_sql('ctx') .
117 " FROM {user} u JOIN {context} ctx ON ctx.contextlevel=:usercontextlevel AND ctx.instanceid = u.id
118 WHERE u.id " . $sql, $params);
119
cf398020
MG
120 foreach ($userids as $userid) {
121 if (!isset($users[$userid])) {
122 throw new \dml_missing_record_exception('user', 'id=?', [$userid]);
123 }
124 $user = $users[$userid];
125 if (isguestuser($user)) {
126 throw new \moodle_exception('noguest');
127 }
e8cf2ffd 128 \context_helper::preload_from_record($user);
461b1931
SA
129 if ($revoke) {
130 api::can_revoke_policies($userid, true);
131 } else {
132 api::can_accept_policies($userid, true);
133 }
cf398020
MG
134 $usernames[$userid] = fullname($user);
135 }
136 return $usernames;
137 }
138
139 /**
140 * Validate versionids and return their names
141 *
142 * @param array $versionids
143 * @return array (versionid=>name)
144 */
145 protected function validate_and_get_versions($versionids) {
146 $versionnames = [];
147 $policies = api::list_policies();
148 foreach ($versionids as $versionid) {
149 $version = api::get_policy_version($versionid, $policies);
150 if ($version->audience == policy_version::AUDIENCE_GUESTS) {
151 throw new \moodle_exception('errorpolicyversionnotfound', 'tool_policy');
152 }
153 $url = new \moodle_url('/admin/tool/policy/view.php', ['versionid' => $version->id]);
154 $policyname = $version->name;
155 if ($version->status != policy_version::STATUS_ACTIVE) {
156 $policyname .= ' ' . $version->revision;
157 }
158 $versionnames[$version->id] = \html_writer::link($url, $policyname,
159 ['data-action' => 'view', 'data-versionid' => $version->id]);
160 }
161 return $versionnames;
162 }
163
164 /**
165 * Process form submission
166 */
167 public function process() {
168 if ($data = $this->get_data()) {
461b1931 169 $revoke = (!empty($data->action) && $data->action == 'revoke');
cf398020 170 foreach ($data->userids as $userid) {
461b1931
SA
171 if ($revoke) {
172 foreach ($data->versionids as $versionid) {
173 \tool_policy\api::revoke_acceptance($versionid, $userid, $data->note);
174 }
175 } else {
176 \tool_policy\api::accept_policies($data->versionids, $userid, $data->note);
177 }
cf398020
MG
178 }
179 }
180 }
181}