From 176058849add80db906f44edff04ed6c5d41f75f Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Thu, 6 Dec 2007 03:57:05 +0000 Subject: [PATCH] MDL-12451, in get_users_by_capability(), defaultroleid bit fixes --- lib/accesslib.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 65f365caf2..a2b9ac63b3 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -4212,14 +4212,26 @@ function get_users_by_capability($context, $capability, $fields='', $sort='', $view=false, $useviewallgroups=false) { global $CFG; +/// Sorting out exceptions + $exceptionsql = $exceptions ? "AND u.id NOT IN ($exceptions)" : ''; + /// check for front page course, and see if default front page role has the required capability +/// if it does, we can just return all users, and we do not need to check further, just return all users - // TODO This bit will break if $fields, $sort or sort are not passed in, and also it ignores $exceptions. There may be other problems too. $frontpagectx = get_context_instance(CONTEXT_COURSE, SITEID); if (!empty($CFG->defaultfrontpageroleid) && ($context->id == $frontpagectx->id || strstr($context->path, '/'.$frontpagectx->id.'/'))) { + $roles = get_roles_with_capability($capability, CAP_ALLOW, $context); + // if this condition is satisfied, then everyone is selected, no need to check further if (in_array($CFG->defaultfrontpageroleid, array_keys($roles))) { - return get_records_sql("SELECT $fields FROM {$CFG->prefix}user u ORDER BY $sort", $limitfrom, $limitnum); + if (empty($fields)) { + $fields = 'u.*'; + } + + if (empty($sort)) { + $sort = 'u.lastaccess'; + } + return get_records_sql("SELECT $fields FROM {$CFG->prefix}user u $exceptionsql ORDER BY $sort", $limitfrom, $limitnum); } } @@ -4245,9 +4257,6 @@ function get_users_by_capability($context, $capability, $fields='', $sort='', $groupsql = ''; } -/// Sorting out exceptions - $exceptionsql = $exceptions ? "AND u.id NOT IN ($exceptions)" : ''; - /// Set up default fields if (empty($fields)) { $fields = 'u.*, ul.timeaccess as lastaccess, ra.hidden'; -- 2.39.5