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