2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Create and allocate users to groups
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
58 /// Get applicable roles
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
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()) {
74 } elseif ($data = $editform->get_data()) {
76 /// Allocate members from the selected role to groups
77 switch ($data->allocateby
) {
81 $orderby = 'lastname, firstname'; break;
83 $orderby = 'firstname, lastname'; break;
85 $orderby = 'idnumber'; break;
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') {
99 // Plan the allocation
100 if ($data->groupby
== 'groups') {
101 $numgrps = $data->number
;
102 $userpergrp = floor($usercnt/$numgrps);
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
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++
) {
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++
) {
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%';
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) {
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']);
167 $line[] = $group['name'];
169 if ($data->allocateby
!= 'no') {
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);
184 $createdgrouping = null;
185 $createdgroups = array();
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
;
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']);
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
);
217 groups_assign_grouping($grouping->id
, $groupid);
222 foreach ($createdgroups as $groupid) {
223 groups_delete_group($groupid);
225 if ($createdgrouping) {
226 groups_delete_grouping($createdgrouping);
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);
239 $PAGE->set_title($strgroups);
240 $PAGE->set_heading($course->fullname
. ': '.$strgroups);
241 echo $OUTPUT->header();
242 echo $OUTPUT->heading($strautocreategroups);
245 echo $OUTPUT->notification($error);
249 $editform->display();
251 if($preview !== '') {
252 echo $OUTPUT->heading(get_string('groupspreview', 'group'));
257 echo $OUTPUT->footer();