mirrored from git://git.moodle.org/moodle.git
/
api.php
270 lines (237 loc) · 11.1 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class exposing the api for the cohortroles tool.
*
* @package tool_cohortroles
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_cohortroles;
use stdClass;
use context_system;
use core_competency\invalid_persistent_exception;
/**
* Class for doing things with cohort roles.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api {
/**
* Create a cohort role assignment from a record containing all the data for the class.
*
* Requires moodle/role:manage capability at the system context.
*
* @param stdClass $record Record containing all the data for an instance of the class.
* @return competency
*/
public static function create_cohort_role_assignment(stdClass $record) {
$cohortroleassignment = new cohort_role_assignment(0, $record);
$context = context_system::instance();
// First we do a permissions check.
require_capability('moodle/role:manage', $context);
// Validate before we check for existing records.
if (!$cohortroleassignment->is_valid()) {
throw new invalid_persistent_exception($cohortroleassignment->get_errors());
}
$existing = cohort_role_assignment::get_record((array) $record);
if (!empty($existing)) {
return false;
} else {
// OK - all set.
$cohortroleassignment->create();
}
return $cohortroleassignment;
}
/**
* Delete a cohort role assignment by id.
*
* Requires moodle/role:manage capability at the system context.
*
* @param int $id The record to delete. This will also remove this role from the user for all users in the system.
* @return boolean
*/
public static function delete_cohort_role_assignment($id) {
$cohortroleassignment = new cohort_role_assignment($id);
$context = context_system::instance();
// First we do a permissions check.
require_capability('moodle/role:manage', $context);
// OK - all set.
return $cohortroleassignment->delete();
}
/**
* Perform a search based on the provided filters and return a paginated list of records.
*
* Requires moodle/role:manage capability at the system context.
*
* @param string $sort The column to sort on
* @param string $order ('ASC' or 'DESC')
* @param int $skip Number of records to skip (pagination)
* @param int $limit Max of records to return (pagination)
* @return array of cohort_role_assignment
*/
public static function list_cohort_role_assignments($sort = '', $order = 'ASC', $skip = 0, $limit = 0) {
$context = context_system::instance();
// First we do a permissions check.
require_capability('moodle/role:manage', $context);
// OK - all set.
return cohort_role_assignment::get_records(array(), $sort, $order, $skip, $limit);
}
/**
* Perform a search based on the provided filters and return a paginated list of records.
*
* Requires moodle/role:manage capability at system context.
*
* @return int
*/
public static function count_cohort_role_assignments() {
$context = context_system::instance();
// First we do a permissions check.
require_capability('moodle/role:manage', $context);
// OK - all set.
return cohort_role_assignment::count_records();
}
/**
* Sync all roles - adding and deleting role assignments as required.
*
* Slow. Should only be called from a background task.
*
* Requires moodle/role:manage capability at the system context.
*
* @return array('rolesadded' => array of (useridassignedto, useridassignedover, roleid),
* 'rolesremoved' => array of (useridassignedto, useridassignedover, roleid))
*/
public static function sync_all_cohort_roles() {
global $DB;
$context = context_system::instance();
// First we do a permissions check.
require_capability('moodle/role:manage', $context);
// Ok ready to go.
$rolesadded = array();
$rolesremoved = array();
// Get all cohort role assignments and group them by user and role.
$all = cohort_role_assignment::get_records(array(), 'userid, roleid');
// We build an better structure to loop on.
$info = array();
foreach ($all as $cra) {
if (!isset($info[$cra->get('userid')])) {
$info[$cra->get('userid')] = array();
}
if (!isset($info[$cra->get('userid')][$cra->get('roleid')])) {
$info[$cra->get('userid')][$cra->get('roleid')] = array();
}
array_push($info[$cra->get('userid')][$cra->get('roleid')], $cra->get('cohortid'));
}
// Then for each user+role combo - find user context in the cohort without a role assigned.
foreach ($info as $userid => $roles) {
foreach ($roles as $roleid => $cohorts) {
list($cohortsql, $params) = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED);
$params['usercontext'] = CONTEXT_USER;
$params['roleid'] = $roleid;
$params['userid'] = $userid;
$sql = 'SELECT DISTINCT u.id AS userid, ra.id, ctx.id AS contextid
FROM {user} u
JOIN {cohort_members} cm ON u.id = cm.userid
JOIN {context} ctx ON u.id = ctx.instanceid AND ctx.contextlevel = :usercontext
LEFT JOIN {role_assignments} ra ON ra.contextid = ctx.id
AND ra.roleid = :roleid
AND ra.userid = :userid
WHERE cm.cohortid ' . $cohortsql . '
AND ra.id IS NULL';
$toadd = $DB->get_records_sql($sql, $params);
foreach ($toadd as $add) {
role_assign($roleid, $userid, $add->contextid, 'tool_cohortroles');
$rolesadded[] = array(
'useridassignedto' => $userid,
'useridassignedover' => $add->userid,
'roleid' => $roleid
);
}
}
}
// And for each user+role combo - find user context not in the cohort with a role assigned.
// If the role was assigned by this component, unassign the role.
foreach ($info as $userid => $roles) {
foreach ($roles as $roleid => $cohorts) {
// Now we are looking for entries NOT in the cohort.
list($cohortsql, $params) = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED);
$params['usercontext'] = CONTEXT_USER;
$params['roleid'] = $roleid;
$params['userid'] = $userid;
$params['component'] = 'tool_cohortroles';
$sql = 'SELECT u.id as userid, ra.id, ctx.id AS contextid
FROM {user} u
JOIN {context} ctx ON u.id = ctx.instanceid AND ctx.contextlevel = :usercontext
JOIN {role_assignments} ra ON ra.contextid = ctx.id AND ra.roleid = :roleid AND ra.userid = :userid
LEFT JOIN {cohort_members} cm ON u.id = cm.userid
AND cm.cohortid ' . $cohortsql . '
WHERE ra.component = :component AND cm.cohortid IS NULL';
$toremove = $DB->get_records_sql($sql, $params);
foreach ($toremove as $remove) {
role_unassign($roleid, $userid, $remove->contextid, 'tool_cohortroles');
$rolesremoved[] = array(
'useridassignedto' => $userid,
'useridassignedover' => $remove->userid,
'roleid' => $roleid
);
}
}
}
// Clean the legacy role assignments which are stale.
$paramsclean['usercontext'] = CONTEXT_USER;
$paramsclean['component'] = 'tool_cohortroles';
$sql = 'SELECT DISTINCT(ra.id), ra.roleid, ra.userid, ra.contextid, ctx.instanceid
FROM {role_assignments} ra
JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.contextlevel = :usercontext
JOIN {cohort_members} cm ON cm.userid = ctx.instanceid
LEFT JOIN {tool_cohortroles} tc ON tc.cohortid = cm.cohortid
AND tc.userid = ra.userid
AND tc.roleid = ra.roleid
WHERE ra.component = :component
AND tc.id is null';
if ($candidatelegacyassignments = $DB->get_records_sql($sql, $paramsclean)) {
$sql = 'SELECT DISTINCT(ra.id)
FROM {role_assignments} ra
JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.contextlevel = :usercontext
JOIN {cohort_members} cm ON cm.userid = ctx.instanceid
JOIN {tool_cohortroles} tc ON tc.cohortid = cm.cohortid AND tc.userid = ra.userid
WHERE ra.component = :component';
if ($currentvalidroleassignments = $DB->get_records_sql($sql, $paramsclean)) {
foreach ($candidatelegacyassignments as $candidate) {
if (!array_key_exists($candidate->id, $currentvalidroleassignments)) {
role_unassign($candidate->roleid, $candidate->userid, $candidate->contextid, 'tool_cohortroles');
$rolesremoved[] = array(
'useridassignedto' => $candidate->userid,
'useridassignedover' => $candidate->instanceid,
'roleid' => $candidate->roleid
);
}
}
} else {
foreach ($candidatelegacyassignments as $candidate) {
role_unassign($candidate->roleid, $candidate->userid, $candidate->contextid, 'tool_cohortroles');
$rolesremoved[] = array(
'useridassignedto' => $candidate->userid,
'useridassignedover' => $candidate->instanceid,
'roleid' => $candidate->roleid
);
}
}
}
return array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
}
}