See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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. 13 // 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/>. 16 17 18 /** 19 * Create and allocate users to groups 20 * 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 24 */ 25 26 require_once('../config.php'); 27 require_once ('lib.php'); 28 require_once ('autogroup_form.php'); 29 30 if (!defined('AUTOGROUP_MIN_RATIO')) { 31 define('AUTOGROUP_MIN_RATIO', 0.7); // means minimum member count is 70% in the smallest group 32 } 33 34 $courseid = required_param('courseid', PARAM_INT); 35 $PAGE->set_url('/group/autogroup.php', array('courseid' => $courseid)); 36 37 if (!$course = $DB->get_record('course', array('id'=>$courseid))) { 38 print_error('invalidcourseid'); 39 } 40 41 // Make sure that the user has permissions to manage groups. 42 require_login($course); 43 44 $context = context_course::instance($courseid); 45 require_capability('moodle/course:managegroups', $context); 46 47 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id; 48 49 $strgroups = get_string('groups'); 50 $strparticipants = get_string('participants'); 51 $strautocreategroups = get_string('autocreategroups', 'group'); 52 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))); 57 58 // Print the page and form 59 $preview = ''; 60 $error = ''; 61 62 /// Get applicable roles - used in menus etc later on 63 $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true); 64 65 /// Create the form 66 $editform = new autogroup_form(null, array('roles' => $rolenames)); 67 $editform->set_data(array('courseid' => $courseid, 'seed' => time())); 68 69 /// Handle form submission 70 if ($editform->is_cancelled()) { 71 redirect($returnurl); 72 73 } elseif ($data = $editform->get_data()) { 74 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'); 87 } 88 $source = array(); 89 if ($data->cohortid) { 90 $source['cohortid'] = $data->cohortid; 91 } 92 if ($data->groupingid) { 93 $source['groupingid'] = $data->groupingid; 94 } 95 if ($data->groupid) { 96 $source['groupid'] = $data->groupid; 97 } 98 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); 101 102 $extrafields = get_extra_user_fields($context); 103 $users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup), 104 $onlyactive, $extrafields); 105 $usercnt = count($users); 106 107 if ($data->allocateby == 'random') { 108 srand($data->seed); 109 shuffle($users); 110 } 111 112 $groups = array(); 113 114 // Plan the allocation 115 if ($data->groupby == 'groups') { 116 $numgrps = $data->number; 117 $userpergrp = floor($usercnt/$numgrps); 118 119 } else { // members 120 $numgrps = ceil($usercnt/$data->number); 121 $userpergrp = $data->number; 122 123 if (!empty($data->nosmallgroups) and $usercnt % $data->number != 0) { 124 // If there would be one group with a small number of member reduce the number of groups 125 $missing = $userpergrp * $numgrps - $usercnt; 126 if ($missing > $userpergrp * (1-AUTOGROUP_MIN_RATIO)) { 127 // spread the users from the last small group 128 $numgrps--; 129 $userpergrp = floor($usercnt/$numgrps); 130 } 131 } 132 } 133 134 // allocate the users - all groups equal count first 135 for ($i=0; $i<$numgrps; $i++) { 136 $groups[$i] = array(); 137 $groups[$i]['name'] = groups_parse_name(trim($data->namingscheme), $i); 138 $groups[$i]['members'] = array(); 139 if ($data->allocateby == 'no') { 140 continue; // do not allocate users 141 } 142 for ($j=0; $j<$userpergrp; $j++) { 143 if (empty($users)) { 144 break 2; 145 } 146 $user = array_shift($users); 147 $groups[$i]['members'][$user->id] = $user; 148 } 149 } 150 // now distribute the rest 151 if ($data->allocateby != 'no') { 152 for ($i=0; $i<$numgrps; $i++) { 153 if (empty($users)) { 154 break 1; 155 } 156 $user = array_shift($users); 157 $groups[$i]['members'][$user->id] = $user; 158 } 159 } 160 161 if (isset($data->preview)) { 162 $table = new html_table(); 163 if ($data->allocateby == 'no') { 164 $table->head = array(get_string('groupscount', 'group', $numgrps)); 165 $table->size = array('100%'); 166 $table->align = array('left'); 167 $table->width = '40%'; 168 } else { 169 $table->head = array(get_string('groupscount', 'group', $numgrps), get_string('groupmembers', 'group'), get_string('usercounttotal', 'group', $usercnt)); 170 $table->size = array('20%', '70%', '10%'); 171 $table->align = array('left', 'left', 'center'); 172 $table->width = '90%'; 173 } 174 $table->data = array(); 175 $viewfullnames = has_capability('moodle/site:viewfullnames', $context); 176 foreach ($groups as $group) { 177 $line = array(); 178 if (groups_get_group_by_name($courseid, $group['name'])) { 179 $line[] = '<span class="notifyproblem">'.get_string('groupnameexists', 'group', $group['name']).'</span>'; 180 $error = get_string('groupnameexists', 'group', $group['name']); 181 } else { 182 $line[] = $group['name']; 183 } 184 if ($data->allocateby != 'no') { 185 $unames = array(); 186 foreach ($group['members'] as $user) { 187 $fullname = fullname($user, $viewfullnames); 188 if ($extrafields) { 189 $extrafieldsdisplay = []; 190 foreach ($extrafields as $field) { 191 $extrafieldsdisplay[] = s($user->{$field}); 192 } 193 $fullname .= ' (' . implode(', ', $extrafieldsdisplay) . ')'; 194 } 195 196 $unames[] = $fullname; 197 } 198 $line[] = implode(', ', $unames); 199 $line[] = count($group['members']); 200 } 201 $table->data[] = $line; 202 } 203 204 $preview .= html_writer::table($table); 205 206 } else { 207 $grouping = null; 208 $createdgrouping = null; 209 $createdgroups = array(); 210 $failed = false; 211 212 // prepare grouping 213 if (!empty($data->grouping)) { 214 if ($data->grouping < 0) { 215 $grouping = new stdClass(); 216 $grouping->courseid = $COURSE->id; 217 $grouping->name = trim($data->groupingname); 218 $grouping->id = groups_create_grouping($grouping); 219 $createdgrouping = $grouping->id; 220 } else { 221 $grouping = groups_get_grouping($data->grouping); 222 } 223 } 224 225 // Save the groups data 226 foreach ($groups as $key=>$group) { 227 if (groups_get_group_by_name($courseid, $group['name'])) { 228 $error = get_string('groupnameexists', 'group', $group['name']); 229 $failed = true; 230 break; 231 } 232 $newgroup = new stdClass(); 233 $newgroup->courseid = $data->courseid; 234 $newgroup->name = $group['name']; 235 $newgroup->enablemessaging = $data->enablemessaging ?? 0; 236 $groupid = groups_create_group($newgroup); 237 $createdgroups[] = $groupid; 238 foreach($group['members'] as $user) { 239 groups_add_member($groupid, $user->id); 240 } 241 if ($grouping) { 242 // Ask this function not to invalidate the cache, we'll do that manually once at the end. 243 groups_assign_grouping($grouping->id, $groupid, null, false); 244 } 245 } 246 247 // Invalidate the course groups cache seeing as we've changed it. 248 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid)); 249 250 if ($failed) { 251 foreach ($createdgroups as $groupid) { 252 groups_delete_group($groupid); 253 } 254 if ($createdgrouping) { 255 groups_delete_grouping($createdgrouping); 256 } 257 } else { 258 redirect($returnurl); 259 } 260 } 261 } 262 263 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid))); 264 $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid))); 265 $PAGE->navbar->add($strautocreategroups); 266 267 echo $OUTPUT->header(); 268 echo $OUTPUT->heading($strautocreategroups); 269 270 if ($error != '') { 271 echo $OUTPUT->notification($error); 272 } 273 274 /// Display the form 275 $editform->display(); 276 277 if($preview !== '') { 278 echo $OUTPUT->heading(get_string('groupspreview', 'group')); 279 280 echo $preview; 281 } 282 283 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body