Skip to content

Commit

Permalink
MDL-21781 added support for autocreation of groups from cohorts
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Apr 23, 2010
1 parent 62bc501 commit 92343cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
36 changes: 36 additions & 0 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion group/autogroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
13 changes: 12 additions & 1 deletion group/autogroup_form.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once($CFG->dirroot.'/lib/formslib.php');
require_once($CFG->dirroot.'/cohort/lib.php');

/// get url variables
class autogroup_form extends moodleform {
Expand All @@ -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);
Expand Down Expand Up @@ -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');
}

Expand Down
11 changes: 10 additions & 1 deletion group/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";

Expand Down
2 changes: 2 additions & 0 deletions lang/en/cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -48,3 +49,4 @@
$string['nocomponent'] = 'Created manually';
$string['potusers'] = 'Potential users';
$string['potusersmatching'] = 'Potential matching users';
$string['selectfromcohort'] = 'Select members from cohort';

0 comments on commit 92343cd

Please sign in to comment.