weekly release 3.10.1+
[moodle.git] / group / autogroup.php
CommitLineData
4ca6cfbf 1<?php
4d8e2417
AG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
acf000b0 18/**
4d8e2417 19 * Create and allocate users to groups
acf000b0 20 *
4d8e2417
AG
21 * @package core_group
22 * @copyright Matt Clarkson mattc@catalyst.net.nz
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
acf000b0 24 */
25
26require_once('../config.php');
f16fa0a3 27require_once('lib.php');
acf000b0 28require_once('autogroup_form.php');
29
9509119b 30if (!defined('AUTOGROUP_MIN_RATIO')) {
31 define('AUTOGROUP_MIN_RATIO', 0.7); // means minimum member count is 70% in the smallest group
32}
e34f6203 33
acf000b0 34$courseid = required_param('courseid', PARAM_INT);
f8065dd2 35$PAGE->set_url('/group/autogroup.php', array('courseid' => $courseid));
acf000b0 36
dfdaabd6 37if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b121a4ee 38 print_error('invalidcourseid');
acf000b0 39}
40
41// Make sure that the user has permissions to manage groups.
42require_login($course);
43
bf0f06b1 44$context = context_course::instance($courseid);
acf000b0 45require_capability('moodle/course:managegroups', $context);
46
47$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id;
48
f16fa0a3 49$strgroups = get_string('groups');
50$strparticipants = get_string('participants');
51$strautocreategroups = get_string('autocreategroups', 'group');
acf000b0 52
f196f42c
FM
53$PAGE->set_title($strgroups);
54$PAGE->set_heading($course->fullname. ': '.$strgroups);
566889aa 55$PAGE->set_pagelayout('admin');
f196f42c
FM
56navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $courseid)));
57
acf000b0 58// Print the page and form
f16fa0a3 59$preview = '';
60$error = '';
acf000b0 61
c52551dc 62/// Get applicable roles - used in menus etc later on
7665c701 63$rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_BOTH, true);
acf000b0 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 }
e4ebf7ef
GF
88 $source = array();
89 if ($data->cohortid) {
90 $source['cohortid'] = $data->cohortid;
91 }
92 if ($data->groupingid) {
93 $source['groupingid'] = $data->groupingid;
94 }
95 if ($data->groupid) {
96 $source['groupid'] = $data->groupid;
97 }
7b96ffb0 98
3ee812fa
MN
99 // Display only active users if the option was selected or they do not have the capability to view suspended users.
100 $onlyactive = !empty($data->includeonlyactiveenrol) || !has_capability('moodle/course:viewsuspendedusers', $context);
7b96ffb0 101
21253ed6 102 $extrafields = get_extra_user_fields($context);
3ee812fa 103 $users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup),
21253ed6 104 $onlyactive, $extrafields);
acf000b0 105 $usercnt = count($users);
f16fa0a3 106
acf000b0 107 if ($data->allocateby == 'random') {
f16fa0a3 108 srand($data->seed);
acf000b0 109 shuffle($users);
110 }
f16fa0a3 111
acf000b0 112 $groups = array();
f16fa0a3 113
e34f6203 114 // Plan the allocation
acf000b0 115 if ($data->groupby == 'groups') {
4454447d 116 $numgrps = $data->number;
f16fa0a3 117 $userpergrp = floor($usercnt/$numgrps);
118
119 } else { // members
4454447d 120 $numgrps = ceil($usercnt/$data->number);
f16fa0a3 121 $userpergrp = $data->number;
122
123 if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
124 // If there would be one group with a small number of member reduce the number of groups
125 $missing = $userpergrp * $numgrps - $usercnt;
9509119b 126 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
0112ed8e 127 // spread the users from the last small group
f16fa0a3 128 $numgrps--;
0112ed8e 129 $userpergrp = floor($usercnt/$numgrps);
f16fa0a3 130 }
131 }
132 }
e34f6203 133
f16fa0a3 134 // allocate the users - all groups equal count first
135 for ($i=0; $i<$numgrps; $i++) {
136 $groups[$i] = array();
137 $groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i);
138 $groups[$i]['members'] = array();
139 if ($data->allocateby == 'no') {
140 continue; // do not allocate users
141 }
142 for ($j=0; $j<$userpergrp; $j++) {
143 if (empty($users)) {
144 break 2;
145 }
146 $user = array_shift($users);
147 $groups[$i]['members'][$user->id] = $user;
e34f6203 148 }
acf000b0 149 }
f16fa0a3 150 // now distribute the rest
151 if ($data->allocateby != 'no') {
152 for ($i=0; $i<$numgrps; $i++) {
153 if (empty($users)) {
154 break 1;
155 }
156 $user = array_shift($users);
157 $groups[$i]['members'][$user->id] = $user;
acf000b0 158 }
159 }
f16fa0a3 160
acf000b0 161 if (isset($data->preview)) {
768cefa0 162 $table = new html_table();
f16fa0a3 163 if ($data->allocateby == 'no') {
164 $table->head = array(get_string('groupscount', 'group', $numgrps));
165 $table->size = array('100%');
166 $table->align = array('left');
167 $table->width = '40%';
168 } else {
169 $table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt));
170 $table->size = array('20%', '70%', '10%');
171 $table->align = array('left', 'left', 'center');
172 $table->width = '90%';
e34f6203 173 }
f16fa0a3 174 $table->data = array();
6e8ed1a8 175 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
acf000b0 176 foreach ($groups as $group) {
f16fa0a3 177 $line = array();
178 if (groups_get_group_by_name($courseid, $group['name'])) {
179 $line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>';
180 $error = get_string('groupnameexists', 'group', $group['name']);
181 } else {
182 $line[] = $group['name'];
acf000b0 183 }
f16fa0a3 184 if ($data->allocateby != 'no') {
185 $unames = array();
186 foreach ($group['members'] as $user) {
6e8ed1a8 187 $fullname = fullname($user, $viewfullnames);
21253ed6
PH
188 if ($extrafields) {
189 $extrafieldsdisplay = [];
190 foreach ($extrafields as $field) {
191 $extrafieldsdisplay[] = s($user->{$field});
192 }
193 $fullname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
194 }
195
196 $unames[] = $fullname;
f16fa0a3 197 }
198 $line[] = implode(', ', $unames);
199 $line[] = count($group['members']);
200 }
201 $table->data[] = $line;
acf000b0 202 }
f16fa0a3 203
16be8974 204 $preview .= html_writer::table($table);
f16fa0a3 205
acf000b0 206 } else {
f16fa0a3 207 $grouping = null;
208 $createdgrouping = null;
209 $createdgroups = array();
210 $failed = false;
211
212 // prepare grouping
213 if (!empty($data->grouping)) {
f16fa0a3 214 if ($data->grouping < 0) {
ace9051c 215 $grouping = new stdClass();
f16fa0a3 216 $grouping->courseid = $COURSE->id;
ca50186f 217 $grouping->name = trim($data->groupingname);
5e2f308b 218 $grouping->id = groups_create_grouping($grouping);
f16fa0a3 219 $createdgrouping = $grouping->id;
220 } else {
221 $grouping = groups_get_grouping($data->grouping);
222 }
223 }
224
225 // Save the groups data
4454447d 226 foreach ($groups as $key=>$group) {
f16fa0a3 227 if (groups_get_group_by_name($courseid, $group['name'])) {
228 $error = get_string('groupnameexists', 'group', $group['name']);
229 $failed = true;
230 break;
231 }
ace9051c 232 $newgroup = new stdClass();
acf000b0 233 $newgroup->courseid = $data->courseid;
f16fa0a3 234 $newgroup->name = $group['name'];
e5e5b377 235 $newgroup->enablemessaging = $data->enablemessaging;
5e2f308b 236 $groupid = groups_create_group($newgroup);
f16fa0a3 237 $createdgroups[] = $groupid;
acf000b0 238 foreach($group['members'] as $user) {
f16fa0a3 239 groups_add_member($groupid, $user->id);
240 }
241 if ($grouping) {
e17dbeeb
SH
242 // Ask this function not to invalidate the cache, we'll do that manually once at the end.
243 groups_assign_grouping($grouping->id, $groupid, null, false);
acf000b0 244 }
245 }
acf000b0 246
e17dbeeb
SH
247 // Invalidate the course groups cache seeing as we've changed it.
248 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
249
f16fa0a3 250 if ($failed) {
251 foreach ($createdgroups as $groupid) {
252 groups_delete_group($groupid);
253 }
254 if ($createdgrouping) {
255 groups_delete_grouping($createdgrouping);
256 }
257 } else {
258 redirect($returnurl);
259 }
260 }
acf000b0 261}
f16fa0a3 262
a6855934
PS
263$PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
264$PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
b87573d7 265$PAGE->navbar->add($strautocreategroups);
266
b87573d7 267echo $OUTPUT->header();
9cb990ec 268echo $OUTPUT->heading($strautocreategroups);
f16fa0a3 269
270if ($error != '') {
a1c54f7a 271 echo $OUTPUT->notification($error);
f16fa0a3 272}
acf000b0 273
274/// Display the form
275$editform->display();
f16fa0a3 276
277if($preview !== '') {
4454447d 278 echo $OUTPUT->heading(get_string('groupspreview', 'group'));
f16fa0a3 279
280 echo $preview;
acf000b0 281}
282
e17dbeeb 283echo $OUTPUT->footer();