bbbf2d40 |
1 | <?php |
cee0901c |
2 | /** |
3 | * Capability session information format |
bbbf2d40 |
4 | * 2 x 2 array |
5 | * [context][capability] |
6 | * where context is the context id of the table 'context' |
7 | * and capability is a string defining the capability |
8 | * e.g. |
9 | * |
10 | * [Capabilities] => [26][mod/forum:viewpost] = 1 |
11 | * [26][mod/forum:startdiscussion] = -8990 |
12 | * [26][mod/forum:editallpost] = -1 |
13 | * [273][moodle:blahblah] = 1 |
14 | * [273][moodle:blahblahblah] = 2 |
15 | */ |
bbbf2d40 |
16 | |
17 | // permission definitions |
e49e61bf |
18 | define('CAP_INHERIT', 0); |
bbbf2d40 |
19 | define('CAP_ALLOW', 1); |
20 | define('CAP_PREVENT', -1); |
21 | define('CAP_PROHIBIT', -1000); |
22 | |
bbbf2d40 |
23 | // context definitions |
24 | define('CONTEXT_SYSTEM', 10); |
25 | define('CONTEXT_PERSONAL', 20); |
4b10f08b |
26 | define('CONTEXT_USER', 30); |
bbbf2d40 |
27 | define('CONTEXT_COURSECAT', 40); |
28 | define('CONTEXT_COURSE', 50); |
29 | define('CONTEXT_GROUP', 60); |
30 | define('CONTEXT_MODULE', 70); |
31 | define('CONTEXT_BLOCK', 80); |
32 | |
21b6db6e |
33 | // capability risks - see http://docs.moodle.org/en/Hardening_new_Roles_system |
34 | define('RISK_MANAGETRUST', 0x0001); |
a6b02b65 |
35 | define('RISK_CONFIG', 0x0002); |
21b6db6e |
36 | define('RISK_XSS', 0x0004); |
37 | define('RISK_PERSONAL', 0x0008); |
38 | define('RISK_SPAM', 0x0010); |
39 | |
40 | |
340ea4e8 |
41 | $context_cache = array(); // Cache of all used context objects for performance (by level and instance) |
42 | $context_cache_id = array(); // Index to above cache by id |
bbbf2d40 |
43 | |
7700027f |
44 | |
e7876c1e |
45 | /** |
46 | * Loads the capabilities for the default guest role to the current user in a specific context |
47 | * @return object |
48 | */ |
49 | function load_guest_role($context=NULL) { |
50 | global $USER; |
51 | |
52 | static $guestrole; |
53 | |
54 | if (!isloggedin()) { |
55 | return false; |
56 | } |
57 | |
58 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
59 | return false; |
60 | } |
61 | |
62 | if (empty($context)) { |
63 | $context = $sitecontext; |
64 | } |
65 | |
66 | if (empty($guestrole)) { |
8f8ed475 |
67 | if (!$guestrole = get_guest_role()) { |
e7876c1e |
68 | return false; |
69 | } |
70 | } |
71 | |
eef868d1 |
72 | if ($capabilities = get_records_select('role_capabilities', |
e7876c1e |
73 | "roleid = $guestrole->id AND contextid = $sitecontext->id")) { |
74 | foreach ($capabilities as $capability) { |
eef868d1 |
75 | $USER->capabilities[$context->id][$capability->capability] = $capability->permission; |
e7876c1e |
76 | } |
77 | } |
78 | |
79 | return true; |
80 | } |
81 | |
7700027f |
82 | /** |
83 | * Load default not logged in role capabilities when user is not logged in |
eef868d1 |
84 | * @return bool |
7700027f |
85 | */ |
20aeb4b8 |
86 | function load_notloggedin_role() { |
87 | global $CFG, $USER; |
88 | |
7700027f |
89 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
90 | return false; |
35a518c5 |
91 | } |
92 | |
7700027f |
93 | if (empty($CFG->notloggedinroleid)) { // Let's set the default to the guest role |
8f8ed475 |
94 | if ($role = get_guest_role()) { |
7700027f |
95 | set_config('notloggedinroleid', $role->id); |
96 | } else { |
97 | return false; |
98 | } |
99 | } |
20aeb4b8 |
100 | |
eef868d1 |
101 | if ($capabilities = get_records_select('role_capabilities', |
7700027f |
102 | "roleid = $CFG->notloggedinroleid AND contextid = $sitecontext->id")) { |
103 | foreach ($capabilities as $capability) { |
eef868d1 |
104 | $USER->capabilities[$sitecontext->id][$capability->capability] = $capability->permission; |
7700027f |
105 | } |
20aeb4b8 |
106 | } |
107 | |
108 | return true; |
109 | } |
cee0901c |
110 | |
8f8ed475 |
111 | /** |
112 | * Load default not logged in role capabilities when user is not logged in |
eef868d1 |
113 | * @return bool |
8f8ed475 |
114 | */ |
115 | function load_defaultuser_role() { |
116 | global $CFG, $USER; |
117 | |
118 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
119 | return false; |
120 | } |
121 | |
122 | if (empty($CFG->defaultuserroleid)) { // Let's set the default to the guest role |
123 | if ($role = get_guest_role()) { |
124 | set_config('defaultuserroleid', $role->id); |
125 | } else { |
126 | return false; |
127 | } |
128 | } |
129 | |
eef868d1 |
130 | if ($capabilities = get_records_select('role_capabilities', |
8f8ed475 |
131 | "roleid = $CFG->defaultuserroleid AND contextid = $sitecontext->id")) { |
132 | foreach ($capabilities as $capability) { |
ca23ffdb |
133 | if (!isset($USER->capabilities[$sitecontext->id][$capability->capability])) { // Don't overwrite |
eef868d1 |
134 | $USER->capabilities[$sitecontext->id][$capability->capability] = $capability->permission; |
ca23ffdb |
135 | } |
8f8ed475 |
136 | } |
137 | |
eef868d1 |
138 | // SPECIAL EXCEPTION: If the default user role is actually a guest role, then |
8f8ed475 |
139 | // remove some capabilities so this user doesn't get confused with a REAL guest |
2f089dd5 |
140 | if (isset($USER->capabilities[$sitecontext->id]['moodle/legacy:guest']) and $USER->username != 'guest') { |
eef868d1 |
141 | unset($USER->capabilities[$sitecontext->id]['moodle/legacy:guest']); |
8f8ed475 |
142 | unset($USER->capabilities[$sitecontext->id]['moodle/course:view']); // No access to courses by default |
143 | } |
144 | } |
145 | |
146 | return true; |
147 | } |
148 | |
149 | |
150 | /** |
151 | * Get the default guest role |
152 | * @return object role |
153 | */ |
154 | function get_guest_role() { |
155 | if ($roles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { |
156 | return array_shift($roles); // Pick the first one |
157 | } else { |
158 | return false; |
159 | } |
160 | } |
161 | |
162 | |
bbbf2d40 |
163 | /** |
164 | * This functions get all the course categories in proper order |
0468976c |
165 | * @param int $context |
bbbf2d40 |
166 | * @param int $type |
167 | * @return array of contextids |
168 | */ |
0468976c |
169 | function get_parent_cats($context, $type) { |
eef868d1 |
170 | |
98882637 |
171 | $parents = array(); |
eef868d1 |
172 | |
c5ddc3fd |
173 | switch ($type) { |
98882637 |
174 | case CONTEXT_COURSECAT: |
c5ddc3fd |
175 | if (!$cat = get_record('course_categories','id',$context->instanceid)) { |
176 | break; |
177 | } |
178 | |
179 | while (!empty($cat->parent)) { |
180 | if (!$context = get_context_instance(CONTEXT_COURSECAT, $cat->parent)) { |
181 | break; |
182 | } |
98882637 |
183 | $parents[] = $context->id; |
184 | $cat = get_record('course_categories','id',$cat->parent); |
185 | } |
98882637 |
186 | break; |
eef868d1 |
187 | |
98882637 |
188 | case CONTEXT_COURSE: |
c5ddc3fd |
189 | if (!$course = get_record('course', 'id', $context->instanceid)) { |
190 | break; |
191 | } |
192 | if (!$catinstance = get_context_instance(CONTEXT_COURSECAT, $course->category)) { |
193 | break; |
194 | } |
195 | |
98882637 |
196 | $parents[] = $catinstance->id; |
c5ddc3fd |
197 | |
198 | if (!$cat = get_record('course_categories','id',$course->category)) { |
199 | break; |
200 | } |
201 | |
202 | while (!empty($cat->parent)) { |
203 | if (!$context = get_context_instance(CONTEXT_COURSECAT, $cat->parent)) { |
204 | break; |
205 | } |
98882637 |
206 | $parents[] = $context->id; |
207 | $cat = get_record('course_categories','id',$cat->parent); |
208 | } |
209 | break; |
eef868d1 |
210 | |
98882637 |
211 | default: |
212 | break; |
98882637 |
213 | } |
98882637 |
214 | return array_reverse($parents); |
bbbf2d40 |
215 | } |
216 | |
217 | |
cee0901c |
218 | |
0468976c |
219 | /** |
220 | * This function checks for a capability assertion being true. If it isn't |
221 | * then the page is terminated neatly with a standard error message |
222 | * @param string $capability - name of the capability |
223 | * @param object $context - a context object (record from context table) |
224 | * @param integer $userid - a userid number |
d74067e8 |
225 | * @param bool $doanything - if false, ignore do anything |
0468976c |
226 | * @param string $errorstring - an errorstring |
d74067e8 |
227 | * @param string $stringfile - which stringfile to get it from |
0468976c |
228 | */ |
eef868d1 |
229 | function require_capability($capability, $context=NULL, $userid=NULL, $doanything=true, |
d74067e8 |
230 | $errormessage="nopermissions", $stringfile='') { |
a9e1c058 |
231 | |
232 | global $USER; |
233 | |
234 | /// If the current user is not logged in, then make sure they are |
235 | |
236 | if (empty($userid) and empty($USER->id)) { |
aad2ba95 |
237 | if ($context && ($context->contextlevel == CONTEXT_COURSE)) { |
a9e1c058 |
238 | require_login($context->instanceid); |
11ac79ff |
239 | } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) { |
240 | if ($cm = get_record('course_modules','id',$context->instanceid)) { |
241 | require_login($cm->course, true, $cm); |
242 | } else { |
243 | require_login(); |
244 | } |
a9e1c058 |
245 | } else { |
246 | require_login(); |
247 | } |
248 | } |
eef868d1 |
249 | |
a9e1c058 |
250 | /// OK, if they still don't have the capability then print a nice error message |
251 | |
d74067e8 |
252 | if (!has_capability($capability, $context, $userid, $doanything)) { |
0468976c |
253 | $capabilityname = get_capability_string($capability); |
254 | print_error($errormessage, $stringfile, '', $capabilityname); |
255 | } |
256 | } |
257 | |
258 | |
bbbf2d40 |
259 | /** |
260 | * This function returns whether the current user has the capability of performing a function |
261 | * For example, we can do has_capability('mod/forum:replypost',$cm) in forum |
262 | * only one of the 4 (moduleinstance, courseid, site, userid) would be set at 1 time |
263 | * This is a recursive funciton. |
bbbf2d40 |
264 | * @uses $USER |
265 | * @param string $capability - name of the capability |
0468976c |
266 | * @param object $context - a context object (record from context table) |
267 | * @param integer $userid - a userid number |
20aeb4b8 |
268 | * @param bool $doanything - if false, ignore do anything |
bbbf2d40 |
269 | * @return bool |
270 | */ |
d74067e8 |
271 | function has_capability($capability, $context=NULL, $userid=NULL, $doanything=true) { |
bbbf2d40 |
272 | |
20aeb4b8 |
273 | global $USER, $CONTEXT, $CFG; |
bbbf2d40 |
274 | |
a8a7300a |
275 | if (debugging() && !record_exists('capabilities', 'name', $capability)) { |
7f7cacdf |
276 | debugging('Cabability "'.$capability.'" was not found! This should be fixed in code.'); |
a8a7300a |
277 | } |
278 | |
8f8ed475 |
279 | if (empty($userid) && empty($USER->capabilities)) { // Real user, first time here |
280 | if (isloggedin()) { |
281 | load_defaultuser_role(); // All users get this by default |
282 | } else { |
283 | load_notloggedin_role(); // others get this by default |
284 | } |
20aeb4b8 |
285 | } |
286 | |
287 | if ($userid && $userid != $USER->id) { |
9425b25f |
288 | if (empty($USER->id) or ($userid != $USER->id)) { |
289 | $capabilities = load_user_capability($capability, $context, $userid); |
290 | } else { //$USER->id == $userid |
291 | $capabilities = empty($USER->capabilities) ? NULL : $USER->capabilities; |
292 | } |
293 | } else { // no userid |
294 | $capabilities = empty($USER->capabilities) ? NULL : $USER->capabilities; |
98882637 |
295 | } |
9425b25f |
296 | |
0468976c |
297 | if (empty($context)) { // Use default CONTEXT if none specified |
340ea4e8 |
298 | if (empty($CONTEXT)) { |
299 | return false; |
300 | } else { |
301 | $context = $CONTEXT; |
302 | } |
0468976c |
303 | } else { // A context was given to us |
304 | if (empty($CONTEXT)) { |
305 | $CONTEXT = $context; // Store FIRST used context in this global as future default |
306 | } |
340ea4e8 |
307 | } |
bbbf2d40 |
308 | |
20aeb4b8 |
309 | if ($doanything) { |
2d07587b |
310 | |
20aeb4b8 |
311 | // Check site |
2d07587b |
312 | if (empty($USER->switchrole[$context->id])) { // Ignore site setting if switchrole is active |
313 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
314 | if (isset($capabilities[$sitecontext->id]['moodle/site:doanything'])) { |
315 | return (0 < $capabilities[$sitecontext->id]['moodle/site:doanything']); |
316 | } |
20aeb4b8 |
317 | } |
eef868d1 |
318 | |
aad2ba95 |
319 | switch ($context->contextlevel) { |
eef868d1 |
320 | |
20aeb4b8 |
321 | case CONTEXT_COURSECAT: |
322 | // Check parent cats. |
323 | $parentcats = get_parent_cats($context, CONTEXT_COURSECAT); |
324 | foreach ($parentcats as $parentcat) { |
325 | if (isset($capabilities[$parentcat]['moodle/site:doanything'])) { |
326 | return (0 < $capabilities[$parentcat]['moodle/site:doanything']); |
327 | } |
cee0901c |
328 | } |
20aeb4b8 |
329 | break; |
bbbf2d40 |
330 | |
20aeb4b8 |
331 | case CONTEXT_COURSE: |
332 | // Check parent cat. |
333 | $parentcats = get_parent_cats($context, CONTEXT_COURSE); |
98882637 |
334 | |
20aeb4b8 |
335 | foreach ($parentcats as $parentcat) { |
336 | if (isset($capabilities[$parentcat]['do_anything'])) { |
337 | return (0 < $capabilities[$parentcat]['do_anything']); |
338 | } |
9425b25f |
339 | } |
20aeb4b8 |
340 | break; |
bbbf2d40 |
341 | |
20aeb4b8 |
342 | case CONTEXT_GROUP: |
343 | // Find course. |
344 | $group = get_record('groups','id',$context->instanceid); |
345 | $courseinstance = get_context_instance(CONTEXT_COURSE, $group->courseid); |
9425b25f |
346 | |
20aeb4b8 |
347 | $parentcats = get_parent_cats($courseinstance, CONTEXT_COURSE); |
348 | foreach ($parentcats as $parentcat) { |
349 | if (isset($capabilities[$parentcat->id]['do_anything'])) { |
350 | return (0 < $capabilities[$parentcat->id]['do_anything']); |
351 | } |
9425b25f |
352 | } |
9425b25f |
353 | |
20aeb4b8 |
354 | $coursecontext = ''; |
355 | if (isset($capabilities[$courseinstance->id]['do_anything'])) { |
356 | return (0 < $capabilities[$courseinstance->id]['do_anything']); |
357 | } |
9425b25f |
358 | |
20aeb4b8 |
359 | break; |
bbbf2d40 |
360 | |
20aeb4b8 |
361 | case CONTEXT_MODULE: |
362 | // Find course. |
363 | $cm = get_record('course_modules', 'id', $context->instanceid); |
364 | $courseinstance = get_context_instance(CONTEXT_COURSE, $cm->course); |
9425b25f |
365 | |
20aeb4b8 |
366 | if ($parentcats = get_parent_cats($courseinstance, CONTEXT_COURSE)) { |
367 | foreach ($parentcats as $parentcat) { |
368 | if (isset($capabilities[$parentcat]['do_anything'])) { |
369 | return (0 < $capabilities[$parentcat]['do_anything']); |
370 | } |
cee0901c |
371 | } |
9425b25f |
372 | } |
98882637 |
373 | |
20aeb4b8 |
374 | if (isset($capabilities[$courseinstance->id]['do_anything'])) { |
375 | return (0 < $capabilities[$courseinstance->id]['do_anything']); |
376 | } |
bbbf2d40 |
377 | |
20aeb4b8 |
378 | break; |
bbbf2d40 |
379 | |
20aeb4b8 |
380 | case CONTEXT_BLOCK: |
381 | // 1 to 1 to course. |
382 | // Find course. |
383 | $block = get_record('block_instance','id',$context->instanceid); |
384 | $courseinstance = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check |
9425b25f |
385 | |
20aeb4b8 |
386 | $parentcats = get_parent_cats($courseinstance, CONTEXT_COURSE); |
387 | foreach ($parentcats as $parentcat) { |
388 | if (isset($capabilities[$parentcat]['do_anything'])) { |
389 | return (0 < $capabilities[$parentcat]['do_anything']); |
390 | } |
cee0901c |
391 | } |
9425b25f |
392 | |
20aeb4b8 |
393 | if (isset($capabilities[$courseinstance->id]['do_anything'])) { |
394 | return (0 < $capabilities[$courseinstance->id]['do_anything']); |
395 | } |
396 | break; |
bbbf2d40 |
397 | |
20aeb4b8 |
398 | default: |
4b10f08b |
399 | // CONTEXT_SYSTEM: CONTEXT_PERSONAL: CONTEXT_USER: |
20aeb4b8 |
400 | // Do nothing. |
401 | break; |
402 | } |
bbbf2d40 |
403 | |
20aeb4b8 |
404 | // Last: check self. |
405 | if (isset($capabilities[$context->id]['do_anything'])) { |
406 | return (0 < $capabilities[$context->id]['do_anything']); |
407 | } |
98882637 |
408 | } |
98882637 |
409 | // do_anything has not been set, we now look for it the normal way. |
9425b25f |
410 | return (0 < capability_search($capability, $context, $capabilities)); |
bbbf2d40 |
411 | |
9425b25f |
412 | } |
bbbf2d40 |
413 | |
414 | |
415 | /** |
416 | * In a separate function so that we won't have to deal with do_anything. |
417 | * again. Used by function has_capability. |
418 | * @param $capability - capability string |
0468976c |
419 | * @param $context - the context object |
bbbf2d40 |
420 | * @param $capabilities - either $USER->capability or loaded array |
421 | * @return permission (int) |
422 | */ |
0468976c |
423 | function capability_search($capability, $context, $capabilities) { |
759ac72d |
424 | |
bbbf2d40 |
425 | global $USER, $CFG; |
0468976c |
426 | |
11ac79ff |
427 | if (!isset($context->id)) { |
428 | return 0; |
429 | } |
430 | |
0468976c |
431 | if (isset($capabilities[$context->id][$capability])) { |
7eb0b60a |
432 | debugging("Found $capability in context $context->id at level $context->contextlevel: ".$capabilities[$context->id][$capability], DEBUG_DEVELOPER); |
0468976c |
433 | return ($capabilities[$context->id][$capability]); |
bbbf2d40 |
434 | } |
9425b25f |
435 | |
bbbf2d40 |
436 | /* Then, we check the cache recursively */ |
9425b25f |
437 | $permission = 0; |
438 | |
aad2ba95 |
439 | switch ($context->contextlevel) { |
bbbf2d40 |
440 | |
441 | case CONTEXT_SYSTEM: // by now it's a definite an inherit |
442 | $permission = 0; |
443 | break; |
444 | |
445 | case CONTEXT_PERSONAL: |
0468976c |
446 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
447 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
448 | break; |
9425b25f |
449 | |
4b10f08b |
450 | case CONTEXT_USER: |
0468976c |
451 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
452 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
453 | break; |
9425b25f |
454 | |
bbbf2d40 |
455 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
456 | $coursecat = get_record('course_categories','id',$context->instanceid); |
0468976c |
457 | if (!empty($coursecat->parent)) { // return parent value if it exists |
458 | $parentcontext = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
bbbf2d40 |
459 | } else { // else return site value |
0468976c |
460 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
bbbf2d40 |
461 | } |
0468976c |
462 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
463 | break; |
464 | |
465 | case CONTEXT_COURSE: // 1 to 1 to course cat |
466 | // find the course cat, and return its value |
467 | $course = get_record('course','id',$context->instanceid); |
0468976c |
468 | $parentcontext = get_context_instance(CONTEXT_COURSECAT, $course->category); |
469 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
470 | break; |
471 | |
472 | case CONTEXT_GROUP: // 1 to 1 to course |
473 | $group = get_record('groups','id',$context->instanceid); |
0468976c |
474 | $parentcontext = get_context_instance(CONTEXT_COURSE, $group->courseid); |
475 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
476 | break; |
477 | |
478 | case CONTEXT_MODULE: // 1 to 1 to course |
479 | $cm = get_record('course_modules','id',$context->instanceid); |
0468976c |
480 | $parentcontext = get_context_instance(CONTEXT_COURSE, $cm->course); |
481 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
482 | break; |
483 | |
484 | case CONTEXT_BLOCK: // 1 to 1 to course |
485 | $block = get_record('block_instance','id',$context->instanceid); |
0468976c |
486 | $parentcontext = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check |
487 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
488 | break; |
489 | |
490 | default: |
491 | error ('This is an unknown context!'); |
492 | return false; |
493 | } |
7eb0b60a |
494 | debugging("Found $capability recursively from context $context->id at level $context->contextlevel: $permission", DEBUG_DEVELOPER); |
9425b25f |
495 | |
98882637 |
496 | return $permission; |
bbbf2d40 |
497 | } |
498 | |
499 | |
500 | /** |
501 | * This function should be called immediately after a login, when $USER is set. |
502 | * It will build an array of all the capabilities at each level |
503 | * i.e. site/metacourse/course_category/course/moduleinstance |
504 | * Note we should only load capabilities if they are explicitly assigned already, |
505 | * we should not load all module's capability! |
506 | * @param $userid - the id of the user whose capabilities we want to load |
507 | * @return array |
508 | * possible just s simple 2D array with [contextid][capabilityname] |
509 | * [Capabilities] => [26][forum_post] = 1 |
510 | * [26][forum_start] = -8990 |
511 | * [26][forum_edit] = -1 |
512 | * [273][blah blah] = 1 |
513 | * [273][blah blah blah] = 2 |
514 | */ |
0468976c |
515 | function load_user_capability($capability='', $context ='', $userid='') { |
d140ad3f |
516 | |
98882637 |
517 | global $USER, $CFG; |
bbbf2d40 |
518 | |
519 | if (empty($userid)) { |
dc411d1b |
520 | if (empty($USER->id)) { // We have no user to get capabilities for |
64026e8c |
521 | debugging('User not logged in for load_user_capability!'); |
dc411d1b |
522 | return false; |
523 | } |
64026e8c |
524 | unset($USER->capabilities); // We don't want possible older capabilites hanging around |
525 | |
526 | check_enrolment_plugins($USER); // Call "enrol" system to ensure that we have the correct picture |
8f8ed475 |
527 | |
bbbf2d40 |
528 | $userid = $USER->id; |
dc411d1b |
529 | $otheruserid = false; |
bbbf2d40 |
530 | } else { |
64026e8c |
531 | if (!$user = get_record('user', 'id', $userid)) { |
532 | debugging('Non-existent userid in load_user_capability!'); |
533 | return false; |
534 | } |
535 | |
536 | check_enrolment_plugins($user); // Ensure that we have the correct picture |
537 | |
9425b25f |
538 | $otheruserid = $userid; |
bbbf2d40 |
539 | } |
9425b25f |
540 | |
5f70bcc3 |
541 | |
542 | /// First we generate a list of all relevant contexts of the user |
543 | |
544 | $usercontexts = array(); |
bbbf2d40 |
545 | |
0468976c |
546 | if ($context) { // if context is specified |
eef868d1 |
547 | $usercontexts = get_parent_contexts($context); |
98882637 |
548 | } else { // else, we load everything |
5f70bcc3 |
549 | if ($userroles = get_records('role_assignments','userid',$userid)) { |
550 | foreach ($userroles as $userrole) { |
551 | $usercontexts[] = $userrole->contextid; |
552 | } |
98882637 |
553 | } |
5f70bcc3 |
554 | } |
555 | |
556 | /// Set up SQL fragments for searching contexts |
557 | |
558 | if ($usercontexts) { |
0468976c |
559 | $listofcontexts = '('.implode(',', $usercontexts).')'; |
5f70bcc3 |
560 | $searchcontexts1 = "c1.id IN $listofcontexts AND"; |
5f70bcc3 |
561 | } else { |
c76e095f |
562 | $searchcontexts1 = ''; |
bbbf2d40 |
563 | } |
c76e095f |
564 | |
64026e8c |
565 | if ($capability) { |
566 | $capsearch = " AND rc.capability = '$capability' "; |
567 | } else { |
eef868d1 |
568 | $capsearch =""; |
64026e8c |
569 | } |
570 | |
e38f38c3 |
571 | /// Set up SQL fragments for timestart, timeend etc |
572 | $now = time(); |
85f101fa |
573 | $timesql = "AND ((ra.timestart = 0 OR ra.timestart < $now) AND (ra.timeend = 0 OR ra.timeend > $now))"; |
e38f38c3 |
574 | |
5f70bcc3 |
575 | /// Then we use 1 giant SQL to bring out all relevant capabilities. |
576 | /// The first part gets the capabilities of orginal role. |
577 | /// The second part gets the capabilities of overriden roles. |
bbbf2d40 |
578 | |
98882637 |
579 | $siteinstance = get_context_instance(CONTEXT_SYSTEM, SITEID); |
bbbf2d40 |
580 | |
c76e095f |
581 | $SQL = " SELECT rc.capability, c1.id, (c1.contextlevel * 100) AS aggrlevel, |
bbbf2d40 |
582 | SUM(rc.permission) AS sum |
583 | FROM |
eef868d1 |
584 | {$CFG->prefix}role_assignments ra, |
42ac3ecf |
585 | {$CFG->prefix}role_capabilities rc, |
586 | {$CFG->prefix}context c1 |
bbbf2d40 |
587 | WHERE |
d4649c76 |
588 | ra.contextid=c1.id AND |
589 | ra.roleid=rc.roleid AND |
bbbf2d40 |
590 | ra.userid=$userid AND |
5f70bcc3 |
591 | $searchcontexts1 |
eef868d1 |
592 | rc.contextid=$siteinstance->id |
98882637 |
593 | $capsearch |
e38f38c3 |
594 | $timesql |
bbbf2d40 |
595 | GROUP BY |
aad2ba95 |
596 | rc.capability, (c1.contextlevel * 100), c1.id |
bbbf2d40 |
597 | HAVING |
41811960 |
598 | SUM(rc.permission) != 0 |
bbbf2d40 |
599 | UNION |
600 | |
c76e095f |
601 | SELECT rc.capability, c2.id, (c1.contextlevel * 100 + c2.contextlevel) AS aggrlevel, |
bbbf2d40 |
602 | SUM(rc.permission) AS sum |
603 | FROM |
42ac3ecf |
604 | {$CFG->prefix}role_assignments ra, |
605 | {$CFG->prefix}role_capabilities rc, |
606 | {$CFG->prefix}context c1, |
607 | {$CFG->prefix}context c2 |
bbbf2d40 |
608 | WHERE |
d4649c76 |
609 | ra.contextid=c1.id AND |
eef868d1 |
610 | ra.roleid=rc.roleid AND |
611 | ra.userid=$userid AND |
612 | rc.contextid=c2.id AND |
5f70bcc3 |
613 | $searchcontexts1 |
5f70bcc3 |
614 | rc.contextid != $siteinstance->id |
bbbf2d40 |
615 | $capsearch |
e38f38c3 |
616 | $timesql |
eef868d1 |
617 | |
bbbf2d40 |
618 | GROUP BY |
c76e095f |
619 | rc.capability, (c1.contextlevel * 100 + c2.contextlevel), c2.id, c1.id |
bbbf2d40 |
620 | HAVING |
41811960 |
621 | SUM(rc.permission) != 0 |
bbbf2d40 |
622 | ORDER BY |
75e84883 |
623 | aggrlevel ASC |
bbbf2d40 |
624 | "; |
625 | |
98882637 |
626 | $capabilities = array(); // Reinitialize. |
75e84883 |
627 | if (!$rs = get_recordset_sql($SQL)) { |
628 | error("Query failed in load_user_capability."); |
629 | } |
5cf38a57 |
630 | |
bbbf2d40 |
631 | if ($rs && $rs->RecordCount() > 0) { |
632 | while (!$rs->EOF) { |
75e84883 |
633 | $array = $rs->fields; |
634 | $temprecord = new object; |
eef868d1 |
635 | |
98882637 |
636 | foreach ($array as $key=>$val) { |
75e84883 |
637 | if ($key == 'aggrlevel') { |
aad2ba95 |
638 | $temprecord->contextlevel = $val; |
75e84883 |
639 | } else { |
640 | $temprecord->{$key} = $val; |
641 | } |
98882637 |
642 | } |
bbbf2d40 |
643 | $capabilities[] = $temprecord; |
644 | $rs->MoveNext(); |
645 | } |
646 | } |
eef868d1 |
647 | |
bbbf2d40 |
648 | /* so up to this point we should have somethign like this |
aad2ba95 |
649 | * $capabilities[1] ->contextlevel = 1000 |
bbbf2d40 |
650 | ->module = SITEID |
651 | ->capability = do_anything |
652 | ->id = 1 (id is the context id) |
653 | ->sum = 0 |
eef868d1 |
654 | |
aad2ba95 |
655 | * $capabilities[2] ->contextlevel = 1000 |
bbbf2d40 |
656 | ->module = SITEID |
657 | ->capability = post_messages |
658 | ->id = 1 |
659 | ->sum = -9000 |
660 | |
aad2ba95 |
661 | * $capabilittes[3] ->contextlevel = 3000 |
bbbf2d40 |
662 | ->module = course |
663 | ->capability = view_course_activities |
664 | ->id = 25 |
665 | ->sum = 1 |
666 | |
aad2ba95 |
667 | * $capabilittes[4] ->contextlevel = 3000 |
bbbf2d40 |
668 | ->module = course |
669 | ->capability = view_course_activities |
670 | ->id = 26 |
671 | ->sum = 0 (this is another course) |
eef868d1 |
672 | |
aad2ba95 |
673 | * $capabilities[5] ->contextlevel = 3050 |
bbbf2d40 |
674 | ->module = course |
675 | ->capability = view_course_activities |
676 | ->id = 25 (override in course 25) |
677 | ->sum = -1 |
678 | * .... |
679 | * now we proceed to write the session array, going from top to bottom |
680 | * at anypoint, we need to go up and check parent to look for prohibit |
681 | */ |
682 | // print_object($capabilities); |
683 | |
684 | /* This is where we write to the actualy capabilities array |
685 | * what we need to do from here on is |
686 | * going down the array from lowest level to highest level |
687 | * 1) recursively check for prohibit, |
688 | * if any, we write prohibit |
689 | * else, we write the value |
690 | * 2) at an override level, we overwrite current level |
691 | * if it's not set to prohibit already, and if different |
692 | * ........ that should be it ........ |
693 | */ |
98882637 |
694 | $usercap = array(); // for other user's capabilities |
bbbf2d40 |
695 | foreach ($capabilities as $capability) { |
696 | |
0468976c |
697 | $context = get_context_instance_by_id($capability->id); |
698 | |
41811960 |
699 | if (!empty($otheruserid)) { // we are pulling out other user's capabilities, do not write to session |
eef868d1 |
700 | |
0468976c |
701 | if (capability_prohibits($capability->capability, $context, $capability->sum, $usercap)) { |
98882637 |
702 | $usercap[$capability->id][$capability->capability] = -9000; |
703 | continue; |
704 | } |
705 | |
eef868d1 |
706 | $usercap[$capability->id][$capability->capability] = $capability->sum; |
707 | |
98882637 |
708 | } else { |
709 | |
0468976c |
710 | if (capability_prohibits($capability->capability, $context, $capability->sum)) { // if any parent or parent's parent is set to prohibit |
98882637 |
711 | $USER->capabilities[$capability->id][$capability->capability] = -9000; |
712 | continue; |
713 | } |
eef868d1 |
714 | |
98882637 |
715 | // if no parental prohibit set |
716 | // just write to session, i am not sure this is correct yet |
717 | // since 3050 shows up after 3000, and 3070 shows up after 3050, |
718 | // it should be ok just to overwrite like this, provided that there's no |
719 | // parental prohibits |
720 | // no point writing 0, since 0 = inherit |
721 | // we need to write even if it's 0, because it could be an inherit override |
722 | $USER->capabilities[$capability->id][$capability->capability] = $capability->sum; |
723 | } |
bbbf2d40 |
724 | } |
eef868d1 |
725 | |
bbbf2d40 |
726 | // now we don't care about the huge array anymore, we can dispose it. |
727 | unset($capabilities); |
eef868d1 |
728 | |
dbe7e582 |
729 | if (!empty($otheruserid)) { |
eef868d1 |
730 | return $usercap; // return the array |
bbbf2d40 |
731 | } |
732 | // see array in session to see what it looks like |
733 | |
734 | } |
735 | |
64026e8c |
736 | /* |
737 | * Check all the login enrolment information for the given user object |
eef868d1 |
738 | * by querying the enrolment plugins |
64026e8c |
739 | */ |
740 | function check_enrolment_plugins(&$user) { |
741 | global $CFG; |
742 | |
e4ec4e41 |
743 | static $inprogress; // To prevent this function being called more than once in an invocation |
744 | |
218eb651 |
745 | if (!empty($inprogress[$user->id])) { |
e4ec4e41 |
746 | return; |
747 | } |
748 | |
218eb651 |
749 | $inprogress[$user->id] = true; // Set the flag |
e4ec4e41 |
750 | |
64026e8c |
751 | require_once($CFG->dirroot .'/enrol/enrol.class.php'); |
eef868d1 |
752 | |
64026e8c |
753 | if (!($plugins = explode(',', $CFG->enrol_plugins_enabled))) { |
754 | $plugins = array($CFG->enrol); |
755 | } |
756 | |
757 | foreach ($plugins as $plugin) { |
758 | $enrol = enrolment_factory::factory($plugin); |
759 | if (method_exists($enrol, 'setup_enrolments')) { /// Plugin supports Roles (Moodle 1.7 and later) |
760 | $enrol->setup_enrolments($user); |
761 | } else { /// Run legacy enrolment methods |
762 | if (method_exists($enrol, 'get_student_courses')) { |
763 | $enrol->get_student_courses($user); |
764 | } |
765 | if (method_exists($enrol, 'get_teacher_courses')) { |
766 | $enrol->get_teacher_courses($user); |
767 | } |
768 | |
769 | /// deal with $user->students and $user->teachers stuff |
770 | unset($user->student); |
771 | unset($user->teacher); |
772 | } |
773 | unset($enrol); |
774 | } |
e4ec4e41 |
775 | |
218eb651 |
776 | unset($inprogress[$user->id]); // Unset the flag |
64026e8c |
777 | } |
778 | |
bbbf2d40 |
779 | |
780 | /** |
781 | * This is a recursive function that checks whether the capability in this |
782 | * context, or the parent capabilities are set to prohibit. |
783 | * |
784 | * At this point, we can probably just use the values already set in the |
785 | * session variable, since we are going down the level. Any prohit set in |
786 | * parents would already reflect in the session. |
787 | * |
788 | * @param $capability - capability name |
789 | * @param $sum - sum of all capabilities values |
0468976c |
790 | * @param $context - the context object |
bbbf2d40 |
791 | * @param $array - when loading another user caps, their caps are not stored in session but an array |
792 | */ |
0468976c |
793 | function capability_prohibits($capability, $context, $sum='', $array='') { |
bbbf2d40 |
794 | global $USER; |
0468976c |
795 | |
bbbf2d40 |
796 | if ($sum < -8000) { |
797 | // If this capability is set to prohibit. |
798 | return true; |
799 | } |
eef868d1 |
800 | |
bbbf2d40 |
801 | if (isset($array)) { |
eef868d1 |
802 | if (isset($array[$context->id][$capability]) |
0468976c |
803 | && $array[$context->id][$capability] < -8000) { |
98882637 |
804 | return true; |
eef868d1 |
805 | } |
bbbf2d40 |
806 | } else { |
98882637 |
807 | // Else if set in session. |
eef868d1 |
808 | if (isset($USER->capabilities[$context->id][$capability]) |
0468976c |
809 | && $USER->capabilities[$context->id][$capability] < -8000) { |
98882637 |
810 | return true; |
811 | } |
bbbf2d40 |
812 | } |
aad2ba95 |
813 | switch ($context->contextlevel) { |
eef868d1 |
814 | |
bbbf2d40 |
815 | case CONTEXT_SYSTEM: |
816 | // By now it's a definite an inherit. |
817 | return 0; |
818 | break; |
819 | |
820 | case CONTEXT_PERSONAL: |
821 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
0468976c |
822 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
823 | break; |
824 | |
4b10f08b |
825 | case CONTEXT_USER: |
bbbf2d40 |
826 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
0468976c |
827 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
828 | break; |
829 | |
830 | case CONTEXT_COURSECAT: |
831 | // Coursecat -> coursecat or site. |
832 | $coursecat = get_record('course_categories','id',$context->instanceid); |
41811960 |
833 | if (!empty($coursecat->parent)) { |
bbbf2d40 |
834 | // return parent value if exist. |
835 | $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
836 | } else { |
837 | // Return site value. |
838 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
839 | } |
0468976c |
840 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
841 | break; |
842 | |
843 | case CONTEXT_COURSE: |
844 | // 1 to 1 to course cat. |
845 | // Find the course cat, and return its value. |
846 | $course = get_record('course','id',$context->instanceid); |
847 | $parent = get_context_instance(CONTEXT_COURSECAT, $course->category); |
0468976c |
848 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
849 | break; |
850 | |
851 | case CONTEXT_GROUP: |
852 | // 1 to 1 to course. |
853 | $group = get_record('groups','id',$context->instanceid); |
854 | $parent = get_context_instance(CONTEXT_COURSE, $group->courseid); |
0468976c |
855 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
856 | break; |
857 | |
858 | case CONTEXT_MODULE: |
859 | // 1 to 1 to course. |
860 | $cm = get_record('course_modules','id',$context->instanceid); |
861 | $parent = get_context_instance(CONTEXT_COURSE, $cm->course); |
0468976c |
862 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
863 | break; |
864 | |
865 | case CONTEXT_BLOCK: |
866 | // 1 to 1 to course. |
867 | $block = get_record('block_instance','id',$context->instanceid); |
868 | $parent = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check |
0468976c |
869 | return capability_prohibits($capability, $parent); |
bbbf2d40 |
870 | break; |
871 | |
872 | default: |
873 | error ('This is an unknown context!'); |
874 | return false; |
875 | } |
876 | } |
877 | |
878 | |
879 | /** |
880 | * A print form function. This should either grab all the capabilities from |
881 | * files or a central table for that particular module instance, then present |
882 | * them in check boxes. Only relevant capabilities should print for known |
883 | * context. |
884 | * @param $mod - module id of the mod |
885 | */ |
886 | function print_capabilities($modid=0) { |
887 | global $CFG; |
eef868d1 |
888 | |
bbbf2d40 |
889 | $capabilities = array(); |
890 | |
891 | if ($modid) { |
892 | // We are in a module specific context. |
893 | |
894 | // Get the mod's name. |
895 | // Call the function that grabs the file and parse. |
896 | $cm = get_record('course_modules', 'id', $modid); |
897 | $module = get_record('modules', 'id', $cm->module); |
eef868d1 |
898 | |
bbbf2d40 |
899 | } else { |
900 | // Print all capabilities. |
901 | foreach ($capabilities as $capability) { |
902 | // Prints the check box component. |
903 | } |
904 | } |
905 | } |
906 | |
907 | |
908 | /** |
1afecc03 |
909 | * Installs the roles system. |
910 | * This function runs on a fresh install as well as on an upgrade from the old |
911 | * hard-coded student/teacher/admin etc. roles to the new roles system. |
bbbf2d40 |
912 | */ |
1afecc03 |
913 | function moodle_install_roles() { |
bbbf2d40 |
914 | |
1afecc03 |
915 | global $CFG, $db; |
eef868d1 |
916 | |
bbbf2d40 |
917 | // Create a system wide context for assignemnt. |
918 | $systemcontext = $context = get_context_instance(CONTEXT_SYSTEM, SITEID); |
919 | |
1afecc03 |
920 | |
921 | // Create default/legacy roles and capabilities. |
922 | // (1 legacy capability per legacy role at system level). |
eef868d1 |
923 | $adminrole = create_role(get_string('administrator'), 'admin', get_string('administratordescription'), 'moodle/legacy:admin'); |
98882637 |
924 | if (!assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $systemcontext->id)) { |
bbbf2d40 |
925 | error('Could not assign moodle/site:doanything to the admin role'); |
926 | } |
eef868d1 |
927 | $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator'); |
928 | $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher'); |
929 | $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'moodle/legacy:teacher'); |
31f26796 |
930 | $studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'moodle/legacy:student'); |
931 | $guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'moodle/legacy:guest'); |
1afecc03 |
932 | |
933 | |
934 | // Look inside user_admin, user_creator, user_teachers, user_students and |
935 | // assign above new roles. If a user has both teacher and student role, |
936 | // only teacher role is assigned. The assignment should be system level. |
937 | $dbtables = $db->MetaTables('TABLES'); |
eef868d1 |
938 | |
1afecc03 |
939 | |
98882637 |
940 | /** |
bbbf2d40 |
941 | * Upgrade the admins. |
1afecc03 |
942 | * Sort using id ASC, first one is primary admin. |
bbbf2d40 |
943 | */ |
1afecc03 |
944 | if (in_array($CFG->prefix.'user_admins', $dbtables)) { |
f1dcf000 |
945 | if ($rs = get_recordset_sql('SELECT * from '.$CFG->prefix.'user_admins ORDER BY ID ASC')) { |
946 | while (! $rs->EOF) { |
947 | $admin = $rs->FetchObj(); |
1afecc03 |
948 | role_assign($adminrole, $admin->userid, 0, $systemcontext->id); |
f1dcf000 |
949 | $rs->MoveNext(); |
1afecc03 |
950 | } |
951 | } |
952 | } else { |
953 | // This is a fresh install. |
bbbf2d40 |
954 | } |
1afecc03 |
955 | |
956 | |
bbbf2d40 |
957 | /** |
958 | * Upgrade course creators. |
959 | */ |
1afecc03 |
960 | if (in_array($CFG->prefix.'user_coursecreators', $dbtables)) { |
f1dcf000 |
961 | if ($rs = get_recordset('user_coursecreators')) { |
962 | while (! $rs->EOF) { |
963 | $coursecreator = $rs->FetchObj(); |
56b4d70d |
964 | role_assign($coursecreatorrole, $coursecreator->userid, 0, $systemcontext->id); |
f1dcf000 |
965 | $rs->MoveNext(); |
1afecc03 |
966 | } |
967 | } |
bbbf2d40 |
968 | } |
969 | |
1afecc03 |
970 | |
bbbf2d40 |
971 | /** |
972 | * Upgrade editting teachers and non-editting teachers. |
973 | */ |
1afecc03 |
974 | if (in_array($CFG->prefix.'user_teachers', $dbtables)) { |
f1dcf000 |
975 | if ($rs = get_recordset('user_teachers')) { |
976 | while (! $rs->EOF) { |
977 | $teacher = $rs->FetchObj(); |
978 | |
17d6a25e |
979 | // populate the user_lastaccess table |
ece4945b |
980 | $access = new object(); |
17d6a25e |
981 | $access->timeaccess = $teacher->timeaccess; |
982 | $access->userid = $teacher->userid; |
983 | $access->courseid = $teacher->course; |
984 | insert_record('user_lastaccess', $access); |
f1dcf000 |
985 | |
17d6a25e |
986 | // assign the default student role |
1afecc03 |
987 | $coursecontext = get_context_instance(CONTEXT_COURSE, $teacher->course); // needs cache |
988 | if ($teacher->editall) { // editting teacher |
989 | role_assign($editteacherrole, $teacher->userid, 0, $coursecontext->id); |
990 | } else { |
991 | role_assign($noneditteacherrole, $teacher->userid, 0, $coursecontext->id); |
992 | } |
f1dcf000 |
993 | |
994 | $rs->MoveNext(); |
1afecc03 |
995 | } |
bbbf2d40 |
996 | } |
997 | } |
1afecc03 |
998 | |
999 | |
bbbf2d40 |
1000 | /** |
1001 | * Upgrade students. |
1002 | */ |
1afecc03 |
1003 | if (in_array($CFG->prefix.'user_students', $dbtables)) { |
f1dcf000 |
1004 | if ($rs = get_recordset('user_students')) { |
1005 | while (! $rs->EOF) { |
1006 | $student = $rs->FetchObj(); |
1007 | |
17d6a25e |
1008 | // populate the user_lastaccess table |
f1dcf000 |
1009 | $access = new object; |
17d6a25e |
1010 | $access->timeaccess = $student->timeaccess; |
1011 | $access->userid = $student->userid; |
1012 | $access->courseid = $student->course; |
1013 | insert_record('user_lastaccess', $access); |
f1dcf000 |
1014 | |
17d6a25e |
1015 | // assign the default student role |
1afecc03 |
1016 | $coursecontext = get_context_instance(CONTEXT_COURSE, $student->course); |
1017 | role_assign($studentrole, $student->userid, 0, $coursecontext->id); |
f1dcf000 |
1018 | |
1019 | $rs->MoveNext(); |
1afecc03 |
1020 | } |
1021 | } |
bbbf2d40 |
1022 | } |
1afecc03 |
1023 | |
1024 | |
bbbf2d40 |
1025 | /** |
1026 | * Upgrade guest (only 1 entry). |
1027 | */ |
1afecc03 |
1028 | if ($guestuser = get_record('user', 'username', 'guest')) { |
1029 | role_assign($guestrole, $guestuser->id, 0, $systemcontext->id); |
1030 | } |
1031 | |
945f88ca |
1032 | /** |
eef868d1 |
1033 | * Insert the correct records for legacy roles |
945f88ca |
1034 | */ |
1035 | allow_assign($adminrole, $adminrole); |
1036 | allow_assign($adminrole, $coursecreatorrole); |
1037 | allow_assign($adminrole, $noneditteacherrole); |
eef868d1 |
1038 | allow_assign($adminrole, $editteacherrole); |
945f88ca |
1039 | allow_assign($adminrole, $studentrole); |
1040 | allow_assign($adminrole, $guestrole); |
eef868d1 |
1041 | |
945f88ca |
1042 | allow_assign($coursecreatorrole, $noneditteacherrole); |
1043 | allow_assign($coursecreatorrole, $editteacherrole); |
eef868d1 |
1044 | allow_assign($coursecreatorrole, $studentrole); |
945f88ca |
1045 | allow_assign($coursecreatorrole, $guestrole); |
eef868d1 |
1046 | |
1047 | allow_assign($editteacherrole, $noneditteacherrole); |
1048 | allow_assign($editteacherrole, $studentrole); |
945f88ca |
1049 | allow_assign($editteacherrole, $guestrole); |
eef868d1 |
1050 | |
945f88ca |
1051 | /// overrides |
1052 | allow_override($adminrole, $adminrole); |
1053 | allow_override($adminrole, $coursecreatorrole); |
1054 | allow_override($adminrole, $noneditteacherrole); |
eef868d1 |
1055 | allow_override($adminrole, $editteacherrole); |
945f88ca |
1056 | allow_override($adminrole, $studentrole); |
eef868d1 |
1057 | allow_override($adminrole, $guestrole); |
1afecc03 |
1058 | |
746a04c5 |
1059 | |
1afecc03 |
1060 | // Should we delete the tables after we are done? Not yet. |
bbbf2d40 |
1061 | } |
1062 | |
bbbf2d40 |
1063 | /** |
1064 | * Assign the defaults found in this capabality definition to roles that have |
1065 | * the corresponding legacy capabilities assigned to them. |
1066 | * @param $legacyperms - an array in the format (example): |
1067 | * 'guest' => CAP_PREVENT, |
1068 | * 'student' => CAP_ALLOW, |
1069 | * 'teacher' => CAP_ALLOW, |
1070 | * 'editingteacher' => CAP_ALLOW, |
1071 | * 'coursecreator' => CAP_ALLOW, |
1072 | * 'admin' => CAP_ALLOW |
1073 | * @return boolean - success or failure. |
1074 | */ |
1075 | function assign_legacy_capabilities($capability, $legacyperms) { |
eef868d1 |
1076 | |
bbbf2d40 |
1077 | foreach ($legacyperms as $type => $perm) { |
eef868d1 |
1078 | |
bbbf2d40 |
1079 | $systemcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
eef868d1 |
1080 | |
bbbf2d40 |
1081 | // The legacy capabilities are: |
1082 | // 'moodle/legacy:guest' |
1083 | // 'moodle/legacy:student' |
1084 | // 'moodle/legacy:teacher' |
1085 | // 'moodle/legacy:editingteacher' |
1086 | // 'moodle/legacy:coursecreator' |
1087 | // 'moodle/legacy:admin' |
eef868d1 |
1088 | |
2e85fffe |
1089 | if ($roles = get_roles_with_capability('moodle/legacy:'.$type, CAP_ALLOW)) { |
1090 | foreach ($roles as $role) { |
1091 | // Assign a site level capability. |
1092 | if (!assign_capability($capability, $perm, $role->id, $systemcontext->id)) { |
1093 | return false; |
1094 | } |
bbbf2d40 |
1095 | } |
1096 | } |
1097 | } |
1098 | return true; |
1099 | } |
1100 | |
1101 | |
cee0901c |
1102 | /** |
1103 | * Checks to see if a capability is a legacy capability. |
1104 | * @param $capabilityname |
1105 | * @return boolean |
1106 | */ |
bbbf2d40 |
1107 | function islegacy($capabilityname) { |
98882637 |
1108 | if (strstr($capabilityname, 'legacy') === false) { |
eef868d1 |
1109 | return false; |
98882637 |
1110 | } else { |
eef868d1 |
1111 | return true; |
98882637 |
1112 | } |
bbbf2d40 |
1113 | } |
1114 | |
cee0901c |
1115 | |
1116 | |
1117 | /********************************** |
bbbf2d40 |
1118 | * Context Manipulation functions * |
1119 | **********************************/ |
1120 | |
bbbf2d40 |
1121 | /** |
9991d157 |
1122 | * Create a new context record for use by all roles-related stuff |
bbbf2d40 |
1123 | * @param $level |
1124 | * @param $instanceid |
1125 | */ |
aad2ba95 |
1126 | function create_context($contextlevel, $instanceid) { |
1127 | if (!get_record('context','contextlevel',$contextlevel,'instanceid',$instanceid)) { |
bbbf2d40 |
1128 | $context = new object; |
aad2ba95 |
1129 | $context->contextlevel = $contextlevel; |
bbbf2d40 |
1130 | $context->instanceid = $instanceid; |
1131 | return insert_record('context',$context); |
1132 | } |
1133 | } |
1134 | |
9991d157 |
1135 | /** |
1136 | * Create a new context record for use by all roles-related stuff |
1137 | * @param $level |
1138 | * @param $instanceid |
1139 | */ |
1140 | function delete_context($contextlevel, $instanceid) { |
1141 | if ($context = get_context_instance($contextlevel, $instanceid)) { |
1142 | return delete_records('context', 'id', $context->id) && |
1143 | delete_records('role_assignments', 'contextid', $context->id) && |
1144 | delete_records('role_role_capabilities', 'contextid', $context->id); |
1145 | } |
1146 | return true; |
1147 | } |
1148 | |
bbbf2d40 |
1149 | |
1150 | /** |
1151 | * Get the context instance as an object. This function will create the |
1152 | * context instance if it does not exist yet. |
1153 | * @param $level |
1154 | * @param $instance |
1155 | */ |
aad2ba95 |
1156 | function get_context_instance($contextlevel=NULL, $instance=SITEID) { |
e5605780 |
1157 | |
51195e6f |
1158 | global $context_cache, $context_cache_id, $CONTEXT; |
d9a35e12 |
1159 | |
340ea4e8 |
1160 | /// If no level is supplied then return the current global context if there is one |
aad2ba95 |
1161 | if (empty($contextlevel)) { |
340ea4e8 |
1162 | if (empty($CONTEXT)) { |
ea82d6b6 |
1163 | debugging("Error: get_context_instance() called without a context"); |
340ea4e8 |
1164 | } else { |
1165 | return $CONTEXT; |
1166 | } |
e5605780 |
1167 | } |
1168 | |
340ea4e8 |
1169 | /// Check the cache |
aad2ba95 |
1170 | if (isset($context_cache[$contextlevel][$instance])) { // Already cached |
1171 | return $context_cache[$contextlevel][$instance]; |
e5605780 |
1172 | } |
1173 | |
340ea4e8 |
1174 | /// Get it from the database, or create it |
aad2ba95 |
1175 | if (!$context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance)) { |
1176 | create_context($contextlevel, $instance); |
1177 | $context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance); |
e5605780 |
1178 | } |
1179 | |
ccfc5ecc |
1180 | /// Only add to cache if context isn't empty. |
1181 | if (!empty($context)) { |
aad2ba95 |
1182 | $context_cache[$contextlevel][$instance] = $context; // Cache it for later |
ccfc5ecc |
1183 | $context_cache_id[$context->id] = $context; // Cache it for later |
1184 | } |
0468976c |
1185 | |
bbbf2d40 |
1186 | return $context; |
1187 | } |
1188 | |
cee0901c |
1189 | |
340ea4e8 |
1190 | /** |
1191 | * Get a context instance as an object, from a given id. |
1192 | * @param $id |
1193 | */ |
1194 | function get_context_instance_by_id($id) { |
1195 | |
d9a35e12 |
1196 | global $context_cache, $context_cache_id; |
1197 | |
340ea4e8 |
1198 | if (isset($context_cache_id[$id])) { // Already cached |
75e84883 |
1199 | return $context_cache_id[$id]; |
340ea4e8 |
1200 | } |
1201 | |
1202 | if ($context = get_record('context', 'id', $id)) { // Update the cache and return |
aad2ba95 |
1203 | $context_cache[$context->contextlevel][$context->instanceid] = $context; |
340ea4e8 |
1204 | $context_cache_id[$context->id] = $context; |
1205 | return $context; |
1206 | } |
1207 | |
1208 | return false; |
1209 | } |
1210 | |
bbbf2d40 |
1211 | |
8737be58 |
1212 | /** |
1213 | * Get the local override (if any) for a given capability in a role in a context |
1214 | * @param $roleid |
0468976c |
1215 | * @param $contextid |
1216 | * @param $capability |
8737be58 |
1217 | */ |
1218 | function get_local_override($roleid, $contextid, $capability) { |
1219 | return get_record('role_capabilities', 'roleid', $roleid, 'capability', $capability, 'contextid', $contextid); |
1220 | } |
1221 | |
1222 | |
bbbf2d40 |
1223 | |
1224 | /************************************ |
1225 | * DB TABLE RELATED FUNCTIONS * |
1226 | ************************************/ |
1227 | |
cee0901c |
1228 | /** |
bbbf2d40 |
1229 | * function that creates a role |
1230 | * @param name - role name |
31f26796 |
1231 | * @param shortname - role short name |
bbbf2d40 |
1232 | * @param description - role description |
1233 | * @param legacy - optional legacy capability |
1234 | * @return id or false |
1235 | */ |
8420bee9 |
1236 | function create_role($name, $shortname, $description, $legacy='') { |
eef868d1 |
1237 | |
98882637 |
1238 | // check for duplicate role name |
eef868d1 |
1239 | |
98882637 |
1240 | if ($role = get_record('role','name', $name)) { |
eef868d1 |
1241 | error('there is already a role with this name!'); |
98882637 |
1242 | } |
eef868d1 |
1243 | |
31f26796 |
1244 | if ($role = get_record('role','shortname', $shortname)) { |
eef868d1 |
1245 | error('there is already a role with this shortname!'); |
31f26796 |
1246 | } |
1247 | |
b5959f30 |
1248 | $role = new object(); |
98882637 |
1249 | $role->name = $name; |
31f26796 |
1250 | $role->shortname = $shortname; |
98882637 |
1251 | $role->description = $description; |
eef868d1 |
1252 | |
8420bee9 |
1253 | //find free sortorder number |
1254 | $role->sortorder = count_records('role'); |
1255 | while (get_record('role','sortorder', $role->sortorder)) { |
1256 | $role->sortorder += 1; |
b5959f30 |
1257 | } |
1258 | |
eef868d1 |
1259 | $context = get_context_instance(CONTEXT_SYSTEM, SITEID); |
1260 | |
98882637 |
1261 | if ($id = insert_record('role', $role)) { |
eef868d1 |
1262 | if ($legacy) { |
1263 | assign_capability($legacy, CAP_ALLOW, $id, $context->id); |
98882637 |
1264 | } |
eef868d1 |
1265 | |
ec7a8b79 |
1266 | /// By default, users with role:manage at site level |
1267 | /// should be able to assign users to this new role, and override this new role's capabilities |
eef868d1 |
1268 | |
ec7a8b79 |
1269 | // find all admin roles |
e46c0987 |
1270 | if ($adminroles = get_roles_with_capability('moodle/role:manage', CAP_ALLOW, $context)) { |
1271 | // foreach admin role |
1272 | foreach ($adminroles as $arole) { |
1273 | // write allow_assign and allow_overrid |
1274 | allow_assign($arole->id, $id); |
eef868d1 |
1275 | allow_override($arole->id, $id); |
e46c0987 |
1276 | } |
ec7a8b79 |
1277 | } |
eef868d1 |
1278 | |
98882637 |
1279 | return $id; |
1280 | } else { |
eef868d1 |
1281 | return false; |
98882637 |
1282 | } |
eef868d1 |
1283 | |
bbbf2d40 |
1284 | } |
1285 | |
8420bee9 |
1286 | /** |
1287 | * function that deletes a role and cleanups up after it |
1288 | * @param roleid - id of role to delete |
1289 | * @return success |
1290 | */ |
1291 | function delete_role($roleid) { |
1292 | $success = true; |
1293 | |
1294 | // first unssign all users |
1295 | if (!role_unassign($roleid)) { |
1296 | debugging("Error while unassigning all users from role with ID $roleid!"); |
1297 | $success = false; |
1298 | } |
1299 | |
1300 | // cleanup all references to this role, ignore errors |
1301 | if ($success) { |
1302 | delete_records('role_capabilities', 'roleid', $roleid); |
1303 | delete_records('role_allow_assign', 'roleid', $roleid); |
1304 | delete_records('role_allow_assign', 'allowassign', $roleid); |
1305 | delete_records('role_allow_override', 'roleid', $roleid); |
1306 | delete_records('role_allow_override', 'allowoverride', $roleid); |
1307 | delete_records('role_names', 'roleid', $roleid); |
1308 | } |
1309 | |
1310 | // finally delete the role itself |
1311 | if ($success and !delete_records('role', 'id', $roleid)) { |
ece4945b |
1312 | debugging("Could not delete role record with ID $roleid!"); |
8420bee9 |
1313 | $success = false; |
1314 | } |
1315 | |
1316 | return $success; |
1317 | } |
1318 | |
bbbf2d40 |
1319 | /** |
1320 | * Function to write context specific overrides, or default capabilities. |
1321 | * @param module - string name |
1322 | * @param capability - string name |
1323 | * @param contextid - context id |
1324 | * @param roleid - role id |
1325 | * @param permission - int 1,-1 or -1000 |
1326 | */ |
e7876c1e |
1327 | function assign_capability($capability, $permission, $roleid, $contextid, $overwrite=false) { |
eef868d1 |
1328 | |
98882637 |
1329 | global $USER; |
eef868d1 |
1330 | |
98882637 |
1331 | if (empty($permission) || $permission == 0) { // if permission is not set |
eef868d1 |
1332 | unassign_capability($capability, $roleid, $contextid); |
98882637 |
1333 | } |
eef868d1 |
1334 | |
2e85fffe |
1335 | $existing = get_record('role_capabilities', 'contextid', $contextid, 'roleid', $roleid, 'capability', $capability); |
e7876c1e |
1336 | |
1337 | if ($existing and !$overwrite) { // We want to keep whatever is there already |
1338 | return true; |
1339 | } |
1340 | |
bbbf2d40 |
1341 | $cap = new object; |
1342 | $cap->contextid = $contextid; |
1343 | $cap->roleid = $roleid; |
1344 | $cap->capability = $capability; |
1345 | $cap->permission = $permission; |
1346 | $cap->timemodified = time(); |
9db12da7 |
1347 | $cap->modifierid = empty($USER->id) ? 0 : $USER->id; |
e7876c1e |
1348 | |
1349 | if ($existing) { |
1350 | $cap->id = $existing->id; |
1351 | return update_record('role_capabilities', $cap); |
1352 | } else { |
1353 | return insert_record('role_capabilities', $cap); |
1354 | } |
bbbf2d40 |
1355 | } |
1356 | |
1357 | |
1358 | /** |
1359 | * Unassign a capability from a role. |
1360 | * @param $roleid - the role id |
1361 | * @param $capability - the name of the capability |
1362 | * @return boolean - success or failure |
1363 | */ |
1364 | function unassign_capability($capability, $roleid, $contextid=NULL) { |
eef868d1 |
1365 | |
98882637 |
1366 | if (isset($contextid)) { |
1367 | $status = delete_records('role_capabilities', 'capability', $capability, |
1368 | 'roleid', $roleid, 'contextid', $contextid); |
1369 | } else { |
1370 | $status = delete_records('role_capabilities', 'capability', $capability, |
1371 | 'roleid', $roleid); |
1372 | } |
1373 | return $status; |
bbbf2d40 |
1374 | } |
1375 | |
1376 | |
1377 | /** |
759ac72d |
1378 | * Get the roles that have a given capability assigned to it. This function |
1379 | * does not resolve the actual permission of the capability. It just checks |
1380 | * for assignment only. |
bbbf2d40 |
1381 | * @param $capability - capability name (string) |
1382 | * @param $permission - optional, the permission defined for this capability |
1383 | * either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT |
1384 | * @return array or role objects |
1385 | */ |
ec7a8b79 |
1386 | function get_roles_with_capability($capability, $permission=NULL, $context='') { |
1387 | |
bbbf2d40 |
1388 | global $CFG; |
eef868d1 |
1389 | |
ec7a8b79 |
1390 | if ($context) { |
1391 | if ($contexts = get_parent_contexts($context)) { |
1392 | $listofcontexts = '('.implode(',', $contexts).')'; |
1393 | } else { |
1394 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
eef868d1 |
1395 | $listofcontexts = '('.$sitecontext->id.')'; // must be site |
1396 | } |
42ac3ecf |
1397 | $contextstr = "AND (rc.contextid = '$context->id' OR rc.contextid IN $listofcontexts)"; |
ec7a8b79 |
1398 | } else { |
1399 | $contextstr = ''; |
1400 | } |
eef868d1 |
1401 | |
1402 | $selectroles = "SELECT r.* |
42ac3ecf |
1403 | FROM {$CFG->prefix}role r, |
1404 | {$CFG->prefix}role_capabilities rc |
bbbf2d40 |
1405 | WHERE rc.capability = '$capability' |
ec7a8b79 |
1406 | AND rc.roleid = r.id $contextstr"; |
bbbf2d40 |
1407 | |
1408 | if (isset($permission)) { |
1409 | $selectroles .= " AND rc.permission = '$permission'"; |
1410 | } |
1411 | return get_records_sql($selectroles); |
1412 | } |
1413 | |
1414 | |
1415 | /** |
a9e1c058 |
1416 | * This function makes a role-assignment (a role for a user or group in a particular context) |
bbbf2d40 |
1417 | * @param $roleid - the role of the id |
1418 | * @param $userid - userid |
1419 | * @param $groupid - group id |
1420 | * @param $contextid - id of the context |
1421 | * @param $timestart - time this assignment becomes effective |
1422 | * @param $timeend - time this assignemnt ceases to be effective |
1423 | * @uses $USER |
1424 | * @return id - new id of the assigment |
1425 | */ |
f44152f4 |
1426 | function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $timeend=0, $hidden=0, $enrol='manual') { |
aa311411 |
1427 | global $USER, $CFG; |
bbbf2d40 |
1428 | |
7eb0b60a |
1429 | debugging("Assign roleid $roleid userid $userid contextid $contextid", DEBUG_DEVELOPER); |
bbbf2d40 |
1430 | |
a9e1c058 |
1431 | /// Do some data validation |
1432 | |
bbbf2d40 |
1433 | if (empty($roleid)) { |
9d829c68 |
1434 | debugging('Role ID not provided'); |
a9e1c058 |
1435 | return false; |
bbbf2d40 |
1436 | } |
1437 | |
1438 | if (empty($userid) && empty($groupid)) { |
9d829c68 |
1439 | debugging('Either userid or groupid must be provided'); |
a9e1c058 |
1440 | return false; |
bbbf2d40 |
1441 | } |
eef868d1 |
1442 | |
7700027f |
1443 | if ($userid && !record_exists('user', 'id', $userid)) { |
82396e5b |
1444 | debugging('User ID '.intval($userid).' does not exist!'); |
7700027f |
1445 | return false; |
1446 | } |
bbbf2d40 |
1447 | |
dc411d1b |
1448 | if ($groupid && !record_exists('groups', 'id', $groupid)) { |
82396e5b |
1449 | debugging('Group ID '.intval($groupid).' does not exist!'); |
dc411d1b |
1450 | return false; |
1451 | } |
1452 | |
7700027f |
1453 | if (!$context = get_context_instance_by_id($contextid)) { |
82396e5b |
1454 | debugging('Context ID '.intval($contextid).' does not exist!'); |
a9e1c058 |
1455 | return false; |
bbbf2d40 |
1456 | } |
1457 | |
a9e1c058 |
1458 | if (($timestart and $timeend) and ($timestart > $timeend)) { |
9d829c68 |
1459 | debugging('The end time can not be earlier than the start time'); |
a9e1c058 |
1460 | return false; |
1461 | } |
1462 | |
7700027f |
1463 | |
a9e1c058 |
1464 | /// Check for existing entry |
1465 | if ($userid) { |
7700027f |
1466 | $ra = get_record('role_assignments', 'roleid', $roleid, 'contextid', $context->id, 'userid', $userid); |
a9e1c058 |
1467 | } else { |
7700027f |
1468 | $ra = get_record('role_assignments', 'roleid', $roleid, 'contextid', $context->id, 'groupid', $groupid); |
a9e1c058 |
1469 | } |
1470 | |
9ebcb4d2 |
1471 | |
a9e1c058 |
1472 | $newra = new object; |
bbbf2d40 |
1473 | |
a9e1c058 |
1474 | if (empty($ra)) { // Create a new entry |
1475 | $newra->roleid = $roleid; |
7700027f |
1476 | $newra->contextid = $context->id; |
a9e1c058 |
1477 | $newra->userid = $userid; |
1478 | $newra->groupid = $groupid; |
1479 | |
1480 | $newra->hidden = $hidden; |
f44152f4 |
1481 | $newra->enrol = $enrol; |
a9e1c058 |
1482 | $newra->timestart = $timestart; |
1483 | $newra->timeend = $timeend; |
1484 | $newra->timemodified = time(); |
1485 | $newra->modifier = empty($USER->id) ? 0 : $USER->id; |
1486 | |
9ebcb4d2 |
1487 | $success = insert_record('role_assignments', $newra); |
a9e1c058 |
1488 | |
1489 | } else { // We already have one, just update it |
1490 | |
1491 | $newra->id = $ra->id; |
1492 | $newra->hidden = $hidden; |
f44152f4 |
1493 | $newra->enrol = $enrol; |
a9e1c058 |
1494 | $newra->timestart = $timestart; |
1495 | $newra->timeend = $timeend; |
1496 | $newra->timemodified = time(); |
1497 | $newra->modifier = empty($USER->id) ? 0 : $USER->id; |
1498 | |
9ebcb4d2 |
1499 | $success = update_record('role_assignments', $newra); |
1500 | } |
1501 | |
7700027f |
1502 | if ($success) { /// Role was assigned, so do some other things |
1503 | |
1504 | /// If the user is the current user, then reload the capabilities too. |
1505 | if (!empty($USER->id) && $USER->id == $userid) { |
1506 | load_user_capability(); |
1507 | } |
1508 | |
0f161e1f |
1509 | /// Ask all the modules if anything needs to be done for this user |
1510 | if ($mods = get_list_of_plugins('mod')) { |
1511 | foreach ($mods as $mod) { |
1512 | include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php'); |
1513 | $functionname = $mod.'_role_assign'; |
1514 | if (function_exists($functionname)) { |
1515 | $functionname($userid, $context); |
1516 | } |
1517 | } |
1518 | } |
1519 | |
1520 | /// Make sure they have an entry in user_lastaccess for courses they can access |
1521 | // role_add_lastaccess_entries($userid, $context); |
a9e1c058 |
1522 | } |
eef868d1 |
1523 | |
4e5f3064 |
1524 | /// now handle metacourse role assignments if in course context |
aad2ba95 |
1525 | if ($success and $context->contextlevel == CONTEXT_COURSE) { |
4e5f3064 |
1526 | if ($parents = get_records('course_meta', 'child_course', $context->instanceid)) { |
1527 | foreach ($parents as $parent) { |
1aad4310 |
1528 | sync_metacourse($parent->parent_course); |
4e5f3064 |
1529 | } |
1530 | } |
1531 | } |
6eb4f823 |
1532 | |
1533 | return $success; |
bbbf2d40 |
1534 | } |
1535 | |
1536 | |
1537 | /** |
1dc1f037 |
1538 | * Deletes one or more role assignments. You must specify at least one parameter. |
bbbf2d40 |
1539 | * @param $roleid |
1540 | * @param $userid |
1541 | * @param $groupid |
1542 | * @param $contextid |
1543 | * @return boolean - success or failure |
1544 | */ |
1dc1f037 |
1545 | function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) { |
d74067e8 |
1546 | |
1547 | global $USER, $CFG; |
eef868d1 |
1548 | |
4e5f3064 |
1549 | $success = true; |
d74067e8 |
1550 | |
1dc1f037 |
1551 | $args = array('roleid', 'userid', 'groupid', 'contextid'); |
1552 | $select = array(); |
1553 | foreach ($args as $arg) { |
1554 | if ($$arg) { |
1555 | $select[] = $arg.' = '.$$arg; |
1556 | } |
1557 | } |
d74067e8 |
1558 | |
1dc1f037 |
1559 | if ($select) { |
4e5f3064 |
1560 | if ($ras = get_records_select('role_assignments', implode(' AND ', $select))) { |
1561 | $mods = get_list_of_plugins('mod'); |
1562 | foreach($ras as $ra) { |
86e2c51d |
1563 | /// infinite loop protection when deleting recursively |
1564 | if (!$ra = get_record('role_assignments', 'id', $ra->id)) { |
1565 | continue; |
1566 | } |
4e5f3064 |
1567 | $success = delete_records('role_assignments', 'id', $ra->id) and $success; |
86e2c51d |
1568 | |
4e5f3064 |
1569 | /// If the user is the current user, then reload the capabilities too. |
1570 | if (!empty($USER->id) && $USER->id == $ra->userid) { |
1571 | load_user_capability(); |
1572 | } |
1573 | $context = get_record('context', 'id', $ra->contextid); |
0f161e1f |
1574 | |
1575 | /// Ask all the modules if anything needs to be done for this user |
4e5f3064 |
1576 | foreach ($mods as $mod) { |
1577 | include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php'); |
1578 | $functionname = $mod.'_role_unassign'; |
1579 | if (function_exists($functionname)) { |
1580 | $functionname($ra->userid, $context); // watch out, $context might be NULL if something goes wrong |
1581 | } |
1582 | } |
1583 | |
1584 | /// now handle metacourse role unassigment and removing from goups if in course context |
aad2ba95 |
1585 | if (!empty($context) and $context->contextlevel == CONTEXT_COURSE) { |
4e5f3064 |
1586 | //remove from groups when user has no role |
1587 | $roles = get_user_roles($context, $ra->userid, true); |
1588 | if (empty($roles)) { |
1589 | if ($groups = get_groups($context->instanceid, $ra->userid)) { |
1590 | foreach ($groups as $group) { |
1591 | delete_records('groups_members', 'groupid', $group->id, 'userid', $ra->userid); |
1592 | } |
1593 | } |
1594 | } |
1aad4310 |
1595 | //unassign roles in metacourses if needed |
4e5f3064 |
1596 | if ($parents = get_records('course_meta', 'child_course', $context->instanceid)) { |
1597 | foreach ($parents as $parent) { |
1aad4310 |
1598 | sync_metacourse($parent->parent_course); |
0f161e1f |
1599 | } |
1600 | } |
0f161e1f |
1601 | } |
1602 | } |
d74067e8 |
1603 | } |
1dc1f037 |
1604 | } |
4e5f3064 |
1605 | |
1606 | return $success; |
bbbf2d40 |
1607 | } |
1608 | |
eef868d1 |
1609 | /* |
1610 | * A convenience function to take care of the common case where you |
b963384f |
1611 | * just want to enrol someone using the default role into a course |
1612 | * |
1613 | * @param object $course |
1614 | * @param object $user |
1615 | * @param string $enrol - the plugin used to do this enrolment |
1616 | */ |
1617 | function enrol_into_course($course, $user, $enrol) { |
1618 | |
1619 | if ($course->enrolperiod) { |
1620 | $timestart = time(); |
1621 | $timeend = time() + $course->enrolperiod; |
1622 | } else { |
1623 | $timestart = $timeend = 0; |
1624 | } |
1625 | |
1626 | if ($role = get_default_course_role($course)) { |
c4381ef5 |
1627 | |
1628 | $context = get_context_instance(CONTEXT_COURSE, $course->id); |
1629 | |
e2183037 |
1630 | if (!role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol)) { |
b963384f |
1631 | return false; |
1632 | } |
eef868d1 |
1633 | |
b963384f |
1634 | email_welcome_message_to_user($course, $user); |
eef868d1 |
1635 | |
b963384f |
1636 | add_to_log($course->id, 'course', 'enrol', 'view.php?id='.$course->id, $user->id); |
1637 | |
1638 | return true; |
1639 | } |
1640 | |
1641 | return false; |
1642 | } |
1643 | |
0f161e1f |
1644 | /** |
1645 | * Add last access times to user_lastaccess as required |
1646 | * @param $userid |
1647 | * @param $context |
1648 | * @return boolean - success or failure |
1649 | */ |
1650 | function role_add_lastaccess_entries($userid, $context) { |
1651 | |
1652 | global $USER, $CFG; |
1653 | |
aad2ba95 |
1654 | if (empty($context->contextlevel)) { |
0f161e1f |
1655 | return false; |
1656 | } |
1657 | |
1658 | $lastaccess = new object; // Reusable object below |
1659 | $lastaccess->userid = $userid; |
1660 | $lastaccess->timeaccess = 0; |
1661 | |
aad2ba95 |
1662 | switch ($context->contextlevel) { |
0f161e1f |
1663 | |
1664 | case CONTEXT_SYSTEM: // For the whole site |
1665 | if ($courses = get_record('course')) { |
1666 | foreach ($courses as $course) { |
1667 | $lastaccess->courseid = $course->id; |
1668 | role_set_lastaccess($lastaccess); |
1669 | } |
1670 | } |
1671 | break; |
1672 | |
1673 | case CONTEXT_CATEGORY: // For a whole category |
1674 | if ($courses = get_record('course', 'category', $context->instanceid)) { |
1675 | foreach ($courses as $course) { |
1676 | $lastaccess->courseid = $course->id; |
1677 | role_set_lastaccess($lastaccess); |
1678 | } |
1679 | } |
1680 | if ($categories = get_record('course_categories', 'parent', $context->instanceid)) { |
1681 | foreach ($categories as $category) { |
1682 | $subcontext = get_context_instance(CONTEXT_CATEGORY, $category->id); |
1683 | role_add_lastaccess_entries($userid, $subcontext); |
1684 | } |
1685 | } |
1686 | break; |
eef868d1 |
1687 | |
0f161e1f |
1688 | |
1689 | case CONTEXT_COURSE: // For a whole course |
1690 | if ($course = get_record('course', 'id', $context->instanceid)) { |
1691 | $lastaccess->courseid = $course->id; |
1692 | role_set_lastaccess($lastaccess); |
1693 | } |
1694 | break; |
1695 | } |
1696 | } |
1697 | |
1698 | /** |
1699 | * Delete last access times from user_lastaccess as required |
1700 | * @param $userid |
1701 | * @param $context |
1702 | * @return boolean - success or failure |
1703 | */ |
1704 | function role_remove_lastaccess_entries($userid, $context) { |
1705 | |
1706 | global $USER, $CFG; |
1707 | |
1708 | } |
1709 | |
bbbf2d40 |
1710 | |
1711 | /** |
1712 | * Loads the capability definitions for the component (from file). If no |
1713 | * capabilities are defined for the component, we simply return an empty array. |
1714 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1715 | * @return array of capabilities |
1716 | */ |
1717 | function load_capability_def($component) { |
1718 | global $CFG; |
1719 | |
1720 | if ($component == 'moodle') { |
1721 | $defpath = $CFG->libdir.'/db/access.php'; |
1722 | $varprefix = 'moodle'; |
1723 | } else { |
0c4d9f49 |
1724 | $compparts = explode('/', $component); |
eef868d1 |
1725 | |
0c4d9f49 |
1726 | if ($compparts[0] == 'block') { |
1727 | // Blocks are an exception. Blocks directory is 'blocks', and not |
1728 | // 'block'. So we need to jump through hoops. |
1729 | $defpath = $CFG->dirroot.'/'.$compparts[0]. |
1730 | 's/'.$compparts[1].'/db/access.php'; |
1731 | $varprefix = $compparts[0].'_'.$compparts[1]; |
1732 | } else { |
1733 | $defpath = $CFG->dirroot.'/'.$component.'/db/access.php'; |
1734 | $varprefix = str_replace('/', '_', $component); |
1735 | } |
bbbf2d40 |
1736 | } |
1737 | $capabilities = array(); |
eef868d1 |
1738 | |
bbbf2d40 |
1739 | if (file_exists($defpath)) { |
1740 | require_once($defpath); |
1741 | $capabilities = ${$varprefix.'_capabilities'}; |
1742 | } |
1743 | return $capabilities; |
1744 | } |
1745 | |
1746 | |
1747 | /** |
1748 | * Gets the capabilities that have been cached in the database for this |
1749 | * component. |
1750 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1751 | * @return array of capabilities |
1752 | */ |
1753 | function get_cached_capabilities($component='moodle') { |
1754 | if ($component == 'moodle') { |
1755 | $storedcaps = get_records_select('capabilities', |
1756 | "name LIKE 'moodle/%:%'"); |
1757 | } else { |
1758 | $storedcaps = get_records_select('capabilities', |
1759 | "name LIKE '$component:%'"); |
1760 | } |
1761 | return $storedcaps; |
1762 | } |
1763 | |
1764 | |
1765 | /** |
1766 | * Updates the capabilities table with the component capability definitions. |
1767 | * If no parameters are given, the function updates the core moodle |
1768 | * capabilities. |
1769 | * |
1770 | * Note that the absence of the db/access.php capabilities definition file |
1771 | * will cause any stored capabilities for the component to be removed from |
eef868d1 |
1772 | * the database. |
bbbf2d40 |
1773 | * |
1774 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1775 | * @return boolean |
1776 | */ |
1777 | function update_capabilities($component='moodle') { |
eef868d1 |
1778 | |
bbbf2d40 |
1779 | $storedcaps = array(); |
be4486da |
1780 | |
1781 | $filecaps = load_capability_def($component); |
bbbf2d40 |
1782 | $cachedcaps = get_cached_capabilities($component); |
1783 | if ($cachedcaps) { |
1784 | foreach ($cachedcaps as $cachedcap) { |
1785 | array_push($storedcaps, $cachedcap->name); |
be4486da |
1786 | // update risk bitmasks in existing capabilitites if needed |
1787 | if (array_key_exists($cachedcap->name, $filecaps)) { |
1788 | if (!array_key_exists('riskbitmask', $filecaps[$cachedcap->name])) { |
2b531945 |
1789 | $filecaps[$cachedcap->name]['riskbitmask'] = 0; // no risk if not specified |
be4486da |
1790 | } |
1791 | if ($cachedcap->riskbitmask != $filecaps[$cachedcap->name]['riskbitmask']) { |
1792 | $updatecap = new object; |
1793 | $updatecap->id = $cachedcap->id; |
1794 | $updatecap->riskbitmask = $filecaps[$cachedcap->name]['riskbitmask']; |
1795 | if (!update_record('capabilities', $updatecap)) { |
1796 | return false; |
1797 | } |
1798 | } |
1799 | } |
bbbf2d40 |
1800 | } |
1801 | } |
be4486da |
1802 | |
bbbf2d40 |
1803 | // Are there new capabilities in the file definition? |
1804 | $newcaps = array(); |
eef868d1 |
1805 | |
bbbf2d40 |
1806 | foreach ($filecaps as $filecap => $def) { |
eef868d1 |
1807 | if (!$storedcaps || |
bbbf2d40 |
1808 | ($storedcaps && in_array($filecap, $storedcaps) === false)) { |
2b531945 |
1809 | if (!array_key_exists('riskbitmask', $def)) { |
1810 | $def['riskbitmask'] = 0; // no risk if not specified |
1811 | } |
bbbf2d40 |
1812 | $newcaps[$filecap] = $def; |
1813 | } |
1814 | } |
1815 | // Add new capabilities to the stored definition. |
1816 | foreach ($newcaps as $capname => $capdef) { |
1817 | $capability = new object; |
1818 | $capability->name = $capname; |
1819 | $capability->captype = $capdef['captype']; |
1820 | $capability->contextlevel = $capdef['contextlevel']; |
1821 | $capability->component = $component; |
be4486da |
1822 | $capability->riskbitmask = $capdef['riskbitmask']; |
eef868d1 |
1823 | |
bbbf2d40 |
1824 | if (!insert_record('capabilities', $capability, false, 'id')) { |
1825 | return false; |
1826 | } |
eef868d1 |
1827 | |
bbbf2d40 |
1828 | // Do we need to assign the new capabilities to roles that have the |
1829 | // legacy capabilities moodle/legacy:* as well? |
1830 | if (isset($capdef['legacy']) && is_array($capdef['legacy']) && |
1831 | !assign_legacy_capabilities($capname, $capdef['legacy'])) { |
2e85fffe |
1832 | notify('Could not assign legacy capabilities for '.$capname); |
bbbf2d40 |
1833 | } |
1834 | } |
1835 | // Are there any capabilities that have been removed from the file |
1836 | // definition that we need to delete from the stored capabilities and |
1837 | // role assignments? |
1838 | capabilities_cleanup($component, $filecaps); |
eef868d1 |
1839 | |
bbbf2d40 |
1840 | return true; |
1841 | } |
1842 | |
1843 | |
1844 | /** |
1845 | * Deletes cached capabilities that are no longer needed by the component. |
1846 | * Also unassigns these capabilities from any roles that have them. |
1847 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1848 | * @param $newcapdef - array of the new capability definitions that will be |
1849 | * compared with the cached capabilities |
1850 | * @return int - number of deprecated capabilities that have been removed |
1851 | */ |
1852 | function capabilities_cleanup($component, $newcapdef=NULL) { |
eef868d1 |
1853 | |
bbbf2d40 |
1854 | $removedcount = 0; |
eef868d1 |
1855 | |
bbbf2d40 |
1856 | if ($cachedcaps = get_cached_capabilities($component)) { |
1857 | foreach ($cachedcaps as $cachedcap) { |
1858 | if (empty($newcapdef) || |
1859 | array_key_exists($cachedcap->name, $newcapdef) === false) { |
eef868d1 |
1860 | |
bbbf2d40 |
1861 | // Remove from capabilities cache. |
1862 | if (!delete_records('capabilities', 'name', $cachedcap->name)) { |
1863 | error('Could not delete deprecated capability '.$cachedcap->name); |
1864 | } else { |
1865 | $removedcount++; |
1866 | } |
1867 | // Delete from roles. |
1868 | if($roles = get_roles_with_capability($cachedcap->name)) { |
1869 | foreach($roles as $role) { |
46943f7b |
1870 | if (!unassign_capability($cachedcap->name, $role->id)) { |
bbbf2d40 |
1871 | error('Could not unassign deprecated capability '. |
1872 | $cachedcap->name.' from role '.$role->name); |
1873 | } |
1874 | } |
1875 | } |
1876 | } // End if. |
1877 | } |
1878 | } |
1879 | return $removedcount; |
1880 | } |
1881 | |
1882 | |
1883 | |
cee0901c |
1884 | /**************** |
1885 | * UI FUNCTIONS * |
1886 | ****************/ |
bbbf2d40 |
1887 | |
1888 | |
1889 | /** |
1890 | * prints human readable context identifier. |
1891 | */ |
0468976c |
1892 | function print_context_name($context) { |
340ea4e8 |
1893 | |
ec0810ee |
1894 | $name = ''; |
aad2ba95 |
1895 | switch ($context->contextlevel) { |
ec0810ee |
1896 | |
bbbf2d40 |
1897 | case CONTEXT_SYSTEM: // by now it's a definite an inherit |
ec0810ee |
1898 | $name = get_string('site'); |
340ea4e8 |
1899 | break; |
bbbf2d40 |
1900 | |
1901 | case CONTEXT_PERSONAL: |
ec0810ee |
1902 | $name = get_string('personal'); |
340ea4e8 |
1903 | break; |
1904 | |
4b10f08b |
1905 | case CONTEXT_USER: |
ec0810ee |
1906 | if ($user = get_record('user', 'id', $context->instanceid)) { |
1907 | $name = get_string('user').': '.fullname($user); |
1908 | } |
340ea4e8 |
1909 | break; |
1910 | |
bbbf2d40 |
1911 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
ec0810ee |
1912 | if ($category = get_record('course_categories', 'id', $context->instanceid)) { |
1913 | $name = get_string('category').': '.$category->name; |
1914 | } |
340ea4e8 |
1915 | break; |
bbbf2d40 |
1916 | |
1917 | case CONTEXT_COURSE: // 1 to 1 to course cat |
ec0810ee |
1918 | if ($course = get_record('course', 'id', $context->instanceid)) { |
1919 | $name = get_string('course').': '.$course->fullname; |
1920 | } |
340ea4e8 |
1921 | break; |
bbbf2d40 |
1922 | |
1923 | case CONTEXT_GROUP: // 1 to 1 to course |
ec0810ee |
1924 | if ($group = get_record('groups', 'id', $context->instanceid)) { |
1925 | $name = get_string('group').': '.$group->name; |
1926 | } |
340ea4e8 |
1927 | break; |
bbbf2d40 |
1928 | |
1929 | case CONTEXT_MODULE: // 1 to 1 to course |
98882637 |
1930 | if ($cm = get_record('course_modules','id',$context->instanceid)) { |
1931 | if ($module = get_record('modules','id',$cm->module)) { |
1932 | if ($mod = get_record($module->name, 'id', $cm->instance)) { |
ec0810ee |
1933 | $name = get_string('activitymodule').': '.$mod->name; |
98882637 |
1934 | } |
ec0810ee |
1935 | } |
1936 | } |
340ea4e8 |
1937 | break; |
bbbf2d40 |
1938 | |
1939 | case CONTEXT_BLOCK: // 1 to 1 to course |
98882637 |
1940 | if ($blockinstance = get_record('block_instance','id',$context->instanceid)) { |
1941 | if ($block = get_record('block','id',$blockinstance->blockid)) { |
91be52d7 |
1942 | global $CFG; |
1943 | require_once("$CFG->dirroot/blocks/moodleblock.class.php"); |
1944 | require_once("$CFG->dirroot/blocks/$block->name/block_$block->name.php"); |
1945 | $blockname = "block_$block->name"; |
1946 | if ($blockobject = new $blockname()) { |
1947 | $name = $blockobject->title.' ('.get_string('block').')'; |
1948 | } |
ec0810ee |
1949 | } |
1950 | } |
340ea4e8 |
1951 | break; |
bbbf2d40 |
1952 | |
1953 | default: |
1954 | error ('This is an unknown context!'); |
340ea4e8 |
1955 | return false; |
1956 | |
1957 | } |
340ea4e8 |
1958 | return $name; |
bbbf2d40 |
1959 | } |
1960 | |
1961 | |
1962 | /** |
eef868d1 |
1963 | * Extracts the relevant capabilities given a contextid. |
bbbf2d40 |
1964 | * All case based, example an instance of forum context. |
1965 | * Will fetch all forum related capabilities, while course contexts |
1966 | * Will fetch all capabilities |
0468976c |
1967 | * @param object context |
bbbf2d40 |
1968 | * @return array(); |
1969 | * |
1970 | * capabilities |
1971 | * `name` varchar(150) NOT NULL, |
1972 | * `captype` varchar(50) NOT NULL, |
1973 | * `contextlevel` int(10) NOT NULL, |
1974 | * `component` varchar(100) NOT NULL, |
1975 | */ |
0468976c |
1976 | function fetch_context_capabilities($context) { |
eef868d1 |
1977 | |
98882637 |
1978 | global $CFG; |
bbbf2d40 |
1979 | |
1980 | $sort = 'ORDER BY contextlevel,component,id'; // To group them sensibly for display |
eef868d1 |
1981 | |
aad2ba95 |
1982 | switch ($context->contextlevel) { |
bbbf2d40 |
1983 | |
98882637 |
1984 | case CONTEXT_SYSTEM: // all |
1985 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
1986 | break; |
1987 | |
1988 | case CONTEXT_PERSONAL: |
0a8a95c9 |
1989 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_PERSONAL; |
bbbf2d40 |
1990 | break; |
eef868d1 |
1991 | |
4b10f08b |
1992 | case CONTEXT_USER: |
1993 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_USER; |
bbbf2d40 |
1994 | break; |
eef868d1 |
1995 | |
bbbf2d40 |
1996 | case CONTEXT_COURSECAT: // all |
98882637 |
1997 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
1998 | break; |
1999 | |
2000 | case CONTEXT_COURSE: // all |
98882637 |
2001 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
2002 | break; |
2003 | |
2004 | case CONTEXT_GROUP: // group caps |
2005 | break; |
2006 | |
2007 | case CONTEXT_MODULE: // mod caps |
98882637 |
2008 | $cm = get_record('course_modules', 'id', $context->instanceid); |
2009 | $module = get_record('modules', 'id', $cm->module); |
eef868d1 |
2010 | |
98882637 |
2011 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_MODULE." |
2012 | and component = 'mod/$module->name'"; |
bbbf2d40 |
2013 | break; |
2014 | |
2015 | case CONTEXT_BLOCK: // block caps |
98882637 |
2016 | $cb = get_record('block_instance', 'id', $context->instanceid); |
2017 | $block = get_record('block', 'id', $cb->blockid); |
eef868d1 |
2018 | |
98882637 |
2019 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_BLOCK." |
2020 | and component = 'block/$block->name'"; |
bbbf2d40 |
2021 | break; |
2022 | |
2023 | default: |
2024 | return false; |
2025 | } |
2026 | |
16e2e2f3 |
2027 | if (!$records = get_records_sql($SQL.' '.$sort)) { |
2028 | $records = array(); |
2029 | } |
759ac72d |
2030 | $contextindependentcaps = fetch_context_independent_capabilities(); |
2031 | $records = array_merge($records, $contextindependentcaps); |
69eb59f2 |
2032 | |
2033 | // special sorting of core system capabiltites and enrollments |
aad2ba95 |
2034 | if ($context->contextlevel == CONTEXT_SYSTEM) { |
69eb59f2 |
2035 | $first = array(); |
2036 | foreach ($records as $key=>$record) { |
2037 | if (preg_match('|^moodle/|', $record->name) and $record->contextlevel == CONTEXT_SYSTEM) { |
2038 | $first[$key] = $record; |
2039 | unset($records[$key]); |
2040 | } else if (count($first)){ |
2041 | break; |
2042 | } |
2043 | } |
2044 | if (count($first)) { |
2045 | $records = $first + $records; // merge the two arrays keeping the keys |
2046 | } |
2047 | } |
2048 | // end of special sorting |
bbbf2d40 |
2049 | return $records; |
eef868d1 |
2050 | |
bbbf2d40 |
2051 | } |
2052 | |
2053 | |
759ac72d |
2054 | /** |
2055 | * Gets the context-independent capabilities that should be overrridable in |
2056 | * any context. |
2057 | * @return array of capability records from the capabilities table. |
2058 | */ |
2059 | function fetch_context_independent_capabilities() { |
eef868d1 |
2060 | |
759ac72d |
2061 | $contextindependentcaps = array( |
2062 | 'moodle/site:accessallgroups' |
2063 | ); |
2064 | |
2065 | $records = array(); |
eef868d1 |
2066 | |
759ac72d |
2067 | foreach ($contextindependentcaps as $capname) { |
2068 | $record = get_record('capabilities', 'name', $capname); |
2069 | array_push($records, $record); |
2070 | } |
2071 | return $records; |
2072 | } |
2073 | |
2074 | |
bbbf2d40 |
2075 | /** |
2076 | * This function pulls out all the resolved capabilities (overrides and |
759ac72d |
2077 | * defaults) of a role used in capability overrides in contexts at a given |
bbbf2d40 |
2078 | * context. |
0a8a95c9 |
2079 | * @param obj $context |
bbbf2d40 |
2080 | * @param int $roleid |
dc558690 |
2081 | * @param bool self - if set to true, resolve till this level, else stop at immediate parent level |
bbbf2d40 |
2082 | * @return array |
2083 | */ |
1648afb2 |
2084 | function role_context_capabilities($roleid, $context, $cap='') { |
dc558690 |
2085 | global $CFG; |
eef868d1 |
2086 | |
8521d83a |
2087 | $contexts = get_parent_contexts($context); |
2088 | $contexts[] = $context->id; |
98882637 |
2089 | $contexts = '('.implode(',', $contexts).')'; |
eef868d1 |
2090 | |
1648afb2 |
2091 | if ($cap) { |
e4697bf7 |
2092 | $search = " AND rc.capability = '$cap' "; |
1648afb2 |
2093 | } else { |
eef868d1 |
2094 | $search = ''; |
1648afb2 |
2095 | } |
eef868d1 |
2096 | |
2097 | $SQL = "SELECT rc.* |
2098 | FROM {$CFG->prefix}role_capabilities rc, |
dc558690 |
2099 | {$CFG->prefix}context c |
2100 | WHERE rc.contextid in $contexts |
2101 | AND rc.roleid = $roleid |
2102 | AND rc.contextid = c.id $search |
aad2ba95 |
2103 | ORDER BY c.contextlevel DESC, |
eef868d1 |
2104 | rc.capability DESC"; |
759ac72d |
2105 | |
98882637 |
2106 | $capabilities = array(); |
eef868d1 |
2107 | |
4729012f |
2108 | if ($records = get_records_sql($SQL)) { |
2109 | // We are traversing via reverse order. |
2110 | foreach ($records as $record) { |
2111 | // If not set yet (i.e. inherit or not set at all), or currently we have a prohibit |
2112 | if (!isset($capabilities[$record->capability]) || $record->permission<-500) { |
2113 | $capabilities[$record->capability] = $record->permission; |
eef868d1 |
2114 | } |
4729012f |
2115 | } |
98882637 |
2116 | } |
2117 | return $capabilities; |
bbbf2d40 |
2118 | } |
2119 | |
bbbf2d40 |
2120 | /** |
eef868d1 |
2121 | * Recursive function which, given a context, find all parent context ids, |
bbbf2d40 |
2122 | * and return the array in reverse order, i.e. parent first, then grand |
2123 | * parent, etc. |
2124 | * @param object $context |
2125 | * @return array() |
2126 | */ |
bbbf2d40 |
2127 | function get_parent_contexts($context) { |
759ac72d |
2128 | |
aad2ba95 |
2129 | switch ($context->contextlevel) { |
bbbf2d40 |
2130 | |
2131 | case CONTEXT_SYSTEM: // no parent |
957861f7 |
2132 | return array(); |
bbbf2d40 |
2133 | break; |
2134 | |
2135 | case CONTEXT_PERSONAL: |
957861f7 |
2136 | if (!$parent = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
2137 | return array(); |
2138 | } else { |
2139 | return array($parent->id); |
2140 | } |
bbbf2d40 |
2141 | break; |
eef868d1 |
2142 | |
4b10f08b |
2143 | case CONTEXT_USER: |
957861f7 |
2144 | if (!$parent = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
2145 | return array(); |
2146 | } else { |
2147 | return array($parent->id); |
2148 | } |
bbbf2d40 |
2149 | break; |
eef868d1 |
2150 | |
bbbf2d40 |
2151 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
957861f7 |
2152 | if (!$coursecat = get_record('course_categories','id',$context->instanceid)) { |
2153 | return array(); |
2154 | } |
c5ddc3fd |
2155 | if (!empty($coursecat->parent)) { // return parent value if exist |
bbbf2d40 |
2156 | $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
2157 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2158 | } else { // else return site value |
2159 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
2160 | return array($parent->id); |
2161 | } |
2162 | break; |
2163 | |
2164 | case CONTEXT_COURSE: // 1 to 1 to course cat |
957861f7 |
2165 | if (!$course = get_record('course','id',$context->instanceid)) { |
2166 | return array(); |
2167 | } |
2168 | if (!empty($course->category)) { |
2169 | $parent = get_context_instance(CONTEXT_COURSECAT, $course->category); |
2170 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2171 | } else { |
2172 | return array(); |
2173 | } |
bbbf2d40 |
2174 | break; |
2175 | |
2176 | case CONTEXT_GROUP: // 1 to 1 to course |
957861f7 |
2177 | if (!$group = get_record('groups','id',$context->instanceid)) { |
2178 | return array(); |
2179 | } |
2180 | if ($parent = get_context_instance(CONTEXT_COURSE, $group->courseid)) { |
2181 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2182 | } else { |
2183 | return array(); |
2184 | } |
bbbf2d40 |
2185 | break; |
2186 | |
2187 | case CONTEXT_MODULE: // 1 to 1 to course |
957861f7 |
2188 | if (!$cm = get_record('course_modules','id',$context->instanceid)) { |
2189 | return array(); |
2190 | } |
2191 | if ($parent = get_context_instance(CONTEXT_COURSE, $cm->course)) { |
2192 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2193 | } else { |
2194 | return array(); |
2195 | } |
bbbf2d40 |
2196 | break; |
2197 | |
2198 | case CONTEXT_BLOCK: // 1 to 1 to course |
957861f7 |
2199 | if (!$block = get_record('block_instance','id',$context->instanceid)) { |
2200 | return array(); |
2201 | } |
2202 | if ($parent = get_context_instance(CONTEXT_COURSE, $block->pageid)) { |
2203 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2204 | } else { |
2205 | return array(); |
2206 | } |
bbbf2d40 |
2207 | break; |
2208 | |
2209 | default: |
957861f7 |
2210 | error('This is an unknown context!'); |
bbbf2d40 |
2211 | return false; |
2212 | } |
bbbf2d40 |
2213 | } |
2214 | |
759ac72d |
2215 | |
2216 | /** |
2217 | * Gets a string for sql calls, searching for stuff in this context or above |
ea8158c1 |
2218 | * @param object $context |
2219 | * @return string |
2220 | */ |
2221 | function get_related_contexts_string($context) { |
2222 | if ($parents = get_parent_contexts($context)) { |
eef868d1 |
2223 | return (' IN ('.$context->id.','.implode(',', $parents).')'); |
ea8158c1 |
2224 | } else { |
2225 | return (' ='.$context->id); |
2226 | } |
2227 | } |
759ac72d |
2228 | |
2229 | |
bbbf2d40 |
2230 | /** |
2231 | * This function gets the capability of a role in a given context. |
2232 | * It is needed when printing override forms. |
2233 | * @param int $contextid |
bbbf2d40 |
2234 | * @param string $capability |
2235 | * @param array $capabilities - array loaded using role_context_capabilities |
2236 | * @return int (allow, prevent, prohibit, inherit) |
2237 | */ |
bbbf2d40 |
2238 | function get_role_context_capability($contextid, $capability, $capabilities) { |
759ac72d |
2239 | if (isset($capabilities[$contextid][$capability])) { |
2240 | return $capabilities[$contextid][$capability]; |
2241 | } |
2242 | else { |
2243 | return false; |
2244 | } |
bbbf2d40 |
2245 | } |
2246 | |
2247 | |
cee0901c |
2248 | /** |
2249 | * Returns the human-readable, translated version of the capability. |
2250 | * Basically a big switch statement. |
2251 | * @param $capabilityname - e.g. mod/choice:readresponses |
2252 | */ |
ceb83c70 |
2253 | function get_capability_string($capabilityname) { |
eef868d1 |
2254 | |
cee0901c |
2255 | // Typical capabilityname is mod/choice:readresponses |
ceb83c70 |
2256 | |
2257 | $names = split('/', $capabilityname); |
2258 | $stringname = $names[1]; // choice:readresponses |
eef868d1 |
2259 | $components = split(':', $stringname); |
ceb83c70 |
2260 | $componentname = $components[0]; // choice |
98882637 |
2261 | |
2262 | switch ($names[0]) { |
2263 | case 'mod': |
ceb83c70 |
2264 | $string = get_string($stringname, $componentname); |
98882637 |
2265 | break; |
eef868d1 |
2266 | |
98882637 |
2267 | case 'block': |
ceb83c70 |
2268 | $string = get_string($stringname, 'block_'.$componentname); |
98882637 |
2269 | break; |
ceb83c70 |
2270 | |
98882637 |
2271 | case 'moodle': |
ceb83c70 |
2272 | $string = get_string($stringname, 'role'); |
98882637 |
2273 | break; |
eef868d1 |
2274 | |
98882637 |
2275 | case 'enrol': |
ceb83c70 |
2276 | $string = get_string($stringname, 'enrol_'.$componentname); |
eef868d1 |
2277 | break; |
2278 | |
98882637 |
2279 | default: |
ceb83c70 |
2280 | $string = get_string($stringname); |
eef868d1 |
2281 | break; |
2282 | |
98882637 |
2283 | } |
ceb83c70 |
2284 | return $string; |
bbbf2d40 |
2285 | } |
2286 | |
2287 | |
cee0901c |
2288 | /** |
2289 | * This gets the mod/block/course/core etc strings. |
2290 | * @param $component |
2291 | * @param $contextlevel |
2292 | */ |
bbbf2d40 |
2293 | function get_component_string($component, $contextlevel) { |
2294 | |
98882637 |
2295 | switch ($contextlevel) { |
bbbf2d40 |
2296 | |
98882637 |
2297 | case CONTEXT_SYSTEM: |
be382aaf |
2298 | if (preg_match('|^enrol/|', $component)) { |
2299 | $langname = str_replace('/', '_', $component); |
2300 | $string = get_string('enrolname', $langname); |
f3652521 |
2301 | } else if (preg_match('|^block/|', $component)) { |
2302 | $langname = str_replace('/', '_', $component); |
2303 | $string = get_string('blockname', $langname); |
69eb59f2 |
2304 | } else { |
2305 | $string = get_string('coresystem'); |
2306 | } |
bbbf2d40 |
2307 | break; |
2308 | |
2309 | case CONTEXT_PERSONAL: |
98882637 |
2310 | $string = get_string('personal'); |
bbbf2d40 |
2311 | break; |
2312 | |
4b10f08b |
2313 | case CONTEXT_USER: |
98882637 |
2314 | $string = get_string('users'); |
bbbf2d40 |
2315 | break; |
2316 | |
2317 | case CONTEXT_COURSECAT: |
98882637 |
2318 | $string = get_string('categories'); |
bbbf2d40 |
2319 | break; |
2320 | |
2321 | case CONTEXT_COURSE: |
98882637 |
2322 | $string = get_string('course'); |
bbbf2d40 |
2323 | break; |
2324 | |
2325 | case CONTEXT_GROUP: |
98882637 |
2326 | $string = get_string('group'); |
bbbf2d40 |
2327 | break; |
2328 | |
2329 | case CONTEXT_MODULE: |
98882637 |
2330 | $string = get_string('modulename', basename($component)); |
bbbf2d40 |
2331 | break; |
2332 | |
2333 | case CONTEXT_BLOCK: |
98882637 |
2334 | $string = get_string('blockname', 'block_'.$component.'.php'); |
bbbf2d40 |
2335 | break; |
2336 | |
2337 | default: |
2338 | error ('This is an unknown context!'); |
2339 | return false; |
eef868d1 |
2340 | |
98882637 |
2341 | } |
98882637 |
2342 | return $string; |
bbbf2d40 |
2343 | } |
cee0901c |
2344 | |
759ac72d |
2345 | /** |
2346 | * Gets the list of roles assigned to this context and up (parents) |
945f88ca |
2347 | * @param object $context |
2348 | * @return array |
2349 | */ |
e4dd3222 |
2350 | function get_roles_used_in_context($context) { |
2351 | |
2352 | global $CFG; |
2d9965e1 |
2353 | $contextlist = get_related_contexts_string($context); |
eef868d1 |
2354 | |
759ac72d |
2355 | $sql = "SELECT DISTINCT r.id, |
2356 | r.name, |
2357 | r.shortname, |
2358 | r.sortorder |
2359 | FROM {$CFG->prefix}role_assignments ra, |
eef868d1 |
2360 | {$CFG->prefix}role r |
2361 | WHERE r.id = ra.roleid |
759ac72d |
2362 | AND ra.contextid $contextlist |
2363 | ORDER BY r.sortorder ASC"; |
eef868d1 |
2364 | |
759ac72d |
2365 | return get_records_sql($sql); |
e4dd3222 |
2366 | } |
2367 | |
eef868d1 |
2368 | /** this function is used to print roles column in user profile page. |
945f88ca |
2369 | * @param int userid |
2370 | * @param int contextid |
2371 | * @return string |
2372 | */ |
0a8a95c9 |
2373 | function get_user_roles_in_context($userid, $contextid){ |
2374 | global $CFG; |
eef868d1 |
2375 | |
0a8a95c9 |
2376 | $rolestring = ''; |
2377 | $SQL = 'select * from '.$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'role r where ra.userid='.$userid.' and ra.contextid='.$contextid.' and ra.roleid = r.id'; |
2378 | if ($roles = get_records_sql($SQL)) { |
2379 | foreach ($roles as $userrole) { |
2380 | $rolestring .= '<a href="'.$CFG->wwwroot.'/user/index.php?contextid='.$userrole->contextid.'&roleid='.$userrole->roleid.'">'.$userrole->name.'</a>, '; |
eef868d1 |
2381 | } |
2382 | |
0a8a95c9 |
2383 | } |
2384 | return rtrim($rolestring, ', '); |
2385 | } |
68c52526 |
2386 | |
2387 | |
945f88ca |
2388 | /** |
2389 | * Checks if a user can override capabilities of a particular role in this context |
2390 | * @param object $context |
2391 | * @param int targetroleid - the id of the role you want to override |
2392 | * @return boolean |
2393 | */ |
68c52526 |
2394 | function user_can_override($context, $targetroleid) { |
2395 | // first check if user has override capability |
2396 | // if not return false; |
2397 | if (!has_capability('moodle/role:override', $context)) { |
eef868d1 |
2398 | return false; |
68c52526 |
2399 | } |
2400 | // pull out all active roles of this user from this context(or above) |
c0614051 |
2401 | if ($userroles = get_user_roles($context)) { |
2402 | foreach ($userroles as $userrole) { |
2403 | // if any in the role_allow_override table, then it's ok |
2404 | if (get_record('role_allow_override', 'roleid', $userrole->roleid, 'allowoverride', $targetroleid)) { |
2405 | return true; |
2406 | } |
68c52526 |
2407 | } |
2408 | } |
eef868d1 |
2409 | |
68c52526 |
2410 | return false; |
eef868d1 |
2411 | |
68c52526 |
2412 | } |
2413 | |
945f88ca |
2414 | /** |
2415 | * Checks if a user can assign users to a particular role in this context |
2416 | * @param object $context |
2417 | * @param int targetroleid - the id of the role you want to assign users to |
2418 | * @return boolean |
2419 | */ |
68c52526 |
2420 | function user_can_assign($context, $targetroleid) { |
eef868d1 |
2421 | |
68c52526 |
2422 | // first check if user has override capability |
2423 | // if not return false; |
2424 | if (!has_capability('moodle/role:assign', $context)) { |
eef868d1 |
2425 | return false; |
68c52526 |
2426 | } |
2427 | // pull out all active roles of this user from this context(or above) |
c0614051 |
2428 | if ($userroles = get_user_roles($context)) { |
2429 | foreach ($userroles as $userrole) { |
2430 | // if any in the role_allow_override table, then it's ok |
2431 | if (get_record('role_allow_assign', 'roleid', $userrole->roleid, 'allowassign', $targetroleid)) { |
2432 | return true; |
2433 | } |
68c52526 |
2434 | } |
2435 | } |
eef868d1 |
2436 | |
2437 | return false; |
68c52526 |
2438 | } |
2439 | |
ece4945b |
2440 | /** Returns all site roles in correct sort order. |
2441 | * |
2442 | */ |
2443 | function get_all_roles() { |
2444 | return get_records('role', '', '', 'sortorder ASC'); |
2445 | } |
2446 | |
945f88ca |
2447 | /** |
2448 | * gets all the user roles assigned in this context, or higher contexts |
2449 | * this is mainly used when checking if a user can assign a role, or overriding a role |
2450 | * i.e. we need to know what this user holds, in order to verify against allow_assign and |
2451 | * allow_override tables |
2452 | * @param object $context |
2453 | * @param int $userid |
2454 | * @return array |
2455 | */ |
5b630667 |
2456 | function get_user_roles($context, $userid=0, $checkparentcontexts=true) { |
68c52526 |
2457 | |
2458 | global $USER, $CFG, $db; |
c0614051 |
2459 | |
2460 | if (empty($userid)) { |
2461 | if (empty($USER->id)) { |
2462 | return array(); |
2463 | } |
2464 | $userid = $USER->id; |
2465 | } |
2466 | |
5b630667 |
2467 | if ($checkparentcontexts && ($parents = get_parent_contexts($context))) { |
2468 | $contexts = ' ra.contextid IN ('.implode(',' , $parents).','.$context->id.')'; |
c0614051 |
2469 | } else { |
5b630667 |
2470 | $contexts = ' ra.contextid = \''.$context->id.'\''; |
c0614051 |
2471 | } |
2472 | |
31f26796 |
2473 | return get_records_sql('SELECT ra.*, r.name, r.shortname |
5b630667 |
2474 | FROM '.$CFG->prefix.'role_assignments ra, |
ec6eb110 |
2475 | '.$CFG->prefix.'role r, |
2476 | '.$CFG->prefix.'context c |
c0614051 |
2477 | WHERE ra.userid = '.$userid. |
5b630667 |
2478 | ' AND ra.roleid = r.id |
ec6eb110 |
2479 | AND ra.contextid = c.id |
eef868d1 |
2480 | AND '.$contexts. |
ece4945b |
2481 | ' ORDER BY c.contextlevel DESC, r.sortorder ASC'); |
68c52526 |
2482 | } |
2483 | |
945f88ca |
2484 | /** |
eef868d1 |
2485 | * Creates a record in the allow_override table |
945f88ca |
2486 | * @param int sroleid - source roleid |
2487 | * @param int troleid - target roleid |
2488 | * @return int - id or false |
2489 | */ |
2490 | function allow_override($sroleid, $troleid) { |
ece4945b |
2491 | $record = new object(); |
945f88ca |
2492 | $record->roleid = $sroleid; |
2493 | $record->allowoverride = $troleid; |
2494 | return insert_record('role_allow_override', $record); |
2495 | } |
2496 | |
2497 | /** |
eef868d1 |
2498 | * Creates a record in the allow_assign table |
945f88ca |
2499 | * @param int sroleid - source roleid |
2500 | * @param int troleid - target roleid |
2501 | * @return int - id or false |
2502 | */ |
2503 | function allow_assign($sroleid, $troleid) { |
ff64aaea |
2504 | $record = new object; |
945f88ca |
2505 | $record->roleid = $sroleid; |
2506 | $record->allowassign = $troleid; |
2507 | return insert_record('role_allow_assign', $record); |
2508 | } |
2509 | |
2510 | /** |
ff64aaea |
2511 | * Gets a list of roles that this user can assign in this context |
945f88ca |
2512 | * @param object $context |
2513 | * @return array |
2514 | */ |
2515 | function get_assignable_roles ($context) { |
2516 | |
945f88ca |
2517 | $options = array(); |
ff64aaea |
2518 | |
ece4945b |
2519 | if ($roles = get_all_roles()) { |
ff64aaea |
2520 | foreach ($roles as $role) { |
2521 | if (user_can_assign($context, $role->id)) { |
65b0c132 |
2522 | $options[$role->id] = strip_tags(format_string($role->name, true)); |
ff64aaea |
2523 | } |
945f88ca |
2524 | } |
2525 | } |
2526 | return $options; |
2527 | } |
2528 | |
2529 | /** |
ff64aaea |
2530 | * Gets a list of roles that this user can override in this context |
945f88ca |
2531 | * @param object $context |
2532 | * @return array |
2533 | */ |
2534 | function get_overridable_roles ($context) { |
2535 | |
945f88ca |
2536 | $options = array(); |
ff64aaea |
2537 | |
ece4945b |
2538 | if ($roles = get_all_roles()) { |
ff64aaea |
2539 | foreach ($roles as $role) { |
2540 | if (user_can_override($context, $role->id)) { |
65b0c132 |
2541 | $options[$role->id] = strip_tags(format_string($role->name, true)); |
ff64aaea |
2542 | } |
945f88ca |
2543 | } |
ff64aaea |
2544 | } |
eef868d1 |
2545 | |
2546 | return $options; |
945f88ca |
2547 | } |
1648afb2 |
2548 | |
b963384f |
2549 | /* |
2550 | * Returns a role object that is the default role for new enrolments |
2551 | * in a given course |
2552 | * |
eef868d1 |
2553 | * @param object $course |
b963384f |
2554 | * @return object $role |
2555 | */ |
2556 | function get_default_course_role($course) { |
2557 | global $CFG; |
2558 | |
2559 | /// First let's take the default role the course may have |
2560 | if (!empty($course->defaultrole)) { |
2561 | if ($role = get_record('role', 'id', $course->defaultrole)) { |
2562 | return $role; |
2563 | } |
2564 | } |
2565 | |
2566 | /// Otherwise the site setting should tell us |
2567 | if ($CFG->defaultcourseroleid) { |
2568 | if ($role = get_record('role', 'id', $CFG->defaultcourseroleid)) { |
2569 | return $role; |
2570 | } |
2571 | } |
2572 | |
2573 | /// It's unlikely we'll get here, but just in case, try and find a student role |
2574 | if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) { |
2575 | return array_shift($studentroles); /// Take the first one |
2576 | } |
2577 | |
2578 | return NULL; |
2579 | } |
2580 | |
1648afb2 |
2581 | |
2582 | /** |
2583 | * who has this capability in this context |
2584 | * does not handling user level resolving!!! |
2585 | * i.e 1 person has 2 roles 1 allow, 1 prevent, this will not work properly |
2586 | * @param $context - object |
2587 | * @param $capability - string capability |
2588 | * @param $fields - fields to be pulled |
2589 | * @param $sort - the sort order |
04417640 |
2590 | * @param $limitfrom - number of records to skip (offset) |
eef868d1 |
2591 | * @param $limitnum - number of records to fetch |
1c45e42e |
2592 | * @param $groups - single group or array of groups - group(s) user is in |
71dea306 |
2593 | * @param $exceptions - list of users to exclude |
1648afb2 |
2594 | */ |
eef868d1 |
2595 | function get_users_by_capability($context, $capability, $fields='', $sort='', |
1d546bb1 |
2596 | $limitfrom='', $limitnum='', $groups='', $exceptions='', $doanything=true) { |
1648afb2 |
2597 | global $CFG; |
eef868d1 |
2598 | |
64026e8c |
2599 | /// Sorting out groups |
1c45e42e |
2600 | if ($groups) { |
71dea306 |
2601 | $groupjoin = 'INNER JOIN '.$CFG->prefix.'groups_members gm ON gm.userid = ra.userid'; |
eef868d1 |
2602 | |
1c45e42e |
2603 | if (is_array($groups)) { |
a05708ad |
2604 | $groupsql = 'AND gm.groupid IN ('.implode(',', $groups).')'; |
1c45e42e |
2605 | } else { |
eef868d1 |
2606 | $groupsql = 'AND gm.groupid = '.$groups; |
1c45e42e |
2607 | } |
2608 | } else { |
2609 | $groupjoin = ''; |
eef868d1 |
2610 | $groupsql = ''; |
1c45e42e |
2611 | } |
eef868d1 |
2612 | |
64026e8c |
2613 | /// Sorting out exceptions |
5081e786 |
2614 | $exceptionsql = $exceptions ? "AND u.id NOT IN ($exceptions)" : ''; |
64026e8c |
2615 | |
2616 | /// Set up default fields |
2617 | if (empty($fields)) { |
5b630667 |
2618 | $fields = 'u.*, ul.timeaccess as lastaccess, ra.hidden'; |
64026e8c |
2619 | } |
2620 | |
2621 | /// Set up default sort |
2622 | if (empty($sort)) { |
2623 | $sort = 'ul.timeaccess'; |
2624 | } |
2625 | |
eef868d1 |
2626 | $sortby = $sort ? " ORDER BY $sort " : ''; |
2627 | |
64026e8c |
2628 | /// If context is a course, then construct sql for ul |
aad2ba95 |
2629 | if ($context->contextlevel == CONTEXT_COURSE) { |
71dea306 |
2630 | $courseid = $context->instanceid; |
f00b7f8d |
2631 | $coursesql = "AND (ul.courseid = $courseid OR ul.courseid IS NULL)"; |
5081e786 |
2632 | } else { |
2633 | $coursesql = ''; |
71dea306 |
2634 | } |
64026e8c |
2635 | |
2636 | /// Sorting out roles with this capability set |
9d829c68 |
2637 | if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) { |
1d546bb1 |
2638 | if (!$doanything) { |
2639 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
2640 | return false; // Something is seriously wrong |
2641 | } |
2642 | $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext); |
e836a7dd |
2643 | } |
e836a7dd |
2644 | |
9d829c68 |
2645 | $validroleids = array(); |
e836a7dd |
2646 | foreach ($possibleroles as $possiblerole) { |
1d546bb1 |
2647 | if (!$doanything) { |
2648 | if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included |
2649 | continue; |
2650 | } |
e836a7dd |
2651 | } |
bccdf227 |
2652 | if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list |
2653 | if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0 |
2654 | $validroleids[] = $possiblerole->id; |
2655 | } |
9d829c68 |
2656 | } |
1648afb2 |
2657 | } |
1d546bb1 |
2658 | if (empty($validroleids)) { |
2659 | return false; |
2660 | } |
9d829c68 |
2661 | $roleids = '('.implode(',', $validroleids).')'; |
2662 | } else { |
2663 | return false; // No need to continue, since no roles have this capability set |
eef868d1 |
2664 | } |
64026e8c |
2665 | |
2666 | /// Construct the main SQL |
71dea306 |
2667 | $select = " SELECT $fields"; |
eef868d1 |
2668 | $from = " FROM {$CFG->prefix}user u |
2669 | INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id |
0a3e9703 |
2670 | INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid |
71dea306 |
2671 | LEFT OUTER JOIN {$CFG->prefix}user_lastaccess ul ON ul.userid = u.id |
2672 | $groupjoin"; |
eef868d1 |
2673 | $where = " WHERE ra.contextid ".get_related_contexts_string($context)." |
2674 | AND u.deleted = 0 |
2675 | AND ra.roleid in $roleids |
71dea306 |
2676 | $exceptionsql |
2677 | $coursesql |
2678 | $groupsql"; |
2679 | |
eef868d1 |
2680 | return get_records_sql($select.$from.$where.$sortby, $limitfrom, $limitnum); |
1648afb2 |
2681 | } |
7700027f |
2682 | |
ab5c9044 |
2683 | /** |
2684 | * gets all the users assigned this role in this context or higher |
2685 | * @param int roleid |
2686 | * @param int contextid |
2687 | * @param bool parent if true, get list of users assigned in higher context too |
2688 | * @return array() |
2689 | */ |
2690 | function get_role_users($roleid, $context, $parent=false) { |
2691 | global $CFG; |
eef868d1 |
2692 | |
ab5c9044 |
2693 | if ($parent) { |
2694 | if ($contexts = get_parent_contexts($context)) { |
2695 | $parentcontexts = 'r.contextid IN ('.implode(',', $contexts).')'; |
2696 | } else { |
eef868d1 |
2697 | $parentcontexts = ''; |
ab5c9044 |
2698 | } |
2699 | } else { |
eef868d1 |
2700 | $parentcontexts = ''; |
2701 | } |
2702 | |
2703 | $SQL = "select u.* |
2704 | from {$CFG->prefix}role_assignments r, |
2705 | {$CFG->prefix}user u |
2706 | where (r.contextid = $context->id $parentcontexts) |
2707 | and r.roleid = $roleid |
ab5c9044 |
2708 | and u.id = r.userid"; // join now so that we can just use fullname() later |
eef868d1 |
2709 | |
ab5c9044 |
2710 | return get_records_sql($SQL); |
2711 | } |
2712 | |
eef868d1 |
2713 | /** |
d76a5a7f |
2714 | * This function gets the list of courses that this user has a particular capability in |
2715 | * This is not the most efficient way of doing this |
2716 | * @param string capability |
2717 | * @param int $userid |
2718 | * @return array |
2719 | */ |
2720 | function get_user_capability_course($capability, $userid='') { |
eef868d1 |
2721 | |
d76a5a7f |
2722 | global $USER; |
2723 | if (!$userid) { |
eef868d1 |
2724 | $userid = $USER->id; |
d76a5a7f |
2725 | } |
eef868d1 |
2726 | |
d76a5a7f |
2727 | $usercourses = array(); |
2728 | $courses = get_records_select('course', '', '', 'id, id'); |
eef868d1 |
2729 | |
d76a5a7f |
2730 | foreach ($courses as $course) { |
2731 | if (has_capability($capability, get_context_capability(CONTEXT_COURSE, $course->id))) { |
2732 | $usercourses[] = $course; |
2733 | } |
2734 | } |
eef868d1 |
2735 | return $usercourses; |
e38f38c3 |
2736 | } |
2737 | |
2738 | |
2739 | /** This function finds the roles assigned directly to this context only |
2740 | * i.e. no parents role |
2741 | * @param object $context |
2742 | * @return array |
2743 | */ |
2744 | function get_roles_on_exact_context($context) { |
b5959f30 |
2745 | |
e38f38c3 |
2746 | global $CFG; |
49293027 |
2747 | |
b5959f30 |
2748 | return get_records_sql("SELECT DISTINCT r.* |
e38f38c3 |
2749 | FROM {$CFG->prefix}role_assignments ra, |
2750 | {$CFG->prefix}role r |
2751 | WHERE ra.roleid = r.id |
2752 | AND ra.contextid = $context->id"); |
b5959f30 |
2753 | |
49293027 |
2754 | } |
2755 | |
b5959f30 |
2756 | /* |
3a52e764 |
2757 | * Switches the current user to another role for the current session and only |
b5959f30 |
2758 | * in the given context. If roleid is not valid (eg 0) or the current user |
2759 | * doesn't have permissions to be switching roles then the user's session |
3a52e764 |
2760 | * is compltely reset to have their normal roles. |
2761 | * @param integer $roleid |
2762 | * @param object $context |
2763 | * @return bool |
2764 | */ |
2765 | function role_switch($roleid, $context) { |
2766 | global $USER; |
2767 | |
2768 | global $db; |
2769 | |
2770 | /// If we can't use this or are already using it or no role was specified then bail completely and reset |
b5959f30 |
2771 | if (empty($roleid) || !has_capability('moodle/role:switchroles', $context) |
2d07587b |
2772 | || !empty($USER->switchrole[$context->id]) || !confirm_sesskey()) { |
2773 | load_user_capability('', $context); // Reset all permissions for this context to normal |
2774 | unset($USER->switchrole[$context->id]); // Delete old capabilities |
3a52e764 |
2775 | return true; |
2776 | } |
2777 | |
2778 | /// We're allowed to switch but can we switch to the specified role? Use assignable roles to check. |
2779 | if (!$roles = get_assignable_roles($context)) { |
2780 | return false; |
2781 | } |
2782 | |
2783 | if (empty($roles[$roleid])) { /// We can't switch to this particular role |
2784 | return false; |
2785 | } |
2786 | |
2787 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
2788 | return false; |
2789 | } |
2790 | |
2791 | /// We have a valid roleid that this user can switch to, so let's set up the session |
2792 | |
2d07587b |
2793 | $USER->switchrole[$context->id] = $roleid; // So we know later what state we are in |
3a52e764 |
2794 | |
2795 | unset($USER->capabilities[$context->id]); // Delete old capabilities |
2796 | |
2797 | if ($capabilities = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $sitecontext->id")) { |
2798 | foreach ($capabilities as $capability) { |
2799 | $USER->capabilities[$context->id][$capability->capability] = $capability->permission; |
2800 | } |
2801 | } |
2802 | |
2d07587b |
2803 | /// Add some permissions we are really going to always need, even if the role doesn't have them! |
3a52e764 |
2804 | |
2805 | $USER->capabilities[$context->id]['moodle/course:view'] = CAP_ALLOW; |
2806 | |
2807 | return true; |
2808 | |
2809 | } |
2810 | |
2811 | |
49293027 |
2812 | // get any role that has an override on exact context |
2813 | function get_roles_with_override_on_context($context) { |
b5959f30 |
2814 | |
49293027 |
2815 | global $CFG; |
b5959f30 |
2816 | |
49293027 |
2817 | return get_records_sql("SELECT DISTINCT r.* |
2818 | FROM {$CFG->prefix}role_capabilities rc, |
2819 | {$CFG->prefix}role r |
2820 | WHERE rc.roleid = r.id |
2821 | AND rc.contextid = $context->id"); |
2822 | } |
2823 | |
2824 | // get all capabilities for this role on this context (overrids) |
2825 | function get_capabilities_from_role_on_context($role, $context) { |
b5959f30 |
2826 | |
49293027 |
2827 | global $CFG; |
b5959f30 |
2828 | |
2829 | return get_records_sql("SELECT * |
49293027 |
2830 | FROM {$CFG->prefix}role_capabilities |
2831 | WHERE contextid = $context->id |
2832 | AND roleid = $role->id"); |
01e52ac7 |
2833 | } |
2834 | |
5b5781f4 |
2835 | // find out which roles has assignment on this context |
2836 | function get_roles_with_assignment_on_context($context) { |
ece4945b |
2837 | |
5b5781f4 |
2838 | global $CFG; |
ece4945b |
2839 | |
5b5781f4 |
2840 | return get_records_sql("SELECT DISTINCT r.* |
2841 | FROM {$CFG->prefix}role_assignments ra, |
2842 | {$CFG->prefix}role r |
2843 | WHERE ra.roleid = r.id |
2844 | AND ra.contextid = $context->id"); |
2845 | } |
2846 | |
2847 | |
2848 | |
01e52ac7 |
2849 | /* find all user assignemnt of users for this role, on this context |
2850 | */ |
2851 | function get_users_from_role_on_context($role, $context) { |
b5959f30 |
2852 | |
01e52ac7 |
2853 | global $CFG; |
b5959f30 |
2854 | |
01e52ac7 |
2855 | return get_records_sql("SELECT * |
2856 | FROM {$CFG->prefix}role_assignments |
2857 | WHERE contextid = $context->id |
b5959f30 |
2858 | AND roleid = $role->id"); |
01e52ac7 |
2859 | } |
3a52e764 |
2860 | |
f3652521 |
2861 | ?> |