Skip to content

Commit

Permalink
MDL-10888: groupings - added some caching of repetative queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mattc-catalyst committed Aug 23, 2007
1 parent 74df435 commit e0bc99e
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/grouplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ function groups_is_member($groupid, $userid=null) {
*/
function groups_has_membership($cm, $userid=null) {
global $CFG, $USER;


static $cache = array();

// groupings are ignored when not enabled
if (empty($CFG->enablegroupings)) {
$cm->groupingid = 0;
Expand All @@ -146,6 +148,11 @@ function groups_has_membership($cm, $userid=null) {
$userid = $USER->id;
}

$cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
if (isset($cache[$cachekey])) {
return($cache[$cachekey]);
}

if ($cm->groupingid) {
// find out if member of any group in selected activity grouping
$sql = "SELECT 'x'
Expand All @@ -158,8 +165,10 @@ function groups_has_membership($cm, $userid=null) {
FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g
WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}";
}

return record_exists_sql($sql);

$cache[$cachekey] = record_exists_sql($sql);

return $cache[$cachekey];
}

/**
Expand Down Expand Up @@ -350,10 +359,7 @@ function groups_course_module_visible($cm, $userid=null) {
if (empty($cm->groupmembersonly)) {
return(true);
}
if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
return(true);
}
if (groups_has_membership($cm, $userid)) {
if (groups_has_membership($cm, $userid) || has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
return(true);
}
return(false);
Expand Down

0 comments on commit e0bc99e

Please sign in to comment.