1 |
efrain |
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 |
$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);
|
|
|
136 |
|
|
|
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 |
}
|
|
|
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);
|
|
|
152 |
}
|
|
|
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) {
|
|
|
189 |
if ($data->action === 'revoke') {
|
|
|
190 |
foreach ($data->versionids as $versionid) {
|
|
|
191 |
\tool_policy\api::revoke_acceptance($versionid, $userid, $data->note);
|
|
|
192 |
}
|
|
|
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);
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
}
|