Merge branch 'MDL-65766-master' of git://github.com/mihailges/moodle
[moodle.git] / group / index.php
blob79c9e21aabd5c5c40847a81cb27c531dec4492e6
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 * The main group management user interface.
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
25 require_once('../config.php');
26 require_once('lib.php');
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();
32 // Support either single group= parameter, or array groups[]
33 if ($groupid) {
34 $groupids = array($groupid);
35 } else {
36 $groupids = optional_param_array('groups', array(), PARAM_INT);
38 $singlegroup = (count($groupids) == 1);
40 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
42 // Get the course information so we can print the header and
43 // check the course id is valid
45 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
47 $url = new moodle_url('/group/index.php', array('id'=>$courseid));
48 navigation_node::override_active_url($url);
49 if ($userid) {
50 $url->param('user', $userid);
52 if ($groupid) {
53 $url->param('group', $groupid);
55 $PAGE->set_url($url);
57 // Make sure that the user has permissions to manage groups.
58 require_login($course);
60 $context = context_course::instance($course->id);
61 require_capability('moodle/course:managegroups', $context);
63 $PAGE->requires->js('/group/clientlib.js', true);
64 $PAGE->requires->js('/group/module.js', true);
66 // Check for multiple/no group errors
67 if (!$singlegroup) {
68 switch($action) {
69 case 'ajax_getmembersingroup':
70 case 'showgroupsettingsform':
71 case 'showaddmembersform':
72 case 'updatemembers':
73 print_error('errorselectone', 'group', $returnurl);
77 switch ($action) {
78 case false: //OK, display form.
79 break;
81 case 'ajax_getmembersingroup':
82 $roles = array();
83 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
84 foreach($groupmemberroles as $roleid=>$roledata) {
85 $shortroledata = new stdClass();
86 $shortroledata->name = $roledata->name;
87 $shortroledata->users = array();
88 foreach($roledata->users as $member) {
89 $shortmember = new stdClass();
90 $shortmember->id = $member->id;
91 $shortmember->name = fullname($member, true);
92 $shortroledata->users[] = $shortmember;
94 $roles[] = $shortroledata;
97 echo json_encode($roles);
98 die; // Client side JavaScript takes it from here.
100 case 'deletegroup':
101 if (count($groupids) == 0) {
102 print_error('errorselectsome','group',$returnurl);
104 $groupidlist = implode(',', $groupids);
105 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
106 break;
108 case 'showcreateorphangroupform':
109 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
110 break;
112 case 'showautocreategroupsform':
113 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
114 break;
116 case 'showimportgroups':
117 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
118 break;
120 case 'showgroupsettingsform':
121 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
122 break;
124 case 'updategroups': //Currently reloading.
125 break;
127 case 'removemembers':
128 break;
130 case 'showaddmembersform':
131 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
132 break;
134 case 'updatemembers': //Currently reloading.
135 break;
137 default: //ERROR.
138 print_error('unknowaction', '', $returnurl);
139 break;
142 // Print the page and form
143 $strgroups = get_string('groups');
144 $strparticipants = get_string('participants');
146 /// Print header
147 $PAGE->set_title($strgroups);
148 $PAGE->set_heading($course->fullname);
149 $PAGE->set_pagelayout('standard');
150 echo $OUTPUT->header();
152 // Add tabs
153 $currenttab = 'groups';
154 require('tabs.php');
156 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$strgroups, 3);
158 $groups = groups_get_all_groups($courseid);
159 $selectedname = null;
160 $preventgroupremoval = array();
162 // Get list of groups to render.
163 $groupoptions = array();
164 if ($groups) {
165 foreach ($groups as $group) {
166 $selected = false;
167 $usercount = $DB->count_records('groups_members', array('groupid' => $group->id));
168 $groupname = format_string($group->name) . ' (' . $usercount . ')';
169 if (in_array($group->id, $groupids)) {
170 $selected = true;
171 if ($singlegroup) {
172 // Only keep selected name if there is one group selected.
173 $selectedname = $groupname;
176 if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
177 $preventgroupremoval[$group->id] = true;
180 $groupoptions[] = (object) [
181 'value' => $group->id,
182 'selected' => $selected,
183 'text' => $groupname
188 // Get list of group members to render if there is a single selected group.
189 $members = array();
190 if ($singlegroup) {
191 $usernamefields = get_all_user_name_fields(true, 'u');
192 if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid, 'u.id, ' . $usernamefields)) {
193 foreach ($groupmemberroles as $roleid => $roledata) {
194 $users = array();
195 foreach ($roledata->users as $member) {
196 $users[] = (object)[
197 'value' => $member->id,
198 'text' => fullname($member, true)
201 $members[] = (object)[
202 'role' => s($roledata->name),
203 'rolemembers' => $users
209 $disableaddedit = !$singlegroup;
210 $disabledelete = !empty($groupids);
211 $renderable = new \core_group\output\index_page($courseid, $groupoptions, $selectedname, $members, $disableaddedit, $disabledelete,
212 $preventgroupremoval);
213 $output = $PAGE->get_renderer('core_group');
214 echo $output->render($renderable);
216 echo $OUTPUT->footer();
219 * Returns the first button action with the given prefix, taken from
220 * POST or GET, otherwise returns false.
221 * @see /lib/moodlelib.php function optional_param().
222 * @param string $prefix 'act_' as in 'action'.
223 * @return string The action without the prefix, or false if no action found.
225 function groups_param_action($prefix = 'act_') {
226 $action = false;
227 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
229 if ($_POST) {
230 $form_vars = $_POST;
232 elseif ($_GET) {
233 $form_vars = $_GET;
235 if ($form_vars) {
236 foreach ($form_vars as $key => $value) {
237 if (preg_match("/$prefix(.+)/", $key, $matches)) {
238 $action = $matches[1];
239 break;
243 if ($action && !preg_match('/^\w+$/', $action)) {
244 $action = false;
245 print_error('unknowaction');
247 ///if (debugging()) echo 'Debug: '.$action;
248 return $action;