forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autogroup.php
243 lines (209 loc) · 7.79 KB
/
autogroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* Create and allocate users go groups
*
* @author Matt Clarkson [email protected]
* @version 0.0.1
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package groups
*/
require_once('../config.php');
require_once('lib.php');
require_once('autogroup_form.php');
if (!defined('AUTOGROUP_MIN_RATIO')) {
define('AUTOGROUP_MIN_RATIO', 0.7); // means minimum member count is 70% in the smallest group
}
$courseid = required_param('courseid', PARAM_INT);
$PAGE->set_url('/group/autogroup.php', array('courseid' => $courseid));
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('invalidcourseid');
}
// Make sure that the user has permissions to manage groups.
require_login($course);
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
require_capability('moodle/course:managegroups', $context);
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id;
$strgroups = get_string('groups');
$strparticipants = get_string('participants');
$strautocreategroups = get_string('autocreategroups', 'group');
// Print the page and form
$preview = '';
$error = '';
/// Get applicable roles
$rolenames = array();
if ($roles = get_profile_roles($context)) {
foreach ($roles as $role) {
$rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
}
}
/// Create the form
$editform = new autogroup_form(null, array('roles' => $rolenames));
$editform->set_data(array('courseid' => $courseid, 'seed' => time()));
/// Handle form submission
if ($editform->is_cancelled()) {
redirect($returnurl);
} elseif ($data = $editform->get_data()) {
/// 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:
print_error('unknoworder');
}
$users = groups_get_potential_members($data->courseid, $data->roleid, $data->cohortid, $orderby);
$usercnt = count($users);
if ($data->allocateby == 'random') {
srand($data->seed);
shuffle($users);
}
$groups = array();
// Plan the allocation
if ($data->groupby == 'groups') {
$numgrps = $data->number;
$userpergrp = floor($usercnt/$numgrps);
} else { // members
$numgrps = ceil($usercnt/$data->number);
$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;
if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
// spread the users from the last small group
$numgrps--;
$userpergrp = floor($usercnt/$numgrps);
}
}
}
// 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;
}
}
// 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;
}
}
if (isset($data->preview)) {
$table = new html_table();
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%';
}
$table->data = array();
foreach ($groups as $group) {
$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'];
}
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;
}
$preview .= html_writer::table($table);
} else {
$grouping = null;
$createdgrouping = null;
$createdgroups = array();
$failed = false;
// prepare grouping
if (!empty($data->grouping)) {
$groupingname = trim($data->groupingname);
if ($data->grouping < 0) {
$grouping = new stdClass();
$grouping->courseid = $COURSE->id;
$grouping->name = $groupingname;
$grouping->id = groups_create_grouping($grouping);
$createdgrouping = $grouping->id;
} else {
$grouping = groups_get_grouping($data->grouping);
}
}
// Save the groups data
foreach ($groups as $key=>$group) {
if (groups_get_group_by_name($courseid, $group['name'])) {
$error = get_string('groupnameexists', 'group', $group['name']);
$failed = true;
break;
}
$newgroup = new stdClass();
$newgroup->courseid = $data->courseid;
$newgroup->name = $group['name'];
$groupid = groups_create_group($newgroup);
$createdgroups[] = $groupid;
foreach($group['members'] as $user) {
groups_add_member($groupid, $user->id);
}
if ($grouping) {
groups_assign_grouping($grouping->id, $groupid);
}
}
if ($failed) {
foreach ($createdgroups as $groupid) {
groups_delete_group($groupid);
}
if ($createdgrouping) {
groups_delete_grouping($createdgrouping);
}
} else {
redirect($returnurl);
}
}
}
$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)));
$PAGE->navbar->add($strautocreategroups);
/// Print header
$PAGE->set_title($strgroups);
$PAGE->set_heading($course->fullname. ': '.$strgroups);
echo $OUTPUT->header();
echo $OUTPUT->heading($strautocreategroups);
if ($error != '') {
echo $OUTPUT->notification($error);
}
/// Display the form
$editform->display();
if($preview !== '') {
echo $OUTPUT->heading(get_string('groupspreview', 'group'));
echo $preview;
}
echo $OUTPUT->footer();