weekly release 3.10.1+
[moodle.git] / group / index.php
CommitLineData
fa19c325 1<?php
4d8e2417
AG
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
fa19c325 18/**
19 * The main group management user interface.
20 *
4d8e2417
AG
21 * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package core_group
fa19c325 24 */
25require_once('../config.php');
26require_once('lib.php');
0addb366 27
2524b0f2 28$courseid = required_param('id', PARAM_INT);
29$groupid = optional_param('group', false, PARAM_INT);
30$userid = optional_param('user', false, PARAM_INT);
31$action = groups_param_action();
f6eece19 32// Support either single group= parameter, or array groups[]
33if ($groupid) {
4f0c2d00 34 $groupids = array($groupid);
f6eece19 35} else {
18bd7573 36 $groupids = optional_param_array('groups', array(), PARAM_INT);
f6eece19 37}
4f0c2d00 38$singlegroup = (count($groupids) == 1);
77ea3330 39
2524b0f2 40$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
77ea3330 41
fa19c325 42// Get the course information so we can print the header and
43// check the course id is valid
2524b0f2 44
74df2951 45$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
fa19c325 46
a6855934 47$url = new moodle_url('/group/index.php', array('id'=>$courseid));
ce1249be 48navigation_node::override_active_url($url);
af333695 49if ($userid) {
50 $url->param('user', $userid);
51}
52if ($groupid) {
53 $url->param('group', $groupid);
54}
55$PAGE->set_url($url);
56
778918fd 57// Make sure that the user has permissions to manage groups.
58require_login($course);
fa19c325 59
bf0f06b1 60$context = context_course::instance($course->id);
69e33fa6
MG
61require_capability('moodle/course:managegroups', $context);
62
c9e30704
HN
63$PAGE->requires->js('/group/clientlib.js', true);
64$PAGE->requires->js('/group/module.js', true);
fa19c325 65
f6eece19 66// Check for multiple/no group errors
4f0c2d00 67if (!$singlegroup) {
f6eece19 68 switch($action) {
69 case 'ajax_getmembersingroup':
70 case 'showgroupsettingsform':
71 case 'showaddmembersform':
72 case 'updatemembers':
4f0c2d00 73 print_error('errorselectone', 'group', $returnurl);
f6eece19 74 }
75}
76
778918fd 77switch ($action) {
78 case false: //OK, display form.
79 break;
80
81 case 'ajax_getmembersingroup':
e254aa34 82 $roles = array();
21253ed6
PH
83
84 $extrafields = get_extra_user_fields($context);
85 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid,
86 'u.id, ' . user_picture::fields('u', $extrafields))) {
87
6e8ed1a8
PH
88 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
89
e254aa34 90 foreach($groupmemberroles as $roleid=>$roledata) {
4f0c2d00
PS
91 $shortroledata = new stdClass();
92 $shortroledata->name = $roledata->name;
93 $shortroledata->users = array();
e254aa34 94 foreach($roledata->users as $member) {
4f0c2d00
PS
95 $shortmember = new stdClass();
96 $shortmember->id = $member->id;
6e8ed1a8 97 $shortmember->name = fullname($member, $viewfullnames);
21253ed6
PH
98 if ($extrafields) {
99 $extrafieldsdisplay = [];
100 foreach ($extrafields as $field) {
101 $extrafieldsdisplay[] = s($member->{$field});
102 }
103 $shortmember->name .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
104 }
105
4f0c2d00 106 $shortroledata->users[] = $shortmember;
e254aa34 107 }
4f0c2d00 108 $roles[] = $shortroledata;
3cdc1e28 109 }
778918fd 110 }
1e074660 111 echo json_encode($roles);
778918fd 112 die; // Client side JavaScript takes it from here.
3cdc1e28 113
778918fd 114 case 'deletegroup':
4f0c2d00 115 if (count($groupids) == 0) {
f6eece19 116 print_error('errorselectsome','group',$returnurl);
117 }
4f0c2d00
PS
118 $groupidlist = implode(',', $groupids);
119 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
778918fd 120 break;
2524b0f2 121
778918fd 122 case 'showcreateorphangroupform':
4f0c2d00 123 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
778918fd 124 break;
2524b0f2 125
acf000b0 126 case 'showautocreategroupsform':
4f0c2d00 127 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
f16fa0a3 128 break;
acf000b0 129
81ed4632
PS
130 case 'showimportgroups':
131 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
132 break;
133
778918fd 134 case 'showgroupsettingsform':
4f0c2d00 135 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
778918fd 136 break;
2524b0f2 137
778918fd 138 case 'updategroups': //Currently reloading.
139 break;
3cdc1e28 140
778918fd 141 case 'removemembers':
142 break;
2524b0f2 143
778918fd 144 case 'showaddmembersform':
4f0c2d00 145 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
778918fd 146 break;
2524b0f2 147
778918fd 148 case 'updatemembers': //Currently reloading.
149 break;
5e4074b6 150
778918fd 151 default: //ERROR.
4f0c2d00 152 print_error('unknowaction', '', $returnurl);
778918fd 153 break;
778918fd 154}
fa19c325 155
778918fd 156// Print the page and form
157$strgroups = get_string('groups');
158$strparticipants = get_string('participants');
fa19c325 159
778918fd 160/// Print header
b87573d7 161$PAGE->set_title($strgroups);
5a47767c 162$PAGE->set_heading($course->fullname);
f196f42c 163$PAGE->set_pagelayout('standard');
b87573d7 164echo $OUTPUT->header();
165
f16fa0a3 166// Add tabs
167$currenttab = 'groups';
168require('tabs.php');
fa19c325 169
8ebbb06a 170echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$strgroups, 3);
08103c93 171
778918fd 172$groups = groups_get_all_groups($courseid);
3b586539 173$selectedname = null;
74b714df 174$preventgroupremoval = array();
62d63838 175
3b586539
JP
176// Get list of groups to render.
177$groupoptions = array();
778918fd 178if ($groups) {
778918fd 179 foreach ($groups as $group) {
3b586539
JP
180 $selected = false;
181 $usercount = $DB->count_records('groups_members', array('groupid' => $group->id));
182 $groupname = format_string($group->name) . ' (' . $usercount . ')';
183 if (in_array($group->id, $groupids)) {
184 $selected = true;
f6eece19 185 if ($singlegroup) {
3b586539 186 // Only keep selected name if there is one group selected.
f6eece19 187 $selectedname = $groupname;
188 }
189 }
74b714df
ARN
190 if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
191 $preventgroupremoval[$group->id] = true;
192 }
6f5e0852 193
3b586539
JP
194 $groupoptions[] = (object) [
195 'value' => $group->id,
196 'selected' => $selected,
197 'text' => $groupname
198 ];
fa19c325 199 }
778918fd 200}
2524b0f2 201
3b586539
JP
202// Get list of group members to render if there is a single selected group.
203$members = array();
f6eece19 204if ($singlegroup) {
21253ed6
PH
205 $extrafields = get_extra_user_fields($context);
206 if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
207 'u.id, ' . user_picture::fields('u', $extrafields))) {
208
6e8ed1a8
PH
209 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
210
3b586539
JP
211 foreach ($groupmemberroles as $roleid => $roledata) {
212 $users = array();
213 foreach ($roledata->users as $member) {
21253ed6
PH
214 $shortmember = new stdClass();
215 $shortmember->value = $member->id;
6e8ed1a8 216 $shortmember->text = fullname($member, $viewfullnames);
21253ed6
PH
217 if ($extrafields) {
218 $extrafieldsdisplay = [];
219 foreach ($extrafields as $field) {
220 $extrafieldsdisplay[] = s($member->{$field});
221 }
222 $shortmember->text .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
223 }
224
225 $users[] = $shortmember;
e254aa34 226 }
3b586539
JP
227 $members[] = (object)[
228 'role' => s($roledata->name),
229 'rolemembers' => $users
230 ];
fa19c325 231 }
1e074660 232 }
778918fd 233}
1e074660 234
3b586539
JP
235$disableaddedit = !$singlegroup;
236$disabledelete = !empty($groupids);
237$renderable = new \core_group\output\index_page($courseid, $groupoptions, $selectedname, $members, $disableaddedit, $disabledelete,
238 $preventgroupremoval);
239$output = $PAGE->get_renderer('core_group');
240echo $output->render($renderable);
3cdc1e28 241
653468d4 242echo $OUTPUT->footer();
2524b0f2 243
2c386f82 244/**
245 * Returns the first button action with the given prefix, taken from
246 * POST or GET, otherwise returns false.
4d8e2417
AG
247 * @see /lib/moodlelib.php function optional_param().
248 * @param string $prefix 'act_' as in 'action'.
2c386f82 249 * @return string The action without the prefix, or false if no action found.
250 */
251function groups_param_action($prefix = 'act_') {
252 $action = false;
253//($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
254
255 if ($_POST) {
256 $form_vars = $_POST;
257 }
258 elseif ($_GET) {
ddff2fa8 259 $form_vars = $_GET;
2c386f82 260 }
261 if ($form_vars) {
262 foreach ($form_vars as $key => $value) {
263 if (preg_match("/$prefix(.+)/", $key, $matches)) {
264 $action = $matches[1];
265 break;
266 }
267 }
268 }
269 if ($action && !preg_match('/^\w+$/', $action)) {
270 $action = false;
b121a4ee 271 print_error('unknowaction');
2c386f82 272 }
273 ///if (debugging()) echo 'Debug: '.$action;
274 return $action;
275}