Merge branch 'MDL-32573' of git://github.com/danpoltawski/moodle
[moodle.git] / group / autogroup.php
blob35d36d0e20b30f959866753de2b8d0f783cac7e2
1 <?php
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.
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/>.
18 /**
19 * Create and allocate users to groups
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
26 require_once('../config.php');
27 require_once('lib.php');
28 require_once('autogroup_form.php');
30 if (!defined('AUTOGROUP_MIN_RATIO')) {
31 define('AUTOGROUP_MIN_RATIO', 0.7); // means minimum member count is 70% in the smallest group
34 $courseid = required_param('courseid', PARAM_INT);
35 $PAGE->set_url('/group/autogroup.php', array('courseid' => $courseid));
37 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
38 print_error('invalidcourseid');
41 // Make sure that the user has permissions to manage groups.
42 require_login($course);
44 $context = get_context_instance(CONTEXT_COURSE, $courseid);
45 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
46 require_capability('moodle/course:managegroups', $context);
48 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id;
50 $strgroups = get_string('groups');
51 $strparticipants = get_string('participants');
52 $strautocreategroups = get_string('autocreategroups', 'group');
54 // Print the page and form
55 $preview = '';
56 $error = '';
58 /// Get applicable roles
59 $rolenames = array();
60 if ($roles = get_profile_roles($context)) {
61 foreach ($roles as $role) {
62 $rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
66 /// Create the form
67 $editform = new autogroup_form(null, array('roles' => $rolenames));
68 $editform->set_data(array('courseid' => $courseid, 'seed' => time()));
70 /// Handle form submission
71 if ($editform->is_cancelled()) {
72 redirect($returnurl);
74 } elseif ($data = $editform->get_data()) {
76 /// Allocate members from the selected role to groups
77 switch ($data->allocateby) {
78 case 'no':
79 case 'random':
80 case 'lastname':
81 $orderby = 'lastname, firstname'; break;
82 case 'firstname':
83 $orderby = 'firstname, lastname'; break;
84 case 'idnumber':
85 $orderby = 'idnumber'; break;
86 default:
87 print_error('unknoworder');
89 $users = groups_get_potential_members($data->courseid, $data->roleid, $data->cohortid, $orderby);
90 $usercnt = count($users);
92 if ($data->allocateby == 'random') {
93 srand($data->seed);
94 shuffle($users);
97 $groups = array();
99 // Plan the allocation
100 if ($data->groupby == 'groups') {
101 $numgrps = $data->number;
102 $userpergrp = floor($usercnt/$numgrps);
104 } else { // members
105 $numgrps = ceil($usercnt/$data->number);
106 $userpergrp = $data->number;
108 if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
109 // If there would be one group with a small number of member reduce the number of groups
110 $missing = $userpergrp * $numgrps - $usercnt;
111 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
112 // spread the users from the last small group
113 $numgrps--;
114 $userpergrp = floor($usercnt/$numgrps);
119 // allocate the users - all groups equal count first
120 for ($i=0; $i<$numgrps; $i++) {
121 $groups[$i] = array();
122 $groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i);
123 $groups[$i]['members'] = array();
124 if ($data->allocateby == 'no') {
125 continue; // do not allocate users
127 for ($j=0; $j<$userpergrp; $j++) {
128 if (empty($users)) {
129 break 2;
131 $user = array_shift($users);
132 $groups[$i]['members'][$user->id] = $user;
135 // now distribute the rest
136 if ($data->allocateby != 'no') {
137 for ($i=0; $i<$numgrps; $i++) {
138 if (empty($users)) {
139 break 1;
141 $user = array_shift($users);
142 $groups[$i]['members'][$user->id] = $user;
146 if (isset($data->preview)) {
147 $table = new html_table();
148 if ($data->allocateby == 'no') {
149 $table->head = array(get_string('groupscount', 'group', $numgrps));
150 $table->size = array('100%');
151 $table->align = array('left');
152 $table->width = '40%';
153 } else {
154 $table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt));
155 $table->size = array('20%', '70%', '10%');
156 $table->align = array('left', 'left', 'center');
157 $table->width = '90%';
159 $table->data = array();
161 foreach ($groups as $group) {
162 $line = array();
163 if (groups_get_group_by_name($courseid, $group['name'])) {
164 $line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>';
165 $error = get_string('groupnameexists', 'group', $group['name']);
166 } else {
167 $line[] = $group['name'];
169 if ($data->allocateby != 'no') {
170 $unames = array();
171 foreach ($group['members'] as $user) {
172 $unames[] = fullname($user, true);
174 $line[] = implode(', ', $unames);
175 $line[] = count($group['members']);
177 $table->data[] = $line;
180 $preview .= html_writer::table($table);
182 } else {
183 $grouping = null;
184 $createdgrouping = null;
185 $createdgroups = array();
186 $failed = false;
188 // prepare grouping
189 if (!empty($data->grouping)) {
190 if ($data->grouping < 0) {
191 $grouping = new stdClass();
192 $grouping->courseid = $COURSE->id;
193 $grouping->name = trim($data->groupingname);
194 $grouping->id = groups_create_grouping($grouping);
195 $createdgrouping = $grouping->id;
196 } else {
197 $grouping = groups_get_grouping($data->grouping);
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;
208 $newgroup = new stdClass();
209 $newgroup->courseid = $data->courseid;
210 $newgroup->name = $group['name'];
211 $groupid = groups_create_group($newgroup);
212 $createdgroups[] = $groupid;
213 foreach($group['members'] as $user) {
214 groups_add_member($groupid, $user->id);
216 if ($grouping) {
217 groups_assign_grouping($grouping->id, $groupid);
221 if ($failed) {
222 foreach ($createdgroups as $groupid) {
223 groups_delete_group($groupid);
225 if ($createdgrouping) {
226 groups_delete_grouping($createdgrouping);
228 } else {
229 redirect($returnurl);
234 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
235 $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
236 $PAGE->navbar->add($strautocreategroups);
238 /// Print header
239 $PAGE->set_title($strgroups);
240 $PAGE->set_heading($course->fullname. ': '.$strgroups);
241 echo $OUTPUT->header();
242 echo $OUTPUT->heading($strautocreategroups);
244 if ($error != '') {
245 echo $OUTPUT->notification($error);
248 /// Display the form
249 $editform->display();
251 if($preview !== '') {
252 echo $OUTPUT->heading(get_string('groupspreview', 'group'));
254 echo $preview;
257 echo $OUTPUT->footer();