Skip to content

Commit

Permalink
MDL-62440 participants: out-of-memory is many site-wide role assigns
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed May 17, 2018
1 parent 22744b7 commit 5359c51
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2702,14 +2702,24 @@ function get_archetype_roles($archetype) {
/**
* Gets all the user roles assigned in this context, or higher contexts for a list of users.
*
* If you try using the combination $userids = [], $checkparentcontexts = true then this is likely
* to cause an out-of-memory error on large Moodle sites, so this combination is deprecated and
* outputs a warning, even though it is the default.
*
* @param context $context
* @param array $userids. An empty list means fetch all role assignments for the context.
* @param bool $checkparentcontexts defaults to true
* @param string $order defaults to 'c.contextlevel DESC, r.sortorder ASC'
* @return array
*/
function get_users_roles(context $context, $userids = [], $checkparentcontexts = true, $order = 'c.contextlevel DESC, r.sortorder ASC') {
global $USER, $DB;
global $DB;

if (!$userids && $checkparentcontexts) {
debugging('Please do not call get_users_roles() with $checkparentcontexts = true ' .
'and $userids array not set. This combination causes large Moodle sites ' .
'with lots of site-wide role assignemnts to run out of memory.', DEBUG_DEVELOPER);
}

if ($checkparentcontexts) {
$contextids = $context->get_parent_context_ids();
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/accesslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ public function test_get_user_roles() {

$u2roles = get_user_roles($coursecontext, $user2->id);

$allroles = get_users_roles($coursecontext);
$allroles = get_users_roles($coursecontext, [], false);
$specificuserroles = get_users_roles($coursecontext, [$user1->id, $user2->id]);
$this->assertEquals($u1roles, $allroles[$user1->id]);
$this->assertEquals($u1roles, $specificuserroles[$user1->id]);
Expand Down
15 changes: 13 additions & 2 deletions user/classes/participants_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ public function __construct($courseid, $currentgroup, $accesssince, $roleid, $en
$this->groups = groups_get_all_groups($courseid, 0, 0, 'g.*', true);
}
$this->allroles = role_fix_names(get_all_roles($this->context), $this->context);
$this->allroleassignments = get_users_roles($this->context, [], true, 'c.contextlevel DESC, r.sortorder ASC');
$this->assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false);
$this->profileroles = get_profile_roles($this->context);
$this->viewableroles = get_viewable_roles($this->context);
Expand Down Expand Up @@ -446,9 +445,21 @@ public function query_db($pagesize, $useinitialsbar = true) {
$sort = 'ORDER BY ' . $sort;
}

$this->rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
$rawdata = user_get_participants($this->course->id, $this->currentgroup, $this->accesssince,
$this->roleid, $this->enrolid, $this->status, $this->search, $twhere, $tparams, $sort, $this->get_page_start(),
$this->get_page_size());
$this->rawdata = [];
foreach ($rawdata as $user) {
$this->rawdata[$user->id] = $user;
}
$rawdata->close();

if ($this->rawdata) {
$this->allroleassignments = get_users_roles($this->context, array_keys($this->rawdata),
true, 'c.contextlevel DESC, r.sortorder ASC');
} else {
$this->allroleassignments = [];
}

// Set initial bars.
if ($useinitialsbar) {
Expand Down

0 comments on commit 5359c51

Please sign in to comment.