From 88af0050a282a9d4070da786e319a46f5404f1f8 Mon Sep 17 00:00:00 2001 From: Gregory Faller Date: Fri, 1 Aug 2014 14:59:38 +0930 Subject: [PATCH] MDL-46524: Auto-create groups from existing group or grouping membership --- group/autogroup.php | 12 +++++++- group/autogroup_form.php | 50 ++++++++++++++++++++++++++++++++- group/lib.php | 32 +++++++++++++++------ group/tests/behat/auto_creation.feature | 32 ++++++++++++++++++++- lang/en/group.php | 4 +++ 5 files changed, 119 insertions(+), 11 deletions(-) diff --git a/group/autogroup.php b/group/autogroup.php index 6c957c8..8270ff3 100644 --- a/group/autogroup.php +++ b/group/autogroup.php @@ -85,7 +85,17 @@ if ($editform->is_cancelled()) { 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') { diff --git a/group/autogroup_form.php b/group/autogroup_form.php index d874911..d16be96 100644 --- a/group/autogroup_form.php +++ b/group/autogroup_form.php @@ -68,6 +68,42 @@ class autogroup_form extends moodleform { $mform->addRule('number', null, 'numeric', null, 'client'); $mform->addRule('number', get_string('required'), 'required', null, 'client'); + $options = array(); + $options[0] = get_string('none'); + $class = ''; + if ($groupings = groups_get_all_groupings($COURSE->id)) { + foreach ($groupings as $grouping) { + $options[$grouping->id] = $grouping->name; + } + } else { + $class = 'disabled'; + } + $mform->addElement('select', 'groupingid', get_string('selectfromgrouping', 'group'), $options, $class); + $mform->addHelpButton('groupingid', 'selectfromgrouping', 'group'); + $mform->setDefault('groupingid', 0); + $mform->setAdvanced('groupingid'); + $mform->disabledIf('groupingid', 'groupid', 'neq', 0); + $mform->disabledIf('groupingid', 'allocateby', 'eq', 'no'); + $mform->disabledIf('groupingid', 'notingroup', 'checked'); + + $options = array(); + $options[0] = get_string('none'); + $class = ''; + if ($groups = groups_get_all_groups($COURSE->id)) { + foreach ($groups as $group) { + $options[$group->id] = $group->name; + } + } else { + $class = 'disabled'; + } + $mform->addElement('select', 'groupid', get_string('selectfromgroup', 'group'), $options, $class); + $mform->addHelpButton('groupid', 'selectfromgroup', 'group'); + $mform->setDefault('groupid', 0); + $mform->setAdvanced('groupid'); + $mform->disabledIf('groupid', 'groupingid', 'neq', 0); + $mform->disabledIf('groupid', 'allocateby', 'eq', 'no'); + $mform->disabledIf('groupid', 'notingroup', 'checked'); + $mform->addElement('header', 'groupmembershdr', get_string('groupmembers', 'group')); $mform->setExpanded('groupmembershdr', true); @@ -111,6 +147,8 @@ class autogroup_form extends moodleform { $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')); @@ -156,7 +194,17 @@ class autogroup_form extends moodleform { $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'); } diff --git a/group/lib.php b/group/lib.php index 7d44a43..088e355 100644 --- a/group/lib.php +++ b/group/lib.php @@ -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; @@ -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 "; + $sourcejoin .= "JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = gg.groupid AND gg.groupingid = :groupingid) "; + $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"; diff --git a/group/tests/behat/auto_creation.feature b/group/tests/behat/auto_creation.feature index ee7f6b0..f52f821 100644 --- a/group/tests/behat/auto_creation.feature +++ b/group/tests/behat/auto_creation.feature @@ -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)" \ No newline at end of file + 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)" \ No newline at end of file diff --git a/lang/en/group.php b/lang/en/group.php index f640d22..ec0b0a6 100644 --- a/lang/en/group.php +++ b/lang/en/group.php @@ -174,6 +174,10 @@ $string['removefromgroupconfirm'] = 'Do you really want to remove user "{$a->use $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['selectfromgroup_help'] = 'Choose a group to restrict the members used in the autocreate groups process'; +$string['selectfromgrouping'] = 'Select members from grouping'; +$string['selectfromgrouping_help'] = 'Choose a grouping to restrict the members used in the autocreate groups process'; $string['selectfromrole'] = 'Select members with role'; $string['showgroupsingrouping'] = 'Show groups in grouping'; $string['showmembersforgroup'] = 'Show members for group'; -- 1.8.5.2 (Apple Git-48)