2009-11-01 12:00:47 +00:00
|
|
|
<?php
|
2007-09-24 21:55:15 +00:00
|
|
|
/**
|
|
|
|
* Create and allocate users go groups
|
|
|
|
*
|
|
|
|
* @author Matt Clarkson mattc@catalyst.net.nz
|
|
|
|
* @version 0.0.1
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @package groups
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once('../config.php');
|
2007-11-19 20:31:57 +00:00
|
|
|
require_once('lib.php');
|
2007-09-24 21:55:15 +00:00
|
|
|
require_once('autogroup_form.php');
|
|
|
|
|
2007-11-19 21:41:02 +00:00
|
|
|
if (!defined('AUTOGROUP_MIN_RATIO')) {
|
|
|
|
define('AUTOGROUP_MIN_RATIO', 0.7); // means minimum member count is 70% in the smallest group
|
|
|
|
}
|
2007-10-18 00:40:04 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
$courseid = required_param('courseid', PARAM_INT);
|
2009-07-27 10:33:00 +00:00
|
|
|
$PAGE->set_url('/group/autogroup.php', array('courseid' => $courseid));
|
2007-09-24 21:55:15 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
|
2008-05-14 06:15:56 +00:00
|
|
|
print_error('invalidcourseid');
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the user has permissions to manage groups.
|
|
|
|
require_login($course);
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
|
|
|
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
|
2007-09-24 21:55:15 +00:00
|
|
|
require_capability('moodle/course:managegroups', $context);
|
|
|
|
|
|
|
|
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id;
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
$strgroups = get_string('groups');
|
|
|
|
$strparticipants = get_string('participants');
|
|
|
|
$strautocreategroups = get_string('autocreategroups', 'group');
|
2007-09-24 21:55:15 +00:00
|
|
|
|
|
|
|
// Print the page and form
|
2007-11-19 20:31:57 +00:00
|
|
|
$preview = '';
|
|
|
|
$error = '';
|
2007-09-24 21:55:15 +00:00
|
|
|
|
|
|
|
/// Get applicable roles
|
|
|
|
$rolenames = array();
|
2010-03-31 07:41:31 +00:00
|
|
|
if ($roles = get_profile_roles($context)) {
|
2007-09-24 21:55:15 +00:00
|
|
|
foreach ($roles as $role) {
|
|
|
|
$rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the form
|
2007-11-19 20:31:57 +00:00
|
|
|
$editform = new autogroup_form(null, array('roles' => $rolenames));
|
|
|
|
$editform->set_data(array('courseid' => $courseid, 'seed' => time()));
|
2007-09-24 21:55:15 +00:00
|
|
|
|
|
|
|
/// Handle form submission
|
|
|
|
if ($editform->is_cancelled()) {
|
|
|
|
redirect($returnurl);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2008-06-09 16:53:30 +00:00
|
|
|
} elseif ($data = $editform->get_data()) {
|
2007-11-19 20:31:57 +00:00
|
|
|
|
|
|
|
/// Allocate members from the selected role to groups
|
|
|
|
switch ($data->allocateby) {
|
|
|
|
case 'no':
|
|
|
|
case 'random':
|
|
|
|
case 'lastname':
|
|
|
|
$orderby = 'lastname, firstname'; break;
|
|
|
|
case 'firstname':
|
|
|
|
$orderby = 'firstname, lastname'; break;
|
|
|
|
case 'idnumber':
|
|
|
|
$orderby = 'idnumber'; break;
|
|
|
|
default:
|
2008-05-14 06:15:56 +00:00
|
|
|
print_error('unknoworder');
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
$users = groups_get_potential_members($data->courseid, $data->roleid, $orderby);
|
2007-09-24 21:55:15 +00:00
|
|
|
$usercnt = count($users);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
if ($data->allocateby == 'random') {
|
2007-11-19 20:31:57 +00:00
|
|
|
srand($data->seed);
|
2007-09-24 21:55:15 +00:00
|
|
|
shuffle($users);
|
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
$groups = array();
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-10-18 00:40:04 +00:00
|
|
|
// Plan the allocation
|
2007-09-24 21:55:15 +00:00
|
|
|
if ($data->groupby == 'groups') {
|
2009-12-16 22:14:17 +00:00
|
|
|
$numgrps = $data->number;
|
2007-11-19 20:31:57 +00:00
|
|
|
$userpergrp = floor($usercnt/$numgrps);
|
|
|
|
|
|
|
|
} else { // members
|
2009-12-16 22:14:17 +00:00
|
|
|
$numgrps = ceil($usercnt/$data->number);
|
2007-11-19 20:31:57 +00:00
|
|
|
$userpergrp = $data->number;
|
|
|
|
|
|
|
|
if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
|
|
|
|
// If there would be one group with a small number of member reduce the number of groups
|
|
|
|
$missing = $userpergrp * $numgrps - $usercnt;
|
2007-11-19 21:41:02 +00:00
|
|
|
if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
|
2007-11-19 22:55:39 +00:00
|
|
|
// spread the users from the last small group
|
2007-11-19 20:31:57 +00:00
|
|
|
$numgrps--;
|
2007-11-19 22:55:39 +00:00
|
|
|
$userpergrp = floor($usercnt/$numgrps);
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-18 00:40:04 +00:00
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
// allocate the users - all groups equal count first
|
|
|
|
for ($i=0; $i<$numgrps; $i++) {
|
|
|
|
$groups[$i] = array();
|
|
|
|
$groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i);
|
|
|
|
$groups[$i]['members'] = array();
|
|
|
|
if ($data->allocateby == 'no') {
|
|
|
|
continue; // do not allocate users
|
|
|
|
}
|
|
|
|
for ($j=0; $j<$userpergrp; $j++) {
|
|
|
|
if (empty($users)) {
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
$user = array_shift($users);
|
|
|
|
$groups[$i]['members'][$user->id] = $user;
|
2007-10-18 00:40:04 +00:00
|
|
|
}
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
// now distribute the rest
|
|
|
|
if ($data->allocateby != 'no') {
|
|
|
|
for ($i=0; $i<$numgrps; $i++) {
|
|
|
|
if (empty($users)) {
|
|
|
|
break 1;
|
|
|
|
}
|
|
|
|
$user = array_shift($users);
|
|
|
|
$groups[$i]['members'][$user->id] = $user;
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
if (isset($data->preview)) {
|
2009-08-20 08:41:11 +00:00
|
|
|
$table = new html_table();
|
2007-11-19 20:31:57 +00:00
|
|
|
if ($data->allocateby == 'no') {
|
|
|
|
$table->head = array(get_string('groupscount', 'group', $numgrps));
|
|
|
|
$table->size = array('100%');
|
|
|
|
$table->align = array('left');
|
|
|
|
$table->width = '40%';
|
|
|
|
} else {
|
|
|
|
$table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt));
|
|
|
|
$table->size = array('20%', '70%', '10%');
|
|
|
|
$table->align = array('left', 'left', 'center');
|
|
|
|
$table->width = '90%';
|
2007-10-18 00:40:04 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
$table->data = array();
|
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
foreach ($groups as $group) {
|
2007-11-19 20:31:57 +00:00
|
|
|
$line = array();
|
|
|
|
if (groups_get_group_by_name($courseid, $group['name'])) {
|
|
|
|
$line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>';
|
|
|
|
$error = get_string('groupnameexists', 'group', $group['name']);
|
|
|
|
} else {
|
|
|
|
$line[] = $group['name'];
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
if ($data->allocateby != 'no') {
|
|
|
|
$unames = array();
|
|
|
|
foreach ($group['members'] as $user) {
|
|
|
|
$unames[] = fullname($user, true);
|
|
|
|
}
|
|
|
|
$line[] = implode(', ', $unames);
|
|
|
|
$line[] = count($group['members']);
|
|
|
|
}
|
|
|
|
$table->data[] = $line;
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2010-03-20 22:15:54 +00:00
|
|
|
$preview .= html_writer::table($table);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
} else {
|
2007-11-19 20:31:57 +00:00
|
|
|
$grouping = null;
|
|
|
|
$createdgrouping = null;
|
|
|
|
$createdgroups = array();
|
|
|
|
$failed = false;
|
|
|
|
|
|
|
|
// prepare grouping
|
|
|
|
if (!empty($data->grouping)) {
|
|
|
|
$groupingname = trim($data->groupingname);
|
|
|
|
if ($data->grouping < 0) {
|
|
|
|
$grouping = new object();
|
|
|
|
$grouping->courseid = $COURSE->id;
|
|
|
|
$grouping->name = $groupingname;
|
2009-02-17 16:50:36 +00:00
|
|
|
$grouping->id = groups_create_grouping($grouping);
|
2007-11-19 20:31:57 +00:00
|
|
|
$createdgrouping = $grouping->id;
|
|
|
|
} else {
|
|
|
|
$grouping = groups_get_grouping($data->grouping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the groups data
|
2009-12-16 22:14:17 +00:00
|
|
|
foreach ($groups as $key=>$group) {
|
2007-11-19 20:31:57 +00:00
|
|
|
if (groups_get_group_by_name($courseid, $group['name'])) {
|
|
|
|
$error = get_string('groupnameexists', 'group', $group['name']);
|
|
|
|
$failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$newgroup = new object();
|
2007-09-24 21:55:15 +00:00
|
|
|
$newgroup->courseid = $data->courseid;
|
2007-11-19 20:31:57 +00:00
|
|
|
$newgroup->name = $group['name'];
|
2009-02-17 16:50:36 +00:00
|
|
|
$groupid = groups_create_group($newgroup);
|
2007-11-19 20:31:57 +00:00
|
|
|
$createdgroups[] = $groupid;
|
2007-09-24 21:55:15 +00:00
|
|
|
foreach($group['members'] as $user) {
|
2007-11-19 20:31:57 +00:00
|
|
|
groups_add_member($groupid, $user->id);
|
|
|
|
}
|
|
|
|
if ($grouping) {
|
|
|
|
groups_assign_grouping($grouping->id, $groupid);
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
if ($failed) {
|
|
|
|
foreach ($createdgroups as $groupid) {
|
|
|
|
groups_delete_group($groupid);
|
|
|
|
}
|
|
|
|
if ($createdgrouping) {
|
|
|
|
groups_delete_grouping($createdgrouping);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redirect($returnurl);
|
|
|
|
}
|
|
|
|
}
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
|
|
|
|
$PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
|
2009-09-07 06:43:02 +00:00
|
|
|
$PAGE->navbar->add($strautocreategroups);
|
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
/// Print header
|
2009-09-07 06:43:02 +00:00
|
|
|
$PAGE->set_title($strgroups);
|
|
|
|
$PAGE->set_heading(': '.$strgroups);
|
|
|
|
echo $OUTPUT->header();
|
2009-08-06 08:15:26 +00:00
|
|
|
echo $OUTPUT->heading($strautocreategroups);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
|
|
|
if ($error != '') {
|
2009-08-18 05:00:29 +00:00
|
|
|
echo $OUTPUT->notification($error);
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
2007-09-24 21:55:15 +00:00
|
|
|
|
|
|
|
/// Display the form
|
|
|
|
$editform->display();
|
2007-11-19 20:31:57 +00:00
|
|
|
|
|
|
|
if($preview !== '') {
|
2009-12-16 22:14:17 +00:00
|
|
|
echo $OUTPUT->heading(get_string('groupspreview', 'group'));
|
2007-11-19 20:31:57 +00:00
|
|
|
|
|
|
|
echo $preview;
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2009-08-06 14:10:09 +00:00
|
|
|
echo $OUTPUT->footer();
|