MDL-21781 added support for autocreation of groups from cohorts
[moodle.git] / group / autogroup.php
CommitLineData
4ca6cfbf 1<?php
acf000b0 2/**
3 * Create and allocate users go groups
4 *
5 * @author Matt Clarkson mattc@catalyst.net.nz
6 * @version 0.0.1
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package groups
9 */
10
11require_once('../config.php');
f16fa0a3 12require_once('lib.php');
acf000b0 13require_once('autogroup_form.php');
14
9509119b 15if (!defined('AUTOGROUP_MIN_RATIO')) {
16 define('AUTOGROUP_MIN_RATIO', 0.7); // means minimum member count is 70% in the smallest group
17}
e34f6203 18
acf000b0 19$courseid = required_param('courseid', PARAM_INT);
f8065dd2 20$PAGE->set_url('/group/autogroup.php', array('courseid' => $courseid));
acf000b0 21
dfdaabd6 22if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b121a4ee 23 print_error('invalidcourseid');
acf000b0 24}
25
26// Make sure that the user has permissions to manage groups.
27require_login($course);
28
f16fa0a3 29$context = get_context_instance(CONTEXT_COURSE, $courseid);
30$systemcontext = get_context_instance(CONTEXT_SYSTEM);
acf000b0 31require_capability('moodle/course:managegroups', $context);
32
33$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id;
34
f16fa0a3 35$strgroups = get_string('groups');
36$strparticipants = get_string('participants');
37$strautocreategroups = get_string('autocreategroups', 'group');
acf000b0 38
39// Print the page and form
f16fa0a3 40$preview = '';
41$error = '';
acf000b0 42
43/// Get applicable roles
44$rolenames = array();
4f0c2d00 45if ($roles = get_profile_roles($context)) {
acf000b0 46 foreach ($roles as $role) {
acf000b0 47 $rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
48 }
49}
50
51/// Create the form
f16fa0a3 52$editform = new autogroup_form(null, array('roles' => $rolenames));
53$editform->set_data(array('courseid' => $courseid, 'seed' => time()));
acf000b0 54
55/// Handle form submission
56if ($editform->is_cancelled()) {
57 redirect($returnurl);
f16fa0a3 58
294ce987 59} elseif ($data = $editform->get_data()) {
f16fa0a3 60
61 /// Allocate members from the selected role to groups
62 switch ($data->allocateby) {
63 case 'no':
64 case 'random':
65 case 'lastname':
66 $orderby = 'lastname, firstname'; break;
67 case 'firstname':
68 $orderby = 'firstname, lastname'; break;
69 case 'idnumber':
70 $orderby = 'idnumber'; break;
71 default:
b121a4ee 72 print_error('unknoworder');
acf000b0 73 }
92343cd2 74 $users = groups_get_potential_members($data->courseid, $data->roleid, $data->cohortid, $orderby);
acf000b0 75 $usercnt = count($users);
f16fa0a3 76
acf000b0 77 if ($data->allocateby == 'random') {
f16fa0a3 78 srand($data->seed);
acf000b0 79 shuffle($users);
80 }
f16fa0a3 81
acf000b0 82 $groups = array();
f16fa0a3 83
e34f6203 84 // Plan the allocation
acf000b0 85 if ($data->groupby == 'groups') {
4454447d 86 $numgrps = $data->number;
f16fa0a3 87 $userpergrp = floor($usercnt/$numgrps);
88
89 } else { // members
4454447d 90 $numgrps = ceil($usercnt/$data->number);
f16fa0a3 91 $userpergrp = $data->number;
92
93 if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
94 // If there would be one group with a small number of member reduce the number of groups
95 $missing = $userpergrp * $numgrps - $usercnt;
9509119b 96 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
0112ed8e 97 // spread the users from the last small group
f16fa0a3 98 $numgrps--;
0112ed8e 99 $userpergrp = floor($usercnt/$numgrps);
f16fa0a3 100 }
101 }
102 }
e34f6203 103
f16fa0a3 104 // allocate the users - all groups equal count first
105 for ($i=0; $i<$numgrps; $i++) {
106 $groups[$i] = array();
107 $groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i);
108 $groups[$i]['members'] = array();
109 if ($data->allocateby == 'no') {
110 continue; // do not allocate users
111 }
112 for ($j=0; $j<$userpergrp; $j++) {
113 if (empty($users)) {
114 break 2;
115 }
116 $user = array_shift($users);
117 $groups[$i]['members'][$user->id] = $user;
e34f6203 118 }
acf000b0 119 }
f16fa0a3 120 // now distribute the rest
121 if ($data->allocateby != 'no') {
122 for ($i=0; $i<$numgrps; $i++) {
123 if (empty($users)) {
124 break 1;
125 }
126 $user = array_shift($users);
127 $groups[$i]['members'][$user->id] = $user;
acf000b0 128 }
129 }
f16fa0a3 130
acf000b0 131 if (isset($data->preview)) {
768cefa0 132 $table = new html_table();
f16fa0a3 133 if ($data->allocateby == 'no') {
134 $table->head = array(get_string('groupscount', 'group', $numgrps));
135 $table->size = array('100%');
136 $table->align = array('left');
137 $table->width = '40%';
138 } else {
139 $table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt));
140 $table->size = array('20%', '70%', '10%');
141 $table->align = array('left', 'left', 'center');
142 $table->width = '90%';
e34f6203 143 }
f16fa0a3 144 $table->data = array();
145
acf000b0 146 foreach ($groups as $group) {
f16fa0a3 147 $line = array();
148 if (groups_get_group_by_name($courseid, $group['name'])) {
149 $line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>';
150 $error = get_string('groupnameexists', 'group', $group['name']);
151 } else {
152 $line[] = $group['name'];
acf000b0 153 }
f16fa0a3 154 if ($data->allocateby != 'no') {
155 $unames = array();
156 foreach ($group['members'] as $user) {
157 $unames[] = fullname($user, true);
158 }
159 $line[] = implode(', ', $unames);
160 $line[] = count($group['members']);
161 }
162 $table->data[] = $line;
acf000b0 163 }
f16fa0a3 164
16be8974 165 $preview .= html_writer::table($table);
f16fa0a3 166
acf000b0 167 } else {
f16fa0a3 168 $grouping = null;
169 $createdgrouping = null;
170 $createdgroups = array();
171 $failed = false;
172
173 // prepare grouping
174 if (!empty($data->grouping)) {
175 $groupingname = trim($data->groupingname);
176 if ($data->grouping < 0) {
177 $grouping = new object();
178 $grouping->courseid = $COURSE->id;
179 $grouping->name = $groupingname;
5e2f308b 180 $grouping->id = groups_create_grouping($grouping);
f16fa0a3 181 $createdgrouping = $grouping->id;
182 } else {
183 $grouping = groups_get_grouping($data->grouping);
184 }
185 }
186
187 // Save the groups data
4454447d 188 foreach ($groups as $key=>$group) {
f16fa0a3 189 if (groups_get_group_by_name($courseid, $group['name'])) {
190 $error = get_string('groupnameexists', 'group', $group['name']);
191 $failed = true;
192 break;
193 }
194 $newgroup = new object();
acf000b0 195 $newgroup->courseid = $data->courseid;
f16fa0a3 196 $newgroup->name = $group['name'];
5e2f308b 197 $groupid = groups_create_group($newgroup);
f16fa0a3 198 $createdgroups[] = $groupid;
acf000b0 199 foreach($group['members'] as $user) {
f16fa0a3 200 groups_add_member($groupid, $user->id);
201 }
202 if ($grouping) {
203 groups_assign_grouping($grouping->id, $groupid);
acf000b0 204 }
205 }
acf000b0 206
f16fa0a3 207 if ($failed) {
208 foreach ($createdgroups as $groupid) {
209 groups_delete_group($groupid);
210 }
211 if ($createdgrouping) {
212 groups_delete_grouping($createdgrouping);
213 }
214 } else {
215 redirect($returnurl);
216 }
217 }
acf000b0 218}
f16fa0a3 219
a6855934
PS
220$PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
221$PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
b87573d7 222$PAGE->navbar->add($strautocreategroups);
223
acf000b0 224/// Print header
b87573d7 225$PAGE->set_title($strgroups);
226$PAGE->set_heading(': '.$strgroups);
227echo $OUTPUT->header();
9cb990ec 228echo $OUTPUT->heading($strautocreategroups);
f16fa0a3 229
230if ($error != '') {
a1c54f7a 231 echo $OUTPUT->notification($error);
f16fa0a3 232}
acf000b0 233
234/// Display the form
235$editform->display();
f16fa0a3 236
237if($preview !== '') {
4454447d 238 echo $OUTPUT->heading(get_string('groupspreview', 'group'));
f16fa0a3 239
240 echo $preview;
acf000b0 241}
242
653468d4 243echo $OUTPUT->footer();