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