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])) { |
432 | return ($capabilities[$context->id][$capability]); |
bbbf2d40 |
433 | } |
9425b25f |
434 | |
bbbf2d40 |
435 | /* Then, we check the cache recursively */ |
9425b25f |
436 | $permission = 0; |
437 | |
aad2ba95 |
438 | switch ($context->contextlevel) { |
bbbf2d40 |
439 | |
440 | case CONTEXT_SYSTEM: // by now it's a definite an inherit |
441 | $permission = 0; |
442 | break; |
443 | |
444 | case CONTEXT_PERSONAL: |
0468976c |
445 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
446 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
447 | break; |
9425b25f |
448 | |
4b10f08b |
449 | case CONTEXT_USER: |
0468976c |
450 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
451 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
452 | break; |
9425b25f |
453 | |
bbbf2d40 |
454 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
455 | $coursecat = get_record('course_categories','id',$context->instanceid); |
0468976c |
456 | if (!empty($coursecat->parent)) { // return parent value if it exists |
457 | $parentcontext = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
bbbf2d40 |
458 | } else { // else return site value |
0468976c |
459 | $parentcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
bbbf2d40 |
460 | } |
0468976c |
461 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
462 | break; |
463 | |
464 | case CONTEXT_COURSE: // 1 to 1 to course cat |
465 | // find the course cat, and return its value |
466 | $course = get_record('course','id',$context->instanceid); |
0468976c |
467 | $parentcontext = get_context_instance(CONTEXT_COURSECAT, $course->category); |
468 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
469 | break; |
470 | |
471 | case CONTEXT_GROUP: // 1 to 1 to course |
472 | $group = get_record('groups','id',$context->instanceid); |
0468976c |
473 | $parentcontext = get_context_instance(CONTEXT_COURSE, $group->courseid); |
474 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
475 | break; |
476 | |
477 | case CONTEXT_MODULE: // 1 to 1 to course |
478 | $cm = get_record('course_modules','id',$context->instanceid); |
0468976c |
479 | $parentcontext = get_context_instance(CONTEXT_COURSE, $cm->course); |
480 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
481 | break; |
482 | |
483 | case CONTEXT_BLOCK: // 1 to 1 to course |
484 | $block = get_record('block_instance','id',$context->instanceid); |
0468976c |
485 | $parentcontext = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check |
486 | $permission = capability_search($capability, $parentcontext, $capabilities); |
bbbf2d40 |
487 | break; |
488 | |
489 | default: |
490 | error ('This is an unknown context!'); |
491 | return false; |
492 | } |
9425b25f |
493 | |
98882637 |
494 | return $permission; |
bbbf2d40 |
495 | } |
496 | |
497 | |
498 | /** |
499 | * This function should be called immediately after a login, when $USER is set. |
500 | * It will build an array of all the capabilities at each level |
501 | * i.e. site/metacourse/course_category/course/moduleinstance |
502 | * Note we should only load capabilities if they are explicitly assigned already, |
503 | * we should not load all module's capability! |
504 | * @param $userid - the id of the user whose capabilities we want to load |
505 | * @return array |
506 | * possible just s simple 2D array with [contextid][capabilityname] |
507 | * [Capabilities] => [26][forum_post] = 1 |
508 | * [26][forum_start] = -8990 |
509 | * [26][forum_edit] = -1 |
510 | * [273][blah blah] = 1 |
511 | * [273][blah blah blah] = 2 |
512 | */ |
0468976c |
513 | function load_user_capability($capability='', $context ='', $userid='') { |
d140ad3f |
514 | |
98882637 |
515 | global $USER, $CFG; |
bbbf2d40 |
516 | |
517 | if (empty($userid)) { |
dc411d1b |
518 | if (empty($USER->id)) { // We have no user to get capabilities for |
64026e8c |
519 | debugging('User not logged in for load_user_capability!'); |
dc411d1b |
520 | return false; |
521 | } |
64026e8c |
522 | unset($USER->capabilities); // We don't want possible older capabilites hanging around |
523 | |
524 | check_enrolment_plugins($USER); // Call "enrol" system to ensure that we have the correct picture |
8f8ed475 |
525 | |
bbbf2d40 |
526 | $userid = $USER->id; |
dc411d1b |
527 | $otheruserid = false; |
bbbf2d40 |
528 | } else { |
64026e8c |
529 | if (!$user = get_record('user', 'id', $userid)) { |
530 | debugging('Non-existent userid in load_user_capability!'); |
531 | return false; |
532 | } |
533 | |
534 | check_enrolment_plugins($user); // Ensure that we have the correct picture |
535 | |
9425b25f |
536 | $otheruserid = $userid; |
bbbf2d40 |
537 | } |
9425b25f |
538 | |
5f70bcc3 |
539 | |
540 | /// First we generate a list of all relevant contexts of the user |
541 | |
542 | $usercontexts = array(); |
bbbf2d40 |
543 | |
0468976c |
544 | if ($context) { // if context is specified |
eef868d1 |
545 | $usercontexts = get_parent_contexts($context); |
98882637 |
546 | } else { // else, we load everything |
5f70bcc3 |
547 | if ($userroles = get_records('role_assignments','userid',$userid)) { |
548 | foreach ($userroles as $userrole) { |
549 | $usercontexts[] = $userrole->contextid; |
550 | } |
98882637 |
551 | } |
5f70bcc3 |
552 | } |
553 | |
554 | /// Set up SQL fragments for searching contexts |
555 | |
556 | if ($usercontexts) { |
0468976c |
557 | $listofcontexts = '('.implode(',', $usercontexts).')'; |
5f70bcc3 |
558 | $searchcontexts1 = "c1.id IN $listofcontexts AND"; |
5f70bcc3 |
559 | } else { |
c76e095f |
560 | $searchcontexts1 = ''; |
bbbf2d40 |
561 | } |
3ca2dea5 |
562 | |
64026e8c |
563 | if ($capability) { |
564 | $capsearch = " AND rc.capability = '$capability' "; |
565 | } else { |
eef868d1 |
566 | $capsearch =""; |
64026e8c |
567 | } |
568 | |
e38f38c3 |
569 | /// Set up SQL fragments for timestart, timeend etc |
570 | $now = time(); |
85f101fa |
571 | $timesql = "AND ((ra.timestart = 0 OR ra.timestart < $now) AND (ra.timeend = 0 OR ra.timeend > $now))"; |
e38f38c3 |
572 | |
5f70bcc3 |
573 | /// Then we use 1 giant SQL to bring out all relevant capabilities. |
574 | /// The first part gets the capabilities of orginal role. |
575 | /// The second part gets the capabilities of overriden roles. |
bbbf2d40 |
576 | |
98882637 |
577 | $siteinstance = get_context_instance(CONTEXT_SYSTEM, SITEID); |
bbbf2d40 |
578 | |
c76e095f |
579 | $SQL = " SELECT rc.capability, c1.id, (c1.contextlevel * 100) AS aggrlevel, |
bbbf2d40 |
580 | SUM(rc.permission) AS sum |
581 | FROM |
eef868d1 |
582 | {$CFG->prefix}role_assignments ra, |
42ac3ecf |
583 | {$CFG->prefix}role_capabilities rc, |
584 | {$CFG->prefix}context c1 |
bbbf2d40 |
585 | WHERE |
d4649c76 |
586 | ra.contextid=c1.id AND |
587 | ra.roleid=rc.roleid AND |
bbbf2d40 |
588 | ra.userid=$userid AND |
5f70bcc3 |
589 | $searchcontexts1 |
eef868d1 |
590 | rc.contextid=$siteinstance->id |
98882637 |
591 | $capsearch |
e38f38c3 |
592 | $timesql |
bbbf2d40 |
593 | GROUP BY |
aad2ba95 |
594 | rc.capability, (c1.contextlevel * 100), c1.id |
bbbf2d40 |
595 | HAVING |
41811960 |
596 | SUM(rc.permission) != 0 |
bbbf2d40 |
597 | UNION |
598 | |
c76e095f |
599 | SELECT rc.capability, c2.id, (c1.contextlevel * 100 + c2.contextlevel) AS aggrlevel, |
bbbf2d40 |
600 | SUM(rc.permission) AS sum |
601 | FROM |
42ac3ecf |
602 | {$CFG->prefix}role_assignments ra, |
603 | {$CFG->prefix}role_capabilities rc, |
604 | {$CFG->prefix}context c1, |
605 | {$CFG->prefix}context c2 |
bbbf2d40 |
606 | WHERE |
d4649c76 |
607 | ra.contextid=c1.id AND |
eef868d1 |
608 | ra.roleid=rc.roleid AND |
609 | ra.userid=$userid AND |
610 | rc.contextid=c2.id AND |
5f70bcc3 |
611 | $searchcontexts1 |
5f70bcc3 |
612 | rc.contextid != $siteinstance->id |
bbbf2d40 |
613 | $capsearch |
e38f38c3 |
614 | $timesql |
eef868d1 |
615 | |
bbbf2d40 |
616 | GROUP BY |
c76e095f |
617 | rc.capability, (c1.contextlevel * 100 + c2.contextlevel), c2.id, c1.id |
bbbf2d40 |
618 | HAVING |
41811960 |
619 | SUM(rc.permission) != 0 |
bbbf2d40 |
620 | ORDER BY |
75e84883 |
621 | aggrlevel ASC |
bbbf2d40 |
622 | "; |
623 | |
98882637 |
624 | $capabilities = array(); // Reinitialize. |
75e84883 |
625 | if (!$rs = get_recordset_sql($SQL)) { |
626 | error("Query failed in load_user_capability."); |
627 | } |
5cf38a57 |
628 | |
bbbf2d40 |
629 | if ($rs && $rs->RecordCount() > 0) { |
630 | while (!$rs->EOF) { |
75e84883 |
631 | $array = $rs->fields; |
632 | $temprecord = new object; |
eef868d1 |
633 | |
98882637 |
634 | foreach ($array as $key=>$val) { |
75e84883 |
635 | if ($key == 'aggrlevel') { |
aad2ba95 |
636 | $temprecord->contextlevel = $val; |
75e84883 |
637 | } else { |
638 | $temprecord->{$key} = $val; |
639 | } |
98882637 |
640 | } |
bbbf2d40 |
641 | $capabilities[] = $temprecord; |
642 | $rs->MoveNext(); |
643 | } |
644 | } |
eef868d1 |
645 | |
bbbf2d40 |
646 | /* so up to this point we should have somethign like this |
aad2ba95 |
647 | * $capabilities[1] ->contextlevel = 1000 |
bbbf2d40 |
648 | ->module = SITEID |
649 | ->capability = do_anything |
650 | ->id = 1 (id is the context id) |
651 | ->sum = 0 |
eef868d1 |
652 | |
aad2ba95 |
653 | * $capabilities[2] ->contextlevel = 1000 |
bbbf2d40 |
654 | ->module = SITEID |
655 | ->capability = post_messages |
656 | ->id = 1 |
657 | ->sum = -9000 |
658 | |
aad2ba95 |
659 | * $capabilittes[3] ->contextlevel = 3000 |
bbbf2d40 |
660 | ->module = course |
661 | ->capability = view_course_activities |
662 | ->id = 25 |
663 | ->sum = 1 |
664 | |
aad2ba95 |
665 | * $capabilittes[4] ->contextlevel = 3000 |
bbbf2d40 |
666 | ->module = course |
667 | ->capability = view_course_activities |
668 | ->id = 26 |
669 | ->sum = 0 (this is another course) |
eef868d1 |
670 | |
aad2ba95 |
671 | * $capabilities[5] ->contextlevel = 3050 |
bbbf2d40 |
672 | ->module = course |
673 | ->capability = view_course_activities |
674 | ->id = 25 (override in course 25) |
675 | ->sum = -1 |
676 | * .... |
677 | * now we proceed to write the session array, going from top to bottom |
678 | * at anypoint, we need to go up and check parent to look for prohibit |
679 | */ |
680 | // print_object($capabilities); |
681 | |
682 | /* This is where we write to the actualy capabilities array |
683 | * what we need to do from here on is |
684 | * going down the array from lowest level to highest level |
685 | * 1) recursively check for prohibit, |
686 | * if any, we write prohibit |
687 | * else, we write the value |
688 | * 2) at an override level, we overwrite current level |
689 | * if it's not set to prohibit already, and if different |
690 | * ........ that should be it ........ |
691 | */ |
98882637 |
692 | $usercap = array(); // for other user's capabilities |
bbbf2d40 |
693 | foreach ($capabilities as $capability) { |
694 | |
7bfa3101 |
695 | if (!$context = get_context_instance_by_id($capability->id)) { |
696 | continue; // incorrect stale context |
697 | } |
0468976c |
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 | |
459c1ff1 |
917 | /// Create a system wide context for assignemnt. |
bbbf2d40 |
918 | $systemcontext = $context = get_context_instance(CONTEXT_SYSTEM, SITEID); |
919 | |
1afecc03 |
920 | |
459c1ff1 |
921 | /// Create default/legacy roles and capabilities. |
922 | /// (1 legacy capability per legacy role at system level). |
923 | |
924 | $adminrole = create_role(get_string('administrator'), 'admin', |
925 | get_string('administratordescription'), 'moodle/legacy:admin'); |
926 | $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', |
927 | get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator'); |
928 | $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', |
929 | get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher'); |
930 | $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', |
931 | get_string('noneditingteacherdescription'), 'moodle/legacy:teacher'); |
932 | $studentrole = create_role(get_string('defaultcoursestudent'), 'student', |
933 | get_string('defaultcoursestudentdescription'), 'moodle/legacy:student'); |
934 | $guestrole = create_role(get_string('guest'), 'guest', |
935 | get_string('guestdescription'), 'moodle/legacy:guest'); |
936 | |
937 | /// Now is the correct moment to install capabilitites - after creation of legacy roles, but before assigning of roles |
938 | |
98882637 |
939 | if (!assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $systemcontext->id)) { |
bbbf2d40 |
940 | error('Could not assign moodle/site:doanything to the admin role'); |
941 | } |
250934b8 |
942 | if (!update_capabilities()) { |
943 | error('Had trouble upgrading the core capabilities for the Roles System'); |
944 | } |
1afecc03 |
945 | |
459c1ff1 |
946 | /// Look inside user_admin, user_creator, user_teachers, user_students and |
947 | /// assign above new roles. If a user has both teacher and student role, |
948 | /// only teacher role is assigned. The assignment should be system level. |
949 | |
1afecc03 |
950 | $dbtables = $db->MetaTables('TABLES'); |
eef868d1 |
951 | |
1afecc03 |
952 | |
459c1ff1 |
953 | /// Upgrade the admins. |
954 | /// Sort using id ASC, first one is primary admin. |
955 | |
1afecc03 |
956 | if (in_array($CFG->prefix.'user_admins', $dbtables)) { |
f1dcf000 |
957 | if ($rs = get_recordset_sql('SELECT * from '.$CFG->prefix.'user_admins ORDER BY ID ASC')) { |
958 | while (! $rs->EOF) { |
959 | $admin = $rs->FetchObj(); |
1afecc03 |
960 | role_assign($adminrole, $admin->userid, 0, $systemcontext->id); |
f1dcf000 |
961 | $rs->MoveNext(); |
1afecc03 |
962 | } |
963 | } |
964 | } else { |
965 | // This is a fresh install. |
bbbf2d40 |
966 | } |
1afecc03 |
967 | |
968 | |
459c1ff1 |
969 | /// Upgrade course creators. |
1afecc03 |
970 | if (in_array($CFG->prefix.'user_coursecreators', $dbtables)) { |
f1dcf000 |
971 | if ($rs = get_recordset('user_coursecreators')) { |
972 | while (! $rs->EOF) { |
973 | $coursecreator = $rs->FetchObj(); |
56b4d70d |
974 | role_assign($coursecreatorrole, $coursecreator->userid, 0, $systemcontext->id); |
f1dcf000 |
975 | $rs->MoveNext(); |
1afecc03 |
976 | } |
977 | } |
bbbf2d40 |
978 | } |
979 | |
1afecc03 |
980 | |
459c1ff1 |
981 | /// Upgrade editting teachers and non-editting teachers. |
1afecc03 |
982 | if (in_array($CFG->prefix.'user_teachers', $dbtables)) { |
f1dcf000 |
983 | if ($rs = get_recordset('user_teachers')) { |
984 | while (! $rs->EOF) { |
985 | $teacher = $rs->FetchObj(); |
986 | |
17d6a25e |
987 | // populate the user_lastaccess table |
ece4945b |
988 | $access = new object(); |
17d6a25e |
989 | $access->timeaccess = $teacher->timeaccess; |
990 | $access->userid = $teacher->userid; |
991 | $access->courseid = $teacher->course; |
992 | insert_record('user_lastaccess', $access); |
f1dcf000 |
993 | |
17d6a25e |
994 | // assign the default student role |
1afecc03 |
995 | $coursecontext = get_context_instance(CONTEXT_COURSE, $teacher->course); // needs cache |
996 | if ($teacher->editall) { // editting teacher |
997 | role_assign($editteacherrole, $teacher->userid, 0, $coursecontext->id); |
998 | } else { |
999 | role_assign($noneditteacherrole, $teacher->userid, 0, $coursecontext->id); |
1000 | } |
f1dcf000 |
1001 | |
1002 | $rs->MoveNext(); |
1afecc03 |
1003 | } |
bbbf2d40 |
1004 | } |
1005 | } |
1afecc03 |
1006 | |
1007 | |
459c1ff1 |
1008 | /// Upgrade students. |
1afecc03 |
1009 | if (in_array($CFG->prefix.'user_students', $dbtables)) { |
f1dcf000 |
1010 | if ($rs = get_recordset('user_students')) { |
1011 | while (! $rs->EOF) { |
1012 | $student = $rs->FetchObj(); |
1013 | |
17d6a25e |
1014 | // populate the user_lastaccess table |
f1dcf000 |
1015 | $access = new object; |
17d6a25e |
1016 | $access->timeaccess = $student->timeaccess; |
1017 | $access->userid = $student->userid; |
1018 | $access->courseid = $student->course; |
1019 | insert_record('user_lastaccess', $access); |
f1dcf000 |
1020 | |
17d6a25e |
1021 | // assign the default student role |
1afecc03 |
1022 | $coursecontext = get_context_instance(CONTEXT_COURSE, $student->course); |
1023 | role_assign($studentrole, $student->userid, 0, $coursecontext->id); |
f1dcf000 |
1024 | |
1025 | $rs->MoveNext(); |
1afecc03 |
1026 | } |
1027 | } |
bbbf2d40 |
1028 | } |
1afecc03 |
1029 | |
1030 | |
459c1ff1 |
1031 | /// Upgrade guest (only 1 entry). |
1afecc03 |
1032 | if ($guestuser = get_record('user', 'username', 'guest')) { |
1033 | role_assign($guestrole, $guestuser->id, 0, $systemcontext->id); |
1034 | } |
1035 | |
459c1ff1 |
1036 | |
1037 | /// Insert the correct records for legacy roles |
945f88ca |
1038 | allow_assign($adminrole, $adminrole); |
1039 | allow_assign($adminrole, $coursecreatorrole); |
1040 | allow_assign($adminrole, $noneditteacherrole); |
eef868d1 |
1041 | allow_assign($adminrole, $editteacherrole); |
945f88ca |
1042 | allow_assign($adminrole, $studentrole); |
1043 | allow_assign($adminrole, $guestrole); |
eef868d1 |
1044 | |
945f88ca |
1045 | allow_assign($coursecreatorrole, $noneditteacherrole); |
1046 | allow_assign($coursecreatorrole, $editteacherrole); |
eef868d1 |
1047 | allow_assign($coursecreatorrole, $studentrole); |
945f88ca |
1048 | allow_assign($coursecreatorrole, $guestrole); |
eef868d1 |
1049 | |
1050 | allow_assign($editteacherrole, $noneditteacherrole); |
1051 | allow_assign($editteacherrole, $studentrole); |
945f88ca |
1052 | allow_assign($editteacherrole, $guestrole); |
eef868d1 |
1053 | |
459c1ff1 |
1054 | /// Set up default permissions for overrides |
945f88ca |
1055 | allow_override($adminrole, $adminrole); |
1056 | allow_override($adminrole, $coursecreatorrole); |
1057 | allow_override($adminrole, $noneditteacherrole); |
eef868d1 |
1058 | allow_override($adminrole, $editteacherrole); |
945f88ca |
1059 | allow_override($adminrole, $studentrole); |
eef868d1 |
1060 | allow_override($adminrole, $guestrole); |
1afecc03 |
1061 | |
746a04c5 |
1062 | |
459c1ff1 |
1063 | /// Delete the old user tables when we are done |
1064 | |
1065 | /// XXX TODO |
1066 | |
bbbf2d40 |
1067 | } |
1068 | |
bbbf2d40 |
1069 | /** |
1070 | * Assign the defaults found in this capabality definition to roles that have |
1071 | * the corresponding legacy capabilities assigned to them. |
1072 | * @param $legacyperms - an array in the format (example): |
1073 | * 'guest' => CAP_PREVENT, |
1074 | * 'student' => CAP_ALLOW, |
1075 | * 'teacher' => CAP_ALLOW, |
1076 | * 'editingteacher' => CAP_ALLOW, |
1077 | * 'coursecreator' => CAP_ALLOW, |
1078 | * 'admin' => CAP_ALLOW |
1079 | * @return boolean - success or failure. |
1080 | */ |
1081 | function assign_legacy_capabilities($capability, $legacyperms) { |
eef868d1 |
1082 | |
bbbf2d40 |
1083 | foreach ($legacyperms as $type => $perm) { |
eef868d1 |
1084 | |
bbbf2d40 |
1085 | $systemcontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
eef868d1 |
1086 | |
bbbf2d40 |
1087 | // The legacy capabilities are: |
1088 | // 'moodle/legacy:guest' |
1089 | // 'moodle/legacy:student' |
1090 | // 'moodle/legacy:teacher' |
1091 | // 'moodle/legacy:editingteacher' |
1092 | // 'moodle/legacy:coursecreator' |
1093 | // 'moodle/legacy:admin' |
eef868d1 |
1094 | |
2e85fffe |
1095 | if ($roles = get_roles_with_capability('moodle/legacy:'.$type, CAP_ALLOW)) { |
1096 | foreach ($roles as $role) { |
1097 | // Assign a site level capability. |
1098 | if (!assign_capability($capability, $perm, $role->id, $systemcontext->id)) { |
1099 | return false; |
1100 | } |
bbbf2d40 |
1101 | } |
1102 | } |
1103 | } |
1104 | return true; |
1105 | } |
1106 | |
1107 | |
cee0901c |
1108 | /** |
1109 | * Checks to see if a capability is a legacy capability. |
1110 | * @param $capabilityname |
1111 | * @return boolean |
1112 | */ |
bbbf2d40 |
1113 | function islegacy($capabilityname) { |
98882637 |
1114 | if (strstr($capabilityname, 'legacy') === false) { |
eef868d1 |
1115 | return false; |
98882637 |
1116 | } else { |
eef868d1 |
1117 | return true; |
98882637 |
1118 | } |
bbbf2d40 |
1119 | } |
1120 | |
cee0901c |
1121 | |
1122 | |
1123 | /********************************** |
bbbf2d40 |
1124 | * Context Manipulation functions * |
1125 | **********************************/ |
1126 | |
bbbf2d40 |
1127 | /** |
9991d157 |
1128 | * Create a new context record for use by all roles-related stuff |
bbbf2d40 |
1129 | * @param $level |
1130 | * @param $instanceid |
3ca2dea5 |
1131 | * |
1132 | * @return object newly created context (or existing one with a debug warning) |
bbbf2d40 |
1133 | */ |
aad2ba95 |
1134 | function create_context($contextlevel, $instanceid) { |
3ca2dea5 |
1135 | if (!$context = get_record('context','contextlevel',$contextlevel,'instanceid',$instanceid)) { |
1136 | if (!validate_context($contextlevel, $instanceid)) { |
1137 | debugging('Error: Invalid context creation request for level "'.s($contextlevel).'", instance "'.s($instanceid).'".'); |
1138 | return NULL; |
1139 | } |
1140 | $context = new object(); |
aad2ba95 |
1141 | $context->contextlevel = $contextlevel; |
bbbf2d40 |
1142 | $context->instanceid = $instanceid; |
3ca2dea5 |
1143 | if ($id = insert_record('context',$context)) { |
1144 | return get_record('context','id',$id); |
1145 | } else { |
1146 | debugging('Error: could not insert new context level "'.s($contextlevel).'", instance "'.s($instanceid).'".'); |
1147 | return NULL; |
1148 | } |
1149 | } else { |
1150 | debugging('Warning: Context id "'.s($context->id).'" not created, because it already exists.'); |
1151 | return $context; |
bbbf2d40 |
1152 | } |
1153 | } |
1154 | |
9991d157 |
1155 | /** |
1156 | * Create a new context record for use by all roles-related stuff |
1157 | * @param $level |
1158 | * @param $instanceid |
3ca2dea5 |
1159 | * |
1160 | * @return true if properly deleted |
9991d157 |
1161 | */ |
1162 | function delete_context($contextlevel, $instanceid) { |
1163 | if ($context = get_context_instance($contextlevel, $instanceid)) { |
1164 | return delete_records('context', 'id', $context->id) && |
1165 | delete_records('role_assignments', 'contextid', $context->id) && |
1166 | delete_records('role_role_capabilities', 'contextid', $context->id); |
1167 | } |
1168 | return true; |
1169 | } |
1170 | |
3ca2dea5 |
1171 | /** |
1172 | * Validate that object with instanceid really exists in given context level. |
1173 | * |
1174 | * return if instanceid object exists |
1175 | */ |
1176 | function validate_context($contextlevel, $instanceid) { |
1177 | switch ($contextlevel) { |
1178 | |
1179 | case CONTEXT_SYSTEM: |
1180 | return ($instanceid == SITEID); |
1181 | |
1182 | case CONTEXT_PERSONAL: |
1183 | return (boolean)count_records('user', 'id', $instanceid); |
1184 | |
1185 | case CONTEXT_USER: |
1186 | return (boolean)count_records('user', 'id', $instanceid); |
1187 | |
1188 | case CONTEXT_COURSECAT: |
1cd3eba9 |
1189 | if ($instanceid == 0) { |
1190 | return true; // site course category |
1191 | } |
3ca2dea5 |
1192 | return (boolean)count_records('course_categories', 'id', $instanceid); |
1193 | |
1194 | case CONTEXT_COURSE: |
1195 | return (boolean)count_records('course', 'id', $instanceid); |
1196 | |
1197 | case CONTEXT_GROUP: |
1198 | return (boolean)count_records('groups', 'id', $instanceid); |
1199 | |
1200 | case CONTEXT_MODULE: |
1201 | return (boolean)count_records('course_modules', 'id', $instanceid); |
1202 | |
1203 | case CONTEXT_BLOCK: |
1204 | return (boolean)count_records('block_instance', 'id', $instanceid); |
1205 | |
1206 | default: |
1207 | return false; |
1208 | } |
1209 | } |
bbbf2d40 |
1210 | |
1211 | /** |
1212 | * Get the context instance as an object. This function will create the |
1213 | * context instance if it does not exist yet. |
1214 | * @param $level |
1215 | * @param $instance |
1216 | */ |
aad2ba95 |
1217 | function get_context_instance($contextlevel=NULL, $instance=SITEID) { |
e5605780 |
1218 | |
51195e6f |
1219 | global $context_cache, $context_cache_id, $CONTEXT; |
a36a3a3f |
1220 | static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_PERSONAL, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK); |
d9a35e12 |
1221 | |
340ea4e8 |
1222 | /// If no level is supplied then return the current global context if there is one |
aad2ba95 |
1223 | if (empty($contextlevel)) { |
340ea4e8 |
1224 | if (empty($CONTEXT)) { |
a36a3a3f |
1225 | //fatal error, code must be fixed |
1226 | error("Error: get_context_instance() called without a context"); |
340ea4e8 |
1227 | } else { |
1228 | return $CONTEXT; |
1229 | } |
e5605780 |
1230 | } |
1231 | |
a36a3a3f |
1232 | /// check allowed context levels |
1233 | if (!in_array($contextlevel, $allowed_contexts)) { |
7bfa3101 |
1234 | // fatal error, code must be fixed - probably typo or switched parameters |
a36a3a3f |
1235 | error('Error: get_context_instance() called with incorrect context level "'.s($contextlevel).'"'); |
1236 | } |
1237 | |
340ea4e8 |
1238 | /// Check the cache |
aad2ba95 |
1239 | if (isset($context_cache[$contextlevel][$instance])) { // Already cached |
1240 | return $context_cache[$contextlevel][$instance]; |
e5605780 |
1241 | } |
1242 | |
340ea4e8 |
1243 | /// Get it from the database, or create it |
aad2ba95 |
1244 | if (!$context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance)) { |
1245 | create_context($contextlevel, $instance); |
1246 | $context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance); |
e5605780 |
1247 | } |
1248 | |
ccfc5ecc |
1249 | /// Only add to cache if context isn't empty. |
1250 | if (!empty($context)) { |
aad2ba95 |
1251 | $context_cache[$contextlevel][$instance] = $context; // Cache it for later |
ccfc5ecc |
1252 | $context_cache_id[$context->id] = $context; // Cache it for later |
1253 | } |
0468976c |
1254 | |
bbbf2d40 |
1255 | return $context; |
1256 | } |
1257 | |
cee0901c |
1258 | |
340ea4e8 |
1259 | /** |
1260 | * Get a context instance as an object, from a given id. |
1261 | * @param $id |
1262 | */ |
1263 | function get_context_instance_by_id($id) { |
1264 | |
d9a35e12 |
1265 | global $context_cache, $context_cache_id; |
1266 | |
340ea4e8 |
1267 | if (isset($context_cache_id[$id])) { // Already cached |
75e84883 |
1268 | return $context_cache_id[$id]; |
340ea4e8 |
1269 | } |
1270 | |
1271 | if ($context = get_record('context', 'id', $id)) { // Update the cache and return |
aad2ba95 |
1272 | $context_cache[$context->contextlevel][$context->instanceid] = $context; |
340ea4e8 |
1273 | $context_cache_id[$context->id] = $context; |
1274 | return $context; |
1275 | } |
1276 | |
1277 | return false; |
1278 | } |
1279 | |
bbbf2d40 |
1280 | |
8737be58 |
1281 | /** |
1282 | * Get the local override (if any) for a given capability in a role in a context |
1283 | * @param $roleid |
0468976c |
1284 | * @param $contextid |
1285 | * @param $capability |
8737be58 |
1286 | */ |
1287 | function get_local_override($roleid, $contextid, $capability) { |
1288 | return get_record('role_capabilities', 'roleid', $roleid, 'capability', $capability, 'contextid', $contextid); |
1289 | } |
1290 | |
1291 | |
bbbf2d40 |
1292 | |
1293 | /************************************ |
1294 | * DB TABLE RELATED FUNCTIONS * |
1295 | ************************************/ |
1296 | |
cee0901c |
1297 | /** |
bbbf2d40 |
1298 | * function that creates a role |
1299 | * @param name - role name |
31f26796 |
1300 | * @param shortname - role short name |
bbbf2d40 |
1301 | * @param description - role description |
1302 | * @param legacy - optional legacy capability |
1303 | * @return id or false |
1304 | */ |
8420bee9 |
1305 | function create_role($name, $shortname, $description, $legacy='') { |
eef868d1 |
1306 | |
98882637 |
1307 | // check for duplicate role name |
eef868d1 |
1308 | |
98882637 |
1309 | if ($role = get_record('role','name', $name)) { |
eef868d1 |
1310 | error('there is already a role with this name!'); |
98882637 |
1311 | } |
eef868d1 |
1312 | |
31f26796 |
1313 | if ($role = get_record('role','shortname', $shortname)) { |
eef868d1 |
1314 | error('there is already a role with this shortname!'); |
31f26796 |
1315 | } |
1316 | |
b5959f30 |
1317 | $role = new object(); |
98882637 |
1318 | $role->name = $name; |
31f26796 |
1319 | $role->shortname = $shortname; |
98882637 |
1320 | $role->description = $description; |
eef868d1 |
1321 | |
8420bee9 |
1322 | //find free sortorder number |
1323 | $role->sortorder = count_records('role'); |
1324 | while (get_record('role','sortorder', $role->sortorder)) { |
1325 | $role->sortorder += 1; |
b5959f30 |
1326 | } |
1327 | |
eef868d1 |
1328 | $context = get_context_instance(CONTEXT_SYSTEM, SITEID); |
1329 | |
98882637 |
1330 | if ($id = insert_record('role', $role)) { |
eef868d1 |
1331 | if ($legacy) { |
1332 | assign_capability($legacy, CAP_ALLOW, $id, $context->id); |
98882637 |
1333 | } |
eef868d1 |
1334 | |
ec7a8b79 |
1335 | /// By default, users with role:manage at site level |
1336 | /// should be able to assign users to this new role, and override this new role's capabilities |
eef868d1 |
1337 | |
ec7a8b79 |
1338 | // find all admin roles |
e46c0987 |
1339 | if ($adminroles = get_roles_with_capability('moodle/role:manage', CAP_ALLOW, $context)) { |
1340 | // foreach admin role |
1341 | foreach ($adminroles as $arole) { |
1342 | // write allow_assign and allow_overrid |
1343 | allow_assign($arole->id, $id); |
eef868d1 |
1344 | allow_override($arole->id, $id); |
e46c0987 |
1345 | } |
ec7a8b79 |
1346 | } |
eef868d1 |
1347 | |
98882637 |
1348 | return $id; |
1349 | } else { |
eef868d1 |
1350 | return false; |
98882637 |
1351 | } |
eef868d1 |
1352 | |
bbbf2d40 |
1353 | } |
1354 | |
8420bee9 |
1355 | /** |
1356 | * function that deletes a role and cleanups up after it |
1357 | * @param roleid - id of role to delete |
1358 | * @return success |
1359 | */ |
1360 | function delete_role($roleid) { |
1361 | $success = true; |
1362 | |
1363 | // first unssign all users |
1364 | if (!role_unassign($roleid)) { |
1365 | debugging("Error while unassigning all users from role with ID $roleid!"); |
1366 | $success = false; |
1367 | } |
1368 | |
1369 | // cleanup all references to this role, ignore errors |
1370 | if ($success) { |
1371 | delete_records('role_capabilities', 'roleid', $roleid); |
1372 | delete_records('role_allow_assign', 'roleid', $roleid); |
1373 | delete_records('role_allow_assign', 'allowassign', $roleid); |
1374 | delete_records('role_allow_override', 'roleid', $roleid); |
1375 | delete_records('role_allow_override', 'allowoverride', $roleid); |
1376 | delete_records('role_names', 'roleid', $roleid); |
1377 | } |
1378 | |
1379 | // finally delete the role itself |
1380 | if ($success and !delete_records('role', 'id', $roleid)) { |
ece4945b |
1381 | debugging("Could not delete role record with ID $roleid!"); |
8420bee9 |
1382 | $success = false; |
1383 | } |
1384 | |
1385 | return $success; |
1386 | } |
1387 | |
bbbf2d40 |
1388 | /** |
1389 | * Function to write context specific overrides, or default capabilities. |
1390 | * @param module - string name |
1391 | * @param capability - string name |
1392 | * @param contextid - context id |
1393 | * @param roleid - role id |
1394 | * @param permission - int 1,-1 or -1000 |
1395 | */ |
e7876c1e |
1396 | function assign_capability($capability, $permission, $roleid, $contextid, $overwrite=false) { |
eef868d1 |
1397 | |
98882637 |
1398 | global $USER; |
eef868d1 |
1399 | |
98882637 |
1400 | if (empty($permission) || $permission == 0) { // if permission is not set |
eef868d1 |
1401 | unassign_capability($capability, $roleid, $contextid); |
98882637 |
1402 | } |
eef868d1 |
1403 | |
2e85fffe |
1404 | $existing = get_record('role_capabilities', 'contextid', $contextid, 'roleid', $roleid, 'capability', $capability); |
e7876c1e |
1405 | |
1406 | if ($existing and !$overwrite) { // We want to keep whatever is there already |
1407 | return true; |
1408 | } |
1409 | |
bbbf2d40 |
1410 | $cap = new object; |
1411 | $cap->contextid = $contextid; |
1412 | $cap->roleid = $roleid; |
1413 | $cap->capability = $capability; |
1414 | $cap->permission = $permission; |
1415 | $cap->timemodified = time(); |
9db12da7 |
1416 | $cap->modifierid = empty($USER->id) ? 0 : $USER->id; |
e7876c1e |
1417 | |
1418 | if ($existing) { |
1419 | $cap->id = $existing->id; |
1420 | return update_record('role_capabilities', $cap); |
1421 | } else { |
1422 | return insert_record('role_capabilities', $cap); |
1423 | } |
bbbf2d40 |
1424 | } |
1425 | |
1426 | |
1427 | /** |
1428 | * Unassign a capability from a role. |
1429 | * @param $roleid - the role id |
1430 | * @param $capability - the name of the capability |
1431 | * @return boolean - success or failure |
1432 | */ |
1433 | function unassign_capability($capability, $roleid, $contextid=NULL) { |
eef868d1 |
1434 | |
98882637 |
1435 | if (isset($contextid)) { |
1436 | $status = delete_records('role_capabilities', 'capability', $capability, |
1437 | 'roleid', $roleid, 'contextid', $contextid); |
1438 | } else { |
1439 | $status = delete_records('role_capabilities', 'capability', $capability, |
1440 | 'roleid', $roleid); |
1441 | } |
1442 | return $status; |
bbbf2d40 |
1443 | } |
1444 | |
1445 | |
1446 | /** |
759ac72d |
1447 | * Get the roles that have a given capability assigned to it. This function |
1448 | * does not resolve the actual permission of the capability. It just checks |
1449 | * for assignment only. |
bbbf2d40 |
1450 | * @param $capability - capability name (string) |
1451 | * @param $permission - optional, the permission defined for this capability |
1452 | * either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT |
1453 | * @return array or role objects |
1454 | */ |
ec7a8b79 |
1455 | function get_roles_with_capability($capability, $permission=NULL, $context='') { |
1456 | |
bbbf2d40 |
1457 | global $CFG; |
eef868d1 |
1458 | |
ec7a8b79 |
1459 | if ($context) { |
1460 | if ($contexts = get_parent_contexts($context)) { |
1461 | $listofcontexts = '('.implode(',', $contexts).')'; |
1462 | } else { |
1463 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
eef868d1 |
1464 | $listofcontexts = '('.$sitecontext->id.')'; // must be site |
1465 | } |
42ac3ecf |
1466 | $contextstr = "AND (rc.contextid = '$context->id' OR rc.contextid IN $listofcontexts)"; |
ec7a8b79 |
1467 | } else { |
1468 | $contextstr = ''; |
1469 | } |
eef868d1 |
1470 | |
1471 | $selectroles = "SELECT r.* |
42ac3ecf |
1472 | FROM {$CFG->prefix}role r, |
1473 | {$CFG->prefix}role_capabilities rc |
bbbf2d40 |
1474 | WHERE rc.capability = '$capability' |
ec7a8b79 |
1475 | AND rc.roleid = r.id $contextstr"; |
bbbf2d40 |
1476 | |
1477 | if (isset($permission)) { |
1478 | $selectroles .= " AND rc.permission = '$permission'"; |
1479 | } |
1480 | return get_records_sql($selectroles); |
1481 | } |
1482 | |
1483 | |
1484 | /** |
a9e1c058 |
1485 | * This function makes a role-assignment (a role for a user or group in a particular context) |
bbbf2d40 |
1486 | * @param $roleid - the role of the id |
1487 | * @param $userid - userid |
1488 | * @param $groupid - group id |
1489 | * @param $contextid - id of the context |
1490 | * @param $timestart - time this assignment becomes effective |
1491 | * @param $timeend - time this assignemnt ceases to be effective |
1492 | * @uses $USER |
1493 | * @return id - new id of the assigment |
1494 | */ |
f44152f4 |
1495 | function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $timeend=0, $hidden=0, $enrol='manual') { |
aa311411 |
1496 | global $USER, $CFG; |
bbbf2d40 |
1497 | |
7eb0b60a |
1498 | debugging("Assign roleid $roleid userid $userid contextid $contextid", DEBUG_DEVELOPER); |
bbbf2d40 |
1499 | |
a9e1c058 |
1500 | /// Do some data validation |
1501 | |
bbbf2d40 |
1502 | if (empty($roleid)) { |
9d829c68 |
1503 | debugging('Role ID not provided'); |
a9e1c058 |
1504 | return false; |
bbbf2d40 |
1505 | } |
1506 | |
1507 | if (empty($userid) && empty($groupid)) { |
9d829c68 |
1508 | debugging('Either userid or groupid must be provided'); |
a9e1c058 |
1509 | return false; |
bbbf2d40 |
1510 | } |
eef868d1 |
1511 | |
7700027f |
1512 | if ($userid && !record_exists('user', 'id', $userid)) { |
82396e5b |
1513 | debugging('User ID '.intval($userid).' does not exist!'); |
7700027f |
1514 | return false; |
1515 | } |
bbbf2d40 |
1516 | |
dc411d1b |
1517 | if ($groupid && !record_exists('groups', 'id', $groupid)) { |
82396e5b |
1518 | debugging('Group ID '.intval($groupid).' does not exist!'); |
dc411d1b |
1519 | return false; |
1520 | } |
1521 | |
7700027f |
1522 | if (!$context = get_context_instance_by_id($contextid)) { |
82396e5b |
1523 | debugging('Context ID '.intval($contextid).' does not exist!'); |
a9e1c058 |
1524 | return false; |
bbbf2d40 |
1525 | } |
1526 | |
a9e1c058 |
1527 | if (($timestart and $timeend) and ($timestart > $timeend)) { |
9d829c68 |
1528 | debugging('The end time can not be earlier than the start time'); |
a9e1c058 |
1529 | return false; |
1530 | } |
1531 | |
7700027f |
1532 | |
a9e1c058 |
1533 | /// Check for existing entry |
1534 | if ($userid) { |
7700027f |
1535 | $ra = get_record('role_assignments', 'roleid', $roleid, 'contextid', $context->id, 'userid', $userid); |
a9e1c058 |
1536 | } else { |
7700027f |
1537 | $ra = get_record('role_assignments', 'roleid', $roleid, 'contextid', $context->id, 'groupid', $groupid); |
a9e1c058 |
1538 | } |
1539 | |
9ebcb4d2 |
1540 | |
a9e1c058 |
1541 | $newra = new object; |
bbbf2d40 |
1542 | |
a9e1c058 |
1543 | if (empty($ra)) { // Create a new entry |
1544 | $newra->roleid = $roleid; |
7700027f |
1545 | $newra->contextid = $context->id; |
a9e1c058 |
1546 | $newra->userid = $userid; |
1547 | $newra->groupid = $groupid; |
1548 | |
1549 | $newra->hidden = $hidden; |
f44152f4 |
1550 | $newra->enrol = $enrol; |
a9e1c058 |
1551 | $newra->timestart = $timestart; |
1552 | $newra->timeend = $timeend; |
1553 | $newra->timemodified = time(); |
1554 | $newra->modifier = empty($USER->id) ? 0 : $USER->id; |
1555 | |
9ebcb4d2 |
1556 | $success = insert_record('role_assignments', $newra); |
a9e1c058 |
1557 | |
1558 | } else { // We already have one, just update it |
1559 | |
1560 | $newra->id = $ra->id; |
1561 | $newra->hidden = $hidden; |
f44152f4 |
1562 | $newra->enrol = $enrol; |
a9e1c058 |
1563 | $newra->timestart = $timestart; |
1564 | $newra->timeend = $timeend; |
1565 | $newra->timemodified = time(); |
1566 | $newra->modifier = empty($USER->id) ? 0 : $USER->id; |
1567 | |
9ebcb4d2 |
1568 | $success = update_record('role_assignments', $newra); |
1569 | } |
1570 | |
7700027f |
1571 | if ($success) { /// Role was assigned, so do some other things |
1572 | |
1573 | /// If the user is the current user, then reload the capabilities too. |
1574 | if (!empty($USER->id) && $USER->id == $userid) { |
1575 | load_user_capability(); |
1576 | } |
1577 | |
0f161e1f |
1578 | /// Ask all the modules if anything needs to be done for this user |
1579 | if ($mods = get_list_of_plugins('mod')) { |
1580 | foreach ($mods as $mod) { |
1581 | include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php'); |
1582 | $functionname = $mod.'_role_assign'; |
1583 | if (function_exists($functionname)) { |
1584 | $functionname($userid, $context); |
1585 | } |
1586 | } |
1587 | } |
1588 | |
1589 | /// Make sure they have an entry in user_lastaccess for courses they can access |
1590 | // role_add_lastaccess_entries($userid, $context); |
a9e1c058 |
1591 | } |
eef868d1 |
1592 | |
4e5f3064 |
1593 | /// now handle metacourse role assignments if in course context |
aad2ba95 |
1594 | if ($success and $context->contextlevel == CONTEXT_COURSE) { |
4e5f3064 |
1595 | if ($parents = get_records('course_meta', 'child_course', $context->instanceid)) { |
1596 | foreach ($parents as $parent) { |
1aad4310 |
1597 | sync_metacourse($parent->parent_course); |
4e5f3064 |
1598 | } |
1599 | } |
1600 | } |
6eb4f823 |
1601 | |
1602 | return $success; |
bbbf2d40 |
1603 | } |
1604 | |
1605 | |
1606 | /** |
1dc1f037 |
1607 | * Deletes one or more role assignments. You must specify at least one parameter. |
bbbf2d40 |
1608 | * @param $roleid |
1609 | * @param $userid |
1610 | * @param $groupid |
1611 | * @param $contextid |
1612 | * @return boolean - success or failure |
1613 | */ |
1dc1f037 |
1614 | function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) { |
d74067e8 |
1615 | |
1616 | global $USER, $CFG; |
eef868d1 |
1617 | |
4e5f3064 |
1618 | $success = true; |
d74067e8 |
1619 | |
1dc1f037 |
1620 | $args = array('roleid', 'userid', 'groupid', 'contextid'); |
1621 | $select = array(); |
1622 | foreach ($args as $arg) { |
1623 | if ($$arg) { |
1624 | $select[] = $arg.' = '.$$arg; |
1625 | } |
1626 | } |
d74067e8 |
1627 | |
1dc1f037 |
1628 | if ($select) { |
4e5f3064 |
1629 | if ($ras = get_records_select('role_assignments', implode(' AND ', $select))) { |
1630 | $mods = get_list_of_plugins('mod'); |
1631 | foreach($ras as $ra) { |
86e2c51d |
1632 | /// infinite loop protection when deleting recursively |
1633 | if (!$ra = get_record('role_assignments', 'id', $ra->id)) { |
1634 | continue; |
1635 | } |
4e5f3064 |
1636 | $success = delete_records('role_assignments', 'id', $ra->id) and $success; |
86e2c51d |
1637 | |
4e5f3064 |
1638 | /// If the user is the current user, then reload the capabilities too. |
1639 | if (!empty($USER->id) && $USER->id == $ra->userid) { |
1640 | load_user_capability(); |
1641 | } |
1642 | $context = get_record('context', 'id', $ra->contextid); |
0f161e1f |
1643 | |
1644 | /// Ask all the modules if anything needs to be done for this user |
4e5f3064 |
1645 | foreach ($mods as $mod) { |
1646 | include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php'); |
1647 | $functionname = $mod.'_role_unassign'; |
1648 | if (function_exists($functionname)) { |
1649 | $functionname($ra->userid, $context); // watch out, $context might be NULL if something goes wrong |
1650 | } |
1651 | } |
1652 | |
1653 | /// now handle metacourse role unassigment and removing from goups if in course context |
aad2ba95 |
1654 | if (!empty($context) and $context->contextlevel == CONTEXT_COURSE) { |
4e5f3064 |
1655 | //remove from groups when user has no role |
1656 | $roles = get_user_roles($context, $ra->userid, true); |
1657 | if (empty($roles)) { |
1658 | if ($groups = get_groups($context->instanceid, $ra->userid)) { |
1659 | foreach ($groups as $group) { |
1660 | delete_records('groups_members', 'groupid', $group->id, 'userid', $ra->userid); |
1661 | } |
1662 | } |
1663 | } |
1aad4310 |
1664 | //unassign roles in metacourses if needed |
4e5f3064 |
1665 | if ($parents = get_records('course_meta', 'child_course', $context->instanceid)) { |
1666 | foreach ($parents as $parent) { |
1aad4310 |
1667 | sync_metacourse($parent->parent_course); |
0f161e1f |
1668 | } |
1669 | } |
0f161e1f |
1670 | } |
1671 | } |
d74067e8 |
1672 | } |
1dc1f037 |
1673 | } |
4e5f3064 |
1674 | |
1675 | return $success; |
bbbf2d40 |
1676 | } |
1677 | |
eef868d1 |
1678 | /* |
1679 | * A convenience function to take care of the common case where you |
b963384f |
1680 | * just want to enrol someone using the default role into a course |
1681 | * |
1682 | * @param object $course |
1683 | * @param object $user |
1684 | * @param string $enrol - the plugin used to do this enrolment |
1685 | */ |
1686 | function enrol_into_course($course, $user, $enrol) { |
1687 | |
1688 | if ($course->enrolperiod) { |
1689 | $timestart = time(); |
1690 | $timeend = time() + $course->enrolperiod; |
1691 | } else { |
1692 | $timestart = $timeend = 0; |
1693 | } |
1694 | |
1695 | if ($role = get_default_course_role($course)) { |
c4381ef5 |
1696 | |
1697 | $context = get_context_instance(CONTEXT_COURSE, $course->id); |
1698 | |
e2183037 |
1699 | if (!role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol)) { |
b963384f |
1700 | return false; |
1701 | } |
eef868d1 |
1702 | |
b963384f |
1703 | email_welcome_message_to_user($course, $user); |
eef868d1 |
1704 | |
b963384f |
1705 | add_to_log($course->id, 'course', 'enrol', 'view.php?id='.$course->id, $user->id); |
1706 | |
1707 | return true; |
1708 | } |
1709 | |
1710 | return false; |
1711 | } |
1712 | |
0f161e1f |
1713 | /** |
1714 | * Add last access times to user_lastaccess as required |
1715 | * @param $userid |
1716 | * @param $context |
1717 | * @return boolean - success or failure |
1718 | */ |
1719 | function role_add_lastaccess_entries($userid, $context) { |
1720 | |
1721 | global $USER, $CFG; |
1722 | |
aad2ba95 |
1723 | if (empty($context->contextlevel)) { |
0f161e1f |
1724 | return false; |
1725 | } |
1726 | |
1727 | $lastaccess = new object; // Reusable object below |
1728 | $lastaccess->userid = $userid; |
1729 | $lastaccess->timeaccess = 0; |
1730 | |
aad2ba95 |
1731 | switch ($context->contextlevel) { |
0f161e1f |
1732 | |
1733 | case CONTEXT_SYSTEM: // For the whole site |
1734 | if ($courses = get_record('course')) { |
1735 | foreach ($courses as $course) { |
1736 | $lastaccess->courseid = $course->id; |
1737 | role_set_lastaccess($lastaccess); |
1738 | } |
1739 | } |
1740 | break; |
1741 | |
1742 | case CONTEXT_CATEGORY: // For a whole category |
1743 | if ($courses = get_record('course', 'category', $context->instanceid)) { |
1744 | foreach ($courses as $course) { |
1745 | $lastaccess->courseid = $course->id; |
1746 | role_set_lastaccess($lastaccess); |
1747 | } |
1748 | } |
1749 | if ($categories = get_record('course_categories', 'parent', $context->instanceid)) { |
1750 | foreach ($categories as $category) { |
1751 | $subcontext = get_context_instance(CONTEXT_CATEGORY, $category->id); |
1752 | role_add_lastaccess_entries($userid, $subcontext); |
1753 | } |
1754 | } |
1755 | break; |
eef868d1 |
1756 | |
0f161e1f |
1757 | |
1758 | case CONTEXT_COURSE: // For a whole course |
1759 | if ($course = get_record('course', 'id', $context->instanceid)) { |
1760 | $lastaccess->courseid = $course->id; |
1761 | role_set_lastaccess($lastaccess); |
1762 | } |
1763 | break; |
1764 | } |
1765 | } |
1766 | |
1767 | /** |
1768 | * Delete last access times from user_lastaccess as required |
1769 | * @param $userid |
1770 | * @param $context |
1771 | * @return boolean - success or failure |
1772 | */ |
1773 | function role_remove_lastaccess_entries($userid, $context) { |
1774 | |
1775 | global $USER, $CFG; |
1776 | |
1777 | } |
1778 | |
bbbf2d40 |
1779 | |
1780 | /** |
1781 | * Loads the capability definitions for the component (from file). If no |
1782 | * capabilities are defined for the component, we simply return an empty array. |
1783 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1784 | * @return array of capabilities |
1785 | */ |
1786 | function load_capability_def($component) { |
1787 | global $CFG; |
1788 | |
1789 | if ($component == 'moodle') { |
1790 | $defpath = $CFG->libdir.'/db/access.php'; |
1791 | $varprefix = 'moodle'; |
1792 | } else { |
0c4d9f49 |
1793 | $compparts = explode('/', $component); |
eef868d1 |
1794 | |
0c4d9f49 |
1795 | if ($compparts[0] == 'block') { |
1796 | // Blocks are an exception. Blocks directory is 'blocks', and not |
1797 | // 'block'. So we need to jump through hoops. |
1798 | $defpath = $CFG->dirroot.'/'.$compparts[0]. |
1799 | 's/'.$compparts[1].'/db/access.php'; |
1800 | $varprefix = $compparts[0].'_'.$compparts[1]; |
1801 | } else { |
1802 | $defpath = $CFG->dirroot.'/'.$component.'/db/access.php'; |
1803 | $varprefix = str_replace('/', '_', $component); |
1804 | } |
bbbf2d40 |
1805 | } |
1806 | $capabilities = array(); |
eef868d1 |
1807 | |
bbbf2d40 |
1808 | if (file_exists($defpath)) { |
1809 | require_once($defpath); |
1810 | $capabilities = ${$varprefix.'_capabilities'}; |
1811 | } |
1812 | return $capabilities; |
1813 | } |
1814 | |
1815 | |
1816 | /** |
1817 | * Gets the capabilities that have been cached in the database for this |
1818 | * component. |
1819 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1820 | * @return array of capabilities |
1821 | */ |
1822 | function get_cached_capabilities($component='moodle') { |
1823 | if ($component == 'moodle') { |
1824 | $storedcaps = get_records_select('capabilities', |
1825 | "name LIKE 'moodle/%:%'"); |
1826 | } else { |
1827 | $storedcaps = get_records_select('capabilities', |
1828 | "name LIKE '$component:%'"); |
1829 | } |
1830 | return $storedcaps; |
1831 | } |
1832 | |
1833 | |
1834 | /** |
1835 | * Updates the capabilities table with the component capability definitions. |
1836 | * If no parameters are given, the function updates the core moodle |
1837 | * capabilities. |
1838 | * |
1839 | * Note that the absence of the db/access.php capabilities definition file |
1840 | * will cause any stored capabilities for the component to be removed from |
eef868d1 |
1841 | * the database. |
bbbf2d40 |
1842 | * |
1843 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1844 | * @return boolean |
1845 | */ |
1846 | function update_capabilities($component='moodle') { |
eef868d1 |
1847 | |
bbbf2d40 |
1848 | $storedcaps = array(); |
be4486da |
1849 | |
1850 | $filecaps = load_capability_def($component); |
bbbf2d40 |
1851 | $cachedcaps = get_cached_capabilities($component); |
1852 | if ($cachedcaps) { |
1853 | foreach ($cachedcaps as $cachedcap) { |
1854 | array_push($storedcaps, $cachedcap->name); |
be4486da |
1855 | // update risk bitmasks in existing capabilitites if needed |
1856 | if (array_key_exists($cachedcap->name, $filecaps)) { |
1857 | if (!array_key_exists('riskbitmask', $filecaps[$cachedcap->name])) { |
2b531945 |
1858 | $filecaps[$cachedcap->name]['riskbitmask'] = 0; // no risk if not specified |
be4486da |
1859 | } |
1860 | if ($cachedcap->riskbitmask != $filecaps[$cachedcap->name]['riskbitmask']) { |
1861 | $updatecap = new object; |
1862 | $updatecap->id = $cachedcap->id; |
1863 | $updatecap->riskbitmask = $filecaps[$cachedcap->name]['riskbitmask']; |
1864 | if (!update_record('capabilities', $updatecap)) { |
1865 | return false; |
1866 | } |
1867 | } |
1868 | } |
bbbf2d40 |
1869 | } |
1870 | } |
be4486da |
1871 | |
bbbf2d40 |
1872 | // Are there new capabilities in the file definition? |
1873 | $newcaps = array(); |
eef868d1 |
1874 | |
bbbf2d40 |
1875 | foreach ($filecaps as $filecap => $def) { |
eef868d1 |
1876 | if (!$storedcaps || |
bbbf2d40 |
1877 | ($storedcaps && in_array($filecap, $storedcaps) === false)) { |
2b531945 |
1878 | if (!array_key_exists('riskbitmask', $def)) { |
1879 | $def['riskbitmask'] = 0; // no risk if not specified |
1880 | } |
bbbf2d40 |
1881 | $newcaps[$filecap] = $def; |
1882 | } |
1883 | } |
1884 | // Add new capabilities to the stored definition. |
1885 | foreach ($newcaps as $capname => $capdef) { |
1886 | $capability = new object; |
1887 | $capability->name = $capname; |
1888 | $capability->captype = $capdef['captype']; |
1889 | $capability->contextlevel = $capdef['contextlevel']; |
1890 | $capability->component = $component; |
be4486da |
1891 | $capability->riskbitmask = $capdef['riskbitmask']; |
eef868d1 |
1892 | |
bbbf2d40 |
1893 | if (!insert_record('capabilities', $capability, false, 'id')) { |
1894 | return false; |
1895 | } |
eef868d1 |
1896 | |
bbbf2d40 |
1897 | // Do we need to assign the new capabilities to roles that have the |
1898 | // legacy capabilities moodle/legacy:* as well? |
1899 | if (isset($capdef['legacy']) && is_array($capdef['legacy']) && |
1900 | !assign_legacy_capabilities($capname, $capdef['legacy'])) { |
2e85fffe |
1901 | notify('Could not assign legacy capabilities for '.$capname); |
bbbf2d40 |
1902 | } |
1903 | } |
1904 | // Are there any capabilities that have been removed from the file |
1905 | // definition that we need to delete from the stored capabilities and |
1906 | // role assignments? |
1907 | capabilities_cleanup($component, $filecaps); |
eef868d1 |
1908 | |
bbbf2d40 |
1909 | return true; |
1910 | } |
1911 | |
1912 | |
1913 | /** |
1914 | * Deletes cached capabilities that are no longer needed by the component. |
1915 | * Also unassigns these capabilities from any roles that have them. |
1916 | * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' |
1917 | * @param $newcapdef - array of the new capability definitions that will be |
1918 | * compared with the cached capabilities |
1919 | * @return int - number of deprecated capabilities that have been removed |
1920 | */ |
1921 | function capabilities_cleanup($component, $newcapdef=NULL) { |
eef868d1 |
1922 | |
bbbf2d40 |
1923 | $removedcount = 0; |
eef868d1 |
1924 | |
bbbf2d40 |
1925 | if ($cachedcaps = get_cached_capabilities($component)) { |
1926 | foreach ($cachedcaps as $cachedcap) { |
1927 | if (empty($newcapdef) || |
1928 | array_key_exists($cachedcap->name, $newcapdef) === false) { |
eef868d1 |
1929 | |
bbbf2d40 |
1930 | // Remove from capabilities cache. |
1931 | if (!delete_records('capabilities', 'name', $cachedcap->name)) { |
1932 | error('Could not delete deprecated capability '.$cachedcap->name); |
1933 | } else { |
1934 | $removedcount++; |
1935 | } |
1936 | // Delete from roles. |
1937 | if($roles = get_roles_with_capability($cachedcap->name)) { |
1938 | foreach($roles as $role) { |
46943f7b |
1939 | if (!unassign_capability($cachedcap->name, $role->id)) { |
bbbf2d40 |
1940 | error('Could not unassign deprecated capability '. |
1941 | $cachedcap->name.' from role '.$role->name); |
1942 | } |
1943 | } |
1944 | } |
1945 | } // End if. |
1946 | } |
1947 | } |
1948 | return $removedcount; |
1949 | } |
1950 | |
1951 | |
1952 | |
cee0901c |
1953 | /**************** |
1954 | * UI FUNCTIONS * |
1955 | ****************/ |
bbbf2d40 |
1956 | |
1957 | |
1958 | /** |
1959 | * prints human readable context identifier. |
1960 | */ |
0468976c |
1961 | function print_context_name($context) { |
340ea4e8 |
1962 | |
ec0810ee |
1963 | $name = ''; |
aad2ba95 |
1964 | switch ($context->contextlevel) { |
ec0810ee |
1965 | |
bbbf2d40 |
1966 | case CONTEXT_SYSTEM: // by now it's a definite an inherit |
ec0810ee |
1967 | $name = get_string('site'); |
340ea4e8 |
1968 | break; |
bbbf2d40 |
1969 | |
1970 | case CONTEXT_PERSONAL: |
ec0810ee |
1971 | $name = get_string('personal'); |
340ea4e8 |
1972 | break; |
1973 | |
4b10f08b |
1974 | case CONTEXT_USER: |
ec0810ee |
1975 | if ($user = get_record('user', 'id', $context->instanceid)) { |
1976 | $name = get_string('user').': '.fullname($user); |
1977 | } |
340ea4e8 |
1978 | break; |
1979 | |
bbbf2d40 |
1980 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
ec0810ee |
1981 | if ($category = get_record('course_categories', 'id', $context->instanceid)) { |
1982 | $name = get_string('category').': '.$category->name; |
1983 | } |
340ea4e8 |
1984 | break; |
bbbf2d40 |
1985 | |
1986 | case CONTEXT_COURSE: // 1 to 1 to course cat |
ec0810ee |
1987 | if ($course = get_record('course', 'id', $context->instanceid)) { |
1988 | $name = get_string('course').': '.$course->fullname; |
1989 | } |
340ea4e8 |
1990 | break; |
bbbf2d40 |
1991 | |
1992 | case CONTEXT_GROUP: // 1 to 1 to course |
ec0810ee |
1993 | if ($group = get_record('groups', 'id', $context->instanceid)) { |
1994 | $name = get_string('group').': '.$group->name; |
1995 | } |
340ea4e8 |
1996 | break; |
bbbf2d40 |
1997 | |
1998 | case CONTEXT_MODULE: // 1 to 1 to course |
98882637 |
1999 | if ($cm = get_record('course_modules','id',$context->instanceid)) { |
2000 | if ($module = get_record('modules','id',$cm->module)) { |
2001 | if ($mod = get_record($module->name, 'id', $cm->instance)) { |
ec0810ee |
2002 | $name = get_string('activitymodule').': '.$mod->name; |
98882637 |
2003 | } |
ec0810ee |
2004 | } |
2005 | } |
340ea4e8 |
2006 | break; |
bbbf2d40 |
2007 | |
2008 | case CONTEXT_BLOCK: // 1 to 1 to course |
98882637 |
2009 | if ($blockinstance = get_record('block_instance','id',$context->instanceid)) { |
2010 | if ($block = get_record('block','id',$blockinstance->blockid)) { |
91be52d7 |
2011 | global $CFG; |
2012 | require_once("$CFG->dirroot/blocks/moodleblock.class.php"); |
2013 | require_once("$CFG->dirroot/blocks/$block->name/block_$block->name.php"); |
2014 | $blockname = "block_$block->name"; |
2015 | if ($blockobject = new $blockname()) { |
2016 | $name = $blockobject->title.' ('.get_string('block').')'; |
2017 | } |
ec0810ee |
2018 | } |
2019 | } |
340ea4e8 |
2020 | break; |
bbbf2d40 |
2021 | |
2022 | default: |
2023 | error ('This is an unknown context!'); |
340ea4e8 |
2024 | return false; |
2025 | |
2026 | } |
340ea4e8 |
2027 | return $name; |
bbbf2d40 |
2028 | } |
2029 | |
2030 | |
2031 | /** |
eef868d1 |
2032 | * Extracts the relevant capabilities given a contextid. |
bbbf2d40 |
2033 | * All case based, example an instance of forum context. |
2034 | * Will fetch all forum related capabilities, while course contexts |
2035 | * Will fetch all capabilities |
0468976c |
2036 | * @param object context |
bbbf2d40 |
2037 | * @return array(); |
2038 | * |
2039 | * capabilities |
2040 | * `name` varchar(150) NOT NULL, |
2041 | * `captype` varchar(50) NOT NULL, |
2042 | * `contextlevel` int(10) NOT NULL, |
2043 | * `component` varchar(100) NOT NULL, |
2044 | */ |
0468976c |
2045 | function fetch_context_capabilities($context) { |
eef868d1 |
2046 | |
98882637 |
2047 | global $CFG; |
bbbf2d40 |
2048 | |
2049 | $sort = 'ORDER BY contextlevel,component,id'; // To group them sensibly for display |
eef868d1 |
2050 | |
aad2ba95 |
2051 | switch ($context->contextlevel) { |
bbbf2d40 |
2052 | |
98882637 |
2053 | case CONTEXT_SYSTEM: // all |
2054 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
2055 | break; |
2056 | |
2057 | case CONTEXT_PERSONAL: |
0a8a95c9 |
2058 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_PERSONAL; |
bbbf2d40 |
2059 | break; |
eef868d1 |
2060 | |
4b10f08b |
2061 | case CONTEXT_USER: |
2062 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_USER; |
bbbf2d40 |
2063 | break; |
eef868d1 |
2064 | |
bbbf2d40 |
2065 | case CONTEXT_COURSECAT: // all |
98882637 |
2066 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
2067 | break; |
2068 | |
2069 | case CONTEXT_COURSE: // all |
98882637 |
2070 | $SQL = "select * from {$CFG->prefix}capabilities"; |
bbbf2d40 |
2071 | break; |
2072 | |
2073 | case CONTEXT_GROUP: // group caps |
2074 | break; |
2075 | |
2076 | case CONTEXT_MODULE: // mod caps |
98882637 |
2077 | $cm = get_record('course_modules', 'id', $context->instanceid); |
2078 | $module = get_record('modules', 'id', $cm->module); |
eef868d1 |
2079 | |
98882637 |
2080 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_MODULE." |
2081 | and component = 'mod/$module->name'"; |
bbbf2d40 |
2082 | break; |
2083 | |
2084 | case CONTEXT_BLOCK: // block caps |
98882637 |
2085 | $cb = get_record('block_instance', 'id', $context->instanceid); |
2086 | $block = get_record('block', 'id', $cb->blockid); |
eef868d1 |
2087 | |
98882637 |
2088 | $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_BLOCK." |
2089 | and component = 'block/$block->name'"; |
bbbf2d40 |
2090 | break; |
2091 | |
2092 | default: |
2093 | return false; |
2094 | } |
2095 | |
16e2e2f3 |
2096 | if (!$records = get_records_sql($SQL.' '.$sort)) { |
2097 | $records = array(); |
2098 | } |
759ac72d |
2099 | $contextindependentcaps = fetch_context_independent_capabilities(); |
2100 | $records = array_merge($records, $contextindependentcaps); |
69eb59f2 |
2101 | |
2102 | // special sorting of core system capabiltites and enrollments |
aad2ba95 |
2103 | if ($context->contextlevel == CONTEXT_SYSTEM) { |
69eb59f2 |
2104 | $first = array(); |
2105 | foreach ($records as $key=>$record) { |
2106 | if (preg_match('|^moodle/|', $record->name) and $record->contextlevel == CONTEXT_SYSTEM) { |
2107 | $first[$key] = $record; |
2108 | unset($records[$key]); |
2109 | } else if (count($first)){ |
2110 | break; |
2111 | } |
2112 | } |
2113 | if (count($first)) { |
2114 | $records = $first + $records; // merge the two arrays keeping the keys |
2115 | } |
2116 | } |
2117 | // end of special sorting |
bbbf2d40 |
2118 | return $records; |
eef868d1 |
2119 | |
bbbf2d40 |
2120 | } |
2121 | |
2122 | |
759ac72d |
2123 | /** |
2124 | * Gets the context-independent capabilities that should be overrridable in |
2125 | * any context. |
2126 | * @return array of capability records from the capabilities table. |
2127 | */ |
2128 | function fetch_context_independent_capabilities() { |
eef868d1 |
2129 | |
759ac72d |
2130 | $contextindependentcaps = array( |
2131 | 'moodle/site:accessallgroups' |
2132 | ); |
2133 | |
2134 | $records = array(); |
eef868d1 |
2135 | |
759ac72d |
2136 | foreach ($contextindependentcaps as $capname) { |
2137 | $record = get_record('capabilities', 'name', $capname); |
2138 | array_push($records, $record); |
2139 | } |
2140 | return $records; |
2141 | } |
2142 | |
2143 | |
bbbf2d40 |
2144 | /** |
2145 | * This function pulls out all the resolved capabilities (overrides and |
759ac72d |
2146 | * defaults) of a role used in capability overrides in contexts at a given |
bbbf2d40 |
2147 | * context. |
0a8a95c9 |
2148 | * @param obj $context |
bbbf2d40 |
2149 | * @param int $roleid |
dc558690 |
2150 | * @param bool self - if set to true, resolve till this level, else stop at immediate parent level |
bbbf2d40 |
2151 | * @return array |
2152 | */ |
1648afb2 |
2153 | function role_context_capabilities($roleid, $context, $cap='') { |
dc558690 |
2154 | global $CFG; |
eef868d1 |
2155 | |
8521d83a |
2156 | $contexts = get_parent_contexts($context); |
2157 | $contexts[] = $context->id; |
98882637 |
2158 | $contexts = '('.implode(',', $contexts).')'; |
eef868d1 |
2159 | |
1648afb2 |
2160 | if ($cap) { |
e4697bf7 |
2161 | $search = " AND rc.capability = '$cap' "; |
1648afb2 |
2162 | } else { |
eef868d1 |
2163 | $search = ''; |
1648afb2 |
2164 | } |
eef868d1 |
2165 | |
2166 | $SQL = "SELECT rc.* |
2167 | FROM {$CFG->prefix}role_capabilities rc, |
dc558690 |
2168 | {$CFG->prefix}context c |
2169 | WHERE rc.contextid in $contexts |
2170 | AND rc.roleid = $roleid |
2171 | AND rc.contextid = c.id $search |
aad2ba95 |
2172 | ORDER BY c.contextlevel DESC, |
eef868d1 |
2173 | rc.capability DESC"; |
759ac72d |
2174 | |
98882637 |
2175 | $capabilities = array(); |
eef868d1 |
2176 | |
4729012f |
2177 | if ($records = get_records_sql($SQL)) { |
2178 | // We are traversing via reverse order. |
2179 | foreach ($records as $record) { |
2180 | // If not set yet (i.e. inherit or not set at all), or currently we have a prohibit |
2181 | if (!isset($capabilities[$record->capability]) || $record->permission<-500) { |
2182 | $capabilities[$record->capability] = $record->permission; |
eef868d1 |
2183 | } |
4729012f |
2184 | } |
98882637 |
2185 | } |
2186 | return $capabilities; |
bbbf2d40 |
2187 | } |
2188 | |
bbbf2d40 |
2189 | /** |
eef868d1 |
2190 | * Recursive function which, given a context, find all parent context ids, |
bbbf2d40 |
2191 | * and return the array in reverse order, i.e. parent first, then grand |
2192 | * parent, etc. |
2193 | * @param object $context |
2194 | * @return array() |
2195 | */ |
bbbf2d40 |
2196 | function get_parent_contexts($context) { |
759ac72d |
2197 | |
aad2ba95 |
2198 | switch ($context->contextlevel) { |
bbbf2d40 |
2199 | |
2200 | case CONTEXT_SYSTEM: // no parent |
957861f7 |
2201 | return array(); |
bbbf2d40 |
2202 | break; |
2203 | |
2204 | case CONTEXT_PERSONAL: |
957861f7 |
2205 | if (!$parent = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
2206 | return array(); |
2207 | } else { |
2208 | return array($parent->id); |
2209 | } |
bbbf2d40 |
2210 | break; |
eef868d1 |
2211 | |
4b10f08b |
2212 | case CONTEXT_USER: |
957861f7 |
2213 | if (!$parent = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
2214 | return array(); |
2215 | } else { |
2216 | return array($parent->id); |
2217 | } |
bbbf2d40 |
2218 | break; |
eef868d1 |
2219 | |
bbbf2d40 |
2220 | case CONTEXT_COURSECAT: // Coursecat -> coursecat or site |
957861f7 |
2221 | if (!$coursecat = get_record('course_categories','id',$context->instanceid)) { |
2222 | return array(); |
2223 | } |
c5ddc3fd |
2224 | if (!empty($coursecat->parent)) { // return parent value if exist |
bbbf2d40 |
2225 | $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent); |
2226 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2227 | } else { // else return site value |
2228 | $parent = get_context_instance(CONTEXT_SYSTEM, SITEID); |
2229 | return array($parent->id); |
2230 | } |
2231 | break; |
2232 | |
2233 | case CONTEXT_COURSE: // 1 to 1 to course cat |
957861f7 |
2234 | if (!$course = get_record('course','id',$context->instanceid)) { |
2235 | return array(); |
2236 | } |
2237 | if (!empty($course->category)) { |
2238 | $parent = get_context_instance(CONTEXT_COURSECAT, $course->category); |
2239 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2240 | } else { |
2241 | return array(); |
2242 | } |
bbbf2d40 |
2243 | break; |
2244 | |
2245 | case CONTEXT_GROUP: // 1 to 1 to course |
957861f7 |
2246 | if (!$group = get_record('groups','id',$context->instanceid)) { |
2247 | return array(); |
2248 | } |
2249 | if ($parent = get_context_instance(CONTEXT_COURSE, $group->courseid)) { |
2250 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2251 | } else { |
2252 | return array(); |
2253 | } |
bbbf2d40 |
2254 | break; |
2255 | |
2256 | case CONTEXT_MODULE: // 1 to 1 to course |
957861f7 |
2257 | if (!$cm = get_record('course_modules','id',$context->instanceid)) { |
2258 | return array(); |
2259 | } |
2260 | if ($parent = get_context_instance(CONTEXT_COURSE, $cm->course)) { |
2261 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2262 | } else { |
2263 | return array(); |
2264 | } |
bbbf2d40 |
2265 | break; |
2266 | |
2267 | case CONTEXT_BLOCK: // 1 to 1 to course |
957861f7 |
2268 | if (!$block = get_record('block_instance','id',$context->instanceid)) { |
2269 | return array(); |
2270 | } |
2271 | if ($parent = get_context_instance(CONTEXT_COURSE, $block->pageid)) { |
2272 | return array_merge(array($parent->id), get_parent_contexts($parent)); |
2273 | } else { |
2274 | return array(); |
2275 | } |
bbbf2d40 |
2276 | break; |
2277 | |
2278 | default: |
957861f7 |
2279 | error('This is an unknown context!'); |
bbbf2d40 |
2280 | return false; |
2281 | } |
bbbf2d40 |
2282 | } |
2283 | |
759ac72d |
2284 | |
2285 | /** |
2286 | * Gets a string for sql calls, searching for stuff in this context or above |
ea8158c1 |
2287 | * @param object $context |
2288 | * @return string |
2289 | */ |
2290 | function get_related_contexts_string($context) { |
2291 | if ($parents = get_parent_contexts($context)) { |
eef868d1 |
2292 | return (' IN ('.$context->id.','.implode(',', $parents).')'); |
ea8158c1 |
2293 | } else { |
2294 | return (' ='.$context->id); |
2295 | } |
2296 | } |
759ac72d |
2297 | |
2298 | |
bbbf2d40 |
2299 | /** |
2300 | * This function gets the capability of a role in a given context. |
2301 | * It is needed when printing override forms. |
2302 | * @param int $contextid |
bbbf2d40 |
2303 | * @param string $capability |
2304 | * @param array $capabilities - array loaded using role_context_capabilities |
2305 | * @return int (allow, prevent, prohibit, inherit) |
2306 | */ |
bbbf2d40 |
2307 | function get_role_context_capability($contextid, $capability, $capabilities) { |
759ac72d |
2308 | if (isset($capabilities[$contextid][$capability])) { |
2309 | return $capabilities[$contextid][$capability]; |
2310 | } |
2311 | else { |
2312 | return false; |
2313 | } |
bbbf2d40 |
2314 | } |
2315 | |
2316 | |
cee0901c |
2317 | /** |
2318 | * Returns the human-readable, translated version of the capability. |
2319 | * Basically a big switch statement. |
2320 | * @param $capabilityname - e.g. mod/choice:readresponses |
2321 | */ |
ceb83c70 |
2322 | function get_capability_string($capabilityname) { |
eef868d1 |
2323 | |
cee0901c |
2324 | // Typical capabilityname is mod/choice:readresponses |
ceb83c70 |
2325 | |
2326 | $names = split('/', $capabilityname); |
2327 | $stringname = $names[1]; // choice:readresponses |
eef868d1 |
2328 | $components = split(':', $stringname); |
ceb83c70 |
2329 | $componentname = $components[0]; // choice |
98882637 |
2330 | |
2331 | switch ($names[0]) { |
2332 | case 'mod': |
ceb83c70 |
2333 | $string = get_string($stringname, $componentname); |
98882637 |
2334 | break; |
eef868d1 |
2335 | |
98882637 |
2336 | case 'block': |
ceb83c70 |
2337 | $string = get_string($stringname, 'block_'.$componentname); |
98882637 |
2338 | break; |
ceb83c70 |
2339 | |
98882637 |
2340 | case 'moodle': |
ceb83c70 |
2341 | $string = get_string($stringname, 'role'); |
98882637 |
2342 | break; |
eef868d1 |
2343 | |
98882637 |
2344 | case 'enrol': |
ceb83c70 |
2345 | $string = get_string($stringname, 'enrol_'.$componentname); |
eef868d1 |
2346 | break; |
2347 | |
98882637 |
2348 | default: |
ceb83c70 |
2349 | $string = get_string($stringname); |
eef868d1 |
2350 | break; |
2351 | |
98882637 |
2352 | } |
ceb83c70 |
2353 | return $string; |
bbbf2d40 |
2354 | } |
2355 | |
2356 | |
cee0901c |
2357 | /** |
2358 | * This gets the mod/block/course/core etc strings. |
2359 | * @param $component |
2360 | * @param $contextlevel |
2361 | */ |
bbbf2d40 |
2362 | function get_component_string($component, $contextlevel) { |
2363 | |
98882637 |
2364 | switch ($contextlevel) { |
bbbf2d40 |
2365 | |
98882637 |
2366 | case CONTEXT_SYSTEM: |
be382aaf |
2367 | if (preg_match('|^enrol/|', $component)) { |
2368 | $langname = str_replace('/', '_', $component); |
2369 | $string = get_string('enrolname', $langname); |
f3652521 |
2370 | } else if (preg_match('|^block/|', $component)) { |
2371 | $langname = str_replace('/', '_', $component); |
2372 | $string = get_string('blockname', $langname); |
69eb59f2 |
2373 | } else { |
2374 | $string = get_string('coresystem'); |
2375 | } |
bbbf2d40 |
2376 | break; |
2377 | |
2378 | case CONTEXT_PERSONAL: |
98882637 |
2379 | $string = get_string('personal'); |
bbbf2d40 |
2380 | break; |
2381 | |
4b10f08b |
2382 | case CONTEXT_USER: |
98882637 |
2383 | $string = get_string('users'); |
bbbf2d40 |
2384 | break; |
2385 | |
2386 | case CONTEXT_COURSECAT: |
98882637 |
2387 | $string = get_string('categories'); |
bbbf2d40 |
2388 | break; |
2389 | |
2390 | case CONTEXT_COURSE: |
98882637 |
2391 | $string = get_string('course'); |
bbbf2d40 |
2392 | break; |
2393 | |
2394 | case CONTEXT_GROUP: |
98882637 |
2395 | $string = get_string('group'); |
bbbf2d40 |
2396 | break; |
2397 | |
2398 | case CONTEXT_MODULE: |
98882637 |
2399 | $string = get_string('modulename', basename($component)); |
bbbf2d40 |
2400 | break; |
2401 | |
2402 | case CONTEXT_BLOCK: |
98882637 |
2403 | $string = get_string('blockname', 'block_'.$component.'.php'); |
bbbf2d40 |
2404 | break; |
2405 | |
2406 | default: |
2407 | error ('This is an unknown context!'); |
2408 | return false; |
eef868d1 |
2409 | |
98882637 |
2410 | } |
98882637 |
2411 | return $string; |
bbbf2d40 |
2412 | } |
cee0901c |
2413 | |
759ac72d |
2414 | /** |
2415 | * Gets the list of roles assigned to this context and up (parents) |
945f88ca |
2416 | * @param object $context |
2417 | * @return array |
2418 | */ |
e4dd3222 |
2419 | function get_roles_used_in_context($context) { |
2420 | |
2421 | global $CFG; |
2d9965e1 |
2422 | $contextlist = get_related_contexts_string($context); |
eef868d1 |
2423 | |
759ac72d |
2424 | $sql = "SELECT DISTINCT r.id, |
2425 | r.name, |
2426 | r.shortname, |
2427 | r.sortorder |
2428 | FROM {$CFG->prefix}role_assignments ra, |
eef868d1 |
2429 | {$CFG->prefix}role r |
2430 | WHERE r.id = ra.roleid |
759ac72d |
2431 | AND ra.contextid $contextlist |
2432 | ORDER BY r.sortorder ASC"; |
eef868d1 |
2433 | |
759ac72d |
2434 | return get_records_sql($sql); |
e4dd3222 |
2435 | } |
2436 | |
eef868d1 |
2437 | /** this function is used to print roles column in user profile page. |
945f88ca |
2438 | * @param int userid |
2439 | * @param int contextid |
2440 | * @return string |
2441 | */ |
0a8a95c9 |
2442 | function get_user_roles_in_context($userid, $contextid){ |
2443 | global $CFG; |
eef868d1 |
2444 | |
0a8a95c9 |
2445 | $rolestring = ''; |
2446 | $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'; |
2447 | if ($roles = get_records_sql($SQL)) { |
2448 | foreach ($roles as $userrole) { |
2449 | $rolestring .= '<a href="'.$CFG->wwwroot.'/user/index.php?contextid='.$userrole->contextid.'&roleid='.$userrole->roleid.'">'.$userrole->name.'</a>, '; |
eef868d1 |
2450 | } |
2451 | |
0a8a95c9 |
2452 | } |
2453 | return rtrim($rolestring, ', '); |
2454 | } |
68c52526 |
2455 | |
2456 | |
945f88ca |
2457 | /** |
2458 | * Checks if a user can override capabilities of a particular role in this context |
2459 | * @param object $context |
2460 | * @param int targetroleid - the id of the role you want to override |
2461 | * @return boolean |
2462 | */ |
68c52526 |
2463 | function user_can_override($context, $targetroleid) { |
2464 | // first check if user has override capability |
2465 | // if not return false; |
2466 | if (!has_capability('moodle/role:override', $context)) { |
eef868d1 |
2467 | return false; |
68c52526 |
2468 | } |
2469 | // pull out all active roles of this user from this context(or above) |
c0614051 |
2470 | if ($userroles = get_user_roles($context)) { |
2471 | foreach ($userroles as $userrole) { |
2472 | // if any in the role_allow_override table, then it's ok |
2473 | if (get_record('role_allow_override', 'roleid', $userrole->roleid, 'allowoverride', $targetroleid)) { |
2474 | return true; |
2475 | } |
68c52526 |
2476 | } |
2477 | } |
eef868d1 |
2478 | |
68c52526 |
2479 | return false; |
eef868d1 |
2480 | |
68c52526 |
2481 | } |
2482 | |
945f88ca |
2483 | /** |
2484 | * Checks if a user can assign users to a particular role in this context |
2485 | * @param object $context |
2486 | * @param int targetroleid - the id of the role you want to assign users to |
2487 | * @return boolean |
2488 | */ |
68c52526 |
2489 | function user_can_assign($context, $targetroleid) { |
eef868d1 |
2490 | |
68c52526 |
2491 | // first check if user has override capability |
2492 | // if not return false; |
2493 | if (!has_capability('moodle/role:assign', $context)) { |
eef868d1 |
2494 | return false; |
68c52526 |
2495 | } |
2496 | // pull out all active roles of this user from this context(or above) |
c0614051 |
2497 | if ($userroles = get_user_roles($context)) { |
2498 | foreach ($userroles as $userrole) { |
2499 | // if any in the role_allow_override table, then it's ok |
2500 | if (get_record('role_allow_assign', 'roleid', $userrole->roleid, 'allowassign', $targetroleid)) { |
2501 | return true; |
2502 | } |
68c52526 |
2503 | } |
2504 | } |
eef868d1 |
2505 | |
2506 | return false; |
68c52526 |
2507 | } |
2508 | |
ece4945b |
2509 | /** Returns all site roles in correct sort order. |
2510 | * |
2511 | */ |
2512 | function get_all_roles() { |
2513 | return get_records('role', '', '', 'sortorder ASC'); |
2514 | } |
2515 | |
945f88ca |
2516 | /** |
2517 | * gets all the user roles assigned in this context, or higher contexts |
2518 | * this is mainly used when checking if a user can assign a role, or overriding a role |
2519 | * i.e. we need to know what this user holds, in order to verify against allow_assign and |
2520 | * allow_override tables |
2521 | * @param object $context |
2522 | * @param int $userid |
2523 | * @return array |
2524 | */ |
5b630667 |
2525 | function get_user_roles($context, $userid=0, $checkparentcontexts=true) { |
68c52526 |
2526 | |
2527 | global $USER, $CFG, $db; |
c0614051 |
2528 | |
2529 | if (empty($userid)) { |
2530 | if (empty($USER->id)) { |
2531 | return array(); |
2532 | } |
2533 | $userid = $USER->id; |
2534 | } |
2535 | |
5b630667 |
2536 | if ($checkparentcontexts && ($parents = get_parent_contexts($context))) { |
2537 | $contexts = ' ra.contextid IN ('.implode(',' , $parents).','.$context->id.')'; |
c0614051 |
2538 | } else { |
5b630667 |
2539 | $contexts = ' ra.contextid = \''.$context->id.'\''; |
c0614051 |
2540 | } |
2541 | |
31f26796 |
2542 | return get_records_sql('SELECT ra.*, r.name, r.shortname |
5b630667 |
2543 | FROM '.$CFG->prefix.'role_assignments ra, |
ec6eb110 |
2544 | '.$CFG->prefix.'role r, |
2545 | '.$CFG->prefix.'context c |
c0614051 |
2546 | WHERE ra.userid = '.$userid. |
5b630667 |
2547 | ' AND ra.roleid = r.id |
ec6eb110 |
2548 | AND ra.contextid = c.id |
eef868d1 |
2549 | AND '.$contexts. |
ece4945b |
2550 | ' ORDER BY c.contextlevel DESC, r.sortorder ASC'); |
68c52526 |
2551 | } |
2552 | |
945f88ca |
2553 | /** |
eef868d1 |
2554 | * Creates a record in the allow_override table |
945f88ca |
2555 | * @param int sroleid - source roleid |
2556 | * @param int troleid - target roleid |
2557 | * @return int - id or false |
2558 | */ |
2559 | function allow_override($sroleid, $troleid) { |
ece4945b |
2560 | $record = new object(); |
945f88ca |
2561 | $record->roleid = $sroleid; |
2562 | $record->allowoverride = $troleid; |
2563 | return insert_record('role_allow_override', $record); |
2564 | } |
2565 | |
2566 | /** |
eef868d1 |
2567 | * Creates a record in the allow_assign table |
945f88ca |
2568 | * @param int sroleid - source roleid |
2569 | * @param int troleid - target roleid |
2570 | * @return int - id or false |
2571 | */ |
2572 | function allow_assign($sroleid, $troleid) { |
ff64aaea |
2573 | $record = new object; |
945f88ca |
2574 | $record->roleid = $sroleid; |
2575 | $record->allowassign = $troleid; |
2576 | return insert_record('role_allow_assign', $record); |
2577 | } |
2578 | |
2579 | /** |
ff64aaea |
2580 | * Gets a list of roles that this user can assign in this context |
945f88ca |
2581 | * @param object $context |
2582 | * @return array |
2583 | */ |
2584 | function get_assignable_roles ($context) { |
2585 | |
945f88ca |
2586 | $options = array(); |
ff64aaea |
2587 | |
ece4945b |
2588 | if ($roles = get_all_roles()) { |
ff64aaea |
2589 | foreach ($roles as $role) { |
2590 | if (user_can_assign($context, $role->id)) { |
65b0c132 |
2591 | $options[$role->id] = strip_tags(format_string($role->name, true)); |
ff64aaea |
2592 | } |
945f88ca |
2593 | } |
2594 | } |
2595 | return $options; |
2596 | } |
2597 | |
2598 | /** |
ff64aaea |
2599 | * Gets a list of roles that this user can override in this context |
945f88ca |
2600 | * @param object $context |
2601 | * @return array |
2602 | */ |
2603 | function get_overridable_roles ($context) { |
2604 | |
945f88ca |
2605 | $options = array(); |
ff64aaea |
2606 | |
ece4945b |
2607 | if ($roles = get_all_roles()) { |
ff64aaea |
2608 | foreach ($roles as $role) { |
2609 | if (user_can_override($context, $role->id)) { |
65b0c132 |
2610 | $options[$role->id] = strip_tags(format_string($role->name, true)); |
ff64aaea |
2611 | } |
945f88ca |
2612 | } |
ff64aaea |
2613 | } |
eef868d1 |
2614 | |
2615 | return $options; |
945f88ca |
2616 | } |
1648afb2 |
2617 | |
b963384f |
2618 | /* |
2619 | * Returns a role object that is the default role for new enrolments |
2620 | * in a given course |
2621 | * |
eef868d1 |
2622 | * @param object $course |
b963384f |
2623 | * @return object $role |
2624 | */ |
2625 | function get_default_course_role($course) { |
2626 | global $CFG; |
2627 | |
2628 | /// First let's take the default role the course may have |
2629 | if (!empty($course->defaultrole)) { |
2630 | if ($role = get_record('role', 'id', $course->defaultrole)) { |
2631 | return $role; |
2632 | } |
2633 | } |
2634 | |
2635 | /// Otherwise the site setting should tell us |
2636 | if ($CFG->defaultcourseroleid) { |
2637 | if ($role = get_record('role', 'id', $CFG->defaultcourseroleid)) { |
2638 | return $role; |
2639 | } |
2640 | } |
2641 | |
2642 | /// It's unlikely we'll get here, but just in case, try and find a student role |
2643 | if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) { |
2644 | return array_shift($studentroles); /// Take the first one |
2645 | } |
2646 | |
2647 | return NULL; |
2648 | } |
2649 | |
1648afb2 |
2650 | |
2651 | /** |
2652 | * who has this capability in this context |
2653 | * does not handling user level resolving!!! |
2654 | * i.e 1 person has 2 roles 1 allow, 1 prevent, this will not work properly |
2655 | * @param $context - object |
2656 | * @param $capability - string capability |
2657 | * @param $fields - fields to be pulled |
2658 | * @param $sort - the sort order |
04417640 |
2659 | * @param $limitfrom - number of records to skip (offset) |
eef868d1 |
2660 | * @param $limitnum - number of records to fetch |
1c45e42e |
2661 | * @param $groups - single group or array of groups - group(s) user is in |
71dea306 |
2662 | * @param $exceptions - list of users to exclude |
1648afb2 |
2663 | */ |
eef868d1 |
2664 | function get_users_by_capability($context, $capability, $fields='', $sort='', |
1d546bb1 |
2665 | $limitfrom='', $limitnum='', $groups='', $exceptions='', $doanything=true) { |
1648afb2 |
2666 | global $CFG; |
eef868d1 |
2667 | |
64026e8c |
2668 | /// Sorting out groups |
1c45e42e |
2669 | if ($groups) { |
71dea306 |
2670 | $groupjoin = 'INNER JOIN '.$CFG->prefix.'groups_members gm ON gm.userid = ra.userid'; |
eef868d1 |
2671 | |
1c45e42e |
2672 | if (is_array($groups)) { |
a05708ad |
2673 | $groupsql = 'AND gm.groupid IN ('.implode(',', $groups).')'; |
1c45e42e |
2674 | } else { |
eef868d1 |
2675 | $groupsql = 'AND gm.groupid = '.$groups; |
1c45e42e |
2676 | } |
2677 | } else { |
2678 | $groupjoin = ''; |
eef868d1 |
2679 | $groupsql = ''; |
1c45e42e |
2680 | } |
eef868d1 |
2681 | |
64026e8c |
2682 | /// Sorting out exceptions |
5081e786 |
2683 | $exceptionsql = $exceptions ? "AND u.id NOT IN ($exceptions)" : ''; |
64026e8c |
2684 | |
2685 | /// Set up default fields |
2686 | if (empty($fields)) { |
5b630667 |
2687 | $fields = 'u.*, ul.timeaccess as lastaccess, ra.hidden'; |
64026e8c |
2688 | } |
2689 | |
2690 | /// Set up default sort |
2691 | if (empty($sort)) { |
2692 | $sort = 'ul.timeaccess'; |
2693 | } |
2694 | |
eef868d1 |
2695 | $sortby = $sort ? " ORDER BY $sort " : ''; |
2696 | |
64026e8c |
2697 | /// If context is a course, then construct sql for ul |
aad2ba95 |
2698 | if ($context->contextlevel == CONTEXT_COURSE) { |
71dea306 |
2699 | $courseid = $context->instanceid; |
f00b7f8d |
2700 | $coursesql = "AND (ul.courseid = $courseid OR ul.courseid IS NULL)"; |
5081e786 |
2701 | } else { |
2702 | $coursesql = ''; |
71dea306 |
2703 | } |
64026e8c |
2704 | |
2705 | /// Sorting out roles with this capability set |
9d829c68 |
2706 | if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) { |
1d546bb1 |
2707 | if (!$doanything) { |
3ca2dea5 |
2708 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
1d546bb1 |
2709 | return false; // Something is seriously wrong |
2710 | } |
2711 | $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext); |
e836a7dd |
2712 | } |
e836a7dd |
2713 | |
9d829c68 |
2714 | $validroleids = array(); |
e836a7dd |
2715 | foreach ($possibleroles as $possiblerole) { |
1d546bb1 |
2716 | if (!$doanything) { |
2717 | if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included |
2718 | continue; |
2719 | } |
e836a7dd |
2720 | } |
bccdf227 |
2721 | if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list |
2722 | if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0 |
2723 | $validroleids[] = $possiblerole->id; |
2724 | } |
9d829c68 |
2725 | } |
1648afb2 |
2726 | } |
1d546bb1 |
2727 | if (empty($validroleids)) { |
2728 | return false; |
2729 | } |
9d829c68 |
2730 | $roleids = '('.implode(',', $validroleids).')'; |
2731 | } else { |
2732 | return false; // No need to continue, since no roles have this capability set |
eef868d1 |
2733 | } |
64026e8c |
2734 | |
2735 | /// Construct the main SQL |
71dea306 |
2736 | $select = " SELECT $fields"; |
eef868d1 |
2737 | $from = " FROM {$CFG->prefix}user u |
2738 | INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id |
0a3e9703 |
2739 | INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid |
71dea306 |
2740 | LEFT OUTER JOIN {$CFG->prefix}user_lastaccess ul ON ul.userid = u.id |
2741 | $groupjoin"; |
eef868d1 |
2742 | $where = " WHERE ra.contextid ".get_related_contexts_string($context)." |
2743 | AND u.deleted = 0 |
2744 | AND ra.roleid in $roleids |
71dea306 |
2745 | $exceptionsql |
2746 | $coursesql |
2747 | $groupsql"; |
2748 | |
eef868d1 |
2749 | return get_records_sql($select.$from.$where.$sortby, $limitfrom, $limitnum); |
1648afb2 |
2750 | } |
7700027f |
2751 | |
ab5c9044 |
2752 | /** |
2753 | * gets all the users assigned this role in this context or higher |
2754 | * @param int roleid |
2755 | * @param int contextid |
2756 | * @param bool parent if true, get list of users assigned in higher context too |
2757 | * @return array() |
2758 | */ |
bac2f88a |
2759 | function get_role_users($roleid, $context, $parent=false, $fields='u.*') { |
ab5c9044 |
2760 | global $CFG; |
eef868d1 |
2761 | |
ab5c9044 |
2762 | if ($parent) { |
2763 | if ($contexts = get_parent_contexts($context)) { |
2764 | $parentcontexts = 'r.contextid IN ('.implode(',', $contexts).')'; |
2765 | } else { |
eef868d1 |
2766 | $parentcontexts = ''; |
ab5c9044 |
2767 | } |
2768 | } else { |
eef868d1 |
2769 | $parentcontexts = ''; |
2770 | } |
2771 | |
bac2f88a |
2772 | $SQL = "select $fields |
eef868d1 |
2773 | from {$CFG->prefix}role_assignments r, |
2774 | {$CFG->prefix}user u |
2775 | where (r.contextid = $context->id $parentcontexts) |
2776 | and r.roleid = $roleid |
ab5c9044 |
2777 | and u.id = r.userid"; // join now so that we can just use fullname() later |
eef868d1 |
2778 | |
ab5c9044 |
2779 | return get_records_sql($SQL); |
2780 | } |
2781 | |
bac2f88a |
2782 | /** |
2783 | * Counts all the users assigned this role in this context or higher |
2784 | * @param int roleid |
2785 | * @param int contextid |
2786 | * @param bool parent if true, get list of users assigned in higher context too |
2787 | * @return array() |
2788 | */ |
2789 | function count_role_users($roleid, $context, $parent=false) { |
2790 | global $CFG; |
2791 | |
2792 | if ($parent) { |
2793 | if ($contexts = get_parent_contexts($context)) { |
2794 | $parentcontexts = 'r.contextid IN ('.implode(',', $contexts).')'; |
2795 | } else { |
2796 | $parentcontexts = ''; |
2797 | } |
2798 | } else { |
2799 | $parentcontexts = ''; |
2800 | } |
2801 | |
2802 | $SQL = "SELECT count(*) |
2803 | FROM {$CFG->prefix}role_assignments r |
2804 | WHERE (r.contextid = $context->id $parentcontexts) |
2805 | AND r.roleid = $roleid"; |
2806 | |
2807 | return count_records_sql($SQL); |
2808 | } |
2809 | |
eef868d1 |
2810 | /** |
d76a5a7f |
2811 | * This function gets the list of courses that this user has a particular capability in |
2812 | * This is not the most efficient way of doing this |
2813 | * @param string capability |
2814 | * @param int $userid |
2815 | * @return array |
2816 | */ |
2817 | function get_user_capability_course($capability, $userid='') { |
eef868d1 |
2818 | |
d76a5a7f |
2819 | global $USER; |
2820 | if (!$userid) { |
eef868d1 |
2821 | $userid = $USER->id; |
d76a5a7f |
2822 | } |
eef868d1 |
2823 | |
d76a5a7f |
2824 | $usercourses = array(); |
2825 | $courses = get_records_select('course', '', '', 'id, id'); |
eef868d1 |
2826 | |
d76a5a7f |
2827 | foreach ($courses as $course) { |
2828 | if (has_capability($capability, get_context_capability(CONTEXT_COURSE, $course->id))) { |
2829 | $usercourses[] = $course; |
2830 | } |
2831 | } |
eef868d1 |
2832 | return $usercourses; |
e38f38c3 |
2833 | } |
2834 | |
2835 | |
2836 | /** This function finds the roles assigned directly to this context only |
2837 | * i.e. no parents role |
2838 | * @param object $context |
2839 | * @return array |
2840 | */ |
2841 | function get_roles_on_exact_context($context) { |
b5959f30 |
2842 | |
e38f38c3 |
2843 | global $CFG; |
49293027 |
2844 | |
b5959f30 |
2845 | return get_records_sql("SELECT DISTINCT r.* |
e38f38c3 |
2846 | FROM {$CFG->prefix}role_assignments ra, |
2847 | {$CFG->prefix}role r |
2848 | WHERE ra.roleid = r.id |
2849 | AND ra.contextid = $context->id"); |
b5959f30 |
2850 | |
49293027 |
2851 | } |
2852 | |
b5959f30 |
2853 | /* |
3a52e764 |
2854 | * Switches the current user to another role for the current session and only |
b5959f30 |
2855 | * in the given context. If roleid is not valid (eg 0) or the current user |
2856 | * doesn't have permissions to be switching roles then the user's session |
3a52e764 |
2857 | * is compltely reset to have their normal roles. |
2858 | * @param integer $roleid |
2859 | * @param object $context |
2860 | * @return bool |
2861 | */ |
2862 | function role_switch($roleid, $context) { |
2863 | global $USER; |
2864 | |
2865 | global $db; |
2866 | |
2867 | /// If we can't use this or are already using it or no role was specified then bail completely and reset |
b5959f30 |
2868 | if (empty($roleid) || !has_capability('moodle/role:switchroles', $context) |
2d07587b |
2869 | || !empty($USER->switchrole[$context->id]) || !confirm_sesskey()) { |
2870 | load_user_capability('', $context); // Reset all permissions for this context to normal |
2871 | unset($USER->switchrole[$context->id]); // Delete old capabilities |
3a52e764 |
2872 | return true; |
2873 | } |
2874 | |
2875 | /// We're allowed to switch but can we switch to the specified role? Use assignable roles to check. |
2876 | if (!$roles = get_assignable_roles($context)) { |
2877 | return false; |
2878 | } |
2879 | |
2880 | if (empty($roles[$roleid])) { /// We can't switch to this particular role |
2881 | return false; |
2882 | } |
2883 | |
2884 | if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) { |
2885 | return false; |
2886 | } |
2887 | |
2888 | /// We have a valid roleid that this user can switch to, so let's set up the session |
2889 | |
2d07587b |
2890 | $USER->switchrole[$context->id] = $roleid; // So we know later what state we are in |
3a52e764 |
2891 | |
2892 | unset($USER->capabilities[$context->id]); // Delete old capabilities |
2893 | |
2894 | if ($capabilities = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $sitecontext->id")) { |
2895 | foreach ($capabilities as $capability) { |
2896 | $USER->capabilities[$context->id][$capability->capability] = $capability->permission; |
2897 | } |
2898 | } |
2899 | |
2d07587b |
2900 | /// Add some permissions we are really going to always need, even if the role doesn't have them! |
3a52e764 |
2901 | |
2902 | $USER->capabilities[$context->id]['moodle/course:view'] = CAP_ALLOW; |
2903 | |
2904 | return true; |
2905 | |
2906 | } |
2907 | |
2908 | |
49293027 |
2909 | // get any role that has an override on exact context |
2910 | function get_roles_with_override_on_context($context) { |
b5959f30 |
2911 | |
49293027 |
2912 | global $CFG; |
b5959f30 |
2913 | |
49293027 |
2914 | return get_records_sql("SELECT DISTINCT r.* |
2915 | FROM {$CFG->prefix}role_capabilities rc, |
2916 | {$CFG->prefix}role r |
2917 | WHERE rc.roleid = r.id |
2918 | AND rc.contextid = $context->id"); |
2919 | } |
2920 | |
2921 | // get all capabilities for this role on this context (overrids) |
2922 | function get_capabilities_from_role_on_context($role, $context) { |
b5959f30 |
2923 | |
49293027 |
2924 | global $CFG; |
b5959f30 |
2925 | |
2926 | return get_records_sql("SELECT * |
49293027 |
2927 | FROM {$CFG->prefix}role_capabilities |
2928 | WHERE contextid = $context->id |
2929 | AND roleid = $role->id"); |
01e52ac7 |
2930 | } |
2931 | |
5b5781f4 |
2932 | // find out which roles has assignment on this context |
2933 | function get_roles_with_assignment_on_context($context) { |
ece4945b |
2934 | |
5b5781f4 |
2935 | global $CFG; |
ece4945b |
2936 | |
5b5781f4 |
2937 | return get_records_sql("SELECT DISTINCT r.* |
2938 | FROM {$CFG->prefix}role_assignments ra, |
2939 | {$CFG->prefix}role r |
2940 | WHERE ra.roleid = r.id |
2941 | AND ra.contextid = $context->id"); |
2942 | } |
2943 | |
2944 | |
2945 | |
01e52ac7 |
2946 | /* find all user assignemnt of users for this role, on this context |
2947 | */ |
2948 | function get_users_from_role_on_context($role, $context) { |
b5959f30 |
2949 | |
01e52ac7 |
2950 | global $CFG; |
b5959f30 |
2951 | |
01e52ac7 |
2952 | return get_records_sql("SELECT * |
2953 | FROM {$CFG->prefix}role_assignments |
2954 | WHERE contextid = $context->id |
b5959f30 |
2955 | AND roleid = $role->id"); |
01e52ac7 |
2956 | } |
3a52e764 |
2957 | |
f3652521 |
2958 | ?> |