Merge branch 'w29_MDL-40611_m24_accesslibtest' of https://github.com/skodak/moodle...
[moodle.git] / group / autogroup.php
blob7372678a969a9ec2f6bd7574c853f2f7e1780394
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 = context_course::instance($courseid);
45 require_capability('moodle/course:managegroups', $context);
47 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id;
49 $strgroups = get_string('groups');
50 $strparticipants = get_string('participants');
51 $strautocreategroups = get_string('autocreategroups', 'group');
53 // Print the page and form
54 $preview = '';
55 $error = '';
57 /// Get applicable roles - used in menus etc later on
58 $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
60 /// Create the form
61 $editform = new autogroup_form(null, array('roles' => $rolenames));
62 $editform->set_data(array('courseid' => $courseid, 'seed' => time()));
64 /// Handle form submission
65 if ($editform->is_cancelled()) {
66 redirect($returnurl);
68 } elseif ($data = $editform->get_data()) {
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:
81 print_error('unknoworder');
83 $users = groups_get_potential_members($data->courseid, $data->roleid, $data->cohortid, $orderby);
84 $usercnt = count($users);
86 if ($data->allocateby == 'random') {
87 srand($data->seed);
88 shuffle($users);
91 $groups = array();
93 // Plan the allocation
94 if ($data->groupby == 'groups') {
95 $numgrps = $data->number;
96 $userpergrp = floor($usercnt/$numgrps);
98 } else { // members
99 $numgrps = ceil($usercnt/$data->number);
100 $userpergrp = $data->number;
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;
105 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
106 // spread the users from the last small group
107 $numgrps--;
108 $userpergrp = floor($usercnt/$numgrps);
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
121 for ($j=0; $j<$userpergrp; $j++) {
122 if (empty($users)) {
123 break 2;
125 $user = array_shift($users);
126 $groups[$i]['members'][$user->id] = $user;
129 // now distribute the rest
130 if ($data->allocateby != 'no') {
131 for ($i=0; $i<$numgrps; $i++) {
132 if (empty($users)) {
133 break 1;
135 $user = array_shift($users);
136 $groups[$i]['members'][$user->id] = $user;
140 if (isset($data->preview)) {
141 $table = new html_table();
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%';
153 $table->data = array();
155 foreach ($groups as $group) {
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'];
163 if ($data->allocateby != 'no') {
164 $unames = array();
165 foreach ($group['members'] as $user) {
166 $unames[] = fullname($user, true);
168 $line[] = implode(', ', $unames);
169 $line[] = count($group['members']);
171 $table->data[] = $line;
174 $preview .= html_writer::table($table);
176 } else {
177 $grouping = null;
178 $createdgrouping = null;
179 $createdgroups = array();
180 $failed = false;
182 // prepare grouping
183 if (!empty($data->grouping)) {
184 if ($data->grouping < 0) {
185 $grouping = new stdClass();
186 $grouping->courseid = $COURSE->id;
187 $grouping->name = trim($data->groupingname);
188 $grouping->id = groups_create_grouping($grouping);
189 $createdgrouping = $grouping->id;
190 } else {
191 $grouping = groups_get_grouping($data->grouping);
195 // Save the groups data
196 foreach ($groups as $key=>$group) {
197 if (groups_get_group_by_name($courseid, $group['name'])) {
198 $error = get_string('groupnameexists', 'group', $group['name']);
199 $failed = true;
200 break;
202 $newgroup = new stdClass();
203 $newgroup->courseid = $data->courseid;
204 $newgroup->name = $group['name'];
205 $groupid = groups_create_group($newgroup);
206 $createdgroups[] = $groupid;
207 foreach($group['members'] as $user) {
208 groups_add_member($groupid, $user->id);
210 if ($grouping) {
211 groups_assign_grouping($grouping->id, $groupid);
215 if ($failed) {
216 foreach ($createdgroups as $groupid) {
217 groups_delete_group($groupid);
219 if ($createdgrouping) {
220 groups_delete_grouping($createdgrouping);
222 } else {
223 redirect($returnurl);
228 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
229 $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
230 $PAGE->navbar->add($strautocreategroups);
232 /// Print header
233 $PAGE->set_title($strgroups);
234 $PAGE->set_heading($course->fullname. ': '.$strgroups);
235 echo $OUTPUT->header();
236 echo $OUTPUT->heading($strautocreategroups);
238 if ($error != '') {
239 echo $OUTPUT->notification($error);
242 /// Display the form
243 $editform->display();
245 if($preview !== '') {
246 echo $OUTPUT->heading(get_string('groupspreview', 'group'));
248 echo $preview;
251 echo $OUTPUT->footer();