Skip to content

Commit

Permalink
Merge branch 'MDL-61967-master' of git://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Sep 25, 2018
2 parents 1a9fc39 + 5290d06 commit 85d4b77
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 25 deletions.
11 changes: 7 additions & 4 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ function is_enrolled(context $context, $user = null, $withcapability = '', $only
* @param string|array $capability optional, may include a capability name, or array of names.
* If an array is provided then this is the equivalent of a logical 'OR',
* i.e. the user needs to have one of these capabilities.
* @param int $group optional, 0 indicates no current group, otherwise the group id
* @param int $group optional, 0 indicates no current group and USERSWITHOUTGROUP users without any group; otherwise the group id
* @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
* @param bool $onlysuspended inverse of onlyactive, consider only suspended enrolments
* @param int $enrolid The enrolment ID. If not 0, only users enrolled using this enrolment method will be returned.
Expand All @@ -1315,9 +1315,12 @@ function get_enrolled_with_capabilities_join(context $context, $prefix = '', $ca
}

if ($group) {
$groupjoin = groups_get_members_join($group, $uid);
$groupjoin = groups_get_members_join($group, $uid, $context);
$joins[] = $groupjoin->joins;
$params = array_merge($params, $groupjoin->params);
if (!empty($groupjoin->wheres)) {
$wheres[] = $groupjoin->wheres;
}
}

$joins = implode("\n", $joins);
Expand All @@ -1335,7 +1338,7 @@ function get_enrolled_with_capabilities_join(context $context, $prefix = '', $ca
*
* @param context $context
* @param string $withcapability
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @param int $groupid 0 means ignore groups, USERSWITHOUTGROUP without any group and any other value limits the result by group id
* @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
* @param bool $onlysuspended inverse of onlyactive, consider only suspended enrolments
* @param int $enrolid The enrolment ID. If not 0, only users enrolled using this enrolment method will be returned.
Expand Down Expand Up @@ -1461,7 +1464,7 @@ function get_enrolled_join(context $context, $useridcolumn, $onlyactive = false,
*
* @param context $context
* @param string $withcapability
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @param int $groupid 0 means ignore groups, USERSWITHOUTGROUP without any group and any other value limits the result by group id
* @param string $userfields requested user record fields
* @param string $orderby
* @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
Expand Down
46 changes: 39 additions & 7 deletions lib/grouplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
*/
define('VISIBLEGROUPS', 2);

/**
* This is for filtering users without any group.
*/
define('USERSWITHOUTGROUP', -1);


/**
* Determines if a group with a given groupid exists.
Expand Down Expand Up @@ -976,36 +981,63 @@ function groups_group_visible($groupid, $course, $cm = null, $userid = null) {
* Get sql and parameters that will return user ids for a group
*
* @param int $groupid
* @param context $context Course context or a context within a course. Mandatory when $groupid = USERSWITHOUTGROUP
* @return array($sql, $params)
* @throws coding_exception if empty or invalid context submitted when $groupid = USERSWITHOUTGROUP
*/
function groups_get_members_ids_sql($groupid) {
$groupjoin = groups_get_members_join($groupid, 'u.id');
function groups_get_members_ids_sql($groupid, context $context = null) {
$groupjoin = groups_get_members_join($groupid, 'u.id', $context);

$sql = "SELECT DISTINCT u.id
FROM {user} u
$groupjoin->joins
WHERE u.deleted = 0";
if (!empty($groupjoin->wheres)) {
$sql .= ' AND '. $groupjoin->wheres;
}

return array($sql, $groupjoin->params);
}

/**
* Get sql join to return users in a group
*
* @param int $groupid
* @param int $groupid The groupid, 0 means all groups and USERSWITHOUTGROUP no group
* @param string $useridcolumn The column of the user id from the calling SQL, e.g. u.id
* @param context $context Course context or a context within a course. Mandatory when $groupid = USERSWITHOUTGROUP
* @return \core\dml\sql_join Contains joins, wheres, params
* @throws coding_exception if empty or invalid context submitted when $groupid = USERSWITHOUTGROUP
*/
function groups_get_members_join($groupid, $useridcolumn) {
function groups_get_members_join($groupid, $useridcolumn, context $context = null) {
// Use unique prefix just in case somebody makes some SQL magic with the result.
static $i = 0;
$i++;
$prefix = 'gm' . $i . '_';

$join = "JOIN {groups_members} {$prefix}gm ON ({$prefix}gm.userid = $useridcolumn AND {$prefix}gm.groupid = :{$prefix}gmid)";
$param = array("{$prefix}gmid" => $groupid);
$coursecontext = (!empty($context)) ? $context->get_course_context() : null;
if ($groupid == USERSWITHOUTGROUP && empty($coursecontext)) {
// Throw an exception if $context is empty or invalid because it's needed to get the users without any group.
throw new coding_exception('Missing or wrong $context parameter in an attempt to get members without any group');
}

if ($groupid == USERSWITHOUTGROUP) {
// Get members without any group.
$join = "LEFT JOIN (
SELECT g.courseid, m.groupid, m.userid
FROM {groups_members} m
JOIN {groups} g ON g.id = m.groupid
) {$prefix}gm ON ({$prefix}gm.userid = $useridcolumn AND {$prefix}gm.courseid = :{$prefix}gcourseid)";
$where = "{$prefix}gm.userid IS NULL";
$param = array("{$prefix}gcourseid" => $coursecontext->instanceid);
} else {
// Get members of defined groupid.
$join = "JOIN {groups_members} {$prefix}gm
ON ({$prefix}gm.userid = $useridcolumn AND {$prefix}gm.groupid = :{$prefix}gmid)";
$where = '';
$param = array("{$prefix}gmid" => $groupid);
}

return new \core\dml\sql_join($join, '', $param);
return new \core\dml\sql_join($join, $where, $param);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions lib/tests/accesslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,40 @@ public function test_get_enrolled_sql_multiple_enrolments() {

}

/**
* Test that enrolled users SQL does not return any values for users
* without a group when $context is not a valid course context.
*/
public function test_get_enrolled_sql_userswithoutgroup() {
global $DB;

$this->resetAfterTest();

$systemcontext = context_system::instance();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();

$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);

$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group, $user1);

$enrolled = get_enrolled_users($coursecontext);
$this->assertCount(2, $enrolled);

// Get users without any group on the course context.
$enrolledwithoutgroup = get_enrolled_users($coursecontext, '', USERSWITHOUTGROUP);
$this->assertCount(1, $enrolledwithoutgroup);
$this->assertFalse(isset($enrolledwithoutgroup[$user1->id]));

// Get users without any group on the system context (it should throw an exception).
$this->expectException('coding_exception');
get_enrolled_users($systemcontext, '', USERSWITHOUTGROUP);
}

public function get_enrolled_sql_provider() {
return array(
array(
Expand Down
121 changes: 112 additions & 9 deletions lib/tests/grouplib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public function test_groups_get_grouping_by_idnumber() {
$this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2));
}


public function test_groups_get_members_ids_sql() {
global $DB;

Expand All @@ -185,7 +184,9 @@ public function test_groups_get_members_ids_sql() {
$generator = $this->getDataGenerator();

$course = $generator->create_course();
$student = $generator->create_user();
$coursecontext = context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
$role = $DB->get_record('role', array('shortname' => 'student'));
$group = $generator->create_group(array('courseid' => $course->id));
Expand All @@ -196,20 +197,122 @@ public function test_groups_get_members_ids_sql() {

$this->assertNotEquals($instance, false);

// Enrol the user in the course.
$plugin->enrol_user($instance, $student->id, $role->id);
// Enrol users in the course.
$plugin->enrol_user($instance, $student1->id, $role->id);
$plugin->enrol_user($instance, $student2->id, $role->id);

list($sql, $params) = groups_get_members_ids_sql($group->id, true);
list($sql, $params) = groups_get_members_ids_sql($group->id);

// Test an empty group.
$users = $DB->get_records_sql($sql, $params);

$this->assertFalse(array_key_exists($student->id, $users));
groups_add_member($group->id, $student->id);
$this->assertFalse(array_key_exists($student1->id, $users));

// Test with a group member.
groups_add_member($group->id, $student1->id);
$users = $DB->get_records_sql($sql, $params);
$this->assertTrue(array_key_exists($student->id, $users));
$this->assertTrue(array_key_exists($student1->id, $users));
}

public function test_groups_get_members_ids_sql_valid_context() {
global $DB;

$this->resetAfterTest(true);

$generator = $this->getDataGenerator();

$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
$role = $DB->get_record('role', array('shortname' => 'student'));
$group = $generator->create_group(array('courseid' => $course->id));
$instance = $DB->get_record('enrol', array(
'courseid' => $course->id,
'enrol' => 'manual',
));

$this->assertNotEquals($instance, false);

// Enrol users in the course.
$plugin->enrol_user($instance, $student1->id, $role->id);
$plugin->enrol_user($instance, $student2->id, $role->id);

// Add student1 to the group.
groups_add_member($group->id, $student1->id);

// Test with members at any group and with a valid $context.
list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP, $coursecontext);
$users = $DB->get_records_sql($sql, $params);
$this->assertFalse(array_key_exists($student1->id, $users));
$this->assertTrue(array_key_exists($student2->id, $users));
}

public function test_groups_get_members_ids_sql_empty_context() {
global $DB;

$this->resetAfterTest(true);

$generator = $this->getDataGenerator();

$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
$role = $DB->get_record('role', array('shortname' => 'student'));
$group = $generator->create_group(array('courseid' => $course->id));
$instance = $DB->get_record('enrol', array(
'courseid' => $course->id,
'enrol' => 'manual',
));

$this->assertNotEquals($instance, false);

// Enrol users in the course.
$plugin->enrol_user($instance, $student1->id, $role->id);
$plugin->enrol_user($instance, $student2->id, $role->id);

// Add student1 to the group.
groups_add_member($group->id, $student1->id);

// Test with members at any group and without the $context.
$this->expectException('coding_exception');
list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP);
}

public function test_groups_get_members_ids_sql_invalid_context() {
global $DB;

$this->resetAfterTest(true);

$generator = $this->getDataGenerator();

$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
$role = $DB->get_record('role', array('shortname' => 'student'));
$group = $generator->create_group(array('courseid' => $course->id));
$instance = $DB->get_record('enrol', array(
'courseid' => $course->id,
'enrol' => 'manual',
));

$this->assertNotEquals($instance, false);

// Enrol users in the course.
$plugin->enrol_user($instance, $student1->id, $role->id);
$plugin->enrol_user($instance, $student2->id, $role->id);

// Add student1 to the group.
groups_add_member($group->id, $student1->id);

// Test with members at any group and with an invalid $context.
$syscontext = context_system::instance();
$this->expectException('coding_exception');
list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP, $syscontext);
}

public function test_groups_get_group_by_name() {
Expand Down
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ information provided here is intended especially for developers.
- Unassigning roles from users.
- Enrolling users into courses.
- Unenrolling users from courses.
* New optional parameter $context for the groups_get_members_join() function and ability to filter users that are not members of
any group. Besides, groups_get_members_ids_sql, get_enrolled_sql and get_enrolled_users now accepts -1 (USERSWITHOUTGROUP) for
the groupid field.

=== 3.5 ===

Expand Down
2 changes: 1 addition & 1 deletion user/classes/participants_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class participants_table extends \table_sql {
* Sets up the table.
*
* @param int $courseid
* @param int|false $currentgroup False if groups not used, int if groups used, 0 for all groups.
* @param int|false $currentgroup False if groups not used, int if groups used, 0 all groups, USERSWITHOUTGROUP for no group
* @param int $accesssince The time the user last accessed the site
* @param int $roleid The role we are including, 0 means all enrolled users
* @param int $enrolid The applied filter for the user enrolment ID.
Expand Down
2 changes: 1 addition & 1 deletion user/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
}
}

if ($groupid && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
if ($groupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
$grouprenderer = $PAGE->get_renderer('core_group');
$groupdetailpage = new \core_group\output\group_details($groupid);
echo $grouprenderer->group_details($groupdetailpage);
Expand Down
6 changes: 3 additions & 3 deletions user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ function user_get_tagged_users($tag, $exclusivemode = false, $fromctx = 0, $ctx
* Returns the SQL used by the participants table.
*
* @param int $courseid The course id
* @param int $groupid The groupid, 0 means all groups
* @param int $groupid The groupid, 0 means all groups and USERSWITHOUTGROUP no group
* @param int $accesssince The time since last access, 0 means any time
* @param int $roleid The role id, 0 means all roles
* @param int $enrolid The enrolment id, 0 means all enrolment methods will be returned.
Expand Down Expand Up @@ -1474,7 +1474,7 @@ function user_get_participants_sql($courseid, $groupid = 0, $accesssince = 0, $r
* Returns the total number of participants for a given course.
*
* @param int $courseid The course id
* @param int $groupid The groupid, 0 means all groups
* @param int $groupid The groupid, 0 means all groups and USERSWITHOUTGROUP no group
* @param int $accesssince The time since last access, 0 means any time
* @param int $roleid The role id, 0 means all roles
* @param int $enrolid The applied filter for the user enrolment ID.
Expand All @@ -1498,7 +1498,7 @@ function user_get_total_participants($courseid, $groupid = 0, $accesssince = 0,
* Returns the participants for a given course.
*
* @param int $courseid The course id
* @param int $groupid The group id
* @param int $groupid The groupid, 0 means all groups and USERSWITHOUTGROUP no group
* @param int $accesssince The time since last access
* @param int $roleid The role id
* @param int $enrolid The applied filter for the user enrolment ID.
Expand Down
5 changes: 5 additions & 0 deletions user/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public function unified_filter($course, $context, $filtersapplied, $baseurl = nu
if (has_capability('moodle/site:accessallgroups', $context) || $course->groupmode != SEPARATEGROUPS) {
// List all groups if the user can access all groups, or we are in visible group mode or no groups mode.
$groups = $manager->get_all_groups();
if (!empty($groups)) {
// Add 'No group' option, to enable filtering users without any group.
$nogroup[USERSWITHOUTGROUP] = (object)['name' => get_string('nogroup', 'group')];
$groups = $nogroup + $groups;
}
} else {
// Otherwise, just list the groups the user belongs to.
$groups = groups_get_all_groups($course->id, $USER->id);
Expand Down
Loading

0 comments on commit 85d4b77

Please sign in to comment.