group MDL-19798 Updated print_header and build_navigation to OUTPUT and PAGE equivalents
[moodle.git] / group / autogroup.php
CommitLineData
acf000b0 1<?php // $Id$
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();
acf000b0 45if ($roles = get_roles_used_in_context($context, true)) {
46 $canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
f16fa0a3 47 $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $systemcontext);
acf000b0 48
49 foreach ($roles as $role) {
50 if (!isset($canviewroles[$role->id])) { // Avoid this role (eg course creator)
acf000b0 51 continue;
52 }
53 if (isset($doanythingroles[$role->id])) { // Avoid this role (ie admin)
acf000b0 54 continue;
55 }
56 $rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
57 }
58}
59
60/// Create the form
f16fa0a3 61$editform = new autogroup_form(null, array('roles' => $rolenames));
62$editform->set_data(array('courseid' => $courseid, 'seed' => time()));
acf000b0 63
64/// Handle form submission
65if ($editform->is_cancelled()) {
66 redirect($returnurl);
f16fa0a3 67
294ce987 68} elseif ($data = $editform->get_data()) {
f16fa0a3 69
70 /// Allocate members from the selected role to groups
71 switch ($data->allocateby) {
72 case 'no':
73 case 'random':
74 case 'lastname':
75 $orderby = 'lastname, firstname'; break;
76 case 'firstname':
77 $orderby = 'firstname, lastname'; break;
78 case 'idnumber':
79 $orderby = 'idnumber'; break;
80 default:
b121a4ee 81 print_error('unknoworder');
acf000b0 82 }
f16fa0a3 83 $users = groups_get_potential_members($data->courseid, $data->roleid, $orderby);
acf000b0 84 $usercnt = count($users);
f16fa0a3 85
acf000b0 86 if ($data->allocateby == 'random') {
f16fa0a3 87 srand($data->seed);
acf000b0 88 shuffle($users);
89 }
f16fa0a3 90
acf000b0 91 $groups = array();
f16fa0a3 92
e34f6203 93 // Plan the allocation
acf000b0 94 if ($data->groupby == 'groups') {
f16fa0a3 95 $numgrps = $data->number;
96 $userpergrp = floor($usercnt/$numgrps);
97
98 } else { // members
99 $numgrps = ceil($usercnt/$data->number);
100 $userpergrp = $data->number;
101
102 if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
103 // If there would be one group with a small number of member reduce the number of groups
104 $missing = $userpergrp * $numgrps - $usercnt;
9509119b 105 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
0112ed8e 106 // spread the users from the last small group
f16fa0a3 107 $numgrps--;
0112ed8e 108 $userpergrp = floor($usercnt/$numgrps);
f16fa0a3 109 }
110 }
111 }
e34f6203 112
f16fa0a3 113 // allocate the users - all groups equal count first
114 for ($i=0; $i<$numgrps; $i++) {
115 $groups[$i] = array();
116 $groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i);
117 $groups[$i]['members'] = array();
118 if ($data->allocateby == 'no') {
119 continue; // do not allocate users
120 }
121 for ($j=0; $j<$userpergrp; $j++) {
122 if (empty($users)) {
123 break 2;
124 }
125 $user = array_shift($users);
126 $groups[$i]['members'][$user->id] = $user;
e34f6203 127 }
acf000b0 128 }
f16fa0a3 129 // now distribute the rest
130 if ($data->allocateby != 'no') {
131 for ($i=0; $i<$numgrps; $i++) {
132 if (empty($users)) {
133 break 1;
134 }
135 $user = array_shift($users);
136 $groups[$i]['members'][$user->id] = $user;
acf000b0 137 }
138 }
f16fa0a3 139
acf000b0 140 if (isset($data->preview)) {
768cefa0 141 $table = new html_table();
f16fa0a3 142 if ($data->allocateby == 'no') {
143 $table->head = array(get_string('groupscount', 'group', $numgrps));
144 $table->size = array('100%');
145 $table->align = array('left');
146 $table->width = '40%';
147 } else {
148 $table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt));
149 $table->size = array('20%', '70%', '10%');
150 $table->align = array('left', 'left', 'center');
151 $table->width = '90%';
e34f6203 152 }
f16fa0a3 153 $table->data = array();
154
acf000b0 155 foreach ($groups as $group) {
f16fa0a3 156 $line = array();
157 if (groups_get_group_by_name($courseid, $group['name'])) {
158 $line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>';
159 $error = get_string('groupnameexists', 'group', $group['name']);
160 } else {
161 $line[] = $group['name'];
acf000b0 162 }
f16fa0a3 163 if ($data->allocateby != 'no') {
164 $unames = array();
165 foreach ($group['members'] as $user) {
166 $unames[] = fullname($user, true);
167 }
168 $line[] = implode(', ', $unames);
169 $line[] = count($group['members']);
170 }
171 $table->data[] = $line;
acf000b0 172 }
f16fa0a3 173
768cefa0 174 $preview .= $OUTPUT->table($table);
f16fa0a3 175
acf000b0 176 } else {
f16fa0a3 177 $grouping = null;
178 $createdgrouping = null;
179 $createdgroups = array();
180 $failed = false;
181
182 // prepare grouping
183 if (!empty($data->grouping)) {
184 $groupingname = trim($data->groupingname);
185 if ($data->grouping < 0) {
186 $grouping = new object();
187 $grouping->courseid = $COURSE->id;
188 $grouping->name = $groupingname;
5e2f308b 189 $grouping->id = groups_create_grouping($grouping);
f16fa0a3 190 $createdgrouping = $grouping->id;
191 } else {
192 $grouping = groups_get_grouping($data->grouping);
193 }
194 }
195
196 // Save the groups data
197 foreach ($groups as $key=>$group) {
198 if (groups_get_group_by_name($courseid, $group['name'])) {
199 $error = get_string('groupnameexists', 'group', $group['name']);
200 $failed = true;
201 break;
202 }
203 $newgroup = new object();
acf000b0 204 $newgroup->courseid = $data->courseid;
f16fa0a3 205 $newgroup->name = $group['name'];
5e2f308b 206 $groupid = groups_create_group($newgroup);
f16fa0a3 207 $createdgroups[] = $groupid;
acf000b0 208 foreach($group['members'] as $user) {
f16fa0a3 209 groups_add_member($groupid, $user->id);
210 }
211 if ($grouping) {
212 groups_assign_grouping($grouping->id, $groupid);
acf000b0 213 }
214 }
acf000b0 215
f16fa0a3 216 if ($failed) {
217 foreach ($createdgroups as $groupid) {
218 groups_delete_group($groupid);
219 }
220 if ($createdgrouping) {
221 groups_delete_grouping($createdgrouping);
222 }
223 } else {
224 redirect($returnurl);
225 }
226 }
acf000b0 227}
f16fa0a3 228
b87573d7 229$PAGE->navbar->add($strparticipants, new moodle_url($CFG->wwwroot.'/user/index.php', array('id'=>$courseid)));
230$PAGE->navbar->add($strgroups, new moodle_url($CFG->wwwroot.'/group/index.php', array('id'=>$courseid)));
231$PAGE->navbar->add($strautocreategroups);
232
acf000b0 233/// Print header
b87573d7 234$PAGE->set_title($strgroups);
235$PAGE->set_heading(': '.$strgroups);
236echo $OUTPUT->header();
9cb990ec 237echo $OUTPUT->heading($strautocreategroups);
f16fa0a3 238
239if ($error != '') {
a1c54f7a 240 echo $OUTPUT->notification($error);
f16fa0a3 241}
acf000b0 242
243/// Display the form
244$editform->display();
f16fa0a3 245
246if($preview !== '') {
9cb990ec 247 echo $OUTPUT->heading(get_string('groupspreview', 'group'));
f16fa0a3 248
249 echo $preview;
acf000b0 250}
251
653468d4 252echo $OUTPUT->footer();
b121a4ee 253?>