mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-21781 added support for autocreation of groups from cohorts
This commit is contained in:
parent
62bc50190b
commit
92343cd2eb
@ -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
|
||||
*/
|
||||
|
@ -71,7 +71,7 @@ if ($editform->is_cancelled()) {
|
||||
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') {
|
||||
|
@ -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 {
|
||||
@ -22,6 +23,16 @@ class autogroup_form extends moodleform {
|
||||
$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 @@ class autogroup_form extends moodleform {
|
||||
$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');
|
||||
}
|
||||
|
||||
|
@ -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";
|
||||
|
||||
|
@ -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['name'] = 'Name';
|
||||
$string['nocomponent'] = 'Created manually';
|
||||
$string['potusers'] = 'Potential users';
|
||||
$string['potusersmatching'] = 'Potential matching users';
|
||||
$string['selectfromcohort'] = 'Select members from cohort';
|
||||
|
Loading…
x
Reference in New Issue
Block a user