From 7b96ffb0449b6f1b458a154fa262fe9e292ecf5f Mon Sep 17 00:00:00 2001 From: Niclas Tollstorff Date: Thu, 11 Feb 2016 10:23:22 +0800 Subject: [PATCH] MDL-53072 group: added 'only active' option when auto-creating groups --- group/autogroup.php | 8 +++++++- group/autogroup_form.php | 9 ++++++++- group/lib.php | 5 +++-- lang/en/group.php | 2 ++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/group/autogroup.php b/group/autogroup.php index 8270ff326ccdd..e9921352f768f 100644 --- a/group/autogroup.php +++ b/group/autogroup.php @@ -95,7 +95,13 @@ if ($data->groupid) { $source['groupid'] = $data->groupid; } - $users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup)); + + $onlyactive = true; + if (!$data->includeonlyactiveenrol && has_capability('moodle/course:viewsuspendedusers', $context)) { + $onlyactive = false; + } + + $users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup), $onlyactive); $usercnt = count($users); if ($data->allocateby == 'random') { diff --git a/group/autogroup_form.php b/group/autogroup_form.php index a04da015a47b6..8e17c17387836 100644 --- a/group/autogroup_form.php +++ b/group/autogroup_form.php @@ -82,7 +82,8 @@ function definition() { $mform->setDefault('roleid', $student->id); } - if ($cohorts = cohort_get_available_cohorts(context_course::instance($COURSE->id), COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0)) { + $coursecontext = context_course::instance($COURSE->id); + if ($cohorts = cohort_get_available_cohorts($coursecontext, COHORT_WITH_ENROLLED_MEMBERS_ONLY)) { $options = array(0 => get_string('anycohort', 'cohort')); foreach ($cohorts as $c) { $options[$c->id] = format_string($c->name, true, context::instance_by_id($c->contextid)); @@ -140,6 +141,12 @@ function definition() { $mform->disabledIf('notingroup', 'groupingid', 'neq', 0); $mform->disabledIf('notingroup', 'groupid', 'neq', 0); + if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) { + $mform->addElement('checkbox', 'includeonlyactiveenrol', get_string('includeonlyactiveenrol', 'group'), ''); + $mform->addHelpButton('includeonlyactiveenrol', 'includeonlyactiveenrol', 'group'); + $mform->setDefault('includeonlyactiveenrol', true); + } + $mform->addElement('header', 'groupinghdr', get_string('grouping', 'group')); $options = array('0' => get_string('nogrouping', 'group'), diff --git a/group/lib.php b/group/lib.php index 3178e7b2f792d..3af8deb5b3a79 100644 --- a/group/lib.php +++ b/group/lib.php @@ -717,16 +717,17 @@ function groups_get_possible_roles($context) { * @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 + * @param bool $onlyactiveenrolments restrict to users who have an active enrolment in the course * @return array An array of the users */ function groups_get_potential_members($courseid, $roleid = null, $source = null, $orderby = 'lastname ASC, firstname ASC', - $notingroup = null) { + $notingroup = null, $onlyactiveenrolments = true) { global $DB; $context = context_course::instance($courseid); - list($esql, $params) = get_enrolled_sql($context); + list($esql, $params) = get_enrolled_sql($context, '', 0, $onlyactiveenrolments); $notingroupsql = ""; if ($notingroup) { diff --git a/lang/en/group.php b/lang/en/group.php index 17007edf5ddae..464ee741061a6 100644 --- a/lang/en/group.php +++ b/lang/en/group.php @@ -137,6 +137,8 @@ * Required fieldname is groupname * Optional fieldnames are description, enrolmentkey, picture, hidepicture'; $string['importgroups_link'] = 'group/import'; +$string['includeonlyactiveenrol'] = 'Include only active enrolments'; +$string['includeonlyactiveenrol_help'] = 'This setting determines if only active enrolled users will be included in the groups. If enabled, suspended users will not be included in groups.'; $string['javascriptrequired'] = 'This page requires JavaScript to be enabled.'; $string['members'] = 'Members per group'; $string['membersofselectedgroup'] = 'Members of:';