From db70c4bdf3b4627acc1a29facfc1705cbc5783c4 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 29 Apr 2008 06:53:19 +0000 Subject: [PATCH] MDL-13261 Merged the removal of get_context_users_bycap and get_context_users_byrole from accesslib --- lib/accesslib.php | 183 +--------------------------------------------- 1 file changed, 3 insertions(+), 180 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 6dfaf69101..22b7c477e2 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -43,10 +43,9 @@ * What courses has this user access to? * - get_user_courses_bycap() * - * What users can do X in this course or context? - * - get_context_users_bycap() - * - get_context_users_byrole() - * + * What users can do X in this context? + * - get_users_by_capability() + * * Enrol/unenrol * - enrol_into_course() * - role_assign()/role_unassign() @@ -1043,182 +1042,6 @@ function get_user_courses_bycap($userid, $cap, $accessdata, $doanything, $sort=' return $courses; } -/** - * Draft - use for the course participants list page - * - * Uses 1 DB query (cheap too - 2~7ms). - * - * TODO: - * - implement additional where clauses - * - sorting - * - get course participants list to use it! - * - * returns a users array, both sorted _and_ keyed - * on id (as get_my_courses() does) - * - * as a bonus, every user record comes with its own - * personal context, as our callers need it straight away - * {save 1 dbquery per user! yay!} - * - */ -function get_context_users_byrole ($context, $roleid, $fields=NULL, $where=NULL, $sort=NULL, $limit=0) { - - global $CFG; - // Slim base fields, let callers ask for what they need... - $basefields = array('id', 'username'); - - if (!is_null($fields)) { - $fields = array_merge($basefields, $fields); - $fields = array_unique($fields); - } else { - $fields = $basefields; - } - $userfields = 'u.' .implode(',u.', $fields); - - $contexts = substr($context->path, 1); // kill leading slash - $contexts = str_replace('/', ',', $contexts); - - $sql = "SELECT $userfields, - ctx.id AS ctxid, ctx.path AS ctxpath, - ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel - FROM {$CFG->prefix}user u - JOIN {$CFG->prefix}context ctx - ON (u.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_USER.") - JOIN {$CFG->prefix}role_assignments ra - ON u.id = ra.userid - WHERE ra.roleid = $roleid - AND ra.contextid IN ($contexts)"; - - $rs = get_recordset_sql($sql); - - $users = array(); - $cc = 0; // keep count - while ($u = rs_fetch_next_record($rs)) { - // build the context obj - $u = make_context_subobj($u); - - $users[] = $u; - if ($limit > 0 && $cc++ > $limit) { - break; - } - } - rs_close($rs); - return $users; -} - -/** - * Draft - use for the course participants list page - * - * Uses 2 fast DB queries - * - * TODO: - * - automagically exclude roles that can-doanything sitewide (See callers) - * - perhaps also allow sitewide do-anything via flag - * - implement additional where clauses - * - sorting - * - get course participants list to use it! - * - * returns a users array, both sorted _and_ keyed - * on id (as get_my_courses() does) - * - * as a bonus, every user record comes with its own - * personal context, as our callers need it straight away - * {save 1 dbquery per user! yay!} - * - */ -function get_context_users_bycap ($context, $capability='moodle/course:view', $fields=NULL, $where=NULL, $sort=NULL, $limit=0) { - global $CFG; - - // Plan - // - // - Get all the *interesting* roles -- those that - // have some rolecap entry in our ctx.path contexts - // - // - Get all RAs for any of those roles in any of our - // interesting contexts, with userid & perm data - // in a nice (per user?) order - // - // - Walk the resultset, computing the permissions - // - actually - this is all a SQL subselect - // - // - Fetch user records against the subselect - // - - // Slim base fields, let callers ask for what they need... - $basefields = array('id', 'username'); - - if (!is_null($fields)) { - $fields = array_merge($basefields, $fields); - $fields = array_unique($fields); - } else { - $fields = $basefields; - } - $userfields = 'u.' .implode(',u.', $fields); - - $contexts = substr($context->path, 1); // kill leading slash - $contexts = str_replace('/', ',', $contexts); - - $roles = array(); - $sql = "SELECT DISTINCT rc.roleid - FROM {$CFG->prefix}role_capabilities rc - WHERE rc.capability = '$capability' - AND rc.contextid IN ($contexts)"; - $rs = get_recordset_sql($sql); - while ($u = rs_fetch_next_record($rs)) { - $roles[] = $u->roleid; - } - rs_close($rs); - $roles = implode(',', $roles); - - if (empty($roles)) { - return array(); - } - - // - // User permissions subselect SQL - // - // - the open join condition to - // role_capabilities - // - // - because both rc and ra entries are - // _at or above_ our context, we don't care - // about their depth, we just need to sum them - // - $sql = "SELECT ra.userid, SUM(rc.permission) AS permission - FROM {$CFG->prefix}role_assignments ra - JOIN {$CFG->prefix}role_capabilities rc - ON (ra.roleid = rc.roleid AND rc.contextid IN ($contexts)) - WHERE ra.contextid IN ($contexts) - AND ra.roleid IN ($roles) - GROUP BY ra.userid"; - - // Get users - $sql = "SELECT $userfields, - ctx.id AS ctxid, ctx.path AS ctxpath, - ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel - FROM {$CFG->prefix}user u - JOIN {$CFG->prefix}context ctx - ON (u.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_USER.") - JOIN ($sql) up - ON u.id = up.userid - WHERE up.permission > 0 AND u.username != 'guest'"; - - $rs = get_recordset_sql($sql); - - $users = array(); - $cc = 0; // keep count - while ($u = rs_fetch_next_record($rs)) { - // build the context obj - $u = make_context_subobj($u); - - $users[] = $u; - if ($limit > 0 && $cc++ > $limit) { - break; - } - } - rs_close($rs); - return $users; -} /** * It will return a nested array showing role assignments -- 2.39.5