MDL-61133 core_output: add fontawesome mapping for tags icon
[moodle.git] / group / autogroup.php
blob0c1bb5d65cd1efcaf07d52ea3a659cf81835cca0
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 $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
59 $preview = '';
60 $error = '';
62 /// Get applicable roles - used in menus etc later on
63 $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
65 /// Create the form
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()) {
71 redirect($returnurl);
73 } elseif ($data = $editform->get_data()) {
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:
86 print_error('unknoworder');
88 $source = array();
89 if ($data->cohortid) {
90 $source['cohortid'] = $data->cohortid;
92 if ($data->groupingid) {
93 $source['groupingid'] = $data->groupingid;
95 if ($data->groupid) {
96 $source['groupid'] = $data->groupid;
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);
102 $users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup),
103 $onlyactive);
104 $usercnt = count($users);
106 if ($data->allocateby == 'random') {
107 srand($data->seed);
108 shuffle($users);
111 $groups = array();
113 // Plan the allocation
114 if ($data->groupby == 'groups') {
115 $numgrps = $data->number;
116 $userpergrp = floor($usercnt/$numgrps);
118 } else { // members
119 $numgrps = ceil($usercnt/$data->number);
120 $userpergrp = $data->number;
122 if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) {
123 // If there would be one group with a small number of member reduce the number of groups
124 $missing = $userpergrp * $numgrps - $usercnt;
125 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) {
126 // spread the users from the last small group
127 $numgrps--;
128 $userpergrp = floor($usercnt/$numgrps);
133 // allocate the users - all groups equal count first
134 for ($i=0; $i<$numgrps; $i++) {
135 $groups[$i] = array();
136 $groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i);
137 $groups[$i]['members'] = array();
138 if ($data->allocateby == 'no') {
139 continue; // do not allocate users
141 for ($j=0; $j<$userpergrp; $j++) {
142 if (empty($users)) {
143 break 2;
145 $user = array_shift($users);
146 $groups[$i]['members'][$user->id] = $user;
149 // now distribute the rest
150 if ($data->allocateby != 'no') {
151 for ($i=0; $i<$numgrps; $i++) {
152 if (empty($users)) {
153 break 1;
155 $user = array_shift($users);
156 $groups[$i]['members'][$user->id] = $user;
160 if (isset($data->preview)) {
161 $table = new html_table();
162 if ($data->allocateby == 'no') {
163 $table->head = array(get_string('groupscount', 'group', $numgrps));
164 $table->size = array('100%');
165 $table->align = array('left');
166 $table->width = '40%';
167 } else {
168 $table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt));
169 $table->size = array('20%', '70%', '10%');
170 $table->align = array('left', 'left', 'center');
171 $table->width = '90%';
173 $table->data = array();
175 foreach ($groups as $group) {
176 $line = array();
177 if (groups_get_group_by_name($courseid, $group['name'])) {
178 $line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>';
179 $error = get_string('groupnameexists', 'group', $group['name']);
180 } else {
181 $line[] = $group['name'];
183 if ($data->allocateby != 'no') {
184 $unames = array();
185 foreach ($group['members'] as $user) {
186 $unames[] = fullname($user, true);
188 $line[] = implode(', ', $unames);
189 $line[] = count($group['members']);
191 $table->data[] = $line;
194 $preview .= html_writer::table($table);
196 } else {
197 $grouping = null;
198 $createdgrouping = null;
199 $createdgroups = array();
200 $failed = false;
202 // prepare grouping
203 if (!empty($data->grouping)) {
204 if ($data->grouping < 0) {
205 $grouping = new stdClass();
206 $grouping->courseid = $COURSE->id;
207 $grouping->name = trim($data->groupingname);
208 $grouping->id = groups_create_grouping($grouping);
209 $createdgrouping = $grouping->id;
210 } else {
211 $grouping = groups_get_grouping($data->grouping);
215 // Save the groups data
216 foreach ($groups as $key=>$group) {
217 if (groups_get_group_by_name($courseid, $group['name'])) {
218 $error = get_string('groupnameexists', 'group', $group['name']);
219 $failed = true;
220 break;
222 $newgroup = new stdClass();
223 $newgroup->courseid = $data->courseid;
224 $newgroup->name = $group['name'];
225 $groupid = groups_create_group($newgroup);
226 $createdgroups[] = $groupid;
227 foreach($group['members'] as $user) {
228 groups_add_member($groupid, $user->id);
230 if ($grouping) {
231 // Ask this function not to invalidate the cache, we'll do that manually once at the end.
232 groups_assign_grouping($grouping->id, $groupid, null, false);
236 // Invalidate the course groups cache seeing as we've changed it.
237 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
239 if ($failed) {
240 foreach ($createdgroups as $groupid) {
241 groups_delete_group($groupid);
243 if ($createdgrouping) {
244 groups_delete_grouping($createdgrouping);
246 } else {
247 redirect($returnurl);
252 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
253 $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
254 $PAGE->navbar->add($strautocreategroups);
256 echo $OUTPUT->header();
257 echo $OUTPUT->heading($strautocreategroups);
259 if ($error != '') {
260 echo $OUTPUT->notification($error);
263 /// Display the form
264 $editform->display();
266 if($preview !== '') {
267 echo $OUTPUT->heading(get_string('groupspreview', 'group'));
269 echo $preview;
272 echo $OUTPUT->footer();