adding keys for new tables, added migration support too
[moodle.git] / lib / accesslib.php
CommitLineData
bbbf2d40 1<?php
2 /* capability session information format
3 * 2 x 2 array
4 * [context][capability]
5 * where context is the context id of the table 'context'
6 * and capability is a string defining the capability
7 * e.g.
8 *
9 * [Capabilities] => [26][mod/forum:viewpost] = 1
10 * [26][mod/forum:startdiscussion] = -8990
11 * [26][mod/forum:editallpost] = -1
12 * [273][moodle:blahblah] = 1
13 * [273][moodle:blahblahblah] = 2
14 */
15
16
17// permission definitions
18define('CAP_ALLOW', 1);
19define('CAP_PREVENT', -1);
20define('CAP_PROHIBIT', -1000);
21
22
23// context definitions
24define('CONTEXT_SYSTEM', 10);
25define('CONTEXT_PERSONAL', 20);
26define('CONTEXT_USERID', 30);
27define('CONTEXT_COURSECAT', 40);
28define('CONTEXT_COURSE', 50);
29define('CONTEXT_GROUP', 60);
30define('CONTEXT_MODULE', 70);
31define('CONTEXT_BLOCK', 80);
32
33
34/**
35 * This functions get all the course categories in proper order
36 * @param int $contextid
37 * @param int $type
38 * @return array of contextids
39 */
40function get_parent_cats($contextid, $type) {
98882637 41
42 $parents = array();
43 $context = get_record('context', 'id', $contextid);
44
45 switch($type) {
46
47 case CONTEXT_COURSECAT:
48
49 $cat = get_record('course_categories','id',$context->instanceid);
50 while ($cat->parent) {
51
52 $context = get_context_instance(CONTEXT_COURSECAT, $cat->parent);
53 $parents[] = $context->id;
54 $cat = get_record('course_categories','id',$cat->parent);
55 }
56
57 break;
58
59 case CONTEXT_COURSE:
60
61 $course = get_record('course', 'id', $context->instanceid);
62 $cat = get_record('course_categories','id',$course->category);
63 $catinstance = get_context_instance(CONTEXT_COURSECAT, $course->category);
64 $parents[] = $catinstance->id;
65
66 // what to do with cat 0?
67 while ($cat->parent) {
68 $context = get_context_instance(CONTEXT_COURSECAT, $cat->parent);
69 $parents[] = $context->id;
70 $cat = get_record('course_categories','id',$cat->parent);
71 }
72 break;
73
74 default:
75 break;
76
77 }
78
79 return array_reverse($parents);
bbbf2d40 80}
81
82
83/* Functions for Roles & Capabilites */
84
85
86/**
87 * This function returns whether the current user has the capability of performing a function
88 * For example, we can do has_capability('mod/forum:replypost',$cm) in forum
89 * only one of the 4 (moduleinstance, courseid, site, userid) would be set at 1 time
90 * This is a recursive funciton.
91 * Might change to require_capability, and throw an error if not authorized.
92 * @uses $USER
93 * @param string $capability - name of the capability
94 * @param int $contextid
95 * @param kill bool - if set, kill when the user has no capability
96 * @return bool
97 */
98function has_capability($capability, $contextid, $kill=false, $userid=NULL) {
99
98882637 100 global $USER;
bbbf2d40 101
98882637 102 if ($userid && $userid != $USER->id) { // loading other user's capability
103 $capabilities = load_user_capability($capability, $contextid, $userid);
104 } else {
105 $capabilities = $USER->capabilities;
106 }
bbbf2d40 107
98882637 108 $context = get_record('context','id',$contextid);
bbbf2d40 109
98882637 110 // Check site
111 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
112 if (isset($capabilities[$sitecontext->id]['moodle/site:doanything'])) {
113 return ($capabilities[$sitecontext->id]['moodle/site:doanything']);
114 }
115
116 switch (context_level($contextid)) {
bbbf2d40 117
118 case CONTEXT_COURSECAT:
98882637 119 // Check parent cats.
120 $parentcats = get_parent_cats($contextid, CONTEXT_COURSECAT);
121 foreach ($parentcats as $parentcat) {
122 if (isset($capabilities[$parentcat]['moodle/site:doanything'])) {
123 return ($capabilities[$parentcat]['moodle/site:doanything']);
124 }
125 }
bbbf2d40 126 break;
127
128 case CONTEXT_COURSE:
98882637 129 // Check parent cat.
130 $parentcats = get_parent_cats($contextid, CONTEXT_COURSE);
131
132 foreach ($parentcats as $parentcat) {
133 if (isset($capabilities[$parentcat]['do_anything'])) {
134 return ($capabilities[$parentcat]['do_anything']);
135 }
136 }
bbbf2d40 137 break;
138
139 case CONTEXT_GROUP:
98882637 140 // Find course.
141 $group = get_record('groups','id',$context->instanceid);
bbbf2d40 142 $courseinstance = get_context_instance(CONTEXT_COURSE, $group->courseid);
98882637 143
144 $parentcats = get_parent_cats($courseinstance->id, CONTEXT_COURSE);
145 foreach ($parentcats as $parentcat) {
146 if (isset($capabilities[$parentcat->id]['do_anything'])) {
147 return ($capabilities[$parentcat->id]['do_anything']);
148 }
149 }
150
151 $coursecontext = '';
152 if (isset($capabilities[$courseinstance->id]['do_anything'])) {
153 return ($capabilities[$courseinstance->id]['do_anything']);
154 }
155
bbbf2d40 156 break;
157
158 case CONTEXT_MODULE:
159 // Find course.
160 $cm = get_record('course_modules', 'id', $context->instanceid);
98882637 161 $courseinstance = get_context_instance(CONTEXT_COURSE, $cm->course);
bbbf2d40 162
98882637 163 if ($parentcats = get_parent_cats($courseinstance->id, CONTEXT_COURSE)) {
164 foreach ($parentcats as $parentcat) {
165 if (isset($capabilities[$parentcat]['do_anything'])) {
166 return ($capabilities[$parentcat]['do_anything']);
167 }
168 }
169 }
170
171 if (isset($capabilities[$courseinstance->id]['do_anything'])) {
172 return ($capabilities[$courseinstance->id]['do_anything']);
173 }
bbbf2d40 174
175 break;
176
177 case CONTEXT_BLOCK:
178 // 1 to 1 to course.
179 // Find course.
180 $block = get_record('block_instance','id',$context->instanceid);
181 $courseinstance = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check
98882637 182
183 $parentcats = get_parent_cats($courseinstance->id, CONTEXT_COURSE);
184 foreach ($parentcats as $parentcat) {
185 if (isset($capabilities[$parentcat]['do_anything'])) {
186 return ($capabilities[$parentcat]['do_anything']);
187 }
188 }
189
190 if (isset($capabilities[$courseinstance->id]['do_anything'])) {
191 return ($capabilities[$courseinstance->id]['do_anything']);
192 }
bbbf2d40 193 break;
194
195 default:
196 // CONTEXT_SYSTEM: CONTEXT_PERSONAL: CONTEXT_USERID:
197 // Do nothing.
198 break;
98882637 199 }
bbbf2d40 200
98882637 201 // Last: check self.
202 if (isset($capabilities[$contextid]['do_anything'])) {
203 return ($capabilities[$contextid]['do_anything']);
204 }
205
206 // do_anything has not been set, we now look for it the normal way.
207 return capability_search($capability, $contextid, $kill, $capabilities);
bbbf2d40 208
209}
210
211
212/**
213 * In a separate function so that we won't have to deal with do_anything.
214 * again. Used by function has_capability.
215 * @param $capability - capability string
216 * @param $contextid - the context id
217 * @param $kill - boolean. Error out and exit if the user doesn't have the
218 * capability?
219 * @param $capabilities - either $USER->capability or loaded array
220 * @return permission (int)
221 */
222function capability_search($capability, $contextid, $kill=false, $capabilities) {
223 global $USER, $CFG;
224
225 if ($CFG->debug) {
98882637 226 notify("We are looking for $capability in context $contextid", 'notifytiny');
bbbf2d40 227 }
228
229 if (isset($capabilities[$contextid][$capability])) {
230 return ($capabilities[$contextid][$capability]);
231 }
232
233 /* Then, we check the cache recursively */
234 $context = get_record('context','id',$contextid); // shared
98882637 235 $permission = 0;
236
bbbf2d40 237 switch (context_level($contextid)) {
238
239 case CONTEXT_SYSTEM: // by now it's a definite an inherit
240 $permission = 0;
241 break;
242
243 case CONTEXT_PERSONAL:
244 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
245 $permission = (capability_search($capability, $parent->id, false, $capabilities));
246 break;
247
248 case CONTEXT_USERID:
249 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
250 $permission = (capability_search($capability, $parent->id, false, $capabilities));
251 break;
252
253 case CONTEXT_COURSECAT: // Coursecat -> coursecat or site
254 $coursecat = get_record('course_categories','id',$context->instanceid);
255 if ($coursecat->parent) { // return parent value if exist
256 $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent);
257 } else { // else return site value
258 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
259 }
260 $permission = (capability_search($capability, $parent->id, false, $capabilities));
261 break;
262
263 case CONTEXT_COURSE: // 1 to 1 to course cat
264 // find the course cat, and return its value
265 $course = get_record('course','id',$context->instanceid);
266 $parent = get_context_instance(CONTEXT_COURSECAT, $course->category);
267 $permission = (capability_search($capability, $parent->id, false, $capabilities));
268 break;
269
270 case CONTEXT_GROUP: // 1 to 1 to course
271 $group = get_record('groups','id',$context->instanceid);
272 $parent = get_context_instance(CONTEXT_COURSE, $group->courseid);
273 $permission = (capability_search($capability, $parent->id, false, $capabilities));
274 break;
275
276 case CONTEXT_MODULE: // 1 to 1 to course
277 $cm = get_record('course_modules','id',$context->instanceid);
278 $parent = get_context_instance(CONTEXT_COURSE, $cm->course);
279 $permission = (capability_search($capability, $parent->id, false, $capabilities));
280 break;
281
282 case CONTEXT_BLOCK: // 1 to 1 to course
283 $block = get_record('block_instance','id',$context->instanceid);
284 $parent = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check
285 $permission = (capability_search($capability, $parent->id, false, $capabilities));
286 break;
287
288 default:
289 error ('This is an unknown context!');
290 return false;
291 }
292
293 if ($kill && ($permission <= 0)) {
98882637 294 error ('You do not have the required capability '.$capability);
295 }
296 return $permission;
bbbf2d40 297}
298
299
300/**
301 * This function should be called immediately after a login, when $USER is set.
302 * It will build an array of all the capabilities at each level
303 * i.e. site/metacourse/course_category/course/moduleinstance
304 * Note we should only load capabilities if they are explicitly assigned already,
305 * we should not load all module's capability!
306 * @param $userid - the id of the user whose capabilities we want to load
307 * @return array
308 * possible just s simple 2D array with [contextid][capabilityname]
309 * [Capabilities] => [26][forum_post] = 1
310 * [26][forum_start] = -8990
311 * [26][forum_edit] = -1
312 * [273][blah blah] = 1
313 * [273][blah blah blah] = 2
314 */
315function load_user_capability($capability='', $contextid ='', $userid='') {
316
98882637 317 global $USER, $CFG;
bbbf2d40 318
319 if (empty($userid)) {
320 $userid = $USER->id;
321 } else {
98882637 322 $otheruserid = $userid;
bbbf2d40 323 }
324
325 if ($capability) {
98882637 326 $capsearch = ' AND rc.capability = '.$capability.' ';
bbbf2d40 327 } else {
98882637 328 $capsearch ='';
bbbf2d40 329 }
330 // First we generate a list of all relevant contexts of the user
331
98882637 332 if ($contextid) { // if context is specified
333 $context = get_record('context', 'id', $contextid);
334
335 $usercontexts = get_parent_contexts($context->id);
336 $listofcontexts = '('.implode(',', $usercontexts).')';
337 } else { // else, we load everything
338 $usercontexts = get_records('role_assignments','userid',$userid);
339 $listofcontexts = '(';
340 foreach ($usercontexts as $usercontext) {
341 $listofcontexts .= $usercontext->contextid;
342 $listofcontexts .= ',';
343 }
344 $listofcontexts = rtrim ($listofcontexts, ",");
345 $listofcontexts .= ')';
bbbf2d40 346 }
347
348 // Then we use 1 giant SQL to bring out all relevant capabilities.
349 // The first part gets the capabilities of orginal role.
350 // The second part gets the capabilities of overriden roles.
351
98882637 352 $siteinstance = get_context_instance(CONTEXT_SYSTEM, SITEID);
bbbf2d40 353
41811960 354 $SQL = " SELECT rc.capability, c1.id, (c1.level * 100) AS aggregatelevel,
bbbf2d40 355 SUM(rc.permission) AS sum
356 FROM
357 {$CFG->prefix}role_assignments AS ra
358 INNER JOIN {$CFG->prefix}role_capabilities AS rc ON ra.roleid=rc.roleid
359 INNER JOIN {$CFG->prefix}context AS c1 ON ra.contextid=c1.id
360 WHERE
361 ra.userid=$userid AND
362 c1.id IN $listofcontexts AND
363 rc.contextid=$siteinstance->id
98882637 364 $capsearch
bbbf2d40 365 GROUP BY
41811960 366 rc.capability,aggregatelevel,c1.id
bbbf2d40 367 HAVING
41811960 368 SUM(rc.permission) != 0
bbbf2d40 369 UNION
370
41811960 371 SELECT rc.capability, c1.id, (c1.level * 100 + c2.level) AS aggregatelevel,
bbbf2d40 372 SUM(rc.permission) AS sum
373 FROM
374 {$CFG->prefix}role_assignments AS ra
375 INNER JOIN {$CFG->prefix}role_capabilities AS rc ON ra.roleid=rc.roleid
376 INNER JOIN {$CFG->prefix}context AS c1 ON ra.contextid=c1.id
377 LEFT OUTER JOIN {$CFG->prefix}context AS c2 ON rc.contextid=c2.id
378 WHERE
379 ra.userid=$userid AND
380 c1.id IN $listofcontexts AND
381 c2.id IN $listofcontexts AND rc.contextid != $siteinstance->id
382 $capsearch
383
384 GROUP BY
41811960 385 rc.capability, aggregatelevel, c1.id
bbbf2d40 386 HAVING
41811960 387 SUM(rc.permission) != 0
bbbf2d40 388 ORDER BY
41811960 389 aggregatelevel ASC
bbbf2d40 390 ";
391
bbbf2d40 392
98882637 393 $capabilities = array(); // Reinitialize.
394 $rs = get_recordset_sql($SQL);
395
bbbf2d40 396 if ($rs && $rs->RecordCount() > 0) {
397 while (!$rs->EOF) {
98882637 398 $array = $rs->fields;
399 $temprecord = new object;
400
401 foreach ($array as $key=>$val) {
402 $temprecord->{$key} = $val;
403 }
bbbf2d40 404 $capabilities[] = $temprecord;
405 $rs->MoveNext();
406 }
407 }
408
409 /* so up to this point we should have somethign like this
41811960 410 * $capabilities[1] ->aggregatelevel = 1000
bbbf2d40 411 ->module = SITEID
412 ->capability = do_anything
413 ->id = 1 (id is the context id)
414 ->sum = 0
415
41811960 416 * $capabilities[2] ->aggregatelevel = 1000
bbbf2d40 417 ->module = SITEID
418 ->capability = post_messages
419 ->id = 1
420 ->sum = -9000
421
41811960 422 * $capabilittes[3] ->aggregatelevel = 3000
bbbf2d40 423 ->module = course
424 ->capability = view_course_activities
425 ->id = 25
426 ->sum = 1
427
41811960 428 * $capabilittes[4] ->aggregatelevel = 3000
bbbf2d40 429 ->module = course
430 ->capability = view_course_activities
431 ->id = 26
432 ->sum = 0 (this is another course)
433
41811960 434 * $capabilities[5] ->aggregatelevel = 3050
bbbf2d40 435 ->module = course
436 ->capability = view_course_activities
437 ->id = 25 (override in course 25)
438 ->sum = -1
439 * ....
440 * now we proceed to write the session array, going from top to bottom
441 * at anypoint, we need to go up and check parent to look for prohibit
442 */
443 // print_object($capabilities);
444
445 /* This is where we write to the actualy capabilities array
446 * what we need to do from here on is
447 * going down the array from lowest level to highest level
448 * 1) recursively check for prohibit,
449 * if any, we write prohibit
450 * else, we write the value
451 * 2) at an override level, we overwrite current level
452 * if it's not set to prohibit already, and if different
453 * ........ that should be it ........
454 */
98882637 455 $usercap = array(); // for other user's capabilities
bbbf2d40 456 foreach ($capabilities as $capability) {
457
41811960 458 if (!empty($otheruserid)) { // we are pulling out other user's capabilities, do not write to session
98882637 459
460 if (capability_prohibits($capability->capability, $capability->id, $capability->sum, $usercap)) {
461 $usercap[$capability->id][$capability->capability] = -9000;
462 continue;
463 }
464
465 $usercap[$capability->id][$capability->capability] = $capability->sum;
466
467 } else {
468
469 if (capability_prohibits($capability->capability, $capability->id, $capability->sum)) { // if any parent or parent's parent is set to prohibit
470 $USER->capabilities[$capability->id][$capability->capability] = -9000;
471 continue;
472 }
473
474 // if no parental prohibit set
475 // just write to session, i am not sure this is correct yet
476 // since 3050 shows up after 3000, and 3070 shows up after 3050,
477 // it should be ok just to overwrite like this, provided that there's no
478 // parental prohibits
479 // no point writing 0, since 0 = inherit
480 // we need to write even if it's 0, because it could be an inherit override
481 $USER->capabilities[$capability->id][$capability->capability] = $capability->sum;
482 }
bbbf2d40 483 }
484
485 // now we don't care about the huge array anymore, we can dispose it.
486 unset($capabilities);
487
41811960 488 if (!empty($otheruseid)) {
98882637 489 return $usercap; // return the array
bbbf2d40 490 }
491 // see array in session to see what it looks like
492
493}
494
495
496/**
497 * This is a recursive function that checks whether the capability in this
498 * context, or the parent capabilities are set to prohibit.
499 *
500 * At this point, we can probably just use the values already set in the
501 * session variable, since we are going down the level. Any prohit set in
502 * parents would already reflect in the session.
503 *
504 * @param $capability - capability name
505 * @param $sum - sum of all capabilities values
506 * @param $contextid - the context id
507 * @param $array - when loading another user caps, their caps are not stored in session but an array
508 */
509function capability_prohibits($capability, $contextid, $sum='', $array='') {
510 global $USER;
511 if ($sum < -8000) {
512 // If this capability is set to prohibit.
513 return true;
514 }
515
516 if (isset($array)) {
517 if (isset($array[$contextid][$capability])
518 && $array[$contextid][$capability] < -8000) {
98882637 519 return true;
520 }
bbbf2d40 521 } else {
98882637 522 // Else if set in session.
523 if (isset($USER->capabilities[$contextid][$capability])
bbbf2d40 524 && $USER->capabilities[$contextid][$capability] < -8000) {
98882637 525 return true;
526 }
bbbf2d40 527 }
528 $context = get_record('context', 'id', $contextid);
529 switch (context_level($contextid)) {
530
531 case CONTEXT_SYSTEM:
532 // By now it's a definite an inherit.
533 return 0;
534 break;
535
536 case CONTEXT_PERSONAL:
537 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
538 return (capability_prohibits($capability, $parent->id));
539 break;
540
541 case CONTEXT_USERID:
542 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
543 return (capability_prohibits($capability, $parent->id));
544 break;
545
546 case CONTEXT_COURSECAT:
547 // Coursecat -> coursecat or site.
548 $coursecat = get_record('course_categories','id',$context->instanceid);
41811960 549 if (!empty($coursecat->parent)) {
bbbf2d40 550 // return parent value if exist.
551 $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent);
552 } else {
553 // Return site value.
554 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
555 }
556 return (capability_prohibits($capability, $parent->id));
557 break;
558
559 case CONTEXT_COURSE:
560 // 1 to 1 to course cat.
561 // Find the course cat, and return its value.
562 $course = get_record('course','id',$context->instanceid);
563 $parent = get_context_instance(CONTEXT_COURSECAT, $course->category);
564 return (capability_prohibits($capability, $parent->id));
565 break;
566
567 case CONTEXT_GROUP:
568 // 1 to 1 to course.
569 $group = get_record('groups','id',$context->instanceid);
570 $parent = get_context_instance(CONTEXT_COURSE, $group->courseid);
571 return (capability_prohibits($capability, $parent->id));
572 break;
573
574 case CONTEXT_MODULE:
575 // 1 to 1 to course.
576 $cm = get_record('course_modules','id',$context->instanceid);
577 $parent = get_context_instance(CONTEXT_COURSE, $cm->course);
578 return (capability_prohibits($capability, $parent->id));
579 break;
580
581 case CONTEXT_BLOCK:
582 // 1 to 1 to course.
583 $block = get_record('block_instance','id',$context->instanceid);
584 $parent = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check
585 return (capability_prohibits($capability, $parent->id));
586 break;
587
588 default:
589 error ('This is an unknown context!');
590 return false;
591 }
592}
593
594
595/**
596 * A print form function. This should either grab all the capabilities from
597 * files or a central table for that particular module instance, then present
598 * them in check boxes. Only relevant capabilities should print for known
599 * context.
600 * @param $mod - module id of the mod
601 */
602function print_capabilities($modid=0) {
603 global $CFG;
604
605 $capabilities = array();
606
607 if ($modid) {
608 // We are in a module specific context.
609
610 // Get the mod's name.
611 // Call the function that grabs the file and parse.
612 $cm = get_record('course_modules', 'id', $modid);
613 $module = get_record('modules', 'id', $cm->module);
614
615 } else {
616 // Print all capabilities.
617 foreach ($capabilities as $capability) {
618 // Prints the check box component.
619 }
620 }
621}
622
623
624/**
625 * This function handles the upgrade process of assigning default roles
626 * to the existing users
627 */
628function moodle_upgrade_roles_system_17() {
629
98882637 630 global $CFG;
bbbf2d40 631 // Create a system wide context for assignemnt.
632 $systemcontext = $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
633
98882637 634 // loading legacy roles and capabilities (1 legacy capability per legacy role at system levle)
bbbf2d40 635 $adminrole = create_role(get_string('administrator'), get_string('administratordescription'), 'moodle/legacy:admin');
98882637 636 if (!assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $systemcontext->id)) {
bbbf2d40 637 error('Could not assign moodle/site:doanything to the admin role');
638 }
639 $coursecreatorrole = create_role(get_string('coursecreators'), get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator');
98882637 640 $noneditteacherrole = create_role(get_string('noneditingteacher'), get_string('noneditingteacherdescription'), 'moodle/legacy:teacher');
641 $editteacherrole = create_role(get_string('defaultcourseteacher'), get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher');
642 $studentrole = create_role(get_string('defaultcoursestudent'), get_string('defaultcoursestudentdescription'), 'moodle/legacy:student');
bbbf2d40 643 $guestrole = create_role(get_string('guest'), get_string('guestdescription'), 'moodle/legacy:guest');
644
98882637 645 // Look inside user_admin, user_creator, $user_teachers, $user_students
bbbf2d40 646 // and assign roles. If a user has both teacher and student role, only
647 // teacher role is assigned. The assignment should be system level.
648
98882637 649 /**
bbbf2d40 650 * Upgrade the admins.
651 */
652
653 // sort using id asc, first one is primary admin
654 $useradmins = get_records_sql('SELECT * from '.$CFG->prefix.'user_admins ORDER BY ID ASC');
655 foreach ($useradmins as $admin) {
98882637 656 role_assign($adminrole, $admin->userid, 0, $systemcontext->id);
bbbf2d40 657 }
658 // Do we assign the capability moodle/doanything to the admin roles here?
659
660
661 /**
662 * Upgrade course creators.
663 */
664
665 $usercoursecreators = get_records('user_coursecreators');
666 foreach ($usercoursecreators as $coursecreator) {
667 role_assign($$coursecreatorrole, $coursecreator->userid, 0, $systemcontext->id);
668 }
669
670 /**
671 * Upgrade editting teachers and non-editting teachers.
672 */
673
674 $userteachers = get_records('user_teachers');
675 foreach ($userteachers as $teacher) {
676 $coursecontext = get_context_instance(CONTEXT_COURSE, $teacher->course); // needs cache
677 if ($teacher->editall) { // editting teacher
678 role_assign($editteacherrole, $teacher->userid, 0, $coursecontext->id);
679 } else {
680 role_assign($noneditteacherrole, $teacher->userid, 0, $coursecontext->id);
681 }
682 }
683
684 /**
685 * Upgrade students.
686 */
687 $userstudents = get_records('user_students');
688 foreach ($userstudents as $student) {
689 $coursecontext = get_context_instance(CONTEXT_COURSE, $student->course);
690 role_assign($studentrole, $student->userid, 0, $coursecontext->id);
691 }
692
693 /**
694 * Upgrade guest (only 1 entry).
695 */
696 $guestuser = get_record('user', 'username', 'guest');
697 role_assign($guestrole, $guestuser->id, 0, $systemcontext->id);
698
699 //
700 // Should we delete the tables after we are done?
701 //
702
703 set_config('rolesactive', 1);
704}
705
706
707/**
708 * Assign the defaults found in this capabality definition to roles that have
709 * the corresponding legacy capabilities assigned to them.
710 * @param $legacyperms - an array in the format (example):
711 * 'guest' => CAP_PREVENT,
712 * 'student' => CAP_ALLOW,
713 * 'teacher' => CAP_ALLOW,
714 * 'editingteacher' => CAP_ALLOW,
715 * 'coursecreator' => CAP_ALLOW,
716 * 'admin' => CAP_ALLOW
717 * @return boolean - success or failure.
718 */
719function assign_legacy_capabilities($capability, $legacyperms) {
720
721 foreach ($legacyperms as $type => $perm) {
722
723 $systemcontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
724
725 // The legacy capabilities are:
726 // 'moodle/legacy:guest'
727 // 'moodle/legacy:student'
728 // 'moodle/legacy:teacher'
729 // 'moodle/legacy:editingteacher'
730 // 'moodle/legacy:coursecreator'
731 // 'moodle/legacy:admin'
732
733 if (!$roles = get_roles_with_capability('moodle/legacy:'.$type, CAP_ALLOW)) {
734 return false;
735 }
736
737 foreach ($roles as $role) {
738 // Assign a site level capability.
739 if(!assign_capability($capability, $perm, $role->id, $systemcontext->id)) {
740 return false;
741 }
742 }
743 }
744 return true;
745}
746
747
748// checks to see if a capability is a legacy capability, returns bool
749function islegacy($capabilityname) {
98882637 750 if (strstr($capabilityname, 'legacy') === false) {
751 return false;
752 } else {
753 return true;
754 }
bbbf2d40 755}
756
757/************************************
758 * Context Manipulation functions *
759 **********************************/
760
761
762/**
763 * This should be called prolly everytime a user, group, module, course,
764 * coursecat or site is set up maybe?
765 * @param $level
766 * @param $instanceid
767 */
768function create_context($level, $instanceid) {
769 if (!get_record('context','level',$level,'instanceid',$instanceid)) {
770 $context = new object;
771 $context->level = $level;
772 $context->instanceid = $instanceid;
773 return insert_record('context',$context);
774 }
775}
776
777
778/**
779 * Get the context instance as an object. This function will create the
780 * context instance if it does not exist yet.
781 * @param $level
782 * @param $instance
783 */
784function get_context_instance($level, $instance=SITEID) {
785 // echo "getting level $level instance $instance";
786
787 // XXX TODO Add caching here
98882637 788 if (!$context = get_record('context', 'level', $level, 'instanceid', $instance)) {
789 //echo "creating ...";
790 create_context($level, $instance);
791 $context = get_record('context', 'level', $level, 'instanceid', $instance);
792 }
bbbf2d40 793 return $context;
794}
795
796
797/**
798 * Looks up the context level.
799 * @param int $contextid
800 * @return int
801 */
802function context_level($contextid) {
803 $context = get_record('context','id',$contextid);
804 return ($context->level);
805}
806
807
808
809/************************************
810 * DB TABLE RELATED FUNCTIONS *
811 ************************************/
812
813/**********************************************
814 * function that creates a role
815 * @param name - role name
816 * @param description - role description
817 * @param legacy - optional legacy capability
818 * @return id or false
819 */
820function create_role($name, $description, $legacy='') {
98882637 821
822 // check for duplicate role name
823
824 if ($role = get_record('role','name', $name)) {
825 print_object($role);
826 error('there is already a role with this name!');
827 }
828
829 $role->name = $name;
830 $role->description = $description;
831
832 if ($id = insert_record('role', $role)) {
833 if ($legacy) {
834 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
835 assign_capability($legacy, CAP_ALLOW, $id, $context->id);
836 }
837 return $id;
838 } else {
839 return false;
840 }
bbbf2d40 841
842}
843
844/**
845 * Function to write context specific overrides, or default capabilities.
846 * @param module - string name
847 * @param capability - string name
848 * @param contextid - context id
849 * @param roleid - role id
850 * @param permission - int 1,-1 or -1000
851 */
852function assign_capability($capability, $permission, $roleid, $contextid) {
98882637 853
854 global $USER;
855
856 if (empty($permission) || $permission == 0) { // if permission is not set
857 unassign_capability($capability, $roleid, $contextid);
858 }
bbbf2d40 859
860 $cap = new object;
861 $cap->contextid = $contextid;
862 $cap->roleid = $roleid;
863 $cap->capability = $capability;
864 $cap->permission = $permission;
865 $cap->timemodified = time();
866 $cap->modifierid = $USER->id;
867
868 return insert_record('role_capabilities', $cap);
869}
870
871
872/**
873 * Unassign a capability from a role.
874 * @param $roleid - the role id
875 * @param $capability - the name of the capability
876 * @return boolean - success or failure
877 */
878function unassign_capability($capability, $roleid, $contextid=NULL) {
98882637 879
880 if (isset($contextid)) {
881 $status = delete_records('role_capabilities', 'capability', $capability,
882 'roleid', $roleid, 'contextid', $contextid);
883 } else {
884 $status = delete_records('role_capabilities', 'capability', $capability,
885 'roleid', $roleid);
886 }
887 return $status;
bbbf2d40 888}
889
890
891/**
892 * Get the roles that have a given capability.
893 * @param $capability - capability name (string)
894 * @param $permission - optional, the permission defined for this capability
895 * either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT
896 * @return array or role objects
897 */
898function get_roles_with_capability($capability, $permission=NULL) {
899
900 global $CFG;
901
902 $selectroles = "SELECT r.*
903 FROM {$CFG->prefix}role AS r,
904 {$CFG->prefix}role_capabilities AS rc
905 WHERE rc.capability = '$capability'
906 AND rc.roleid = r.id";
907
908 if (isset($permission)) {
909 $selectroles .= " AND rc.permission = '$permission'";
910 }
911 return get_records_sql($selectroles);
912}
913
914
915/**
916 * This function makes a role-assignment (user to a role)
917 * @param $roleid - the role of the id
918 * @param $userid - userid
919 * @param $groupid - group id
920 * @param $contextid - id of the context
921 * @param $timestart - time this assignment becomes effective
922 * @param $timeend - time this assignemnt ceases to be effective
923 * @uses $USER
924 * @return id - new id of the assigment
925 */
926function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $timeend=0, $hidden=0) {
aa311411 927 global $USER, $CFG;
bbbf2d40 928
aa311411 929 if ($CFG->debug) {
98882637 930 notify("Assign roleid $roleid userid $userid contextid $contextid", 'notifytiny');
aa311411 931 }
bbbf2d40 932
933 if (empty($roleid)) {
934 error ('you need to select a role');
935 }
936
937 if (empty($userid) && empty($groupid)) {
938 error ('you need to assign this role to a user or a group');
939 }
940
941 if (empty($contextid)) {
942 error ('you need to assign this role to a context, e.g. a course, or an activity');
943 }
944
945 $ra = new object;
946 $ra->roleid = $roleid;
947 $ra->contextid = $contextid;
948 $ra->userid = $userid;
949 $ra->hidden = $hidden;
950 $ra->groupid = $groupid;
951 $ra->timestart = $timestart;
952 $ra->timeend = $timeend;
953 $ra->timemodified = time();
954 $ra->modifier = $USER->id;
955
956 return insert_record('role_assignments', $ra);
957
958}
959
960
961/**
962 * Deletes a role assignment.
963 * @param $roleid
964 * @param $userid
965 * @param $groupid
966 * @param $contextid
967 * @return boolean - success or failure
968 */
969function role_unassign($roleid, $userid, $groupid, $contextid) {
98882637 970 if ($groupid) {
971 // do nothing yet as this is not implemented
972 }
973 else {
974 return delete_records('role_assignments', 'userid', $userid,
975 'roleid', $roleid, 'contextid', $contextid);
976 }
bbbf2d40 977}
978
979
980/**
981 * Loads the capability definitions for the component (from file). If no
982 * capabilities are defined for the component, we simply return an empty array.
983 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
984 * @return array of capabilities
985 */
986function load_capability_def($component) {
987 global $CFG;
988
989 if ($component == 'moodle') {
990 $defpath = $CFG->libdir.'/db/access.php';
991 $varprefix = 'moodle';
992 } else {
993 $defpath = $CFG->dirroot.'/'.$component.'/db/access.php';
994 $varprefix = str_replace('/', '_', $component);
995 }
996 $capabilities = array();
997
998 if (file_exists($defpath)) {
999 require_once($defpath);
1000 $capabilities = ${$varprefix.'_capabilities'};
1001 }
1002 return $capabilities;
1003}
1004
1005
1006/**
1007 * Gets the capabilities that have been cached in the database for this
1008 * component.
1009 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
1010 * @return array of capabilities
1011 */
1012function get_cached_capabilities($component='moodle') {
1013 if ($component == 'moodle') {
1014 $storedcaps = get_records_select('capabilities',
1015 "name LIKE 'moodle/%:%'");
1016 } else {
1017 $storedcaps = get_records_select('capabilities',
1018 "name LIKE '$component:%'");
1019 }
1020 return $storedcaps;
1021}
1022
1023
1024/**
1025 * Updates the capabilities table with the component capability definitions.
1026 * If no parameters are given, the function updates the core moodle
1027 * capabilities.
1028 *
1029 * Note that the absence of the db/access.php capabilities definition file
1030 * will cause any stored capabilities for the component to be removed from
1031 * the database.
1032 *
1033 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
1034 * @return boolean
1035 */
1036function update_capabilities($component='moodle') {
1037
1038 $storedcaps = array();
1039 $filecaps = array();
1040
1041 $cachedcaps = get_cached_capabilities($component);
1042 if ($cachedcaps) {
1043 foreach ($cachedcaps as $cachedcap) {
1044 array_push($storedcaps, $cachedcap->name);
1045 }
1046 }
1047
1048 $filecaps = load_capability_def($component);
1049
1050 // Are there new capabilities in the file definition?
1051 $newcaps = array();
1052
1053 foreach ($filecaps as $filecap => $def) {
1054 if (!$storedcaps ||
1055 ($storedcaps && in_array($filecap, $storedcaps) === false)) {
1056 $newcaps[$filecap] = $def;
1057 }
1058 }
1059 // Add new capabilities to the stored definition.
1060 foreach ($newcaps as $capname => $capdef) {
1061 $capability = new object;
1062 $capability->name = $capname;
1063 $capability->captype = $capdef['captype'];
1064 $capability->contextlevel = $capdef['contextlevel'];
1065 $capability->component = $component;
1066
1067 if (!insert_record('capabilities', $capability, false, 'id')) {
1068 return false;
1069 }
1070 // Do we need to assign the new capabilities to roles that have the
1071 // legacy capabilities moodle/legacy:* as well?
1072 if (isset($capdef['legacy']) && is_array($capdef['legacy']) &&
1073 !assign_legacy_capabilities($capname, $capdef['legacy'])) {
1074 error('Could not assign legacy capabilities');
1075 return false;
1076 }
1077 }
1078 // Are there any capabilities that have been removed from the file
1079 // definition that we need to delete from the stored capabilities and
1080 // role assignments?
1081 capabilities_cleanup($component, $filecaps);
1082
1083 return true;
1084}
1085
1086
1087/**
1088 * Deletes cached capabilities that are no longer needed by the component.
1089 * Also unassigns these capabilities from any roles that have them.
1090 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
1091 * @param $newcapdef - array of the new capability definitions that will be
1092 * compared with the cached capabilities
1093 * @return int - number of deprecated capabilities that have been removed
1094 */
1095function capabilities_cleanup($component, $newcapdef=NULL) {
1096
1097 $removedcount = 0;
1098
1099 if ($cachedcaps = get_cached_capabilities($component)) {
1100 foreach ($cachedcaps as $cachedcap) {
1101 if (empty($newcapdef) ||
1102 array_key_exists($cachedcap->name, $newcapdef) === false) {
1103
1104 // Remove from capabilities cache.
1105 if (!delete_records('capabilities', 'name', $cachedcap->name)) {
1106 error('Could not delete deprecated capability '.$cachedcap->name);
1107 } else {
1108 $removedcount++;
1109 }
1110 // Delete from roles.
1111 if($roles = get_roles_with_capability($cachedcap->name)) {
1112 foreach($roles as $role) {
1113 if (!unassign_capability($role->id, $cachedcap->name)) {
1114 error('Could not unassign deprecated capability '.
1115 $cachedcap->name.' from role '.$role->name);
1116 }
1117 }
1118 }
1119 } // End if.
1120 }
1121 }
1122 return $removedcount;
1123}
1124
1125
1126
1127
1128/************************************************************
1129 * * UI FUNCTIONS * *
1130 ************************************************************/
1131
1132
1133/**
1134 * prints human readable context identifier.
1135 */
1136function print_context_name($contextid) {
1137
ec0810ee 1138 $name = '';
1139
98882637 1140 $context = get_record('context', 'id', $contextid);
ec0810ee 1141
98882637 1142 switch ($context->level) {
1143
bbbf2d40 1144 case CONTEXT_SYSTEM: // by now it's a definite an inherit
ec0810ee 1145 $name = get_string('site');
bbbf2d40 1146 break;
1147
1148 case CONTEXT_PERSONAL:
ec0810ee 1149 $name = get_string('personal');
bbbf2d40 1150 break;
1151
1152 case CONTEXT_USERID:
ec0810ee 1153 if ($user = get_record('user', 'id', $context->instanceid)) {
1154 $name = get_string('user').': '.fullname($user);
1155 }
bbbf2d40 1156 break;
1157
1158 case CONTEXT_COURSECAT: // Coursecat -> coursecat or site
ec0810ee 1159 if ($category = get_record('course_categories', 'id', $context->instanceid)) {
1160 $name = get_string('category').': '.$category->name;
1161 }
bbbf2d40 1162 break;
1163
1164 case CONTEXT_COURSE: // 1 to 1 to course cat
ec0810ee 1165 if ($course = get_record('course', 'id', $context->instanceid)) {
1166 $name = get_string('course').': '.$course->fullname;
1167 }
bbbf2d40 1168 break;
1169
1170 case CONTEXT_GROUP: // 1 to 1 to course
ec0810ee 1171 if ($group = get_record('groups', 'id', $context->instanceid)) {
1172 $name = get_string('group').': '.$group->name;
1173 }
bbbf2d40 1174 break;
1175
1176 case CONTEXT_MODULE: // 1 to 1 to course
98882637 1177 if ($cm = get_record('course_modules','id',$context->instanceid)) {
1178 if ($module = get_record('modules','id',$cm->module)) {
1179 if ($mod = get_record($module->name, 'id', $cm->instance)) {
ec0810ee 1180 $name = get_string('activitymodule').': '.$mod->name;
98882637 1181 }
ec0810ee 1182 }
1183 }
bbbf2d40 1184 break;
1185
1186 case CONTEXT_BLOCK: // 1 to 1 to course
98882637 1187 if ($blockinstance = get_record('block_instance','id',$context->instanceid)) {
1188 if ($block = get_record('block','id',$blockinstance->blockid)) {
ec0810ee 1189 $name = get_string('blocks').': '.get_string($block->name, 'block_'.$block->name);
1190 }
1191 }
bbbf2d40 1192 break;
1193
1194 default:
1195 error ('This is an unknown context!');
1196 return false;
98882637 1197
1198 }
bbbf2d40 1199
98882637 1200 return $name;
bbbf2d40 1201}
1202
1203
1204/**
1205 * Extracts the relevant capabilities given a contextid.
1206 * All case based, example an instance of forum context.
1207 * Will fetch all forum related capabilities, while course contexts
1208 * Will fetch all capabilities
1209 * @param int contextid
1210 * @return array();
1211 *
1212 * capabilities
1213 * `name` varchar(150) NOT NULL,
1214 * `captype` varchar(50) NOT NULL,
1215 * `contextlevel` int(10) NOT NULL,
1216 * `component` varchar(100) NOT NULL,
1217 */
1218function fetch_context_capabilities($contextid) {
98882637 1219
1220 global $CFG;
bbbf2d40 1221
1222 $sort = 'ORDER BY contextlevel,component,id'; // To group them sensibly for display
98882637 1223
bbbf2d40 1224 switch (context_level($contextid)) {
1225
98882637 1226 case CONTEXT_SYSTEM: // all
1227 $SQL = "select * from {$CFG->prefix}capabilities";
bbbf2d40 1228 break;
1229
1230 case CONTEXT_PERSONAL:
1231 break;
1232
1233 case CONTEXT_USERID:
1234 break;
1235
1236 case CONTEXT_COURSECAT: // all
98882637 1237 $SQL = "select * from {$CFG->prefix}capabilities";
bbbf2d40 1238 break;
1239
1240 case CONTEXT_COURSE: // all
98882637 1241 $SQL = "select * from {$CFG->prefix}capabilities";
bbbf2d40 1242 break;
1243
1244 case CONTEXT_GROUP: // group caps
1245 break;
1246
1247 case CONTEXT_MODULE: // mod caps
98882637 1248 $context = get_record('context','id',$contextid);
1249 $cm = get_record('course_modules', 'id', $context->instanceid);
1250 $module = get_record('modules', 'id', $cm->module);
bbbf2d40 1251
98882637 1252 $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_MODULE."
1253 and component = 'mod/$module->name'";
bbbf2d40 1254 break;
1255
1256 case CONTEXT_BLOCK: // block caps
1257 $context = get_record('context','id',$contextid);
98882637 1258 $cb = get_record('block_instance', 'id', $context->instanceid);
1259 $block = get_record('block', 'id', $cb->blockid);
bbbf2d40 1260
98882637 1261 $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_BLOCK."
1262 and component = 'block/$block->name'";
bbbf2d40 1263 break;
1264
1265 default:
1266 return false;
1267 }
1268
1269 $records = get_records_sql($SQL.' '.$sort);
1270 return $records;
1271
1272}
1273
1274
1275/**
1276 * This function pulls out all the resolved capabilities (overrides and
1277 * defaults) of a role used in capability overrieds in contexts at a given
1278 * context.
1279 * @param int $contextid
1280 * @param int $roleid
1281 * @return array
1282 */
1283function role_context_capabilities($roleid, $contextid) {
98882637 1284 global $CFG;
1285
1286 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
1287 if ($sitecontext->id == $contextid) {
1288 return array();
1289 }
1290
1291 // first of all, figure out all parental contexts
1292 $context = get_record('context', 'id', $contextid);
1293 $contexts = array_reverse(get_parent_contexts($context));
1294 $contexts = '('.implode(',', $contexts).')';
1295
1296 $SQL = "SELECT rc.* FROM {$CFG->prefix}role_capabilities rc, {$CFG->prefix}context c
1297 where rc.contextid in $contexts
1298 and rc.roleid = $roleid
1299 and rc.contextid = c.id
1300 ORDER BY c.level DESC, rc.capability DESC";
1301
1302 $records = get_records_sql($SQL);
1303
1304 $capabilities = array();
1305
1306 // We are traversing via reverse order.
1307 foreach ($records as $record) {
1308 // If not set yet (i.e. inherit or not set at all), or currently we have a prohibit
1309 if (!isset($capabilities[$record->capability]) || $record->permission<-500) {
1310 $capabilities[$record->capability] = $record->permission;
1311 }
1312 }
1313 return $capabilities;
bbbf2d40 1314}
1315
1316
1317/**
1318 * Recursive function which, given a contextid, find all parent context ids,
1319 * and return the array in reverse order, i.e. parent first, then grand
1320 * parent, etc.
1321 * @param object $context
1322 * @return array()
1323 */
1324
1325
1326function get_parent_contexts($context) {
1327
1328 switch (context_level($context->id)) {
1329
1330 case CONTEXT_SYSTEM: // no parent
98882637 1331 return null;
bbbf2d40 1332 break;
1333
1334 case CONTEXT_PERSONAL:
1335 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
1336 return array($parent->id);
1337 break;
1338
1339 case CONTEXT_USERID:
1340 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
1341 return array($parent->id);
1342 break;
1343
1344 case CONTEXT_COURSECAT: // Coursecat -> coursecat or site
1345 $coursecat = get_record('course_categories','id',$context->instanceid);
1346 if ($coursecat->parent) { // return parent value if exist
1347 $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent);
1348 return array_merge(array($parent->id), get_parent_contexts($parent));
1349 } else { // else return site value
1350 $parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
1351 return array($parent->id);
1352 }
1353 break;
1354
1355 case CONTEXT_COURSE: // 1 to 1 to course cat
1356 // find the course cat, and return its value
1357 $course = get_record('course','id',$context->instanceid);
1358 $parent = get_context_instance(CONTEXT_COURSECAT, $course->category);
1359 return array_merge(array($parent->id), get_parent_contexts($parent));
1360 break;
1361
1362 case CONTEXT_GROUP: // 1 to 1 to course
1363 $group = get_record('groups','id',$context->instanceid);
1364 $parent = get_context_instance(CONTEXT_COURSE, $group->courseid);
1365 return array_merge(array($parent->id), get_parent_contexts($parent));
1366 break;
1367
1368 case CONTEXT_MODULE: // 1 to 1 to course
1369 $cm = get_record('course_modules','id',$context->instanceid);
1370 $parent = get_context_instance(CONTEXT_COURSE, $cm->course);
1371 return array_merge(array($parent->id), get_parent_contexts($parent));
1372 break;
1373
1374 case CONTEXT_BLOCK: // 1 to 1 to course
1375 $block = get_record('block_instance','id',$context->instanceid);
1376 $parent = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check
1377 return array_merge(array($parent->id), get_parent_contexts($parent));
1378 break;
1379
1380 default:
1381 error ('This is an unknown context!');
1382 return false;
1383 }
1384
1385}
1386
1387
1388/**
1389 * This function gets the capability of a role in a given context.
1390 * It is needed when printing override forms.
1391 * @param int $contextid
1392 * @param int $roleid // no need? since role is used in extraction in $capability
1393 * @param string $capability
1394 * @param array $capabilities - array loaded using role_context_capabilities
1395 * @return int (allow, prevent, prohibit, inherit)
1396 */
1397
1398
1399function get_role_context_capability($contextid, $capability, $capabilities) {
98882637 1400 return $capabilities[$contextid][$capability];
bbbf2d40 1401}
1402
1403
1404// a big switch statement
1405function get_capability_string($capname) {
1406
98882637 1407 $names = split('/', $capname);
1408 $componentname = split(':', $names[1]);
1409 $componentname = $componentname[0];
1410 $capability = split(':', $capname);
1411 $capability = 'capability_'.$capability[1];
1412
1413 switch ($names[0]) {
1414 case 'mod':
1415 $string = get_string($capability, $componentname);
1416 break;
1417
1418 case 'block':
1419 $string = get_string($capability, 'block_'.$componentname);
1420 break;
1421
1422 case 'moodle':
1423 $string = get_string($capability);
1424 break;
1425
1426 case 'enrol':
1427 $string = get_string($capability, 'enrol_'.$componentname);
1428
1429 default:
1430 $string = get_string($capability);
1431 break;
1432
1433
1434 }
1435
1436 return $string;
bbbf2d40 1437}
1438
1439
1440// this gets the mod/block/course/core etc strings
1441function get_component_string($component, $contextlevel) {
1442
98882637 1443 switch ($contextlevel) {
bbbf2d40 1444
98882637 1445 case CONTEXT_SYSTEM:
1446 $string = get_string('system');
bbbf2d40 1447 break;
1448
1449 case CONTEXT_PERSONAL:
98882637 1450 $string = get_string('personal');
bbbf2d40 1451 break;
1452
1453 case CONTEXT_USERID:
98882637 1454 $string = get_string('users');
bbbf2d40 1455 break;
1456
1457 case CONTEXT_COURSECAT:
98882637 1458 $string = get_string('categories');
bbbf2d40 1459 break;
1460
1461 case CONTEXT_COURSE:
98882637 1462 $string = get_string('course');
bbbf2d40 1463 break;
1464
1465 case CONTEXT_GROUP:
98882637 1466 $string = get_string('group');
bbbf2d40 1467 break;
1468
1469 case CONTEXT_MODULE:
98882637 1470 $string = get_string('modulename', basename($component));
bbbf2d40 1471 break;
1472
1473 case CONTEXT_BLOCK:
98882637 1474 $string = get_string('blockname', 'block_'.$component.'.php');
bbbf2d40 1475 break;
1476
1477 default:
1478 error ('This is an unknown context!');
1479 return false;
98882637 1480
1481 }
1482
1483 return $string;
bbbf2d40 1484
1485}
1486?>