From b5a645b4ebd89bf1c305cf01b7eabe4ff556b042 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 07:05:21 +0000 Subject: [PATCH] accesslib: Draft get_context_users_byrole() 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!} --- lib/accesslib.php | 76 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index c519e8d604..d31f6be9e8 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -687,9 +687,6 @@ function get_user_courses_bycap($userid, $cap, $sess, $doanything, $sort='c.sort $basefields = array('id', 'sortorder', 'shortname', 'idnumber'); if (!is_null($fields)) { - if (!is_array($fields)) { - error_log(print_r($fields,1)); - } $fields = array_merge($basefields, $fields); $fields = array_unique($fields); } else { @@ -791,6 +788,75 @@ function get_user_courses_bycap($userid, $cap, $sess, $doanything, $sort='c.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.' .join(',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 + 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 + if ($rs->RecordCount()) { + while ($u = rs_fetch_next_record($rs)) { + // build the context obj + $ctx = new StdClass; + $ctx->id = $u->ctxid; unset($u->ctxid); + $ctx->path = $u->ctxpath; unset($u->ctxpath); + $ctx->depth = $u->ctxdepth; unset($u->ctxdepth); + $ctx->instanceid = $u->id; + $ctx->contextlevel = CONTEXT_USER; + $u->context = $ctx; + $users[] = $u; + if ($limit > 0 && $cc++ > $limit) { + break; + } + } + } + rs_close($rs); + return $users; +} + /** * This function returns whether the current user has the capability of performing a function * For example, we can do has_capability('mod/forum:replypost',$context) in forum @@ -1604,7 +1670,9 @@ function get_user_access_sitewide($userid) { $acc['rdef'][$k][$ra->capability] = $ra->permission; } $parentids = explode('/', $ra->path); - array_pop($parentids); array_shift($parentids); + array_shift($parentids); // drop empty leading "context" + array_pop($parentids); // drop _this_ context + if (isset($raparents[$ra->roleid])) { $raparents[$ra->roleid] = array_merge($raparents[$ra->roleid], $parentids); } else { -- 2.39.5