Skip to content

Commit

Permalink
MDL-45678 assignsubmission_comments: Improve performance for permissi…
Browse files Browse the repository at this point in the history
…ons checking
  • Loading branch information
Damien Bezborodov committed Jul 4, 2014
1 parent fc415ee commit 0ba5f02
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions lang/en/cache.php
Expand Up @@ -46,6 +46,7 @@
$string['cachedef_databasemeta'] = 'Database meta information';
$string['cachedef_eventinvalidation'] = 'Event invalidation';
$string['cachedef_externalbadges'] = 'External badges for particular user';
$string['cachedef_get_suspended_userids'] = 'List of suspended user identifiers per course';
$string['cachedef_gradecondition'] = 'User grades cached for evaluating conditional availability';
$string['cachedef_groupdata'] = 'Course group information';
$string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content';
Expand Down
19 changes: 18 additions & 1 deletion lib/accesslib.php
Expand Up @@ -7460,11 +7460,21 @@ function extract_suspended_users($context, &$users, $ignoreusers=array()) {
* or enrolment has expired or not started.
*
* @param context $context context in which user enrolment is checked.
* @param bool $context Enable or disable (default) the request cache
* @return array list of suspended user id's.
*/
function get_suspended_userids($context){
function get_suspended_userids(context $context, $usecache = false) {
global $DB;

// Check the cache first for performance reasons if enabled.
if ($usecache) {
$cache = cache::make('core', 'get_suspended_userids');
$susers = $cache->get($context->id);
if ($susers !== false) {
return $susers;
}
}

// Get all enrolled users.
list($sql, $params) = get_enrolled_sql($context);
$users = $DB->get_records_sql($sql, $params);
Expand All @@ -7481,5 +7491,12 @@ function get_suspended_userids($context){
}
}
}

// Cache results for the remainder of this request.
if ($usecache) {
$cache->set($context->id, $susers);
}

// Return.
return $susers;
}
9 changes: 8 additions & 1 deletion lib/db/caches.php
Expand Up @@ -221,5 +221,12 @@
'mode' => cache_store::MODE_SESSION,
'simplekeys' => true,
'simpledata' => true
)
),

// For the function get_suspended_userids() in core_access.
'get_suspended_userids' => array(
'mode' => cache_store::MODE_REQUEST,
'simplekeys' => true,
'simpledata' => true,
),
);
3 changes: 3 additions & 0 deletions lib/enrollib.php
Expand Up @@ -1406,6 +1406,9 @@ public function update_user_enrol(stdClass $instance, $userid, $status = NULL, $
$DB->update_record('user_enrolments', $ue);
context_course::instance($instance->courseid)->mark_dirty(); // reset enrol caches

// Invalidate core_access cache for get_suspended_userids.
cache_helper::invalidate_by_definition('core', 'get_suspended_userids', array(), array($instance->courseid));

// Trigger event.
$event = \core\event\user_enrolment_updated::create(
array(
Expand Down
8 changes: 1 addition & 7 deletions mod/assign/locallib.php
Expand Up @@ -132,9 +132,6 @@ class assign {
/** @var bool whether to exclude users with inactive enrolment */
private $showonlyactiveenrol = null;

/** @var array list of suspended user IDs in form of ([id1] => id1) */
public $susers = null;

/** @var array cached list of participants for this assignment. The cache key will be group, showactive and the context id */
private $participants = array();

Expand Down Expand Up @@ -6958,10 +6955,7 @@ public function show_only_active_users() {
* @return bool true is user is active in course.
*/
public function is_active_user($userid) {
if (is_null($this->susers) && !is_null($this->context)) {
$this->susers = get_suspended_userids($this->context);
}
return !in_array($userid, $this->susers);
return !in_array($userid, get_suspended_userids($this->context, true));
}

/**
Expand Down

0 comments on commit 0ba5f02

Please sign in to comment.