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