43fde862cac78a4ed614ec4175324ac2d9456f5b
[moodle.git] / lib / accesslib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file contains functions for managing user access
19  *
20  * <b>Public API vs internals</b>
21  *
22  * General users probably only care about
23  *
24  * Context handling
25  * - context_course::instance($courseid), context_module::instance($cm->id), context_coursecat::instance($catid)
26  * - context::instance_by_id($contextid)
27  * - $context->get_parent_contexts();
28  * - $context->get_child_contexts();
29  *
30  * Whether the user can do something...
31  * - has_capability()
32  * - has_any_capability()
33  * - has_all_capabilities()
34  * - require_capability()
35  * - require_login() (from moodlelib)
36  * - is_siteadmin()
37  *
38  * What courses has this user access to?
39  * - get_enrolled_users()
40  *
41  * What users can do X in this context?
42  * - get_users_by_capability()
43  *
44  * Modify roles
45  * - role_assign()
46  * - role_unassign()
47  * - role_unassign_all()
48  *
49  *
50  * Advanced - for internal use only
51  * - load_all_capabilities()
52  * - reload_all_capabilities()
53  * - has_capability_in_accessdata()
54  * - get_user_access_sitewide()
55  * - load_course_context()
56  * - load_role_access_by_context()
57  * - etc.
58  *
59  * <b>Name conventions</b>
60  *
61  * "ctx" means context
62  *
63  * <b>accessdata</b>
64  *
65  * Access control data is held in the "accessdata" array
66  * which - for the logged-in user, will be in $USER->access
67  *
68  * For other users can be generated and passed around (but may also be cached
69  * against userid in $ACCESSLIB_PRIVATE->accessdatabyuser).
70  *
71  * $accessdata is a multidimensional array, holding
72  * role assignments (RAs), role-capabilities-perm sets
73  * (role defs) and a list of courses we have loaded
74  * data for.
75  *
76  * Things are keyed on "contextpaths" (the path field of
77  * the context table) for fast walking up/down the tree.
78  * <code>
79  * $accessdata['ra'][$contextpath] = array($roleid=>$roleid)
80  *                  [$contextpath] = array($roleid=>$roleid)
81  *                  [$contextpath] = array($roleid=>$roleid)
82  * </code>
83  *
84  * Role definitions are stored like this
85  * (no cap merge is done - so it's compact)
86  *
87  * <code>
88  * $accessdata['rdef']["$contextpath:$roleid"]['mod/forum:viewpost'] = 1
89  *                                            ['mod/forum:editallpost'] = -1
90  *                                            ['mod/forum:startdiscussion'] = -1000
91  * </code>
92  *
93  * See how has_capability_in_accessdata() walks up the tree.
94  *
95  * First we only load rdef and ra down to the course level, but not below.
96  * This keeps accessdata small and compact. Below-the-course ra/rdef
97  * are loaded as needed. We keep track of which courses we have loaded ra/rdef in
98  * <code>
99  * $accessdata['loaded'] = array($courseid1=>1, $courseid2=>1)
100  * </code>
101  *
102  * <b>Stale accessdata</b>
103  *
104  * For the logged-in user, accessdata is long-lived.
105  *
106  * On each pageload we load $ACCESSLIB_PRIVATE->dirtycontexts which lists
107  * context paths affected by changes. Any check at-or-below
108  * a dirty context will trigger a transparent reload of accessdata.
109  *
110  * Changes at the system level will force the reload for everyone.
111  *
112  * <b>Default role caps</b>
113  * The default role assignment is not in the DB, so we
114  * add it manually to accessdata.
115  *
116  * This means that functions that work directly off the
117  * DB need to ensure that the default role caps
118  * are dealt with appropriately.
119  *
120  * @package    core
121  * @subpackage role
122  * @copyright  1999 onwards Martin Dougiamas  http://dougiamas.com
123  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
124  */
126 defined('MOODLE_INTERNAL') || die();
128 /** No capability change */
129 define('CAP_INHERIT', 0);
130 /** Allow permission, overrides CAP_PREVENT defined in parent contexts */
131 define('CAP_ALLOW', 1);
132 /** Prevent permission, overrides CAP_ALLOW defined in parent contexts */
133 define('CAP_PREVENT', -1);
134 /** Prohibit permission, overrides everything in current and child contexts */
135 define('CAP_PROHIBIT', -1000);
137 /** System context level - only one instance in every system */
138 define('CONTEXT_SYSTEM', 10);
139 /** User context level -  one instance for each user describing what others can do to user */
140 define('CONTEXT_USER', 30);
141 /** Course category context level - one instance for each category */
142 define('CONTEXT_COURSECAT', 40);
143 /** Course context level - one instances for each course */
144 define('CONTEXT_COURSE', 50);
145 /** Course module context level - one instance for each course module */
146 define('CONTEXT_MODULE', 70);
147 /**
148  * Block context level - one instance for each block, sticky blocks are tricky
149  * because ppl think they should be able to override them at lower contexts.
150  * Any other context level instance can be parent of block context.
151  */
152 define('CONTEXT_BLOCK', 80);
154 /** Capability allow management of trusts - NOT IMPLEMENTED YET - see {@link http://docs.moodle.org/dev/Hardening_new_Roles_system} */
155 define('RISK_MANAGETRUST', 0x0001);
156 /** Capability allows changes in system configuration - see {@link http://docs.moodle.org/dev/Hardening_new_Roles_system} */
157 define('RISK_CONFIG',      0x0002);
158 /** Capability allows user to add scritped content - see {@link http://docs.moodle.org/dev/Hardening_new_Roles_system} */
159 define('RISK_XSS',         0x0004);
160 /** Capability allows access to personal user information - see {@link http://docs.moodle.org/dev/Hardening_new_Roles_system} */
161 define('RISK_PERSONAL',    0x0008);
162 /** Capability allows users to add content otehrs may see - see {@link http://docs.moodle.org/dev/Hardening_new_Roles_system} */
163 define('RISK_SPAM',        0x0010);
164 /** capability allows mass delete of data belonging to other users - see {@link http://docs.moodle.org/dev/Hardening_new_Roles_system} */
165 define('RISK_DATALOSS',    0x0020);
167 /** rolename displays - the name as defined in the role definition */
168 define('ROLENAME_ORIGINAL', 0);
169 /** rolename displays - the name as defined by a role alias */
170 define('ROLENAME_ALIAS', 1);
171 /** rolename displays - Both, like this:  Role alias (Original) */
172 define('ROLENAME_BOTH', 2);
173 /** rolename displays - the name as defined in the role definition and the shortname in brackets */
174 define('ROLENAME_ORIGINALANDSHORT', 3);
175 /** rolename displays - the name as defined by a role alias, in raw form suitable for editing */
176 define('ROLENAME_ALIAS_RAW', 4);
177 /** rolename displays - the name is simply short role name */
178 define('ROLENAME_SHORT', 5);
180 /** maximum size of context cache - it is possible to tweak this config.php or in any script before inclusion of context.php */
181 if (!defined('CONTEXT_CACHE_MAX_SIZE')) {
182     define('CONTEXT_CACHE_MAX_SIZE', 2500);
185 /**
186  * Although this looks like a global variable, it isn't really.
187  *
188  * It is just a private implementation detail to accesslib that MUST NOT be used elsewhere.
189  * It is used to cache various bits of data between function calls for performance reasons.
190  * Sadly, a PHP global variable is the only way to implement this, without rewriting everything
191  * as methods of a class, instead of functions.
192  *
193  * @private
194  * @global stdClass $ACCESSLIB_PRIVATE
195  * @name $ACCESSLIB_PRIVATE
196  */
197 global $ACCESSLIB_PRIVATE;
198 $ACCESSLIB_PRIVATE = new stdClass();
199 $ACCESSLIB_PRIVATE->dirtycontexts    = null;    // Dirty contexts cache, loaded from DB once per page
200 $ACCESSLIB_PRIVATE->accessdatabyuser = array(); // Holds the cache of $accessdata structure for users (including $USER)
201 $ACCESSLIB_PRIVATE->rolepermissions  = array(); // role permissions cache - helps a lot with mem usage
202 $ACCESSLIB_PRIVATE->capabilities     = null;    // detailed information about the capabilities
204 /**
205  * Clears accesslib's private caches. ONLY BE USED BY UNIT TESTS
206  *
207  * This method should ONLY BE USED BY UNIT TESTS. It clears all of
208  * accesslib's private caches. You need to do this before setting up test data,
209  * and also at the end of the tests.
210  *
211  * @return void
212  */
213 function accesslib_clear_all_caches_for_unit_testing() {
214     global $UNITTEST, $USER;
215     if (empty($UNITTEST->running)) {
216         throw new coding_exception('You must not call clear_all_caches outside of unit tests.');
217     }
219     accesslib_clear_all_caches(true);
221     unset($USER->access);
224 /**
225  * Clears accesslib's private caches. ONLY BE USED FROM THIS LIBRARY FILE!
226  *
227  * This reset does not touch global $USER.
228  *
229  * @private
230  * @param bool $resetcontexts
231  * @return void
232  */
233 function accesslib_clear_all_caches($resetcontexts) {
234     global $ACCESSLIB_PRIVATE;
236     $ACCESSLIB_PRIVATE->dirtycontexts    = null;
237     $ACCESSLIB_PRIVATE->accessdatabyuser = array();
238     $ACCESSLIB_PRIVATE->rolepermissions  = array();
239     $ACCESSLIB_PRIVATE->capabilities     = null;
241     if ($resetcontexts) {
242         context_helper::reset_caches();
243     }
246 /**
247  * Gets the accessdata for role "sitewide" (system down to course)
248  *
249  * @private
250  * @param int $roleid
251  * @return array
252  */
253 function get_role_access($roleid) {
254     global $DB, $ACCESSLIB_PRIVATE;
256     /* Get it in 1 DB query...
257      * - relevant role caps at the root and down
258      *   to the course level - but not below
259      */
261     //TODO: MUC - this could be cached in shared memory to speed up first page loading, web crawlers, etc.
263     $accessdata = get_empty_accessdata();
265     $accessdata['ra']['/'.SYSCONTEXTID] = array((int)$roleid => (int)$roleid);
267     //
268     // Overrides for the role IN ANY CONTEXTS
269     // down to COURSE - not below -
270     //
271     $sql = "SELECT ctx.path,
272                    rc.capability, rc.permission
273               FROM {context} ctx
274               JOIN {role_capabilities} rc ON rc.contextid = ctx.id
275          LEFT JOIN {context} cctx
276                    ON (cctx.contextlevel = ".CONTEXT_COURSE." AND ctx.path LIKE ".$DB->sql_concat('cctx.path',"'/%'").")
277              WHERE rc.roleid = ? AND cctx.id IS NULL";
278     $params = array($roleid);
280     // we need extra caching in CLI scripts and cron
281     $rs = $DB->get_recordset_sql($sql, $params);
282     foreach ($rs as $rd) {
283         $k = "{$rd->path}:{$roleid}";
284         $accessdata['rdef'][$k][$rd->capability] = (int)$rd->permission;
285     }
286     $rs->close();
288     // share the role definitions
289     foreach ($accessdata['rdef'] as $k=>$unused) {
290         if (!isset($ACCESSLIB_PRIVATE->rolepermissions[$k])) {
291             $ACCESSLIB_PRIVATE->rolepermissions[$k] = $accessdata['rdef'][$k];
292         }
293         $accessdata['rdef_count']++;
294         $accessdata['rdef'][$k] =& $ACCESSLIB_PRIVATE->rolepermissions[$k];
295     }
297     return $accessdata;
300 /**
301  * Get the default guest role, this is used for guest account,
302  * search engine spiders, etc.
303  *
304  * @return stdClass role record
305  */
306 function get_guest_role() {
307     global $CFG, $DB;
309     if (empty($CFG->guestroleid)) {
310         if ($roles = $DB->get_records('role', array('archetype'=>'guest'))) {
311             $guestrole = array_shift($roles);   // Pick the first one
312             set_config('guestroleid', $guestrole->id);
313             return $guestrole;
314         } else {
315             debugging('Can not find any guest role!');
316             return false;
317         }
318     } else {
319         if ($guestrole = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
320             return $guestrole;
321         } else {
322             // somebody is messing with guest roles, remove incorrect setting and try to find a new one
323             set_config('guestroleid', '');
324             return get_guest_role();
325         }
326     }
329 /**
330  * Check whether a user has a particular capability in a given context.
331  *
332  * For example:
333  *      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
334  *      has_capability('mod/forum:replypost',$context)
335  *
336  * By default checks the capabilities of the current user, but you can pass a
337  * different userid. By default will return true for admin users, but you can override that with the fourth argument.
338  *
339  * Guest and not-logged-in users can never get any dangerous capability - that is any write capability
340  * or capabilities with XSS, config or data loss risks.
341  *
342  * @param string $capability the name of the capability to check. For example mod/forum:view
343  * @param context $context the context to check the capability in. You normally get this with {@link get_context_instance}.
344  * @param integer|object $user A user id or object. By default (null) checks the permissions of the current user.
345  * @param boolean $doanything If false, ignores effect of admin role assignment
346  * @return boolean true if the user has this capability. Otherwise false.
347  */
348 function has_capability($capability, context $context, $user = null, $doanything = true) {
349     global $USER, $CFG, $SCRIPT, $ACCESSLIB_PRIVATE;
351     if (during_initial_install()) {
352         if ($SCRIPT === "/$CFG->admin/index.php" or $SCRIPT === "/$CFG->admin/cli/install.php" or $SCRIPT === "/$CFG->admin/cli/install_database.php") {
353             // we are in an installer - roles can not work yet
354             return true;
355         } else {
356             return false;
357         }
358     }
360     if (strpos($capability, 'moodle/legacy:') === 0) {
361         throw new coding_exception('Legacy capabilities can not be used any more!');
362     }
364     if (!is_bool($doanything)) {
365         throw new coding_exception('Capability parameter "doanything" is wierd, only true or false is allowed. This has to be fixed in code.');
366     }
368     // capability must exist
369     if (!$capinfo = get_capability_info($capability)) {
370         debugging('Capability "'.$capability.'" was not found! This has to be fixed in code.');
371         return false;
372     }
374     if (!isset($USER->id)) {
375         // should never happen
376         $USER->id = 0;
377     }
379     // make sure there is a real user specified
380     if ($user === null) {
381         $userid = $USER->id;
382     } else {
383         $userid = is_object($user) ? $user->id : $user;
384     }
386     // make sure forcelogin cuts off not-logged-in users if enabled
387     if (!empty($CFG->forcelogin) and $userid == 0) {
388         return false;
389     }
391     // make sure the guest account and not-logged-in users never get any risky caps no matter what the actual settings are.
392     if (($capinfo->captype === 'write') or ($capinfo->riskbitmask & (RISK_XSS | RISK_CONFIG | RISK_DATALOSS))) {
393         if (isguestuser($userid) or $userid == 0) {
394             return false;
395         }
396     }
398     // somehow make sure the user is not deleted and actually exists
399     if ($userid != 0) {
400         if ($userid == $USER->id and isset($USER->deleted)) {
401             // this prevents one query per page, it is a bit of cheating,
402             // but hopefully session is terminated properly once user is deleted
403             if ($USER->deleted) {
404                 return false;
405             }
406         } else {
407             if (!context_user::instance($userid, IGNORE_MISSING)) {
408                 // no user context == invalid userid
409                 return false;
410             }
411         }
412     }
414     // context path/depth must be valid
415     if (empty($context->path) or $context->depth == 0) {
416         // this should not happen often, each upgrade tries to rebuild the context paths
417         debugging('Context id '.$context->id.' does not have valid path, please use build_context_path()');
418         if (is_siteadmin($userid)) {
419             return true;
420         } else {
421             return false;
422         }
423     }
425     // Find out if user is admin - it is not possible to override the doanything in any way
426     // and it is not possible to switch to admin role either.
427     if ($doanything) {
428         if (is_siteadmin($userid)) {
429             if ($userid != $USER->id) {
430                 return true;
431             }
432             // make sure switchrole is not used in this context
433             if (empty($USER->access['rsw'])) {
434                 return true;
435             }
436             $parts = explode('/', trim($context->path, '/'));
437             $path = '';
438             $switched = false;
439             foreach ($parts as $part) {
440                 $path .= '/' . $part;
441                 if (!empty($USER->access['rsw'][$path])) {
442                     $switched = true;
443                     break;
444                 }
445             }
446             if (!$switched) {
447                 return true;
448             }
449             //ok, admin switched role in this context, let's use normal access control rules
450         }
451     }
453     // Careful check for staleness...
454     $context->reload_if_dirty();
456     if ($USER->id == $userid) {
457         if (!isset($USER->access)) {
458             load_all_capabilities();
459         }
460         $access =& $USER->access;
462     } else {
463         // make sure user accessdata is really loaded
464         get_user_accessdata($userid, true);
465         $access =& $ACCESSLIB_PRIVATE->accessdatabyuser[$userid];
466     }
469     // Load accessdata for below-the-course context if necessary,
470     // all contexts at and above all courses are already loaded
471     if ($context->contextlevel != CONTEXT_COURSE and $coursecontext = $context->get_course_context(false)) {
472         load_course_context($userid, $coursecontext, $access);
473     }
475     return has_capability_in_accessdata($capability, $context, $access);
478 /**
479  * Check if the user has any one of several capabilities from a list.
480  *
481  * This is just a utility method that calls has_capability in a loop. Try to put
482  * the capabilities that most users are likely to have first in the list for best
483  * performance.
484  *
485  * @see has_capability()
486  * @param array $capabilities an array of capability names.
487  * @param context $context the context to check the capability in. You normally get this with {@link get_context_instance}.
488  * @param integer $userid A user id. By default (null) checks the permissions of the current user.
489  * @param boolean $doanything If false, ignore effect of admin role assignment
490  * @return boolean true if the user has any of these capabilities. Otherwise false.
491  */
492 function has_any_capability(array $capabilities, context $context, $userid = null, $doanything = true) {
493     foreach ($capabilities as $capability) {
494         if (has_capability($capability, $context, $userid, $doanything)) {
495             return true;
496         }
497     }
498     return false;
501 /**
502  * Check if the user has all the capabilities in a list.
503  *
504  * This is just a utility method that calls has_capability in a loop. Try to put
505  * the capabilities that fewest users are likely to have first in the list for best
506  * performance.
507  *
508  * @see has_capability()
509  * @param array $capabilities an array of capability names.
510  * @param context $context the context to check the capability in. You normally get this with {@link get_context_instance}.
511  * @param integer $userid A user id. By default (null) checks the permissions of the current user.
512  * @param boolean $doanything If false, ignore effect of admin role assignment
513  * @return boolean true if the user has all of these capabilities. Otherwise false.
514  */
515 function has_all_capabilities(array $capabilities, context $context, $userid = null, $doanything = true) {
516     foreach ($capabilities as $capability) {
517         if (!has_capability($capability, $context, $userid, $doanything)) {
518             return false;
519         }
520     }
521     return true;
524 /**
525  * Check if the user is an admin at the site level.
526  *
527  * Please note that use of proper capabilities is always encouraged,
528  * this function is supposed to be used from core or for temporary hacks.
529  *
530  * @param  int|stdClass  $user_or_id user id or user object
531  * @return bool true if user is one of the administrators, false otherwise
532  */
533 function is_siteadmin($user_or_id = null) {
534     global $CFG, $USER;
536     if ($user_or_id === null) {
537         $user_or_id = $USER;
538     }
540     if (empty($user_or_id)) {
541         return false;
542     }
543     if (!empty($user_or_id->id)) {
544         $userid = $user_or_id->id;
545     } else {
546         $userid = $user_or_id;
547     }
549     $siteadmins = explode(',', $CFG->siteadmins);
550     return in_array($userid, $siteadmins);
553 /**
554  * Returns true if user has at least one role assign
555  * of 'coursecontact' role (is potentially listed in some course descriptions).
556  *
557  * @param int $userid
558  * @return bool
559  */
560 function has_coursecontact_role($userid) {
561     global $DB, $CFG;
563     if (empty($CFG->coursecontact)) {
564         return false;
565     }
566     $sql = "SELECT 1
567               FROM {role_assignments}
568              WHERE userid = :userid AND roleid IN ($CFG->coursecontact)";
569     return $DB->record_exists_sql($sql, array('userid'=>$userid));
572 /**
573  * Does the user have a capability to do something?
574  *
575  * Walk the accessdata array and return true/false.
576  * Deals with prohibits, role switching, aggregating
577  * capabilities, etc.
578  *
579  * The main feature of here is being FAST and with no
580  * side effects.
581  *
582  * Notes:
583  *
584  * Switch Role merges with default role
585  * ------------------------------------
586  * If you are a teacher in course X, you have at least
587  * teacher-in-X + defaultloggedinuser-sitewide. So in the
588  * course you'll have techer+defaultloggedinuser.
589  * We try to mimic that in switchrole.
590  *
591  * Permission evaluation
592  * ---------------------
593  * Originally there was an extremely complicated way
594  * to determine the user access that dealt with
595  * "locality" or role assignments and role overrides.
596  * Now we simply evaluate access for each role separately
597  * and then verify if user has at least one role with allow
598  * and at the same time no role with prohibit.
599  *
600  * @private
601  * @param string $capability
602  * @param context $context
603  * @param array $accessdata
604  * @return bool
605  */
606 function has_capability_in_accessdata($capability, context $context, array &$accessdata) {
607     global $CFG;
609     // Build $paths as a list of current + all parent "paths" with order bottom-to-top
610     $path = $context->path;
611     $paths = array($path);
612     while($path = rtrim($path, '0123456789')) {
613         $path = rtrim($path, '/');
614         if ($path === '') {
615             break;
616         }
617         $paths[] = $path;
618     }
620     $roles = array();
621     $switchedrole = false;
623     // Find out if role switched
624     if (!empty($accessdata['rsw'])) {
625         // From the bottom up...
626         foreach ($paths as $path) {
627             if (isset($accessdata['rsw'][$path])) {
628                 // Found a switchrole assignment - check for that role _plus_ the default user role
629                 $roles = array($accessdata['rsw'][$path]=>null, $CFG->defaultuserroleid=>null);
630                 $switchedrole = true;
631                 break;
632             }
633         }
634     }
636     if (!$switchedrole) {
637         // get all users roles in this context and above
638         foreach ($paths as $path) {
639             if (isset($accessdata['ra'][$path])) {
640                 foreach ($accessdata['ra'][$path] as $roleid) {
641                     $roles[$roleid] = null;
642                 }
643             }
644         }
645     }
647     // Now find out what access is given to each role, going bottom-->up direction
648     $allowed = false;
649     foreach ($roles as $roleid => $ignored) {
650         foreach ($paths as $path) {
651             if (isset($accessdata['rdef']["{$path}:$roleid"][$capability])) {
652                 $perm = (int)$accessdata['rdef']["{$path}:$roleid"][$capability];
653                 if ($perm === CAP_PROHIBIT) {
654                     // any CAP_PROHIBIT found means no permission for the user
655                     return false;
656                 }
657                 if (is_null($roles[$roleid])) {
658                     $roles[$roleid] = $perm;
659                 }
660             }
661         }
662         // CAP_ALLOW in any role means the user has a permission, we continue only to detect prohibits
663         $allowed = ($allowed or $roles[$roleid] === CAP_ALLOW);
664     }
666     return $allowed;
669 /**
670  * A convenience function that tests has_capability, and displays an error if
671  * the user does not have that capability.
672  *
673  * NOTE before Moodle 2.0, this function attempted to make an appropriate
674  * require_login call before checking the capability. This is no longer the case.
675  * You must call require_login (or one of its variants) if you want to check the
676  * user is logged in, before you call this function.
677  *
678  * @see has_capability()
679  *
680  * @param string $capability the name of the capability to check. For example mod/forum:view
681  * @param context $context the context to check the capability in. You normally get this with {@link get_context_instance}.
682  * @param int $userid A user id. By default (null) checks the permissions of the current user.
683  * @param bool $doanything If false, ignore effect of admin role assignment
684  * @param string $errormessage The error string to to user. Defaults to 'nopermissions'.
685  * @param string $stringfile The language file to load the error string from. Defaults to 'error'.
686  * @return void terminates with an error if the user does not have the given capability.
687  */
688 function require_capability($capability, context $context, $userid = null, $doanything = true,
689                             $errormessage = 'nopermissions', $stringfile = '') {
690     if (!has_capability($capability, $context, $userid, $doanything)) {
691         throw new required_capability_exception($context, $capability, $errormessage, $stringfile);
692     }
695 /**
696  * Return a nested array showing role assignments
697  * all relevant role capabilities for the user at
698  * site/course_category/course levels
699  *
700  * We do _not_ delve deeper than courses because the number of
701  * overrides at the module/block levels can be HUGE.
702  *
703  * [ra]   => [/path][roleid]=roleid
704  * [rdef] => [/path:roleid][capability]=permission
705  *
706  * @private
707  * @param int $userid - the id of the user
708  * @return array access info array
709  */
710 function get_user_access_sitewide($userid) {
711     global $CFG, $DB, $ACCESSLIB_PRIVATE;
713     /* Get in a few cheap DB queries...
714      * - role assignments
715      * - relevant role caps
716      *   - above and within this user's RAs
717      *   - below this user's RAs - limited to course level
718      */
720     // raparents collects paths & roles we need to walk up the parenthood to build the minimal rdef
721     $raparents = array();
722     $accessdata = get_empty_accessdata();
724     // start with the default role
725     if (!empty($CFG->defaultuserroleid)) {
726         $syscontext = context_system::instance();
727         $accessdata['ra'][$syscontext->path][(int)$CFG->defaultuserroleid] = (int)$CFG->defaultuserroleid;
728         $raparents[$CFG->defaultuserroleid][$syscontext->id] = $syscontext->id;
729     }
731     // load the "default frontpage role"
732     if (!empty($CFG->defaultfrontpageroleid)) {
733         $frontpagecontext = context_course::instance(get_site()->id);
734         if ($frontpagecontext->path) {
735             $accessdata['ra'][$frontpagecontext->path][(int)$CFG->defaultfrontpageroleid] = (int)$CFG->defaultfrontpageroleid;
736             $raparents[$CFG->defaultfrontpageroleid][$frontpagecontext->id] = $frontpagecontext->id;
737         }
738     }
740     // preload every assigned role at and above course context
741     $sql = "SELECT ctx.path, ra.roleid, ra.contextid
742               FROM {role_assignments} ra
743               JOIN {context} ctx
744                    ON ctx.id = ra.contextid
745          LEFT JOIN {block_instances} bi
746                    ON (ctx.contextlevel = ".CONTEXT_BLOCK." AND bi.id = ctx.instanceid)
747          LEFT JOIN {context} bpctx
748                    ON (bpctx.id = bi.parentcontextid)
749              WHERE ra.userid = :userid
750                    AND (ctx.contextlevel <= ".CONTEXT_COURSE." OR bpctx.contextlevel < ".CONTEXT_COURSE.")";
751     $params = array('userid'=>$userid);
752     $rs = $DB->get_recordset_sql($sql, $params);
753     foreach ($rs as $ra) {
754         // RAs leafs are arrays to support multi-role assignments...
755         $accessdata['ra'][$ra->path][(int)$ra->roleid] = (int)$ra->roleid;
756         $raparents[$ra->roleid][$ra->contextid] = $ra->contextid;
757     }
758     $rs->close();
760     if (empty($raparents)) {
761         return $accessdata;
762     }
764     // now get overrides of interesting roles in all interesting child contexts
765     // hopefully we will not run out of SQL limits here,
766     // users would have to have very many roles at/above course context...
767     $sqls = array();
768     $params = array();
770     static $cp = 0;
771     foreach ($raparents as $roleid=>$ras) {
772         $cp++;
773         list($sqlcids, $cids) = $DB->get_in_or_equal($ras, SQL_PARAMS_NAMED, 'c'.$cp.'_');
774         $params = array_merge($params, $cids);
775         $params['r'.$cp] = $roleid;
776         $sqls[] = "(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
777                      FROM {role_capabilities} rc
778                      JOIN {context} ctx
779                           ON (ctx.id = rc.contextid)
780                      JOIN {context} pctx
781                           ON (pctx.id $sqlcids
782                               AND (ctx.id = pctx.id
783                                    OR ctx.path LIKE ".$DB->sql_concat('pctx.path',"'/%'")."
784                                    OR pctx.path LIKE ".$DB->sql_concat('ctx.path',"'/%'")."))
785                 LEFT JOIN {block_instances} bi
786                           ON (ctx.contextlevel = ".CONTEXT_BLOCK." AND bi.id = ctx.instanceid)
787                 LEFT JOIN {context} bpctx
788                           ON (bpctx.id = bi.parentcontextid)
789                     WHERE rc.roleid = :r{$cp}
790                           AND (ctx.contextlevel <= ".CONTEXT_COURSE." OR bpctx.contextlevel < ".CONTEXT_COURSE.")
791                    )";
792     }
794     // fixed capability order is necessary for rdef dedupe
795     $rs = $DB->get_recordset_sql(implode("\nUNION\n", $sqls). "ORDER BY capability", $params);
797     foreach ($rs as $rd) {
798         $k = $rd->path.':'.$rd->roleid;
799         $accessdata['rdef'][$k][$rd->capability] = (int)$rd->permission;
800     }
801     $rs->close();
803     // share the role definitions
804     foreach ($accessdata['rdef'] as $k=>$unused) {
805         if (!isset($ACCESSLIB_PRIVATE->rolepermissions[$k])) {
806             $ACCESSLIB_PRIVATE->rolepermissions[$k] = $accessdata['rdef'][$k];
807         }
808         $accessdata['rdef_count']++;
809         $accessdata['rdef'][$k] =& $ACCESSLIB_PRIVATE->rolepermissions[$k];
810     }
812     return $accessdata;
815 /**
816  * Add to the access ctrl array the data needed by a user for a given course.
817  *
818  * This function injects all course related access info into the accessdata array.
819  *
820  * @private
821  * @param int $userid the id of the user
822  * @param context_course $coursecontext course context
823  * @param array $accessdata accessdata array (modified)
824  * @return void modifies $accessdata parameter
825  */
826 function load_course_context($userid, context_course $coursecontext, &$accessdata) {
827     global $DB, $CFG, $ACCESSLIB_PRIVATE;
829     if (empty($coursecontext->path)) {
830         // weird, this should not happen
831         return;
832     }
834     if (isset($accessdata['loaded'][$coursecontext->instanceid])) {
835         // already loaded, great!
836         return;
837     }
839     $roles = array();
841     if (empty($userid)) {
842         if (!empty($CFG->notloggedinroleid)) {
843             $roles[$CFG->notloggedinroleid] = $CFG->notloggedinroleid;
844         }
846     } else if (isguestuser($userid)) {
847         if ($guestrole = get_guest_role()) {
848             $roles[$guestrole->id] = $guestrole->id;
849         }
851     } else {
852         // Interesting role assignments at, above and below the course context
853         list($parentsaself, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'pc_');
854         $params['userid'] = $userid;
855         $params['children'] = $coursecontext->path."/%";
856         $sql = "SELECT ra.*, ctx.path
857                   FROM {role_assignments} ra
858                   JOIN {context} ctx ON ra.contextid = ctx.id
859                  WHERE ra.userid = :userid AND (ctx.id $parentsaself OR ctx.path LIKE :children)";
860         $rs = $DB->get_recordset_sql($sql, $params);
862         // add missing role definitions
863         foreach ($rs as $ra) {
864             $accessdata['ra'][$ra->path][(int)$ra->roleid] = (int)$ra->roleid;
865             $roles[$ra->roleid] = $ra->roleid;
866         }
867         $rs->close();
869         // add the "default frontpage role" when on the frontpage
870         if (!empty($CFG->defaultfrontpageroleid)) {
871             $frontpagecontext = context_course::instance(get_site()->id);
872             if ($frontpagecontext->id == $coursecontext->id) {
873                 $roles[$CFG->defaultfrontpageroleid] = $CFG->defaultfrontpageroleid;
874             }
875         }
877         // do not forget the default role
878         if (!empty($CFG->defaultuserroleid)) {
879             $roles[$CFG->defaultuserroleid] = $CFG->defaultuserroleid;
880         }
881     }
883     if (!$roles) {
884         // weird, default roles must be missing...
885         $accessdata['loaded'][$coursecontext->instanceid] = 1;
886         return;
887     }
889     // now get overrides of interesting roles in all interesting contexts (this course + children + parents)
890     $params = array('c'=>$coursecontext->id);
891     list($parentsaself, $rparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'pc_');
892     $params = array_merge($params, $rparams);
893     list($roleids, $rparams) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'r_');
894     $params = array_merge($params, $rparams);
896     $sql = "SELECT ctx.path, rc.roleid, rc.capability, rc.permission
897                  FROM {role_capabilities} rc
898                  JOIN {context} ctx
899                       ON (ctx.id = rc.contextid)
900                  JOIN {context} cctx
901                       ON (cctx.id = :c
902                           AND (ctx.id $parentsaself OR ctx.path LIKE ".$DB->sql_concat('cctx.path',"'/%'")."))
903                 WHERE rc.roleid $roleids
904              ORDER BY rc.capability"; // fixed capability order is necessary for rdef dedupe
905     $rs = $DB->get_recordset_sql($sql, $params);
907     $newrdefs = array();
908     foreach ($rs as $rd) {
909         $k = $rd->path.':'.$rd->roleid;
910         if (isset($accessdata['rdef'][$k])) {
911             continue;
912         }
913         $newrdefs[$k][$rd->capability] = (int)$rd->permission;
914     }
915     $rs->close();
917     // share new role definitions
918     foreach ($newrdefs as $k=>$unused) {
919         if (!isset($ACCESSLIB_PRIVATE->rolepermissions[$k])) {
920             $ACCESSLIB_PRIVATE->rolepermissions[$k] = $newrdefs[$k];
921         }
922         $accessdata['rdef_count']++;
923         $accessdata['rdef'][$k] =& $ACCESSLIB_PRIVATE->rolepermissions[$k];
924     }
926     $accessdata['loaded'][$coursecontext->instanceid] = 1;
928     // we want to deduplicate the USER->access from time to time, this looks like a good place,
929     // because we have to do it before the end of session
930     dedupe_user_access();
933 /**
934  * Add to the access ctrl array the data needed by a role for a given context.
935  *
936  * The data is added in the rdef key.
937  * This role-centric function is useful for role_switching
938  * and temporary course roles.
939  *
940  * @private
941  * @param int $roleid the id of the user
942  * @param context $context needs path!
943  * @param array $accessdata accessdata array (is modified)
944  * @return array
945  */
946 function load_role_access_by_context($roleid, context $context, &$accessdata) {
947     global $DB, $ACCESSLIB_PRIVATE;
949     /* Get the relevant rolecaps into rdef
950      * - relevant role caps
951      *   - at ctx and above
952      *   - below this ctx
953      */
955     if (empty($context->path)) {
956         // weird, this should not happen
957         return;
958     }
960     list($parentsaself, $params) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'pc_');
961     $params['roleid'] = $roleid;
962     $params['childpath'] = $context->path.'/%';
964     $sql = "SELECT ctx.path, rc.capability, rc.permission
965               FROM {role_capabilities} rc
966               JOIN {context} ctx ON (rc.contextid = ctx.id)
967              WHERE rc.roleid = :roleid AND (ctx.id $parentsaself OR ctx.path LIKE :childpath)
968           ORDER BY rc.capability"; // fixed capability order is necessary for rdef dedupe
969     $rs = $DB->get_recordset_sql($sql, $params);
971     $newrdefs = array();
972     foreach ($rs as $rd) {
973         $k = $rd->path.':'.$roleid;
974         if (isset($accessdata['rdef'][$k])) {
975             continue;
976         }
977         $newrdefs[$k][$rd->capability] = (int)$rd->permission;
978     }
979     $rs->close();
981     // share new role definitions
982     foreach ($newrdefs as $k=>$unused) {
983         if (!isset($ACCESSLIB_PRIVATE->rolepermissions[$k])) {
984             $ACCESSLIB_PRIVATE->rolepermissions[$k] = $newrdefs[$k];
985         }
986         $accessdata['rdef_count']++;
987         $accessdata['rdef'][$k] =& $ACCESSLIB_PRIVATE->rolepermissions[$k];
988     }
991 /**
992  * Returns empty accessdata structure.
993  *
994  * @private
995  * @return array empt accessdata
996  */
997 function get_empty_accessdata() {
998     $accessdata               = array(); // named list
999     $accessdata['ra']         = array();
1000     $accessdata['rdef']       = array();
1001     $accessdata['rdef_count'] = 0;       // this bloody hack is necessary because count($array) is slooooowwww in PHP
1002     $accessdata['rdef_lcc']   = 0;       // rdef_count during the last compression
1003     $accessdata['loaded']     = array(); // loaded course contexts
1004     $accessdata['time']       = time();
1006     return $accessdata;
1009 /**
1010  * Get accessdata for a given user.
1011  *
1012  * @private
1013  * @param int $userid
1014  * @param bool $preloadonly true means do not return access array
1015  * @return array accessdata
1016  */
1017 function get_user_accessdata($userid, $preloadonly=false) {
1018     global $CFG, $ACCESSLIB_PRIVATE, $USER;
1020     if (!empty($USER->acces['rdef']) and empty($ACCESSLIB_PRIVATE->rolepermissions)) {
1021         // share rdef from USER session with rolepermissions cache in order to conserve memory
1022         foreach($USER->acces['rdef'] as $k=>$v) {
1023             $ACCESSLIB_PRIVATE->rolepermissions[$k] =& $USER->acces['rdef'][$k];
1024         }
1025         $ACCESSLIB_PRIVATE->accessdatabyuser[$USER->id] = $USER->acces;
1026     }
1028     if (!isset($ACCESSLIB_PRIVATE->accessdatabyuser[$userid])) {
1029         if (empty($userid)) {
1030             if (!empty($CFG->notloggedinroleid)) {
1031                 $accessdata = get_role_access($CFG->notloggedinroleid);
1032             } else {
1033                 // weird
1034                 return get_empty_accessdata();
1035             }
1037         } else if (isguestuser($userid)) {
1038             if ($guestrole = get_guest_role()) {
1039                 $accessdata = get_role_access($guestrole->id);
1040             } else {
1041                 //weird
1042                 return get_empty_accessdata();
1043             }
1045         } else {
1046             $accessdata = get_user_access_sitewide($userid); // includes default role and frontpage role
1047         }
1049         $ACCESSLIB_PRIVATE->accessdatabyuser[$userid] = $accessdata;
1050     }
1052     if ($preloadonly) {
1053         return;
1054     } else {
1055         return $ACCESSLIB_PRIVATE->accessdatabyuser[$userid];
1056     }
1059 /**
1060  * Try to minimise the size of $USER->access by eliminating duplicate override storage,
1061  * this function looks for contexts with the same overrides and shares them.
1062  *
1063  * @private
1064  * @return void
1065  */
1066 function dedupe_user_access() {
1067     global $USER;
1069     if (CLI_SCRIPT) {
1070         // no session in CLI --> no compression necessary
1071         return;
1072     }
1074     if (empty($USER->access['rdef_count'])) {
1075         // weird, this should not happen
1076         return;
1077     }
1079     // the rdef is growing only, we never remove stuff from it, the rdef_lcc helps us to detect new stuff in rdef
1080     if ($USER->access['rdef_count'] - $USER->access['rdef_lcc'] > 10) {
1081         // do not compress after each change, wait till there is more stuff to be done
1082         return;
1083     }
1085     $hashmap = array();
1086     foreach ($USER->access['rdef'] as $k=>$def) {
1087         $hash = sha1(serialize($def));
1088         if (isset($hashmap[$hash])) {
1089             $USER->access['rdef'][$k] =& $hashmap[$hash];
1090         } else {
1091             $hashmap[$hash] =& $USER->access['rdef'][$k];
1092         }
1093     }
1095     $USER->access['rdef_lcc'] = $USER->access['rdef_count'];
1098 /**
1099  * A convenience function to completely load all the capabilities
1100  * for the current user. It is called from has_capability() and functions change permissions.
1101  *
1102  * Call it only _after_ you've setup $USER and called check_enrolment_plugins();
1103  * @see check_enrolment_plugins()
1104  *
1105  * @private
1106  * @return void
1107  */
1108 function load_all_capabilities() {
1109     global $USER;
1111     // roles not installed yet - we are in the middle of installation
1112     if (during_initial_install()) {
1113         return;
1114     }
1116     if (!isset($USER->id)) {
1117         // this should not happen
1118         $USER->id = 0;
1119     }
1121     unset($USER->access);
1122     $USER->access = get_user_accessdata($USER->id);
1124     // deduplicate the overrides to minimize session size
1125     dedupe_user_access();
1127     // Clear to force a refresh
1128     unset($USER->mycourses);
1130     // init/reset internal enrol caches - active course enrolments and temp access
1131     $USER->enrol = array('enrolled'=>array(), 'tempguest'=>array());
1134 /**
1135  * A convenience function to completely reload all the capabilities
1136  * for the current user when roles have been updated in a relevant
1137  * context -- but PRESERVING switchroles and loginas.
1138  * This function resets all accesslib and context caches.
1139  *
1140  * That is - completely transparent to the user.
1141  *
1142  * Note: reloads $USER->access completely.
1143  *
1144  * @private
1145  * @return void
1146  */
1147 function reload_all_capabilities() {
1148     global $USER, $DB, $ACCESSLIB_PRIVATE;
1150     // copy switchroles
1151     $sw = array();
1152     if (isset($USER->access['rsw'])) {
1153         $sw = $USER->access['rsw'];
1154     }
1156     accesslib_clear_all_caches(true);
1157     unset($USER->access);
1158     $ACCESSLIB_PRIVATE->dirtycontexts = array(); // prevent dirty flags refetching on this page
1160     load_all_capabilities();
1162     foreach ($sw as $path => $roleid) {
1163         if ($record = $DB->get_record('context', array('path'=>$path))) {
1164             $context = context::instance_by_id($record->id);
1165             role_switch($roleid, $context);
1166         }
1167     }
1170 /**
1171  * Adds a temp role to current USER->access array.
1172  *
1173  * Useful for the "temporary guest" access we grant to logged-in users.
1174  * @since 2.2
1175  *
1176  * @param context_course $coursecontext
1177  * @param int $roleid
1178  * @return void
1179  */
1180 function load_temp_course_role(context_course $coursecontext, $roleid) {
1181     global $USER, $SITE;
1183     if (empty($roleid)) {
1184         debugging('invalid role specified in load_temp_course_role()');
1185         return;
1186     }
1188     if ($coursecontext->instanceid == $SITE->id) {
1189         debugging('Can not use temp roles on the frontpage');
1190         return;
1191     }
1193     if (!isset($USER->access)) {
1194         load_all_capabilities();
1195     }
1197     $coursecontext->reload_if_dirty();
1199     if (isset($USER->access['ra'][$coursecontext->path][$roleid])) {
1200         return;
1201     }
1203     // load course stuff first
1204     load_course_context($USER->id, $coursecontext, $USER->access);
1206     $USER->access['ra'][$coursecontext->path][(int)$roleid] = (int)$roleid;
1208     load_role_access_by_context($roleid, $coursecontext, $USER->access);
1211 /**
1212  * Removes any extra guest roles from current USER->access array.
1213  * @since 2.2
1214  *
1215  * @param context_course $coursecontext
1216  * @return void
1217  */
1218 function remove_temp_course_roles(context_course $coursecontext) {
1219     global $DB, $USER, $SITE;
1221     if ($coursecontext->instanceid == $SITE->id) {
1222         debugging('Can not use temp roles on the frontpage');
1223         return;
1224     }
1226     if (empty($USER->access['ra'][$coursecontext->path])) {
1227         //no roles here, weird
1228         return;
1229     }
1231     $sql = "SELECT DISTINCT ra.roleid AS id
1232               FROM {role_assignments} ra
1233              WHERE ra.contextid = :contextid AND ra.userid = :userid";
1234     $ras = $DB->get_records_sql($sql, array('contextid'=>$coursecontext->id, 'userid'=>$USER->id));
1236     $USER->access['ra'][$coursecontext->path] = array();
1237     foreach($ras as $r) {
1238         $USER->access['ra'][$coursecontext->path][(int)$r->id] = (int)$r->id;
1239     }
1242 /**
1243  * Returns array of all role archetypes.
1244  *
1245  * @return array
1246  */
1247 function get_role_archetypes() {
1248     return array(
1249         'manager'        => 'manager',
1250         'coursecreator'  => 'coursecreator',
1251         'editingteacher' => 'editingteacher',
1252         'teacher'        => 'teacher',
1253         'student'        => 'student',
1254         'guest'          => 'guest',
1255         'user'           => 'user',
1256         'frontpage'      => 'frontpage'
1257     );
1260 /**
1261  * Assign the defaults found in this capability definition to roles that have
1262  * the corresponding legacy capabilities assigned to them.
1263  *
1264  * @param string $capability
1265  * @param array $legacyperms an array in the format (example):
1266  *                      'guest' => CAP_PREVENT,
1267  *                      'student' => CAP_ALLOW,
1268  *                      'teacher' => CAP_ALLOW,
1269  *                      'editingteacher' => CAP_ALLOW,
1270  *                      'coursecreator' => CAP_ALLOW,
1271  *                      'manager' => CAP_ALLOW
1272  * @return boolean success or failure.
1273  */
1274 function assign_legacy_capabilities($capability, $legacyperms) {
1276     $archetypes = get_role_archetypes();
1278     foreach ($legacyperms as $type => $perm) {
1280         $systemcontext = context_system::instance();
1281         if ($type === 'admin') {
1282             debugging('Legacy type admin in access.php was renamed to manager, please update the code.');
1283             $type = 'manager';
1284         }
1286         if (!array_key_exists($type, $archetypes)) {
1287             print_error('invalidlegacy', '', '', $type);
1288         }
1290         if ($roles = get_archetype_roles($type)) {
1291             foreach ($roles as $role) {
1292                 // Assign a site level capability.
1293                 if (!assign_capability($capability, $perm, $role->id, $systemcontext->id)) {
1294                     return false;
1295                 }
1296             }
1297         }
1298     }
1299     return true;
1302 /**
1303  * Verify capability risks.
1304  *
1305  * @param object $capability a capability - a row from the capabilities table.
1306  * @return boolean whether this capability is safe - that is, whether people with the
1307  *      safeoverrides capability should be allowed to change it.
1308  */
1309 function is_safe_capability($capability) {
1310     return !((RISK_DATALOSS | RISK_MANAGETRUST | RISK_CONFIG | RISK_XSS | RISK_PERSONAL) & $capability->riskbitmask);
1313 /**
1314  * Get the local override (if any) for a given capability in a role in a context
1315  *
1316  * @param int $roleid
1317  * @param int $contextid
1318  * @param string $capability
1319  * @return stdClass local capability override
1320  */
1321 function get_local_override($roleid, $contextid, $capability) {
1322     global $DB;
1323     return $DB->get_record('role_capabilities', array('roleid'=>$roleid, 'capability'=>$capability, 'contextid'=>$contextid));
1326 /**
1327  * Returns context instance plus related course and cm instances
1328  *
1329  * @param int $contextid
1330  * @return array of ($context, $course, $cm)
1331  */
1332 function get_context_info_array($contextid) {
1333     global $DB;
1335     $context = context::instance_by_id($contextid, MUST_EXIST);
1336     $course  = null;
1337     $cm      = null;
1339     if ($context->contextlevel == CONTEXT_COURSE) {
1340         $course = $DB->get_record('course', array('id'=>$context->instanceid), '*', MUST_EXIST);
1342     } else if ($context->contextlevel == CONTEXT_MODULE) {
1343         $cm = get_coursemodule_from_id('', $context->instanceid, 0, false, MUST_EXIST);
1344         $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
1346     } else if ($context->contextlevel == CONTEXT_BLOCK) {
1347         $parent = $context->get_parent_context();
1349         if ($parent->contextlevel == CONTEXT_COURSE) {
1350             $course = $DB->get_record('course', array('id'=>$parent->instanceid), '*', MUST_EXIST);
1351         } else if ($parent->contextlevel == CONTEXT_MODULE) {
1352             $cm = get_coursemodule_from_id('', $parent->instanceid, 0, false, MUST_EXIST);
1353             $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
1354         }
1355     }
1357     return array($context, $course, $cm);
1360 /**
1361  * Function that creates a role
1362  *
1363  * @param string $name role name
1364  * @param string $shortname role short name
1365  * @param string $description role description
1366  * @param string $archetype
1367  * @return int id or dml_exception
1368  */
1369 function create_role($name, $shortname, $description, $archetype = '') {
1370     global $DB;
1372     if (strpos($archetype, 'moodle/legacy:') !== false) {
1373         throw new coding_exception('Use new role archetype parameter in create_role() instead of old legacy capabilities.');
1374     }
1376     // verify role archetype actually exists
1377     $archetypes = get_role_archetypes();
1378     if (empty($archetypes[$archetype])) {
1379         $archetype = '';
1380     }
1382     // Insert the role record.
1383     $role = new stdClass();
1384     $role->name        = $name;
1385     $role->shortname   = $shortname;
1386     $role->description = $description;
1387     $role->archetype   = $archetype;
1389     //find free sortorder number
1390     $role->sortorder = $DB->get_field('role', 'MAX(sortorder) + 1', array());
1391     if (empty($role->sortorder)) {
1392         $role->sortorder = 1;
1393     }
1394     $id = $DB->insert_record('role', $role);
1396     return $id;
1399 /**
1400  * Function that deletes a role and cleanups up after it
1401  *
1402  * @param int $roleid id of role to delete
1403  * @return bool always true
1404  */
1405 function delete_role($roleid) {
1406     global $DB;
1408     // first unssign all users
1409     role_unassign_all(array('roleid'=>$roleid));
1411     // cleanup all references to this role, ignore errors
1412     $DB->delete_records('role_capabilities',   array('roleid'=>$roleid));
1413     $DB->delete_records('role_allow_assign',   array('roleid'=>$roleid));
1414     $DB->delete_records('role_allow_assign',   array('allowassign'=>$roleid));
1415     $DB->delete_records('role_allow_override', array('roleid'=>$roleid));
1416     $DB->delete_records('role_allow_override', array('allowoverride'=>$roleid));
1417     $DB->delete_records('role_names',          array('roleid'=>$roleid));
1418     $DB->delete_records('role_context_levels', array('roleid'=>$roleid));
1420     // finally delete the role itself
1421     // get this before the name is gone for logging
1422     $rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
1424     $DB->delete_records('role', array('id'=>$roleid));
1426     add_to_log(SITEID, 'role', 'delete', 'admin/roles/action=delete&roleid='.$roleid, $rolename, '');
1428     return true;
1431 /**
1432  * Function to write context specific overrides, or default capabilities.
1433  *
1434  * NOTE: use $context->mark_dirty() after this
1435  *
1436  * @param string $capability string name
1437  * @param int $permission CAP_ constants
1438  * @param int $roleid role id
1439  * @param int|context $contextid context id
1440  * @param bool $overwrite
1441  * @return bool always true or exception
1442  */
1443 function assign_capability($capability, $permission, $roleid, $contextid, $overwrite = false) {
1444     global $USER, $DB;
1446     if ($contextid instanceof context) {
1447         $context = $contextid;
1448     } else {
1449         $context = context::instance_by_id($contextid);
1450     }
1452     if (empty($permission) || $permission == CAP_INHERIT) { // if permission is not set
1453         unassign_capability($capability, $roleid, $context->id);
1454         return true;
1455     }
1457     $existing = $DB->get_record('role_capabilities', array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability));
1459     if ($existing and !$overwrite) {   // We want to keep whatever is there already
1460         return true;
1461     }
1463     $cap = new stdClass();
1464     $cap->contextid    = $context->id;
1465     $cap->roleid       = $roleid;
1466     $cap->capability   = $capability;
1467     $cap->permission   = $permission;
1468     $cap->timemodified = time();
1469     $cap->modifierid   = empty($USER->id) ? 0 : $USER->id;
1471     if ($existing) {
1472         $cap->id = $existing->id;
1473         $DB->update_record('role_capabilities', $cap);
1474     } else {
1475         if ($DB->record_exists('context', array('id'=>$context->id))) {
1476             $DB->insert_record('role_capabilities', $cap);
1477         }
1478     }
1479     return true;
1482 /**
1483  * Unassign a capability from a role.
1484  *
1485  * NOTE: use $context->mark_dirty() after this
1486  *
1487  * @param string $capability the name of the capability
1488  * @param int $roleid the role id
1489  * @param int|context $contextid null means all contexts
1490  * @return boolean true or exception
1491  */
1492 function unassign_capability($capability, $roleid, $contextid = null) {
1493     global $DB;
1495     if (!empty($contextid)) {
1496         if ($contextid instanceof context) {
1497             $context = $contextid;
1498         } else {
1499             $context = context::instance_by_id($contextid);
1500         }
1501         // delete from context rel, if this is the last override in this context
1502         $DB->delete_records('role_capabilities', array('capability'=>$capability, 'roleid'=>$roleid, 'contextid'=>$context->id));
1503     } else {
1504         $DB->delete_records('role_capabilities', array('capability'=>$capability, 'roleid'=>$roleid));
1505     }
1506     return true;
1509 /**
1510  * Get the roles that have a given capability assigned to it
1511  *
1512  * This function does not resolve the actual permission of the capability.
1513  * It just checks for permissions and overrides.
1514  * Use get_roles_with_cap_in_context() if resolution is required.
1515  *
1516  * @param string $capability - capability name (string)
1517  * @param string $permission - optional, the permission defined for this capability
1518  *                      either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT. Defaults to null which means any.
1519  * @param stdClass $context, null means any
1520  * @return array of role records
1521  */
1522 function get_roles_with_capability($capability, $permission = null, $context = null) {
1523     global $DB;
1525     if ($context) {
1526         $contexts = $context->get_parent_context_ids(true);
1527         list($insql, $params) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED, 'ctx');
1528         $contextsql = "AND rc.contextid $insql";
1529     } else {
1530         $params = array();
1531         $contextsql = '';
1532     }
1534     if ($permission) {
1535         $permissionsql = " AND rc.permission = :permission";
1536         $params['permission'] = $permission;
1537     } else {
1538         $permissionsql = '';
1539     }
1541     $sql = "SELECT r.*
1542               FROM {role} r
1543              WHERE r.id IN (SELECT rc.roleid
1544                               FROM {role_capabilities} rc
1545                              WHERE rc.capability = :capname
1546                                    $contextsql
1547                                    $permissionsql)";
1548     $params['capname'] = $capability;
1551     return $DB->get_records_sql($sql, $params);
1554 /**
1555  * This function makes a role-assignment (a role for a user in a particular context)
1556  *
1557  * @param int $roleid the role of the id
1558  * @param int $userid userid
1559  * @param int|context $contextid id of the context
1560  * @param string $component example 'enrol_ldap', defaults to '' which means manual assignment,
1561  * @param int $itemid id of enrolment/auth plugin
1562  * @param string $timemodified defaults to current time
1563  * @return int new/existing id of the assignment
1564  */
1565 function role_assign($roleid, $userid, $contextid, $component = '', $itemid = 0, $timemodified = '') {
1566     global $USER, $DB;
1568     // first of all detect if somebody is using old style parameters
1569     if ($contextid === 0 or is_numeric($component)) {
1570         throw new coding_exception('Invalid call to role_assign(), code needs to be updated to use new order of parameters');
1571     }
1573     // now validate all parameters
1574     if (empty($roleid)) {
1575         throw new coding_exception('Invalid call to role_assign(), roleid can not be empty');
1576     }
1578     if (empty($userid)) {
1579         throw new coding_exception('Invalid call to role_assign(), userid can not be empty');
1580     }
1582     if ($itemid) {
1583         if (strpos($component, '_') === false) {
1584             throw new coding_exception('Invalid call to role_assign(), component must start with plugin type such as"enrol_" when itemid specified', 'component:'.$component);
1585         }
1586     } else {
1587         $itemid = 0;
1588         if ($component !== '' and strpos($component, '_') === false) {
1589             throw new coding_exception('Invalid call to role_assign(), invalid component string', 'component:'.$component);
1590         }
1591     }
1593     if (!$DB->record_exists('user', array('id'=>$userid, 'deleted'=>0))) {
1594         throw new coding_exception('User ID does not exist or is deleted!', 'userid:'.$userid);
1595     }
1597     if ($contextid instanceof context) {
1598         $context = $contextid;
1599     } else {
1600         $context = context::instance_by_id($contextid, MUST_EXIST);
1601     }
1603     if (!$timemodified) {
1604         $timemodified = time();
1605     }
1607 /// Check for existing entry
1608     $ras = $DB->get_records('role_assignments', array('roleid'=>$roleid, 'contextid'=>$context->id, 'userid'=>$userid, 'component'=>$component, 'itemid'=>$itemid), 'id');
1610     if ($ras) {
1611         // role already assigned - this should not happen
1612         if (count($ras) > 1) {
1613             // very weird - remove all duplicates!
1614             $ra = array_shift($ras);
1615             foreach ($ras as $r) {
1616                 $DB->delete_records('role_assignments', array('id'=>$r->id));
1617             }
1618         } else {
1619             $ra = reset($ras);
1620         }
1622         // actually there is no need to update, reset anything or trigger any event, so just return
1623         return $ra->id;
1624     }
1626     // Create a new entry
1627     $ra = new stdClass();
1628     $ra->roleid       = $roleid;
1629     $ra->contextid    = $context->id;
1630     $ra->userid       = $userid;
1631     $ra->component    = $component;
1632     $ra->itemid       = $itemid;
1633     $ra->timemodified = $timemodified;
1634     $ra->modifierid   = empty($USER->id) ? 0 : $USER->id;
1636     $ra->id = $DB->insert_record('role_assignments', $ra);
1638     // mark context as dirty - again expensive, but needed
1639     $context->mark_dirty();
1641     if (!empty($USER->id) && $USER->id == $userid) {
1642         // If the user is the current user, then do full reload of capabilities too.
1643         reload_all_capabilities();
1644     }
1646     events_trigger('role_assigned', $ra);
1648     return $ra->id;
1651 /**
1652  * Removes one role assignment
1653  *
1654  * @param int $roleid
1655  * @param int  $userid
1656  * @param int|context  $contextid
1657  * @param string $component
1658  * @param int  $itemid
1659  * @return void
1660  */
1661 function role_unassign($roleid, $userid, $contextid, $component = '', $itemid = 0) {
1662     // first make sure the params make sense
1663     if ($roleid == 0 or $userid == 0 or $contextid == 0) {
1664         throw new coding_exception('Invalid call to role_unassign(), please use role_unassign_all() when removing multiple role assignments');
1665     }
1667     if ($itemid) {
1668         if (strpos($component, '_') === false) {
1669             throw new coding_exception('Invalid call to role_assign(), component must start with plugin type such as "enrol_" when itemid specified', 'component:'.$component);
1670         }
1671     } else {
1672         $itemid = 0;
1673         if ($component !== '' and strpos($component, '_') === false) {
1674             throw new coding_exception('Invalid call to role_assign(), invalid component string', 'component:'.$component);
1675         }
1676     }
1678     role_unassign_all(array('roleid'=>$roleid, 'userid'=>$userid, 'contextid'=>$contextid, 'component'=>$component, 'itemid'=>$itemid), false, false);
1681 /**
1682  * Removes multiple role assignments, parameters may contain:
1683  *   'roleid', 'userid', 'contextid', 'component', 'enrolid'.
1684  *
1685  * @param array $params role assignment parameters
1686  * @param bool $subcontexts unassign in subcontexts too
1687  * @param bool $includemanual include manual role assignments too
1688  * @return void
1689  */
1690 function role_unassign_all(array $params, $subcontexts = false, $includemanual = false) {
1691     global $USER, $CFG, $DB;
1693     if (!$params) {
1694         throw new coding_exception('Missing parameters in role_unsassign_all() call');
1695     }
1697     $allowed = array('roleid', 'userid', 'contextid', 'component', 'itemid');
1698     foreach ($params as $key=>$value) {
1699         if (!in_array($key, $allowed)) {
1700             throw new coding_exception('Unknown role_unsassign_all() parameter key', 'key:'.$key);
1701         }
1702     }
1704     if (isset($params['component']) and $params['component'] !== '' and strpos($params['component'], '_') === false) {
1705         throw new coding_exception('Invalid component paramter in role_unsassign_all() call', 'component:'.$params['component']);
1706     }
1708     if ($includemanual) {
1709         if (!isset($params['component']) or $params['component'] === '') {
1710             throw new coding_exception('include manual parameter requires component parameter in role_unsassign_all() call');
1711         }
1712     }
1714     if ($subcontexts) {
1715         if (empty($params['contextid'])) {
1716             throw new coding_exception('subcontexts paramtere requires component parameter in role_unsassign_all() call');
1717         }
1718     }
1720     $ras = $DB->get_records('role_assignments', $params);
1721     foreach($ras as $ra) {
1722         $DB->delete_records('role_assignments', array('id'=>$ra->id));
1723         if ($context = context::instance_by_id($ra->contextid, IGNORE_MISSING)) {
1724             // this is a bit expensive but necessary
1725             $context->mark_dirty();
1726             /// If the user is the current user, then do full reload of capabilities too.
1727             if (!empty($USER->id) && $USER->id == $ra->userid) {
1728                 reload_all_capabilities();
1729             }
1730         }
1731         events_trigger('role_unassigned', $ra);
1732     }
1733     unset($ras);
1735     // process subcontexts
1736     if ($subcontexts and $context = context::instance_by_id($params['contextid'], IGNORE_MISSING)) {
1737         if ($params['contextid'] instanceof context) {
1738             $context = $params['contextid'];
1739         } else {
1740             $context = context::instance_by_id($params['contextid'], IGNORE_MISSING);
1741         }
1743         if ($context) {
1744             $contexts = $context->get_child_contexts();
1745             $mparams = $params;
1746             foreach($contexts as $context) {
1747                 $mparams['contextid'] = $context->id;
1748                 $ras = $DB->get_records('role_assignments', $mparams);
1749                 foreach($ras as $ra) {
1750                     $DB->delete_records('role_assignments', array('id'=>$ra->id));
1751                     // this is a bit expensive but necessary
1752                     $context->mark_dirty();
1753                     /// If the user is the current user, then do full reload of capabilities too.
1754                     if (!empty($USER->id) && $USER->id == $ra->userid) {
1755                         reload_all_capabilities();
1756                     }
1757                     events_trigger('role_unassigned', $ra);
1758                 }
1759             }
1760         }
1761     }
1763     // do this once more for all manual role assignments
1764     if ($includemanual) {
1765         $params['component'] = '';
1766         role_unassign_all($params, $subcontexts, false);
1767     }
1770 /**
1771  * Determines if a user is currently logged in
1772  *
1773  * @return bool
1774  */
1775 function isloggedin() {
1776     global $USER;
1778     return (!empty($USER->id));
1781 /**
1782  * Determines if a user is logged in as real guest user with username 'guest'.
1783  *
1784  * @param int|object $user mixed user object or id, $USER if not specified
1785  * @return bool true if user is the real guest user, false if not logged in or other user
1786  */
1787 function isguestuser($user = null) {
1788     global $USER, $DB, $CFG;
1790     // make sure we have the user id cached in config table, because we are going to use it a lot
1791     if (empty($CFG->siteguest)) {
1792         if (!$guestid = $DB->get_field('user', 'id', array('username'=>'guest', 'mnethostid'=>$CFG->mnet_localhost_id))) {
1793             // guest does not exist yet, weird
1794             return false;
1795         }
1796         set_config('siteguest', $guestid);
1797     }
1798     if ($user === null) {
1799         $user = $USER;
1800     }
1802     if ($user === null) {
1803         // happens when setting the $USER
1804         return false;
1806     } else if (is_numeric($user)) {
1807         return ($CFG->siteguest == $user);
1809     } else if (is_object($user)) {
1810         if (empty($user->id)) {
1811             return false; // not logged in means is not be guest
1812         } else {
1813             return ($CFG->siteguest == $user->id);
1814         }
1816     } else {
1817         throw new coding_exception('Invalid user parameter supplied for isguestuser() function!');
1818     }
1821 /**
1822  * Does user have a (temporary or real) guest access to course?
1823  *
1824  * @param context $context
1825  * @param stdClass|int $user
1826  * @return bool
1827  */
1828 function is_guest(context $context, $user = null) {
1829     global $USER;
1831     // first find the course context
1832     $coursecontext = $context->get_course_context();
1834     // make sure there is a real user specified
1835     if ($user === null) {
1836         $userid = isset($USER->id) ? $USER->id : 0;
1837     } else {
1838         $userid = is_object($user) ? $user->id : $user;
1839     }
1841     if (isguestuser($userid)) {
1842         // can not inspect or be enrolled
1843         return true;
1844     }
1846     if (has_capability('moodle/course:view', $coursecontext, $user)) {
1847         // viewing users appear out of nowhere, they are neither guests nor participants
1848         return false;
1849     }
1851     // consider only real active enrolments here
1852     if (is_enrolled($coursecontext, $user, '', true)) {
1853         return false;
1854     }
1856     return true;
1859 /**
1860  * Returns true if the user has moodle/course:view capability in the course,
1861  * this is intended for admins, managers (aka small admins), inspectors, etc.
1862  *
1863  * @param context $context
1864  * @param int|stdClass $user, if null $USER is used
1865  * @param string $withcapability extra capability name
1866  * @return bool
1867  */
1868 function is_viewing(context $context, $user = null, $withcapability = '') {
1869     // first find the course context
1870     $coursecontext = $context->get_course_context();
1872     if (isguestuser($user)) {
1873         // can not inspect
1874         return false;
1875     }
1877     if (!has_capability('moodle/course:view', $coursecontext, $user)) {
1878         // admins are allowed to inspect courses
1879         return false;
1880     }
1882     if ($withcapability and !has_capability($withcapability, $context, $user)) {
1883         // site admins always have the capability, but the enrolment above blocks
1884         return false;
1885     }
1887     return true;
1890 /**
1891  * Returns true if user is enrolled (is participating) in course
1892  * this is intended for students and teachers.
1893  *
1894  * Since 2.2 the result for active enrolments and current user are cached.
1895  *
1896  * @param context $context
1897  * @param int|stdClass $user, if null $USER is used, otherwise user object or id expected
1898  * @param string $withcapability extra capability name
1899  * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1900  * @return bool
1901  */
1902 function is_enrolled(context $context, $user = null, $withcapability = '', $onlyactive = false) {
1903     global $USER, $DB;
1905     // first find the course context
1906     $coursecontext = $context->get_course_context();
1908     // make sure there is a real user specified
1909     if ($user === null) {
1910         $userid = isset($USER->id) ? $USER->id : 0;
1911     } else {
1912         $userid = is_object($user) ? $user->id : $user;
1913     }
1915     if (empty($userid)) {
1916         // not-logged-in!
1917         return false;
1918     } else if (isguestuser($userid)) {
1919         // guest account can not be enrolled anywhere
1920         return false;
1921     }
1923     if ($coursecontext->instanceid == SITEID) {
1924         // everybody participates on frontpage
1925     } else {
1926         // try cached info first - the enrolled flag is set only when active enrolment present
1927         if ($USER->id == $userid) {
1928             $coursecontext->reload_if_dirty();
1929             if (isset($USER->enrol['enrolled'][$coursecontext->instanceid])) {
1930                 if ($USER->enrol['enrolled'][$coursecontext->instanceid] > time()) {
1931                     return true;
1932                 }
1933             }
1934         }
1936         if ($onlyactive) {
1937             // look for active enrolments only
1938             $until = enrol_get_enrolment_end($coursecontext->instanceid, $userid);
1940             if ($until === false) {
1941                 return false;
1942             }
1944             if ($USER->id == $userid) {
1945                 if ($until == 0) {
1946                     $until = ENROL_MAX_TIMESTAMP;
1947                 }
1948                 $USER->enrol['enrolled'][$coursecontext->instanceid] = $until;
1949                 if (isset($USER->enrol['tempguest'][$coursecontext->instanceid])) {
1950                     unset($USER->enrol['tempguest'][$coursecontext->instanceid]);
1951                     remove_temp_course_roles($coursecontext);
1952                 }
1953             }
1955         } else {
1956             // any enrolment is good for us here, even outdated, disabled or inactive
1957             $sql = "SELECT 'x'
1958                       FROM {user_enrolments} ue
1959                       JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)
1960                       JOIN {user} u ON u.id = ue.userid
1961                      WHERE ue.userid = :userid AND u.deleted = 0";
1962             $params = array('userid'=>$userid, 'courseid'=>$coursecontext->instanceid);
1963             if (!$DB->record_exists_sql($sql, $params)) {
1964                 return false;
1965             }
1966         }
1967     }
1969     if ($withcapability and !has_capability($withcapability, $context, $userid)) {
1970         return false;
1971     }
1973     return true;
1976 /**
1977  * Returns true if the user is able to access the course.
1978  *
1979  * This function is in no way, shape, or form a substitute for require_login.
1980  * It should only be used in circumstances where it is not possible to call require_login
1981  * such as the navigation.
1982  *
1983  * This function checks many of the methods of access to a course such as the view
1984  * capability, enrollments, and guest access. It also makes use of the cache
1985  * generated by require_login for guest access.
1986  *
1987  * The flags within the $USER object that are used here should NEVER be used outside
1988  * of this function can_access_course and require_login. Doing so WILL break future
1989  * versions.
1990  *
1991  * @param stdClass $course record
1992  * @param stdClass|int|null $user user record or id, current user if null
1993  * @param string $withcapability Check for this capability as well.
1994  * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1995  * @return boolean Returns true if the user is able to access the course
1996  */
1997 function can_access_course(stdClass $course, $user = null, $withcapability = '', $onlyactive = false) {
1998     global $DB, $USER;
2000     // this function originally accepted $coursecontext parameter
2001     if ($course instanceof context) {
2002         if ($course instanceof context_course) {
2003             debugging('deprecated context parameter, please use $course record');
2004             $coursecontext = $course;
2005             $course = $DB->get_record('course', array('id'=>$coursecontext->instanceid));
2006         } else {
2007             debugging('Invalid context parameter, please use $course record');
2008             return false;
2009         }
2010     } else {
2011         $coursecontext = context_course::instance($course->id);
2012     }
2014     if (!isset($USER->id)) {
2015         // should never happen
2016         $USER->id = 0;
2017     }
2019     // make sure there is a user specified
2020     if ($user === null) {
2021         $userid = $USER->id;
2022     } else {
2023         $userid = is_object($user) ? $user->id : $user;
2024     }
2025     unset($user);
2027     if ($withcapability and !has_capability($withcapability, $coursecontext, $userid)) {
2028         return false;
2029     }
2031     if ($userid == $USER->id) {
2032         if (!empty($USER->access['rsw'][$coursecontext->path])) {
2033             // the fact that somebody switched role means they can access the course no matter to what role they switched
2034             return true;
2035         }
2036     }
2038     if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext, $userid)) {
2039         return false;
2040     }
2042     if (is_viewing($coursecontext, $userid)) {
2043         return true;
2044     }
2046     if ($userid != $USER->id) {
2047         // for performance reasons we do not verify temporary guest access for other users, sorry...
2048         return is_enrolled($coursecontext, $userid, '', $onlyactive);
2049     }
2051     // === from here we deal only with $USER ===
2053     $coursecontext->reload_if_dirty();
2055     if (isset($USER->enrol['enrolled'][$course->id])) {
2056         if ($USER->enrol['enrolled'][$course->id] > time()) {
2057             return true;
2058         }
2059     }
2060     if (isset($USER->enrol['tempguest'][$course->id])) {
2061         if ($USER->enrol['tempguest'][$course->id] > time()) {
2062             return true;
2063         }
2064     }
2066     if (is_enrolled($coursecontext, $USER, '', $onlyactive)) {
2067         return true;
2068     }
2070     // if not enrolled try to gain temporary guest access
2071     $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'status'=>ENROL_INSTANCE_ENABLED), 'sortorder, id ASC');
2072     $enrols = enrol_get_plugins(true);
2073     foreach($instances as $instance) {
2074         if (!isset($enrols[$instance->enrol])) {
2075             continue;
2076         }
2077         // Get a duration for the guest access, a timestamp in the future, 0 (always) or false.
2078         $until = $enrols[$instance->enrol]->try_guestaccess($instance);
2079         if ($until !== false and $until > time()) {
2080             $USER->enrol['tempguest'][$course->id] = $until;
2081             return true;
2082         }
2083     }
2084     if (isset($USER->enrol['tempguest'][$course->id])) {
2085         unset($USER->enrol['tempguest'][$course->id]);
2086         remove_temp_course_roles($coursecontext);
2087     }
2089     return false;
2092 /**
2093  * Returns array with sql code and parameters returning all ids
2094  * of users enrolled into course.
2095  *
2096  * This function is using 'eu[0-9]+_' prefix for table names and parameters.
2097  *
2098  * @param context $context
2099  * @param string $withcapability
2100  * @param int $groupid 0 means ignore groups, any other value limits the result by group id
2101  * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
2102  * @return array list($sql, $params)
2103  */
2104 function get_enrolled_sql(context $context, $withcapability = '', $groupid = 0, $onlyactive = false) {
2105     global $DB, $CFG;
2107     // use unique prefix just in case somebody makes some SQL magic with the result
2108     static $i = 0;
2109     $i++;
2110     $prefix = 'eu'.$i.'_';
2112     // first find the course context
2113     $coursecontext = $context->get_course_context();
2115     $isfrontpage = ($coursecontext->instanceid == SITEID);
2117     $joins  = array();
2118     $wheres = array();
2119     $params = array();
2121     list($contextids, $contextpaths) = get_context_info_list($context);
2123     // get all relevant capability info for all roles
2124     if ($withcapability) {
2125         list($incontexts, $cparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED, 'ctx');
2126         $cparams['cap'] = $withcapability;
2128         $defs = array();
2129         $sql = "SELECT rc.id, rc.roleid, rc.permission, ctx.path
2130                   FROM {role_capabilities} rc
2131                   JOIN {context} ctx on rc.contextid = ctx.id
2132                  WHERE rc.contextid $incontexts AND rc.capability = :cap";
2133         $rcs = $DB->get_records_sql($sql, $cparams);
2134         foreach ($rcs as $rc) {
2135             $defs[$rc->path][$rc->roleid] = $rc->permission;
2136         }
2138         $access = array();
2139         if (!empty($defs)) {
2140             foreach ($contextpaths as $path) {
2141                 if (empty($defs[$path])) {
2142                     continue;
2143                 }
2144                 foreach($defs[$path] as $roleid => $perm) {
2145                     if ($perm == CAP_PROHIBIT) {
2146                         $access[$roleid] = CAP_PROHIBIT;
2147                         continue;
2148                     }
2149                     if (!isset($access[$roleid])) {
2150                         $access[$roleid] = (int)$perm;
2151                     }
2152                 }
2153             }
2154         }
2156         unset($defs);
2158         // make lists of roles that are needed and prohibited
2159         $needed     = array(); // one of these is enough
2160         $prohibited = array(); // must not have any of these
2161         foreach ($access as $roleid => $perm) {
2162             if ($perm == CAP_PROHIBIT) {
2163                 unset($needed[$roleid]);
2164                 $prohibited[$roleid] = true;
2165             } else if ($perm == CAP_ALLOW and empty($prohibited[$roleid])) {
2166                 $needed[$roleid] = true;
2167             }
2168         }
2170         $defaultuserroleid      = isset($CFG->defaultuserroleid) ? $CFG->defaultuserroleid : 0;
2171         $defaultfrontpageroleid = isset($CFG->defaultfrontpageroleid) ? $CFG->defaultfrontpageroleid : 0;
2173         $nobody = false;
2175         if ($isfrontpage) {
2176             if (!empty($prohibited[$defaultuserroleid]) or !empty($prohibited[$defaultfrontpageroleid])) {
2177                 $nobody = true;
2178             } else if (!empty($needed[$defaultuserroleid]) or !empty($needed[$defaultfrontpageroleid])) {
2179                 // everybody not having prohibit has the capability
2180                 $needed = array();
2181             } else if (empty($needed)) {
2182                 $nobody = true;
2183             }
2184         } else {
2185             if (!empty($prohibited[$defaultuserroleid])) {
2186                 $nobody = true;
2187             } else if (!empty($needed[$defaultuserroleid])) {
2188                 // everybody not having prohibit has the capability
2189                 $needed = array();
2190             } else if (empty($needed)) {
2191                 $nobody = true;
2192             }
2193         }
2195         if ($nobody) {
2196             // nobody can match so return some SQL that does not return any results
2197             $wheres[] = "1 = 2";
2199         } else {
2201             if ($needed) {
2202                 $ctxids = implode(',', $contextids);
2203                 $roleids = implode(',', array_keys($needed));
2204                 $joins[] = "JOIN {role_assignments} {$prefix}ra3 ON ({$prefix}ra3.userid = {$prefix}u.id AND {$prefix}ra3.roleid IN ($roleids) AND {$prefix}ra3.contextid IN ($ctxids))";
2205             }
2207             if ($prohibited) {
2208                 $ctxids = implode(',', $contextids);
2209                 $roleids = implode(',', array_keys($prohibited));
2210                 $joins[] = "LEFT JOIN {role_assignments} {$prefix}ra4 ON ({$prefix}ra4.userid = {$prefix}u.id AND {$prefix}ra4.roleid IN ($roleids) AND {$prefix}ra4.contextid IN ($ctxids))";
2211                 $wheres[] = "{$prefix}ra4.id IS NULL";
2212             }
2214             if ($groupid) {
2215                 $joins[] = "JOIN {groups_members} {$prefix}gm ON ({$prefix}gm.userid = {$prefix}u.id AND {$prefix}gm.groupid = :{$prefix}gmid)";
2216                 $params["{$prefix}gmid"] = $groupid;
2217             }
2218         }
2220     } else {
2221         if ($groupid) {
2222             $joins[] = "JOIN {groups_members} {$prefix}gm ON ({$prefix}gm.userid = {$prefix}u.id AND {$prefix}gm.groupid = :{$prefix}gmid)";
2223             $params["{$prefix}gmid"] = $groupid;
2224         }
2225     }
2227     $wheres[] = "{$prefix}u.deleted = 0 AND {$prefix}u.id <> :{$prefix}guestid";
2228     $params["{$prefix}guestid"] = $CFG->siteguest;
2230     if ($isfrontpage) {
2231         // all users are "enrolled" on the frontpage
2232     } else {
2233         $joins[] = "JOIN {user_enrolments} {$prefix}ue ON {$prefix}ue.userid = {$prefix}u.id";
2234         $joins[] = "JOIN {enrol} {$prefix}e ON ({$prefix}e.id = {$prefix}ue.enrolid AND {$prefix}e.courseid = :{$prefix}courseid)";
2235         $params[$prefix.'courseid'] = $coursecontext->instanceid;
2237         if ($onlyactive) {
2238             $wheres[] = "{$prefix}ue.status = :{$prefix}active AND {$prefix}e.status = :{$prefix}enabled";
2239             $wheres[] = "{$prefix}ue.timestart < :{$prefix}now1 AND ({$prefix}ue.timeend = 0 OR {$prefix}ue.timeend > :{$prefix}now2)";
2240             $now = round(time(), -2); // rounding helps caching in DB
2241             $params = array_merge($params, array($prefix.'enabled'=>ENROL_INSTANCE_ENABLED,
2242                                                  $prefix.'active'=>ENROL_USER_ACTIVE,
2243                                                  $prefix.'now1'=>$now, $prefix.'now2'=>$now));
2244         }
2245     }
2247     $joins = implode("\n", $joins);
2248     $wheres = "WHERE ".implode(" AND ", $wheres);
2250     $sql = "SELECT DISTINCT {$prefix}u.id
2251               FROM {user} {$prefix}u
2252             $joins
2253            $wheres";
2255     return array($sql, $params);
2258 /**
2259  * Returns list of users enrolled into course.
2260  *
2261  * @param context $context
2262  * @param string $withcapability
2263  * @param int $groupid 0 means ignore groups, any other value limits the result by group id
2264  * @param string $userfields requested user record fields
2265  * @param string $orderby
2266  * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
2267  * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
2268  * @return array of user records
2269  */
2270 function get_enrolled_users(context $context, $withcapability = '', $groupid = 0, $userfields = 'u.*', $orderby = '', $limitfrom = 0, $limitnum = 0) {
2271     global $DB;
2273     list($esql, $params) = get_enrolled_sql($context, $withcapability, $groupid);
2274     $sql = "SELECT $userfields
2275               FROM {user} u
2276               JOIN ($esql) je ON je.id = u.id
2277              WHERE u.deleted = 0";
2279     if ($orderby) {
2280         $sql = "$sql ORDER BY $orderby";
2281     } else {
2282         $sql = "$sql ORDER BY u.lastname ASC, u.firstname ASC";
2283     }
2285     return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
2288 /**
2289  * Counts list of users enrolled into course (as per above function)
2290  *
2291  * @param context $context
2292  * @param string $withcapability
2293  * @param int $groupid 0 means ignore groups, any other value limits the result by group id
2294  * @return array of user records
2295  */
2296 function count_enrolled_users(context $context, $withcapability = '', $groupid = 0) {
2297     global $DB;
2299     list($esql, $params) = get_enrolled_sql($context, $withcapability, $groupid);
2300     $sql = "SELECT count(u.id)
2301               FROM {user} u
2302               JOIN ($esql) je ON je.id = u.id
2303              WHERE u.deleted = 0";
2305     return $DB->count_records_sql($sql, $params);
2308 /**
2309  * Loads the capability definitions for the component (from file).
2310  *
2311  * Loads the capability definitions for the component (from file). If no
2312  * capabilities are defined for the component, we simply return an empty array.
2313  *
2314  * @param string $component full plugin name, examples: 'moodle', 'mod_forum'
2315  * @return array array of capabilities
2316  */
2317 function load_capability_def($component) {
2318     $defpath = get_component_directory($component).'/db/access.php';
2320     $capabilities = array();
2321     if (file_exists($defpath)) {
2322         require($defpath);
2323         if (!empty(${$component.'_capabilities'})) {
2324             // BC capability array name
2325             // since 2.0 we prefer $capabilities instead - it is easier to use and matches db/* files
2326             debugging('componentname_capabilities array is deprecated, please use $capabilities array only in access.php files');
2327             $capabilities = ${$component.'_capabilities'};
2328         }
2329     }
2331     return $capabilities;
2334 /**
2335  * Gets the capabilities that have been cached in the database for this component.
2336  *
2337  * @param string $component - examples: 'moodle', 'mod_forum'
2338  * @return array array of capabilities
2339  */
2340 function get_cached_capabilities($component = 'moodle') {
2341     global $DB;
2342     return $DB->get_records('capabilities', array('component'=>$component));
2345 /**
2346  * Returns default capabilities for given role archetype.
2347  *
2348  * @param string $archetype role archetype
2349  * @return array
2350  */
2351 function get_default_capabilities($archetype) {
2352     global $DB;
2354     if (!$archetype) {
2355         return array();
2356     }
2358     $alldefs = array();
2359     $defaults = array();
2360     $components = array();
2361     $allcaps = $DB->get_records('capabilities');
2363     foreach ($allcaps as $cap) {
2364         if (!in_array($cap->component, $components)) {
2365             $components[] = $cap->component;
2366             $alldefs = array_merge($alldefs, load_capability_def($cap->component));
2367         }
2368     }
2369     foreach($alldefs as $name=>$def) {
2370         // Use array 'archetypes if available. Only if not specified, use 'legacy'.
2371         if (isset($def['archetypes'])) {
2372             if (isset($def['archetypes'][$archetype])) {
2373                 $defaults[$name] = $def['archetypes'][$archetype];
2374             }
2375         // 'legacy' is for backward compatibility with 1.9 access.php
2376         } else {
2377             if (isset($def['legacy'][$archetype])) {
2378                 $defaults[$name] = $def['legacy'][$archetype];
2379             }
2380         }
2381     }
2383     return $defaults;
2386 /**
2387  * Reset role capabilities to default according to selected role archetype.
2388  * If no archetype selected, removes all capabilities.
2389  *
2390  * @param int $roleid
2391  * @return void
2392  */
2393 function reset_role_capabilities($roleid) {
2394     global $DB;
2396     $role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST);
2397     $defaultcaps = get_default_capabilities($role->archetype);
2399     $systemcontext = context_system::instance();
2401     $DB->delete_records('role_capabilities', array('roleid'=>$roleid));
2403     foreach($defaultcaps as $cap=>$permission) {
2404         assign_capability($cap, $permission, $roleid, $systemcontext->id);
2405     }
2408 /**
2409  * Updates the capabilities table with the component capability definitions.
2410  * If no parameters are given, the function updates the core moodle
2411  * capabilities.
2412  *
2413  * Note that the absence of the db/access.php capabilities definition file
2414  * will cause any stored capabilities for the component to be removed from
2415  * the database.
2416  *
2417  * @param string $component examples: 'moodle', 'mod/forum', 'block/quiz_results'
2418  * @return boolean true if success, exception in case of any problems
2419  */
2420 function update_capabilities($component = 'moodle') {
2421     global $DB, $OUTPUT;
2423     $storedcaps = array();
2425     $filecaps = load_capability_def($component);
2426     foreach($filecaps as $capname=>$unused) {
2427         if (!preg_match('|^[a-z]+/[a-z_0-9]+:[a-z_0-9]+$|', $capname)) {
2428             debugging("Coding problem: Invalid capability name '$capname', use 'clonepermissionsfrom' field for migration.");
2429         }
2430     }
2432     $cachedcaps = get_cached_capabilities($component);
2433     if ($cachedcaps) {
2434         foreach ($cachedcaps as $cachedcap) {
2435             array_push($storedcaps, $cachedcap->name);
2436             // update risk bitmasks and context levels in existing capabilities if needed
2437             if (array_key_exists($cachedcap->name, $filecaps)) {
2438                 if (!array_key_exists('riskbitmask', $filecaps[$cachedcap->name])) {
2439                     $filecaps[$cachedcap->name]['riskbitmask'] = 0; // no risk if not specified
2440                 }
2441                 if ($cachedcap->captype != $filecaps[$cachedcap->name]['captype']) {
2442                     $updatecap = new stdClass();
2443                     $updatecap->id = $cachedcap->id;
2444                     $updatecap->captype = $filecaps[$cachedcap->name]['captype'];
2445                     $DB->update_record('capabilities', $updatecap);
2446                 }
2447                 if ($cachedcap->riskbitmask != $filecaps[$cachedcap->name]['riskbitmask']) {
2448                     $updatecap = new stdClass();
2449                     $updatecap->id = $cachedcap->id;
2450                     $updatecap->riskbitmask = $filecaps[$cachedcap->name]['riskbitmask'];
2451                     $DB->update_record('capabilities', $updatecap);
2452                 }
2454                 if (!array_key_exists('contextlevel', $filecaps[$cachedcap->name])) {
2455                     $filecaps[$cachedcap->name]['contextlevel'] = 0; // no context level defined
2456                 }
2457                 if ($cachedcap->contextlevel != $filecaps[$cachedcap->name]['contextlevel']) {
2458                     $updatecap = new stdClass();
2459                     $updatecap->id = $cachedcap->id;
2460                     $updatecap->contextlevel = $filecaps[$cachedcap->name]['contextlevel'];
2461                     $DB->update_record('capabilities', $updatecap);
2462                 }
2463             }
2464         }
2465     }
2467     // Are there new capabilities in the file definition?
2468     $newcaps = array();
2470     foreach ($filecaps as $filecap => $def) {
2471         if (!$storedcaps ||
2472                 ($storedcaps && in_array($filecap, $storedcaps) === false)) {
2473             if (!array_key_exists('riskbitmask', $def)) {
2474                 $def['riskbitmask'] = 0; // no risk if not specified
2475             }
2476             $newcaps[$filecap] = $def;
2477         }
2478     }
2479     // Add new capabilities to the stored definition.
2480     foreach ($newcaps as $capname => $capdef) {
2481         $capability = new stdClass();
2482         $capability->name         = $capname;
2483         $capability->captype      = $capdef['captype'];
2484         $capability->contextlevel = $capdef['contextlevel'];
2485         $capability->component    = $component;
2486         $capability->riskbitmask  = $capdef['riskbitmask'];
2488         $DB->insert_record('capabilities', $capability, false);
2490         if (isset($capdef['clonepermissionsfrom']) && in_array($capdef['clonepermissionsfrom'], $storedcaps)){
2491             if ($rolecapabilities = $DB->get_records('role_capabilities', array('capability'=>$capdef['clonepermissionsfrom']))){
2492                 foreach ($rolecapabilities as $rolecapability){
2493                     //assign_capability will update rather than insert if capability exists
2494                     if (!assign_capability($capname, $rolecapability->permission,
2495                                             $rolecapability->roleid, $rolecapability->contextid, true)){
2496                          echo $OUTPUT->notification('Could not clone capabilities for '.$capname);
2497                     }
2498                 }
2499             }
2500         // we ignore archetype key if we have cloned permissions
2501         } else if (isset($capdef['archetypes']) && is_array($capdef['archetypes'])) {
2502             assign_legacy_capabilities($capname, $capdef['archetypes']);
2503         // 'legacy' is for backward compatibility with 1.9 access.php
2504         } else if (isset($capdef['legacy']) && is_array($capdef['legacy'])) {
2505             assign_legacy_capabilities($capname, $capdef['legacy']);
2506         }
2507     }
2508     // Are there any capabilities that have been removed from the file
2509     // definition that we need to delete from the stored capabilities and
2510     // role assignments?
2511     capabilities_cleanup($component, $filecaps);
2513     // reset static caches
2514     accesslib_clear_all_caches(false);
2516     return true;
2519 /**
2520  * Deletes cached capabilities that are no longer needed by the component.
2521  * Also unassigns these capabilities from any roles that have them.
2522  *
2523  * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
2524  * @param array $newcapdef array of the new capability definitions that will be
2525  *                     compared with the cached capabilities
2526  * @return int number of deprecated capabilities that have been removed
2527  */
2528 function capabilities_cleanup($component, $newcapdef = null) {
2529     global $DB;
2531     $removedcount = 0;
2533     if ($cachedcaps = get_cached_capabilities($component)) {
2534         foreach ($cachedcaps as $cachedcap) {
2535             if (empty($newcapdef) ||
2536                         array_key_exists($cachedcap->name, $newcapdef) === false) {
2538                 // Remove from capabilities cache.
2539                 $DB->delete_records('capabilities', array('name'=>$cachedcap->name));
2540                 $removedcount++;
2541                 // Delete from roles.
2542                 if ($roles = get_roles_with_capability($cachedcap->name)) {
2543                     foreach($roles as $role) {
2544                         if (!unassign_capability($cachedcap->name, $role->id)) {
2545                             print_error('cannotunassigncap', 'error', '', (object)array('cap'=>$cachedcap->name, 'role'=>$role->name));
2546                         }
2547                     }
2548                 }
2549             } // End if.
2550         }
2551     }
2552     return $removedcount;
2555 /**
2556  * Returns an array of all the known types of risk
2557  * The array keys can be used, for example as CSS class names, or in calls to
2558  * print_risk_icon. The values are the corresponding RISK_ constants.
2559  *
2560  * @return array all the known types of risk.
2561  */
2562 function get_all_risks() {
2563     return array(
2564         'riskmanagetrust' => RISK_MANAGETRUST,
2565         'riskconfig'      => RISK_CONFIG,
2566         'riskxss'         => RISK_XSS,
2567         'riskpersonal'    => RISK_PERSONAL,
2568         'riskspam'        => RISK_SPAM,
2569         'riskdataloss'    => RISK_DATALOSS,
2570     );
2573 /**
2574  * Return a link to moodle docs for a given capability name
2575  *
2576  * @param object $capability a capability - a row from the mdl_capabilities table.
2577  * @return string the human-readable capability name as a link to Moodle Docs.
2578  */
2579 function get_capability_docs_link($capability) {
2580     $url = get_docs_url('Capabilities/' . $capability->name);
2581     return '<a onclick="this.target=\'docspopup\'" href="' . $url . '">' . get_capability_string($capability->name) . '</a>';
2584 /**
2585  * This function pulls out all the resolved capabilities (overrides and
2586  * defaults) of a role used in capability overrides in contexts at a given
2587  * context.
2588  *
2589  * @param context $context
2590  * @param int $roleid
2591  * @param string $cap capability, optional, defaults to ''
2592  * @return array of capabilities
2593  */
2594 function role_context_capabilities($roleid, context $context, $cap = '') {
2595     global $DB;
2597     $contexts = $context->get_parent_context_ids(true);
2598     $contexts = '('.implode(',', $contexts).')';
2600     $params = array($roleid);
2602     if ($cap) {
2603         $search = " AND rc.capability = ? ";
2604         $params[] = $cap;
2605     } else {
2606         $search = '';
2607     }
2609     $sql = "SELECT rc.*
2610               FROM {role_capabilities} rc, {context} c
2611              WHERE rc.contextid in $contexts
2612                    AND rc.roleid = ?
2613                    AND rc.contextid = c.id $search
2614           ORDER BY c.contextlevel DESC, rc.capability DESC";
2616     $capabilities = array();
2618     if ($records = $DB->get_records_sql($sql, $params)) {
2619         // We are traversing via reverse order.
2620         foreach ($records as $record) {
2621             // If not set yet (i.e. inherit or not set at all), or currently we have a prohibit
2622             if (!isset($capabilities[$record->capability]) || $record->permission<-500) {
2623                 $capabilities[$record->capability] = $record->permission;
2624             }
2625         }
2626     }
2627     return $capabilities;
2630 /**
2631  * Constructs array with contextids as first parameter and context paths,
2632  * in both cases bottom top including self.
2633  *
2634  * @private
2635  * @param context $context
2636  * @return array
2637  */
2638 function get_context_info_list(context $context) {
2639     $contextids = explode('/', ltrim($context->path, '/'));
2640     $contextpaths = array();
2641     $contextids2 = $contextids;
2642     while ($contextids2) {
2643         $contextpaths[] = '/' . implode('/', $contextids2);
2644         array_pop($contextids2);
2645     }
2646     return array($contextids, $contextpaths);
2649 /**
2650  * Check if context is the front page context or a context inside it
2651  *
2652  * Returns true if this context is the front page context, or a context inside it,
2653  * otherwise false.
2654  *
2655  * @param context $context a context object.
2656  * @return bool
2657  */
2658 function is_inside_frontpage(context $context) {
2659     $frontpagecontext = context_course::instance(SITEID);
2660     return strpos($context->path . '/', $frontpagecontext->path . '/') === 0;
2663 /**
2664  * Returns capability information (cached)
2665  *
2666  * @param string $capabilityname
2667  * @return object or null if capability not found
2668  */
2669 function get_capability_info($capabilityname) {
2670     global $ACCESSLIB_PRIVATE, $DB; // one request per page only
2672     //TODO: MUC - this could be cached in shared memory, it would eliminate 1 query per page
2674     if (empty($ACCESSLIB_PRIVATE->capabilities)) {
2675         $ACCESSLIB_PRIVATE->capabilities = array();
2676         $caps = $DB->get_records('capabilities', array(), 'id, name, captype, riskbitmask');
2677         foreach ($caps as $cap) {
2678             $capname = $cap->name;
2679             unset($cap->id);
2680             unset($cap->name);
2681             $cap->riskbitmask = (int)$cap->riskbitmask;
2682             $ACCESSLIB_PRIVATE->capabilities[$capname] = $cap;
2683         }
2684     }
2686     return isset($ACCESSLIB_PRIVATE->capabilities[$capabilityname]) ? $ACCESSLIB_PRIVATE->capabilities[$capabilityname] : null;
2689 /**
2690  * Returns the human-readable, translated version of the capability.
2691  * Basically a big switch statement.
2692  *
2693  * @param string $capabilityname e.g. mod/choice:readresponses
2694  * @return string
2695  */
2696 function get_capability_string($capabilityname) {
2698     // Typical capability name is 'plugintype/pluginname:capabilityname'
2699     list($type, $name, $capname) = preg_split('|[/:]|', $capabilityname);
2701     if ($type === 'moodle') {
2702         $component = 'core_role';
2703     } else if ($type === 'quizreport') {
2704         //ugly hack!!
2705         $component = 'quiz_'.$name;
2706     } else {
2707         $component = $type.'_'.$name;
2708     }
2710     $stringname = $name.':'.$capname;
2712     if ($component === 'core_role' or get_string_manager()->string_exists($stringname, $component)) {
2713         return get_string($stringname, $component);
2714     }
2716     $dir = get_component_directory($component);
2717     if (!file_exists($dir)) {
2718         // plugin broken or does not exist, do not bother with printing of debug message
2719         return $capabilityname.' ???';
2720     }
2722     // something is wrong in plugin, better print debug
2723     return get_string($stringname, $component);
2726 /**
2727  * This gets the mod/block/course/core etc strings.
2728  *
2729  * @param string $component
2730  * @param int $contextlevel
2731  * @return string|bool String is success, false if failed
2732  */
2733 function get_component_string($component, $contextlevel) {
2735     if ($component === 'moodle' or $component === 'core') {
2736         switch ($contextlevel) {
2737             // TODO: this should probably use context level names instead
2738             case CONTEXT_SYSTEM:    return get_string('coresystem');
2739             case CONTEXT_USER:      return get_string('users');
2740             case CONTEXT_COURSECAT: return get_string('categories');
2741             case CONTEXT_COURSE:    return get_string('course');
2742             case CONTEXT_MODULE:    return get_string('activities');
2743             case CONTEXT_BLOCK:     return get_string('block');
2744             default:                print_error('unknowncontext');
2745         }
2746     }
2748     list($type, $name) = normalize_component($component);
2749     $dir = get_plugin_directory($type, $name);
2750     if (!file_exists($dir)) {
2751         // plugin not installed, bad luck, there is no way to find the name
2752         return $component.' ???';
2753     }
2755     switch ($type) {
2756         // TODO: this is really hacky, anyway it should be probably moved to lib/pluginlib.php
2757         case 'quiz':         return get_string($name.':componentname', $component);// insane hack!!!
2758         case 'repository':   return get_string('repository', 'repository').': '.get_string('pluginname', $component);
2759         case 'gradeimport':  return get_string('gradeimport', 'grades').': '.get_string('pluginname', $component);
2760         case 'gradeexport':  return get_string('gradeexport', 'grades').': '.get_string('pluginname', $component);
2761         case 'gradereport':  return get_string('gradereport', 'grades').': '.get_string('pluginname', $component);
2762         case 'webservice':   return get_string('webservice', 'webservice').': '.get_string('pluginname', $component);
2763         case 'block':        return get_string('block').': '.get_string('pluginname', basename($component));
2764         case 'mod':
2765             if (get_string_manager()->string_exists('pluginname', $component)) {
2766                 return get_string('activity').': '.get_string('pluginname', $component);
2767             } else {
2768                 return get_string('activity').': '.get_string('modulename', $component);
2769             }
2770         default: return get_string('pluginname', $component);
2771     }
2774 /**
2775  * Gets the list of roles assigned to this context and up (parents)
2776  * from the list of roles that are visible on user profile page
2777  * and participants page.
2778  *
2779  * @param context $context
2780  * @return array
2781  */
2782 function get_profile_roles(context $context) {
2783     global $CFG, $DB;
2785     if (empty($CFG->profileroles)) {
2786         return array();
2787     }
2789     list($rallowed, $params) = $DB->get_in_or_equal(explode(',', $CFG->profileroles), SQL_PARAMS_NAMED, 'a');
2790     list($contextlist, $cparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'p');
2791     $params = array_merge($params, $cparams);
2793     $sql = "SELECT DISTINCT r.id, r.name, r.shortname, r.sortorder
2794               FROM {role_assignments} ra, {role} r
2795              WHERE r.id = ra.roleid
2796                    AND ra.contextid $contextlist
2797                    AND r.id $rallowed
2798           ORDER BY r.sortorder ASC";
2800     return $DB->get_records_sql($sql, $params);
2803 /**
2804  * Gets the list of roles assigned to this context and up (parents)
2805  *
2806  * @param context $context
2807  * @return array
2808  */
2809 function get_roles_used_in_context(context $context) {
2810     global $DB;
2812     list($contextlist, $params) = $DB->get_in_or_equal($context->get_parent_context_ids(true));
2814     $sql = "SELECT DISTINCT r.id, r.name, r.shortname, r.sortorder
2815               FROM {role_assignments} ra, {role} r
2816              WHERE r.id = ra.roleid
2817                    AND ra.contextid $contextlist
2818           ORDER BY r.sortorder ASC";
2820     return $DB->get_records_sql($sql, $params);
2823 /**
2824  * This function is used to print roles column in user profile page.
2825  * It is using the CFG->profileroles to limit the list to only interesting roles.
2826  * (The permission tab has full details of user role assignments.)
2827  *
2828  * @param int $userid
2829  * @param int $courseid
2830  * @return string
2831  */
2832 function get_user_roles_in_course($userid, $courseid) {
2833     global $CFG, $DB;
2835     if (empty($CFG->profileroles)) {
2836         return '';
2837     }
2839     if ($courseid == SITEID) {
2840         $context = context_system::instance();
2841     } else {
2842         $context = context_course::instance($courseid);
2843     }
2845     if (empty($CFG->profileroles)) {
2846         return array();
2847     }
2849     list($rallowed, $params) = $DB->get_in_or_equal(explode(',', $CFG->profileroles), SQL_PARAMS_NAMED, 'a');
2850     list($contextlist, $cparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'p');
2851     $params = array_merge($params, $cparams);
2853     $sql = "SELECT DISTINCT r.id, r.name, r.shortname, r.sortorder
2854               FROM {role_assignments} ra, {role} r
2855              WHERE r.id = ra.roleid
2856                    AND ra.contextid $contextlist
2857                    AND r.id $rallowed
2858                    AND ra.userid = :userid
2859           ORDER BY r.sortorder ASC";
2860     $params['userid'] = $userid;
2862     $rolestring = '';
2864     if ($roles = $DB->get_records_sql($sql, $params)) {
2865         foreach ($roles as $userrole) {
2866             $rolenames[$userrole->id] = $userrole->name;
2867         }
2869         $rolenames = role_fix_names($rolenames, $context);   // Substitute aliases
2871         foreach ($rolenames as $roleid => $rolename) {
2872             $rolenames[$roleid] = '<a href="'.$CFG->wwwroot.'/user/index.php?contextid='.$context->id.'&amp;roleid='.$roleid.'">'.$rolename.'</a>';
2873         }
2874         $rolestring = implode(',', $rolenames);
2875     }
2877     return $rolestring;
2880 /**
2881  * Checks if a user can assign users to a particular role in this context
2882  *
2883  * @param context $context
2884  * @param int $targetroleid - the id of the role you want to assign users to
2885  * @return boolean
2886  */
2887 function user_can_assign(context $context, $targetroleid) {
2888     global $DB;
2890     // first check if user has override capability
2891     // if not return false;
2892     if (!has_capability('moodle/role:assign', $context)) {
2893         return false;
2894     }
2895     // pull out all active roles of this user from this context(or above)
2896     if ($userroles = get_user_roles($context)) {
2897         foreach ($userroles as $userrole) {
2898             // if any in the role_allow_override table, then it's ok
2899             if ($DB->get_record('role_allow_assign', array('roleid'=>$userrole->roleid, 'allowassign'=>$targetroleid))) {
2900                 return true;
2901             }
2902         }
2903     }
2905     return false;
2908 /**
2909  * Returns all site roles in correct sort order.
2910  *
2911  * @return array
2912  */
2913 function get_all_roles() {
2914     global $DB;
2915     return $DB->get_records('role', null, 'sortorder ASC');
2918 /**
2919  * Returns roles of a specified archetype
2920  *
2921  * @param string $archetype
2922  * @return array of full role records
2923  */
2924 function get_archetype_roles($archetype) {
2925     global $DB;
2926     return $DB->get_records('role', array('archetype'=>$archetype), 'sortorder ASC');
2929 /**
2930  * Gets all the user roles assigned in this context, or higher contexts
2931  * this is mainly used when checking if a user can assign a role, or overriding a role
2932  * i.e. we need to know what this user holds, in order to verify against allow_assign and
2933  * allow_override tables
2934  *
2935  * @param context $context
2936  * @param int $userid
2937  * @param bool $checkparentcontexts defaults to true
2938  * @param string $order defaults to 'c.contextlevel DESC, r.sortorder ASC'
2939  * @return array
2940  */
2941 function get_user_roles(context $context, $userid = 0, $checkparentcontexts = true, $order = 'c.contextlevel DESC, r.sortorder ASC') {
2942     global $USER, $DB;
2944     if (empty($userid)) {
2945         if (empty($USER->id)) {
2946             return array();
2947         }
2948         $userid = $USER->id;
2949     }
2951     if ($checkparentcontexts) {
2952         $contextids = $context->get_parent_context_ids();
2953     } else {
2954         $contextids = array();
2955     }
2956     $contextids[] = $context->id;
2958     list($contextids, $params) = $DB->get_in_or_equal($contextids, SQL_PARAMS_QM);
2960     array_unshift($params, $userid);
2962     $sql = "SELECT ra.*, r.name, r.shortname
2963               FROM {role_assignments} ra, {role} r, {context} c
2964              WHERE ra.userid = ?
2965                    AND ra.roleid = r.id
2966                    AND ra.contextid = c.id
2967                    AND ra.contextid $contextids
2968           ORDER BY $order";
2970     return $DB->get_records_sql($sql ,$params);
2973 /**
2974  * Creates a record in the role_allow_override table
2975  *
2976  * @param int $sroleid source roleid
2977  * @param int $troleid target roleid
2978  * @return void
2979  */
2980 function allow_override($sroleid, $troleid) {
2981     global $DB;
2983     $record = new stdClass();
2984     $record->roleid        = $sroleid;
2985     $record->allowoverride = $troleid;
2986     $DB->insert_record('role_allow_override', $record);
2989 /**
2990  * Creates a record in the role_allow_assign table
2991  *
2992  * @param int $fromroleid source roleid
2993  * @param int $targetroleid target roleid
2994  * @return void
2995  */
2996 function allow_assign($fromroleid, $targetroleid) {
2997     global $DB;
2999     $record = new stdClass();
3000     $record->roleid      = $fromroleid;
3001     $record->allowassign = $targetroleid;
3002     $DB->insert_record('role_allow_assign', $record);
3005 /**
3006  * Creates a record in the role_allow_switch table
3007  *
3008  * @param int $fromroleid source roleid
3009  * @param int $targetroleid target roleid
3010  * @return void
3011  */
3012 function allow_switch($fromroleid, $targetroleid) {
3013     global $DB;
3015     $record = new stdClass();
3016     $record->roleid      = $fromroleid;
3017     $record->allowswitch = $targetroleid;
3018     $DB->insert_record('role_allow_switch', $record);
3021 /**
3022  * Gets a list of roles that this user can assign in this context
3023  *
3024  * @param context $context the context.
3025  * @param int $rolenamedisplay the type of role name to display. One of the
3026  *      ROLENAME_X constants. Default ROLENAME_ALIAS.
3027  * @param bool $withusercounts if true, count the number of users with each role.
3028  * @param integer|object $user A user id or object. By default (null) checks the permissions of the current user.
3029  * @return array if $withusercounts is false, then an array $roleid => $rolename.
3030  *      if $withusercounts is true, returns a list of three arrays,
3031  *      $rolenames, $rolecounts, and $nameswithcounts.
3032  */
3033 function get_assignable_roles(context $context, $rolenamedisplay = ROLENAME_ALIAS, $withusercounts = false, $user = null) {
3034     global $USER, $DB;
3036     // make sure there is a real user specified
3037     if ($user === null) {
3038         $userid = isset($USER->id) ? $USER->id : 0;
3039     } else {
3040         $userid = is_object($user) ? $user->id : $user;
3041     }
3043     if (!has_capability('moodle/role:assign', $context, $userid)) {
3044         if ($withusercounts) {
3045             return array(array(), array(), array());
3046         } else {
3047             return array();
3048         }
3049     }
3051     $parents = $context->get_parent_context_ids(true);
3052     $contexts = implode(',' , $parents);
3054     $params = array();
3055     $extrafields = '';
3056     if ($rolenamedisplay == ROLENAME_ORIGINALANDSHORT or $rolenamedisplay == ROLENAME_SHORT) {
3057         $extrafields .= ', r.shortname';
3058     }
3060     if ($withusercounts) {
3061         $extrafields = ', (SELECT count(u.id)
3062                              FROM {role_assignments} cra JOIN {user} u ON cra.userid = u.id
3063                             WHERE cra.roleid = r.id AND cra.contextid = :conid AND u.deleted = 0
3064                           ) AS usercount';
3065         $params['conid'] = $context->id;
3066     }
3068     if (is_siteadmin($userid)) {
3069         // show all roles allowed in this context to admins
3070         $assignrestriction = "";
3071     } else {
3072         $assignrestriction = "JOIN (SELECT DISTINCT raa.allowassign AS id
3073                                       FROM {role_allow_assign} raa
3074                                       JOIN {role_assignments} ra ON ra.roleid = raa.roleid
3075                                      WHERE ra.userid = :userid AND ra.contextid IN ($contexts)
3076                                    ) ar ON ar.id = r.id";
3077         $params['userid'] = $userid;
3078     }
3079     $params['contextlevel'] = $context->contextlevel;
3080     $sql = "SELECT r.id, r.name $extrafields
3081               FROM {role} r
3082               $assignrestriction
3083               JOIN {role_context_levels} rcl ON r.id = rcl.roleid
3084              WHERE rcl.contextlevel = :contextlevel
3085           ORDER BY r.sortorder ASC";
3086     $roles = $DB->get_records_sql($sql, $params);
3088     $rolenames = array();
3089     foreach ($roles as $role) {
3090         if ($rolenamedisplay == ROLENAME_SHORT) {
3091             $rolenames[$role->id] = $role->shortname;
3092             continue;
3093         }
3094         $rolenames[$role->id] = $role->name;
3095         if ($rolenamedisplay == ROLENAME_ORIGINALANDSHORT) {
3096             $rolenames[$role->id] .= ' (' . $role->shortname . ')';
3097         }
3098     }
3099     if ($rolenamedisplay != ROLENAME_ORIGINALANDSHORT and $rolenamedisplay != ROLENAME_SHORT) {
3100         $rolenames = role_fix_names($rolenames, $context, $rolenamedisplay);
3101     }
3103     if (!$withusercounts) {
3104         return $rolenames;
3105     }
3107     $rolecounts = array();
3108     $nameswithcounts = array();
3109     foreach ($roles as $role) {
3110         $nameswithcounts[$role->id] = $rolenames[$role->id] . ' (' . $roles[$role->id]->usercount . ')';
3111         $rolecounts[$role->id] = $roles[$role->id]->usercount;
3112     }
3113     return array($rolenames, $rolecounts, $nameswithcounts);
3116 /**
3117  * Gets a list of roles that this user can switch to in a context
3118  *
3119  * Gets a list of roles that this user can switch to in a context, for the switchrole menu.
3120  * This function just process the contents of the role_allow_switch table. You also need to
3121  * test the moodle/role:switchroles to see if the user is allowed to switch in the first place.
3122  *
3123  * @param context $context a context.
3124  * @return array an array $roleid => $rolename.
3125  */
3126 function get_switchable_roles(context $context) {
3127     global $USER, $DB;
3129     $params = array();
3130     $extrajoins = '';
3131     $extrawhere = '';
3132     if (!is_siteadmin()) {
3133         // Admins are allowed to switch to any role with.
3134         // Others are subject to the additional constraint that the switch-to role must be allowed by
3135         // 'role_allow_switch' for some role they have assigned in this context or any parent.
3136         $parents = $context->get_parent_context_ids(true);
3137         $contexts = implode(',' , $parents);
3139         $extrajoins = "JOIN {role_allow_switch} ras ON ras.allowswitch = rc.roleid
3140         JOIN {role_assignments} ra ON ra.roleid = ras.roleid";
3141         $extrawhere = "WHERE ra.userid = :userid AND ra.contextid IN ($contexts)";
3142         $params['userid'] = $USER->id;
3143     }
3145     $query = "
3146         SELECT r.id, r.name
3147           FROM (SELECT DISTINCT rc.roleid
3148                   FROM {role_capabilities} rc
3149                   $extrajoins
3150                   $extrawhere) idlist
3151           JOIN {role} r ON r.id = idlist.roleid
3152       ORDER BY r.sortorder";
3154     $rolenames = $DB->get_records_sql_menu($query, $params);
3155     return role_fix_names($rolenames, $context, ROLENAME_ALIAS);
3158 /**
3159  * Gets a list of roles that this user can override in this context.
3160  *
3161  * @param context $context the context.
3162  * @param int $rolenamedisplay the type of role name to display. One of the
3163  *      ROLENAME_X constants. Default ROLENAME_ALIAS.
3164  * @param bool $withcounts if true, count the number of overrides that are set for each role.
3165  * @return array if $withcounts is false, then an array $roleid => $rolename.
3166  *      if $withusercounts is true, returns a list of three arrays,
3167  *      $rolenames, $rolecounts, and $nameswithcounts.
3168  */
3169 function get_overridable_roles(context $context, $rolenamedisplay = ROLENAME_ALIAS, $withcounts = false) {
3170     global $USER, $DB;
3172     if (!has_any_capability(array('moodle/role:safeoverride', 'moodle/role:override'), $context)) {
3173         if ($withcounts) {
3174             return array(array(), array(), array());
3175         } else {
3176             return array();
3177         }
3178     }
3180     $parents = $context->get_parent_context_ids(true);
3181     $contexts = implode(',' , $parents);
3183     $params = array();
3184     $extrafields = '';
3185     if ($rolenamedisplay == ROLENAME_ORIGINALANDSHORT) {
3186         $extrafields .= ', ro.shortname';
3187     }
3189     $params['userid'] = $USER->id;
3190     if ($withcounts) {
3191         $extrafields = ', (SELECT COUNT(rc.id) FROM {role_capabilities} rc
3192                 WHERE rc.roleid = ro.id AND rc.contextid = :conid) AS overridecount';
3193         $params['conid'] = $context->id;
3194     }
3196     if (is_siteadmin()) {
3197         // show all roles to admins
3198         $roles = $DB->get_records_sql("
3199             SELECT ro.id, ro.name$extrafields
3200               FROM {role} ro
3201           ORDER BY ro.sortorder ASC", $params);
3203     } else {
3204         $roles = $DB->get_records_sql("
3205             SELECT ro.id, ro.name$extrafields
3206               FROM {role} ro
3207               JOIN (SELECT DISTINCT r.id
3208                       FROM {role} r
3209                       JOIN {role_allow_override} rao ON r.id = rao.allowoverride
3210                       JOIN {role_assignments} ra ON rao.roleid = ra.roleid
3211                      WHERE ra.userid = :userid AND ra.contextid IN ($contexts)
3212                    ) inline_view ON ro.id = inline_view.id
3213           ORDER BY ro.sortorder ASC", $params);
3214     }
3216     $rolenames = array();
3217     foreach ($roles as $role) {
3218         $rolenames[$role->id] = $role->name;
3219         if ($rolenamedisplay == ROLENAME_ORIGINALANDSHORT) {
3220             $rolenames[$role->id] .= ' (' . $role->shortname . ')';
3221         }
3222     }
3223     if ($rolenamedisplay != ROLENAME_ORIGINALANDSHORT) {
3224         $rolenames = role_fix_names($rolenames, $context, $rolenamedisplay);
3225     }
3227     if (!$withcounts) {
3228         return $rolenames;
3231     $rolecounts = array();
3232     $nameswithcounts = array();
3233     foreach ($roles as $role) {
3234         $nameswithcounts[$role->id] = $rolenames[$role->id] . ' (' . $roles[$role->id]->overridecount . ')';
3235         $rolecounts[$role->id] = $roles[$role->id]->overridecount;
3236     }
3237     return array($rolenames, $rolecounts, $nameswithcounts);
3240 /**
3241  * Create a role menu suitable for default role selection in enrol plugins.
3242  * @param context $context
3243  * @param int $addroleid current or default role - always added to list
3244  * @return array roleid=>localised role name
3245  */
3246 function get_default_enrol_roles(context $context, $addroleid = null) {
3247     global $DB;
3249     $params = array('contextlevel'=>CONTEXT_COURSE);
3250     if ($addroleid) {
3251         $addrole = "OR r.id = :addroleid";
3252         $params['addroleid'] = $addroleid;
3253     } else {
3254         $addrole = "";
3255     }
3256     $sql = "SELECT r.id, r.name
3257               FROM {role} r
3258          LEFT JOIN {role_context_levels} rcl ON (rcl.roleid = r.id AND rcl.contextlevel = :contextlevel)
3259              WHERE rcl.id IS NOT NULL $addrole
3260           ORDER BY sortorder DESC";
3262     $roles = $DB->get_records_sql_menu($sql, $params);
3263     $roles = role_fix_names($roles, $context, ROLENAME_BOTH);
3265     return $roles;
3268 /**
3269  * Return context levels where this role is assignable.
3270  * @param integer $roleid the id of a role.
3271  * @return array list of the context levels at which this role may be assigned.
3272  */
3273 function get_role_contextlevels($roleid) {
3274     global $DB;
3275     return $DB->get_records_menu('role_context_levels', array('roleid' => $roleid),
3276             'contextlevel', 'id,contextlevel');
3279 /**
3280  * Return roles suitable for assignment at the specified context level.
3281  *
3282  * NOTE: this function name looks like a typo, should be probably get_roles_for_contextlevel()
3283  *
3284  * @param integer $contextlevel a contextlevel.
3285  * @return array list of role ids that are assignable at this context level.
3286  */
3287 function get_roles_for_contextlevels($contextlevel) {
3288     global $DB;
3289     return $DB->get_records_menu('role_context_levels', array('contextlevel' => $contextlevel),
3290             '', 'id,roleid');
3293 /**
3294  * Returns default context levels where roles can be assigned.
3295  *
3296  * @param string $rolearchetype one of the role archetypes - that is, one of the keys
3297  *      from the array returned by get_role_archetypes();
3298  * @return array list of the context levels at which this type of role may be assigned by default.
3299  */
3300 function get_default_contextlevels($rolearchetype) {
3301     static $defaults = array(
3302         'manager'        => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
3303         'coursecreator'  => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
3304         'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3305         'teacher'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3306         'student'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3307         'guest'          => array(),
3308         'user'           => array(),
3309         'frontpage'      => array());
3311     if (isset($defaults[$rolearchetype])) {
3312         return $defaults[$rolearchetype];
3313     } else {
3314         return array();
3315     }
3318 /**
3319  * Set the context levels at which a particular role can be assigned.
3320  * Throws exceptions in case of error.
3321  *
3322  * @param integer $roleid the id of a role.
3323  * @param array $contextlevels the context levels at which this role should be assignable,
3324  *      duplicate levels are removed.
3325  * @return void
3326  */
3327 function set_role_contextlevels($roleid, array $contextlevels) {
3328     global $DB;
3329     $DB->delete_records('role_context_levels', array('roleid' => $roleid));
3330     $rcl = new stdClass();
3331     $rcl->roleid = $roleid;
3332     $contextlevels = array_unique($contextlevels);
3333     foreach ($contextlevels as $level) {
3334         $rcl->contextlevel = $level;
3335         $DB->insert_record('role_context_levels', $rcl, false, true);
3336     }
3339 /**
3340  * Who has this capability in this context?
3341  *
3342  * This can be a very expensive call - use sparingly and keep
3343  * the results if you are going to need them again soon.
3344  *
3345  * Note if $fields is empty this function attempts to get u.*
3346  * which can get rather large - and has a serious perf impact
3347  * on some DBs.
3348  *
3349  * @param context $context
3350  * @param string|array $capability - capability name(s)
3351  * @param string $fields - fields to be pulled. The user table is aliased to 'u'. u.id MUST be included.
3352  * @param string $sort - the sort order. Default is lastaccess time.
3353  * @param mixed $limitfrom - number of records to skip (offset)
3354  * @param mixed $limitnum - number of records to fetch
3355  * @param string|array $groups - single group or array of groups - only return
3356  *               users who are in one of these group(s).
3357  * @param string|array $exceptions - list of users to exclude, comma separated or array
3358  * @param bool $doanything_ignored not used any more, admin accounts are never returned
3359  * @param bool $view_ignored - use get_enrolled_sql() instead
3360  * @param bool $useviewallgroups if $groups is set the return users who
3361  *               have capability both $capability and moodle/site:accessallgroups
3362  *               in this context, as well as users who have $capability and who are
3363  *               in $groups.
3364  * @return mixed
3365  */
3366 function get_users_by_capability(context $context, $capability, $fields = '', $sort = '', $limitfrom = '', $limitnum = '',
3367                                  $groups = '', $exceptions = '', $doanything_ignored = null, $view_ignored = null, $useviewallgroups = false) {
3368     global $CFG, $DB;
3370     $defaultuserroleid      = isset($CFG->defaultuserroleid) ? $CFG->defaultuserroleid : 0;
3371     $defaultfrontpageroleid = isset($CFG->defaultfrontpageroleid) ? $CFG->defaultfrontpageroleid : 0;
3373     $ctxids = trim($context->path, '/');
3374     $ctxids = str_replace('/', ',', $ctxids);
3376     // Context is the frontpage
3377     $iscoursepage = false; // coursepage other than fp
3378     $isfrontpage = false;
3379     if ($context->contextlevel == CONTEXT_COURSE) {
3380         if ($context->instanceid == SITEID) {
3381             $isfrontpage = true;
3382         } else {
3383             $iscoursepage = true;
3384         }
3385     }
3386     $isfrontpage = ($isfrontpage || is_inside_frontpage($context));
3388     $caps = (array)$capability;
3390     // construct list of context paths bottom-->top
3391     list($contextids, $paths) = get_context_info_list($context);
3393     // we need to find out all roles that have these capabilities either in definition or in overrides
3394     $defs = array();
3395     list($incontexts, $params) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED, 'con');
3396     list($incaps, $params2) = $DB->get_in_or_equal($caps, SQL_PARAMS_NAMED, 'cap');
3397     $params = array_merge($params, $params2);
3398     $sql = "SELECT rc.id, rc.roleid, rc.permission, rc.capability, ctx.path
3399               FROM {role_capabilities} rc
3400               JOIN {context} ctx on rc.contextid = ctx.id
3401              WHERE rc.contextid $incontexts AND rc.capability $incaps";
3403     $rcs = $DB->get_records_sql($sql, $params);
3404     foreach ($rcs as $rc) {
3405         $defs[$rc->capability][$rc->path][$rc->roleid] = $rc->permission;
3406     }
3408     // go through the permissions bottom-->top direction to evaluate the current permission,
3409     // first one wins (prohibit is an exception that always wins)
3410     $access = array();
3411     foreach ($caps as $cap) {
3412         foreach ($paths as $path) {
3413             if (empty($defs[$cap][$path])) {
3414                 continue;
3415             }
3416             foreach($defs[$cap][$path] as $roleid => $perm) {
3417                 if ($perm == CAP_PROHIBIT) {
3418                     $access[$cap][$roleid] = CAP_PROHIBIT;
3419                     continue;
3420                 }
3421                 if (!isset($access[$cap][$roleid])) {
3422                     $access[$cap][$roleid] = (int)$perm;
3423                 }
3424             }
3425         }
3426     }
3428     // make lists of roles that are needed and prohibited in this context
3429     $needed = array(); // one of these is enough
3430     $prohibited = array(); // must not have any of these
3431     foreach ($caps as $cap) {
3432         if (empty($access[$cap])) {
3433             continue;
3434         }
3435         foreach ($access[$cap] as $roleid => $perm) {
3436             if ($perm == CAP_PROHIBIT) {
3437                 unset($needed[$cap][$roleid]);
3438                 $prohibited[$cap][$roleid] = true;
3439             } else if ($perm == CAP_ALLOW and empty($prohibited[$cap][$roleid])) {
3440                 $needed[$cap][$roleid] = true;
3441             }
3442         }
3443         if (empty($needed[$cap]) or !empty($prohibited[$cap][$defaultuserroleid])) {
3444             // easy, nobody has the permission
3445             unset($needed[$cap]);
3446             unset($prohibited[$cap]);
3447         } else if ($isfrontpage and !empty($prohibited[$cap][$defaultfrontpageroleid])) {
3448             // everybody is disqualified on the frontapge
3449             unset($needed[$cap]);
3450             unset($prohibited[$cap]);
3451         }
3452         if (empty($prohibited[$cap])) {
3453             unset($prohibited[$cap]);
3454         }
3455     }
3457     if (empty($needed)) {
3458         // there can not be anybody if no roles match this request
3459         return array();
3460     }
3462     if (empty($prohibited)) {
3463         // we can compact the needed roles
3464         $n = array();
3465         foreach ($needed as $cap) {
3466             foreach ($cap as $roleid=>$unused) {
3467                 $n[$roleid] = true;
3468             }
3469         }
3470         $needed = array('any'=>$n);
3471         unset($n);
3472     }
3474     /// ***** Set up default fields ******
3475     if (empty($fields)) {
3476         if ($iscoursepage) {
3477             $fields = 'u.*, ul.timeaccess AS lastaccess';
3478         } else {
3479             $fields = 'u.*';
3480         }
3481     } else {
3482         if (debugging('', DEBUG_DEVELOPER) && strpos($fields, 'u.*') === false && strpos($fields, 'u.id') === false) {
3483             debugging('u.id must be included in the list of fields passed to get_users_by_capability().', DEBUG_DEVELOPER);
3484         }
3485     }
3487     /// Set up default sort
3488     if (empty($sort)) { // default to course lastaccess or just lastaccess
3489         if ($iscoursepage) {
3490             $sort = 'ul.timeaccess';
3491         } else {
3492             $sort = 'u.lastaccess';
3493         }
3494     }
3496     // Prepare query clauses
3497     $wherecond = array();
3498     $params    = array();
3499     $joins     = array();
3501     // User lastaccess JOIN
3502     if ((strpos($sort, 'ul.timeaccess') === false) and (strpos($fields, 'ul.timeaccess') === false)) {
3503          // user_lastaccess is not required MDL-13810
3504     } else {
3505         if ($iscoursepage) {
3506             $joins[] = "LEFT OUTER JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = {$context->instanceid})";
3507         } else {
3508             throw new coding_exception('Invalid sort in get_users_by_capability(), ul.timeaccess allowed only for course contexts.');
3509         }
3510     }
3512     /// We never return deleted users or guest account.
3513     $wherecond[] = "u.deleted = 0 AND u.id <> :guestid";
3514     $params['guestid'] = $CFG->siteguest;
3516     /// Groups
3517     if ($groups) {
3518         $groups = (array)$groups;
3519         list($grouptest, $grpparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED, 'grp');
3520         $grouptest = "u.id IN (SELECT userid FROM {groups_members} gm WHERE gm.groupid $grouptest)";
3521         $params = array_merge($params, $grpparams);
3523         if ($useviewallgroups) {
3524             $viewallgroupsusers = get_users_by_capability($context, 'moodle/site:accessallgroups', 'u.id, u.id', '', '', '', '', $exceptions);
3525             if (!empty($viewallgroupsusers)) {
3526                 $wherecond[] =  "($grouptest OR u.id IN (" . implode(',', array_keys($viewallgroupsusers)) . '))';
3527             } else {
3528                 $wherecond[] =  "($grouptest)";
3529             }
3530         } else {
3531             $wherecond[] =  "($grouptest)";
3532         }
3533     }
3535     /// User exceptions
3536     if (!empty($exceptions)) {
3537         $exceptions = (array)$exceptions;
3538         list($exsql, $exparams) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'exc', false);
3539         $params = array_merge($params, $exparams);
3540         $wherecond[] = "u.id $exsql";
3541     }
3543     // now add the needed and prohibited roles conditions as joins
3544     if (!empty($needed['any'])) {
3545         // simple case - there are no prohibits involved
3546         if (!empty($needed['any'][$defaultuserroleid]) or ($isfrontpage and !empty($needed['any'][$defaultfrontpageroleid]))) {
3547             // everybody
3548         } else {
3549             $joins[] = "JOIN (SELECT DISTINCT userid
3550                                 FROM {role_assignments}
3551                                WHERE contextid IN ($ctxids)
3552                                      AND roleid IN (".implode(',', array_keys($needed['any'])) .")
3553                              ) ra ON ra.userid = u.id";
3554         }
3555     } else {
3556         $unions = array();
3557         $everybody = false;
3558         foreach ($needed as $cap=>$unused) {
3559             if (empty($prohibited[$cap])) {
3560                 if (!empty($needed[$cap][$defaultuserroleid]) or ($isfrontpage and !empty($needed[$cap][$defaultfrontpageroleid]))) {
3561                     $everybody = true;
3562                     break;
3563                 } else {
3564                     $unions[] = "SELECT userid
3565                                    FROM {role_assignments}
3566                                   WHERE contextid IN ($ctxids)
3567                                         AND roleid IN (".implode(',', array_keys($needed[$cap])) .")";
3568                 }
3569             } else {
3570                 if (!empty($prohibited[$cap][$defaultuserroleid]) or ($isfrontpage and !empty($prohibited[$cap][$defaultfrontpageroleid]))) {
3571                     // nobody can have this cap because it is prevented in default roles
3572                     continue;
3574                 } else if (!empty($needed[$cap][$defaultuserroleid]) or ($isfrontpage and !empty($needed[$cap][$defaultfrontpageroleid]))) {
3575                     // everybody except the prohibitted - hiding does not matter
3576                     $unions[] = "SELECT id AS userid
3577                                    FROM {user}
3578                                   WHERE id NOT IN (SELECT userid
3579                                                      FROM {role_assignments}
3580                                                     WHERE contextid IN ($ctxids)
3581                                                           AND roleid IN (".implode(',', array_keys($prohibited[$cap])) ."))";
3583                 } else {
3584                     $unions[] = "SELECT userid
3585                                    FROM {role_assignments}
3586                                   WHERE contextid IN ($ctxids)
3587                                         AND roleid IN (".implode(',', array_keys($needed[$cap])) .")
3588                                         AND roleid NOT IN (".implode(',', array_keys($prohibited[$cap])) .")";
3589                 }
3590             }
3591         }
3592         if (!$everybody) {
3593             if ($unions) {
3594                 $joins[] = "JOIN (SELECT DISTINCT userid FROM ( ".implode(' UNION ', $unions)." ) us) ra ON ra.userid = u.id";
3595             } else {
3596                 // only prohibits found - nobody can be matched
3597                 $wherecond[] = "1 = 2";
3598             }
3599         }
3600     }
3602     // Collect WHERE conditions and needed joins
3603     $where = implode(' AND ', $wherecond);
3604     if ($where !== '') {
3605         $where = 'WHERE ' . $where;
3606     }
3607     $joins = implode("\n", $joins);
3609     /// Ok, let's get the users!
3610     $sql = "SELECT $fields
3611               FROM {user} u
3612             $joins
3613             $where
3614           ORDER BY $sort";
3616     return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
3619 /**
3620  * Re-sort a users array based on a sorting policy
3621  *
3622  * Will re-sort a $users results array (from get_users_by_capability(), usually)
3623  * based on a sorting policy. This is to support the odd practice of
3624  * sorting teachers by 'authority', where authority was "lowest id of the role
3625  * assignment".
3626  *
3627  * Will execute 1 database query. Only suitable for small numbers of users, as it
3628  * uses an u.id IN() clause.
3629  *
3630  * Notes about the sorting criteria.
3631  *
3632  * As a default, we cannot rely on role.sortorder because then
3633  * admins/coursecreators will always win. That is why the sane
3634  * rule "is locality matters most", with sortorder as 2nd
3635  * consideration.
3636  *
3637  * If you want role.sortorder, use the 'sortorder' policy, and
3638  * name explicitly what roles you want to cover. It's probably
3639  * a good idea to see what roles have the capabilities you want
3640  * (array_diff() them against roiles that have 'can-do-anything'
3641  * to weed out admin-ish roles. Or fetch a list of roles from
3642  * variables like $CFG->coursecontact .
3643  *
3644  * @param array $users Users array, keyed on userid
3645  * @param context $context
3646  * @param array $roles ids of the roles to include, optional
3647  * @param string $sortpolicy defaults to locality, more about
3648  * @return array sorted copy of the array
3649  */
3650 function sort_by_roleassignment_authority($users, context $context, $roles = array(), $sortpolicy = 'locality') {
3651     global $DB;
3653     $userswhere = ' ra.userid IN (' . implode(',',array_keys($users)) . ')';
3654     $contextwhere = 'AND ra.contextid IN ('.str_replace('/', ',',substr($context->path, 1)).')';
3655     if (empty($roles)) {
3656       &