From 92343cd2eb2d3ce283f91328cada06e982bf82c1 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Fri, 23 Apr 2010 09:15:55 +0000 Subject: [PATCH] MDL-21781 added support for autocreation of groups from cohorts --- cohort/lib.php | 36 ++++++++++++++++++++++++++++++++++++ group/autogroup.php | 2 +- group/autogroup_form.php | 13 ++++++++++++- group/lib.php | 11 ++++++++++- lang/en/cohort.php | 2 ++ 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/cohort/lib.php b/cohort/lib.php index 35d11e7ee6bf2..176093d54d29e 100644 --- a/cohort/lib.php +++ b/cohort/lib.php @@ -117,6 +117,42 @@ function cohort_remove_member($cohortid, $userid) { $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid)); } +/** + * Returns list of visible cohorts in course. + * + * @param object $course + * @param bool $enrolled true means include only cohorts with enrolled users + * @return array + */ +function cohort_get_visible_list($course) { + global $DB, $USER; + + $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); + list($esql, $params) = get_enrolled_sql($context); + $parentsql = get_related_contexts_string($context); + + $sql = "SELECT c.id, c.name, c.idnumber, COUNT(u.id) AS cnt + FROM {cohort} c + JOIN {cohort_members} cm ON cm.cohortid = c.id + JOIN ($esql) u ON u.id = cm.userid + WHERE c.contextid $parentsql + GROUP BY c.id, c.name, c.idnumber + HAVING COUNT(u.id) > 0 + ORDER BY c.name, c.idnumber"; + $params['ctx'] = $context->id; + + $cohorts = $DB->get_records_sql($sql, $params); + + foreach ($cohorts as $cid=>$cohort) { + $cohorts[$cid] = format_string($cohort->name); + if ($cohort->idnumber) { + $cohorts[$cid] .= ' (' . $cohort->cnt . ')'; + } + } + + return $cohorts; +} + /** * Cohort assignment candidates */ diff --git a/group/autogroup.php b/group/autogroup.php index 830ad5d005d3b..fa37ddb4c26cb 100644 --- a/group/autogroup.php +++ b/group/autogroup.php @@ -71,7 +71,7 @@ default: print_error('unknoworder'); } - $users = groups_get_potential_members($data->courseid, $data->roleid, $orderby); + $users = groups_get_potential_members($data->courseid, $data->roleid, $data->cohortid, $orderby); $usercnt = count($users); if ($data->allocateby == 'random') { diff --git a/group/autogroup_form.php b/group/autogroup_form.php index c159ac0eb517d..078e481a7ae86 100644 --- a/group/autogroup_form.php +++ b/group/autogroup_form.php @@ -1,6 +1,7 @@ dirroot.'/lib/formslib.php'); +require_once($CFG->dirroot.'/cohort/lib.php'); /// get url variables class autogroup_form extends moodleform { @@ -22,6 +23,16 @@ function definition() { $mform->setDefault('roleid', $CFG->defaultcourseroleid); } + $options = cohort_get_visible_list($COURSE); + if ($options) { + $options = array(0=>get_string('anycohort', 'cohort')) + $options; + $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options); + } else { + $mform->addElement('hidden','cohortid'); + $mform->setType('cohortid', PARAM_INT); + $mform->setDefault('cohortid', '0'); + } + $options = array('groups' => get_string('numgroups', 'group'), 'members' => get_string('nummembers', 'group')); $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options); @@ -91,7 +102,7 @@ function validation($data, $files) { $errors = parent::validation($data, $files); if ($data['allocateby'] != 'no') { - if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'])) { + if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $data['cohortid'])) { $errors['roleid'] = get_string('nousersinrole', 'group'); } diff --git a/group/lib.php b/group/lib.php index 9274b2f2a70d5..7111c4a15bc7d 100644 --- a/group/lib.php +++ b/group/lib.php @@ -513,10 +513,11 @@ function groups_get_possible_roles($context) { * Gets potential group members for grouping * @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 string $orderby The colum to sort users by * @return array An array of the users */ -function groups_get_potential_members($courseid, $roleid = null, $orderby = 'lastname ASC, firstname ASC') { +function groups_get_potential_members($courseid, $roleid = null, $cohortid = null, $orderby = 'lastname ASC, firstname ASC') { global $DB; $context = get_context_instance(CONTEXT_COURSE, $courseid); @@ -535,9 +536,17 @@ function groups_get_potential_members($courseid, $roleid = null, $orderby = 'las $where = ""; } + if ($cohortid) { + $cohortjoin = "JOIN {cohort_members} cm ON cm.userid = u.id + JOIN {cohort} c ON c.id = cm.cohortid"; + } else { + $cohortjoin = ""; + } + $sql = "SELECT u.id, u.username, u.firstname, u.lastname, u.idnumber FROM {user} u JOIN ($esql) e ON e.id = u.id + $cohortjoin $where ORDER BY $orderby"; diff --git a/lang/en/cohort.php b/lang/en/cohort.php index eaed0e5a118cc..414c992c46720 100644 --- a/lang/en/cohort.php +++ b/lang/en/cohort.php @@ -25,6 +25,7 @@ */ $string['addcohort'] = 'Add new cohort'; +$string['anycohort'] = 'Any'; $string['assign'] = 'Assign'; $string['assignto'] = 'Cohort \'{$a}\' members'; $string['backtocohorts'] = 'Back to cohorts'; @@ -48,3 +49,4 @@ $string['nocomponent'] = 'Created manually'; $string['potusers'] = 'Potential users'; $string['potusersmatching'] = 'Potential matching users'; +$string['selectfromcohort'] = 'Select members from cohort';