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 = 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 $PAGE->set_title($strgroups);
54 $PAGE->set_heading($course->fullname
. ': '.$strgroups);
55 $PAGE->set_pagelayout('admin');
56 navigation_node
::override_active_url(new moodle_url('/group/index.php', array('id' => $courseid)));
58 // Print the page and form
62 /// Get applicable roles - used in menus etc later on
63 $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS
, true);
66 $editform = new autogroup_form(null, array('roles' => $rolenames));
67 $editform->set_data(array('courseid' => $courseid, 'seed' => time()));
69 /// Handle form submission
70 if ($editform->is_cancelled()) {
73 } elseif ($data = $editform->get_data()) {
75 /// Allocate members from the selected role to groups
76 switch ($data->allocateby
) {
80 $orderby = 'lastname, firstname'; break;
82 $orderby = 'firstname, lastname'; break;
84 $orderby = 'idnumber'; break;
86 print_error('unknoworder');
88 $users = groups_get_potential_members($data->courseid
, $data->roleid
, $data->cohortid
, $orderby);
89 $usercnt = count($users);
91 if ($data->allocateby
== 'random') {
98 // Plan the allocation
99 if ($data->groupby
== 'groups') {
100 $numgrps = $data->number
;
101 $userpergrp = floor($usercnt/$numgrps);
104 $numgrps = ceil($usercnt/$data->number
);
105 $userpergrp = $data->number
;
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;
110 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO
)) {
111 // spread the users from the last small group
113 $userpergrp = floor($usercnt/$numgrps);
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
126 for ($j=0; $j<$userpergrp; $j++
) {
130 $user = array_shift($users);
131 $groups[$i]['members'][$user->id
] = $user;
134 // now distribute the rest
135 if ($data->allocateby
!= 'no') {
136 for ($i=0; $i<$numgrps; $i++
) {
140 $user = array_shift($users);
141 $groups[$i]['members'][$user->id
] = $user;
145 if (isset($data->preview
)) {
146 $table = new html_table();
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%';
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%';
158 $table->data
= array();
160 foreach ($groups as $group) {
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']);
166 $line[] = $group['name'];
168 if ($data->allocateby
!= 'no') {
170 foreach ($group['members'] as $user) {
171 $unames[] = fullname($user, true);
173 $line[] = implode(', ', $unames);
174 $line[] = count($group['members']);
176 $table->data
[] = $line;
179 $preview .= html_writer
::table($table);
183 $createdgrouping = null;
184 $createdgroups = array();
188 if (!empty($data->grouping
)) {
189 if ($data->grouping
< 0) {
190 $grouping = new stdClass();
191 $grouping->courseid
= $COURSE->id
;
192 $grouping->name
= trim($data->groupingname
);
193 $grouping->id
= groups_create_grouping($grouping);
194 $createdgrouping = $grouping->id
;
196 $grouping = groups_get_grouping($data->grouping
);
200 // Save the groups data
201 foreach ($groups as $key=>$group) {
202 if (groups_get_group_by_name($courseid, $group['name'])) {
203 $error = get_string('groupnameexists', 'group', $group['name']);
207 $newgroup = new stdClass();
208 $newgroup->courseid
= $data->courseid
;
209 $newgroup->name
= $group['name'];
210 $groupid = groups_create_group($newgroup);
211 $createdgroups[] = $groupid;
212 foreach($group['members'] as $user) {
213 groups_add_member($groupid, $user->id
);
216 // Ask this function not to invalidate the cache, we'll do that manually once at the end.
217 groups_assign_grouping($grouping->id
, $groupid, null, false);
221 // Invalidate the course groups cache seeing as we've changed it.
222 cache_helper
::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
225 foreach ($createdgroups as $groupid) {
226 groups_delete_group($groupid);
228 if ($createdgrouping) {
229 groups_delete_grouping($createdgrouping);
232 redirect($returnurl);
237 $PAGE->navbar
->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
238 $PAGE->navbar
->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
239 $PAGE->navbar
->add($strautocreategroups);
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();