Skip to content

Commit

Permalink
Merge branch 'MDL-43721-25' of git://github.com/damyon/moodle into MO…
Browse files Browse the repository at this point in the history
…ODLE_25_STABLE
  • Loading branch information
Sam Hemelryk committed Mar 10, 2014
2 parents 57ce177 + 32c9c25 commit e75faa2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 14 deletions.
71 changes: 71 additions & 0 deletions lib/grouplib.php
Expand Up @@ -790,6 +790,77 @@ function groups_get_activity_allowed_groups($cm,$userid=0) {
}
}

/**
* Filter a user list and return only the users that can see the course module based on
* groups/permissions etc. It is assumed that the users are pre-filtered to those who are enrolled in the course.
*
* @category group
* @param stdClass $cm The course module
* @param array $users An array of users, indexed by userid
* @return array A filtered list of users that can see the module, indexed by userid.
*/
function groups_filter_users_by_course_module_visible($cm, $users) {
global $CFG, $DB;

if (empty($CFG->enablegroupmembersonly)) {
return $users;
}
if (empty($cm->groupmembersonly)) {
return $users;
}
list($usql, $uparams) = $DB->get_in_or_equal(array_keys($users), SQL_PARAMS_NAMED, 'userid', true);

// Group membership sub-query.
if ($cm->groupingid) {
// Find out if member of any group in selected activity grouping.
$igsql = "SELECT gm.userid
FROM {groups_members} gm
LEFT JOIN {groupings_groups} gg
ON gm.groupid = gg.groupid
WHERE gm.userid $usql AND gg.groupingid = :groupingid";
$igparams = array_merge($uparams, array('groupingid' => $cm->groupingid));

} else {
// No grouping used - check all groups in course.
$igsql = "SELECT gm.userid
FROM {groups_members} gm
LEFT JOIN {groups} g
ON gm.groupid = g.id
WHERE gm.userid $usql AND g.courseid = :courseid";
$igparams = array_merge($uparams, array('courseid' => $cm->course));
}

$context = context_module::instance($cm->id);

// Get the list of users in a valid group.
$usersingroup = $DB->get_records_sql($igsql, $igparams);
if (!$usersingroup) {
$usersingroup = array();
} else {
$usersingroup = array_keys($usersingroup);
}

// Get the list of users who can access all groups.
list($accessallgroupssql, $accessallgroupsparams) = get_enrolled_sql($context, 'moodle/site:accessallgroups');

$userswithaccessallgroups = $DB->get_records_sql($accessallgroupssql, $accessallgroupsparams);
if (!$userswithaccessallgroups) {
$userswithaccessallgroups = array();
} else {
$userswithaccessallgroups = array_keys($userswithaccessallgroups);
}

// Explaining this array mangling:
// $users is an array of $user[$userid] => stdClass etc
// $userswithaccessallgroups is an array of $userswithaccessallgroups[random int] = $userid
// $usersingroup is an array of $usersingroup[random int] = $userid
// so - the inner array_merge combines the values of the 2 arrays disregarding the keys (because they are ints)
// this is then flipped so the values become the keys (and the values are now nonsense)
// this is then intersected with the users array only by looking at the keys - this
// returns only the users from the original array that had a value in one of the two $usersxxx lists.
return array_intersect_key($users, array_flip(array_merge($userswithaccessallgroups, $usersingroup)));
}

/**
* Determine if a course module is currently visible to a user
*
Expand Down
19 changes: 15 additions & 4 deletions mod/assign/assignmentplugin.php
Expand Up @@ -42,7 +42,10 @@ abstract class assign_plugin {
private $type = '';
/** @var string $error error message */
private $error = '';

/** @var boolean|null $enabledcache Cached lookup of the is_enabled function */
private $enabledcache = null;
/** @var boolean|null $enabledcache Cached lookup of the is_visible function */
private $visiblecache = null;

/**
* Constructor for the abstract plugin type class
Expand Down Expand Up @@ -203,6 +206,7 @@ public function save(stdClass $submissionorgrade, stdClass $data) {
* @return bool
*/
public final function enable() {
$this->enabledcache = true;
return $this->set_config('enabled', 1);
}

Expand All @@ -212,6 +216,7 @@ public final function enable() {
* @return bool
*/
public final function disable() {
$this->enabledcache = false;
return $this->set_config('enabled', 0);
}

Expand All @@ -221,7 +226,10 @@ public final function disable() {
* @return bool - if false - this plugin will not accept submissions / feedback
*/
public function is_enabled() {
return $this->get_config('enabled');
if ($this->enabledcache === null) {
$this->enabledcache = $this->get_config('enabled');
}
return $this->enabledcache;
}


Expand Down Expand Up @@ -282,8 +290,11 @@ public final function get_sort_order() {
* @return bool
*/
public final function is_visible() {
$disabled = get_config($this->get_subtype() . '_' . $this->get_type(), 'disabled');
return !$disabled;
if ($this->visiblecache === null) {
$disabled = get_config($this->get_subtype() . '_' . $this->get_type(), 'disabled');
$this->visiblecache = !$disabled;
}
return $this->visiblecache;
}


Expand Down
28 changes: 18 additions & 10 deletions mod/assign/locallib.php
Expand Up @@ -114,6 +114,9 @@ class assign {
/** @var string modulenameplural prevents excessive calls to get_string */
private static $modulenameplural = null;

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

/**
* Constructor for the base assign class.
*
Expand Down Expand Up @@ -1243,20 +1246,25 @@ class="quickgrade"/>';
* @return array List of user records
*/
public function list_participants($currentgroup, $idsonly) {
if ($idsonly) {
$users = get_enrolled_users($this->context, 'mod/assign:submit', $currentgroup, 'u.id');
} else {
$users = get_enrolled_users($this->context, 'mod/assign:submit', $currentgroup);
$key = $this->context->id . '-' . $currentgroup;
if (!isset($this->participants[$key])) {
$users = get_enrolled_users($this->context, 'mod/assign:submit', $currentgroup, 'u.*');

$cm = $this->get_course_module();
$users = groups_filter_users_by_course_module_visible($cm, $users);

$this->participants[$key] = $users;
}

$cm = $this->get_course_module();
foreach ($users as $userid => $user) {
if (!groups_course_module_visible($cm, $userid)) {
unset($users[$userid]);
if ($idsonly) {
$idslist = array();
foreach ($this->participants[$key] as $id => $user) {
$idslist[$id] = new stdClass();
$idslist[$id]->id = $id;
}
return $idslist;
}

return $users;
return $this->participants[$key];
}

/**
Expand Down

0 comments on commit e75faa2

Please sign in to comment.