Skip to content

Commit

Permalink
MDL-46524: Auto-create groups from existing group or grouping membership
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-or committed Aug 6, 2014
1 parent 40f0ad2 commit e4ebf7e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 11 deletions.
12 changes: 11 additions & 1 deletion group/autogroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,17 @@
default:
print_error('unknoworder');
}
$users = groups_get_potential_members($data->courseid, $data->roleid, $data->cohortid, $orderby, !empty($data->notingroup));
$source = array();
if ($data->cohortid) {
$source['cohortid'] = $data->cohortid;
}
if ($data->groupingid) {
$source['groupingid'] = $data->groupingid;
}
if ($data->groupid) {
$source['groupid'] = $data->groupid;
}
$users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup));
$usercnt = count($users);

if ($data->allocateby == 'random') {
Expand Down
45 changes: 44 additions & 1 deletion group/autogroup_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,37 @@ function definition() {
$mform->setType('cohortid', PARAM_INT);
$mform->setConstant('cohortid', '0');
}

if ($groupings = groups_get_all_groupings($COURSE->id)) {
$options = array();
$options[0] = get_string('none');
foreach ($groupings as $grouping) {
$options[$grouping->id] = format_string($grouping->name);
}
$mform->addElement('select', 'groupingid', get_string('selectfromgrouping', 'group'), $options);
$mform->setDefault('groupingid', 0);
$mform->disabledIf('groupingid', 'notingroup', 'checked');
} else {
$mform->addElement('hidden', 'groupingid');
$mform->setType('groupingid', PARAM_INT);
$mform->setConstant('groupingid', 0);
}

if ($groups = groups_get_all_groups($COURSE->id)) {
$options = array();
$options[0] = get_string('none');
foreach ($groups as $group) {
$options[$group->id] = format_string($group->name);
}
$mform->addElement('select', 'groupid', get_string('selectfromgroup', 'group'), $options);
$mform->setDefault('groupid', 0);
$mform->disabledIf('groupid', 'notingroup', 'checked');
} else {
$mform->addElement('hidden', 'groupid');
$mform->setType('groupid', PARAM_INT);
$mform->setConstant('groupid', 0);
}

$options = array('no' => get_string('noallocation', 'group'),
'random' => get_string('random', 'group'),
'firstname' => get_string('byfirstname', 'group'),
Expand All @@ -111,6 +142,8 @@ function definition() {
$mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');

$mform->addElement('checkbox', 'notingroup', get_string('notingroup', 'group'));
$mform->disabledIf('notingroup', 'groupingid', 'neq', 0);
$mform->disabledIf('notingroup', 'groupid', 'neq', 0);

$mform->addElement('header', 'groupinghdr', get_string('grouping', 'group'));

Expand Down Expand Up @@ -156,7 +189,17 @@ function validation($data, $files) {
$errors = parent::validation($data, $files);

if ($data['allocateby'] != 'no') {
if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) {
$source = array();
if ($data['cohortid']) {
$source['cohortid'] = $data['cohortid'];
}
if ($data['groupingid']) {
$source['groupingid'] = $data['groupingid'];
}
if ($data['groupid']) {
$source['groupid'] = $data['groupid'];
}
if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
$errors['roleid'] = get_string('nousersinrole', 'group');
}

Expand Down
32 changes: 24 additions & 8 deletions group/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,12 @@ function groups_get_possible_roles($context) {
*
* @param int $courseid The id of the course
* @param int $roleid The role to select users from
* @param int $cohortid restrict to cohort id
* @param mixed $source restrict to cohort, grouping or group id
* @param string $orderby The column to sort users by
* @param int $notingroup restrict to users not in existing groups
* @return array An array of the users
*/
function groups_get_potential_members($courseid, $roleid = null, $cohortid = null,
function groups_get_potential_members($courseid, $roleid = null, $source = null,
$orderby = 'lastname ASC, firstname ASC',
$notingroup = null) {
global $DB;
Expand Down Expand Up @@ -749,18 +749,34 @@ function groups_get_potential_members($courseid, $roleid = null, $cohortid = nul
$where = "";
}

if ($cohortid) {
$cohortjoin = "JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)";
$params['cohortid'] = $cohortid;
$sourcejoin = "";
if (is_int($source)) {
$sourcejoin .= "JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid) ";
$params['cohortid'] = $source;
} else {
$cohortjoin = "";
// Auto-create groups from an existing cohort membership.
if (isset($source['cohortid'])) {
$sourcejoin .= "JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid) ";
$params['cohortid'] = $source['cohortid'];
}
// Auto-create groups from an existing group membership.
if (isset($source['groupid'])) {
$sourcejoin .= "JOIN {groups_members} gp ON (gp.userid = u.id AND gp.groupid = :groupid) ";
$params['groupid'] = $source['groupid'];
}
// Auto-create groups from an existing grouping membership.
if (isset($source['groupingid'])) {
$sourcejoin .= "JOIN {groupings_groups} gg ON gg.groupingid = :groupingid ";
$sourcejoin .= "JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = gg.groupid) ";
$params['groupingid'] = $source['groupingid'];
}
}

$allusernamefields = get_all_user_name_fields(true, 'u');
$sql = "SELECT u.id, u.username, $allusernamefields, u.idnumber
$sql = "SELECT DISTINCT u.id, u.username, $allusernamefields, u.idnumber
FROM {user} u
JOIN ($esql) e ON e.id = u.id
$cohortjoin
$sourcejoin
$where
ORDER BY $orderby";

Expand Down
32 changes: 31 additions & 1 deletion group/tests/behat/auto_creation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,34 @@ Feature: Automatic creation of groups
And I set the field "Ignore users in groups" to "1"
And I press "Submit"
And the "groups" select box should contain "Group A (3)"
And the "groups" select box should contain "Group B (3)"
And the "groups" select box should contain "Group B (3)"

@javascript
Scenario: Split users into groups based on existing groups or groupings
Given I set the following fields to these values:
| Naming scheme | Group @ |
| Auto create based on | Number of groups |
| Group/member count | 2 |
| Grouping of auto-created groups | No grouping |
And I press "Submit"
And I press "Auto-create groups"
And I set the following fields to these values:
| Naming scheme | Test @ |
| Auto create based on | Number of groups |
| Group/member count | 2 |
| groupid | Group A |
| Grouping of auto-created groups | New grouping |
| Grouping name | Sub Grouping |
And I press "Submit"
And the "groups" select box should contain "Test A (3)"
And the "groups" select box should contain "Test B (2)"
And I press "Auto-create groups"
And I set the following fields to these values:
| Naming scheme | Test # |
| Auto create based on | Number of groups |
| Group/member count | 2 |
| Select members from grouping | Sub Grouping |
| Grouping of auto-created groups | No grouping |
And I press "Submit"
And the "groups" select box should contain "Test 1 (3)"
And the "groups" select box should contain "Test 2 (2)"
53 changes: 53 additions & 0 deletions group/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,57 @@ public function test_groups_delete_groupings() {
$this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id, 'courseid' => $course2->id)));
$this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
}

public function test_groups_create_autogroups () {
global $DB;
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
$this->getDataGenerator()->create_grouping_group(array('groupid' => $group2->id, 'groupingid' => $grouping1->id));
$this->getDataGenerator()->create_grouping_group(array('groupid' => $group3->id, 'groupingid' => $grouping1->id));
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
$this->getDataGenerator()->enrol_user($user3->id, $course->id);
$this->getDataGenerator()->enrol_user($user4->id, $course->id);
$this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user3->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user4->id));

// Test autocreate group based on all course users.
$users = groups_get_potential_members($course->id);
$group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
foreach ($users as $user) {
$this->getDataGenerator()->create_group_member(array('groupid' => $group4->id, 'userid' => $user->id));
}
$this->assertEquals(4, $DB->count_records('groups_members', array('groupid' => $group4->id)));

// Test autocreate group based on existing group.
$source = array();
$source['groupid'] = $group1->id;
$users = groups_get_potential_members($course->id, 0, $source);
$group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
foreach ($users as $user) {
$this->getDataGenerator()->create_group_member(array('groupid' => $group5->id, 'userid' => $user->id));
}
$this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group5->id)));

// Test autocreate group based on existing grouping.
$source = array();
$source['groupingid'] = $grouping1->id;
$users = groups_get_potential_members($course->id, 0, $source);
$group6 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
foreach ($users as $user) {
$this->getDataGenerator()->create_group_member(array('groupid' => $group6->id, 'userid' => $user->id));
}
$this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id)));
}
}
2 changes: 2 additions & 0 deletions lang/en/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
$string['removegroupingsmembers'] = 'Remove all groups from groupings';
$string['removegroupsmembers'] = 'Remove all group members';
$string['removeselectedusers'] = 'Remove selected users';
$string['selectfromgroup'] = 'Select members from group';
$string['selectfromgrouping'] = 'Select members from grouping';
$string['selectfromrole'] = 'Select members with role';
$string['showgroupsingrouping'] = 'Show groups in grouping';
$string['showmembersforgroup'] = 'Show members for group';
Expand Down

0 comments on commit e4ebf7e

Please sign in to comment.