MDL-11328 Added the missing variable
[moodle-pu.git] / group / index.php
blobf187b2726c0e067de889bd96c456b26b49c42e27
1 <?php
2 /**
3 * The main group management user interface.
5 * @copyright &copy; 2006 The Open University
6 * @author N.D.Freear AT open.ac.uk
7 * @author J.White AT open.ac.uk
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package groups
11 require_once('../config.php');
12 require_once('lib.php');
13 require_once($CFG->libdir.'/json/JSON.php');
15 require_js('yui_yahoo');
16 require_js('yui_dom');
17 require_js('yui_utilities');
18 require_js('yui_connection');
19 require_js($CFG->wwwroot.'/group/clientlib.js');
21 $courseid = required_param('id', PARAM_INT);
22 $groupid = optional_param('group', false, PARAM_INT);
23 $userid = optional_param('user', false, PARAM_INT);
24 $action = groups_param_action();
26 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
28 // Get the course information so we can print the header and
29 // check the course id is valid
31 if (!$course = get_record('course', 'id',$courseid)) {
32 $success = false;
33 print_error('invalidcourse'); //'The course ID is invalid'
36 // Make sure that the user has permissions to manage groups.
37 require_login($course);
39 $context = get_context_instance(CONTEXT_COURSE, $courseid);
40 if (! has_capability('moodle/course:managegroups', $context)) {
41 redirect(); //"group.php?id=$course->id"); // Not allowed to see all groups
44 switch ($action) {
45 case false: //OK, display form.
46 break;
48 case 'ajax_getmembersingroup':
49 $members = array();
50 if ($members = groups_get_members($groupid)) {
51 $member_names = array();
52 foreach($members as $member) {
53 $user = new object();
54 $user->id = $member->id;
55 $user->name = fullname($member, true);
56 $member_names[] = $user;
58 $json = new Services_JSON();
59 echo $json->encode($member_names);
61 die; // Client side JavaScript takes it from here.
63 case 'deletegroup':
64 redirect('group.php?delete=1&amp;courseid='.$courseid.'&amp;id='.$groupid);
65 break;
67 case 'showcreateorphangroupform':
68 redirect('group.php?courseid='.$courseid);
69 break;
71 case 'showgroupsettingsform':
72 redirect('group.php?courseid='.$courseid.'&amp;id='.$groupid);
73 break;
75 case 'updategroups': //Currently reloading.
76 break;
78 case 'removemembers':
79 break;
81 case 'showaddmembersform':
82 redirect('members.php?group='.$groupid);
83 break;
85 case 'updatemembers': //Currently reloading.
86 break;
88 default: //ERROR.
89 if (debugging()) {
90 error('Error, unknown button/action. Probably a user-interface bug!', $returnurl);
91 break;
95 // Print the page and form
96 $strgroups = get_string('groups');
97 $strparticipants = get_string('participants');
99 $navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
100 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
101 $navigation = build_navigation($navlinks);
103 /// Print header
104 print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
106 if (!empty($CFG->enablegroupings)) {
107 // Add tabs
108 $currenttab = 'groups';
109 require('tabs.php');
112 $disabled = 'disabled="disabled"';
114 $showeditgroupsettingsform_disabled = $disabled;
115 $deletegroup_disabled = $disabled;
116 $showcreategroupform_disabled = $disabled;
118 if (!empty($groupid)) {
119 $showaddmembersform_disabled = '';
120 $showeditgroupsettingsform_disabled = '';
121 $deletegroup_disabled = '';
122 } else {
123 $deletegroup_disabled = $disabled;
124 $showeditgroupsettingsform_disabled = $disabled;
125 $showaddmembersform_disabled = $disabled;
128 print_heading(format_string($course->shortname) .' '.$strgroups, 'center', 3);
129 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
130 echo '<div>'."\n";
131 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
133 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
134 echo '<tr>'."\n";
137 echo "<td>\n";
138 // NO GROUPINGS YET!
139 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
141 if (ajaxenabled()) {
142 $onchange = 'membersCombo.refreshMembers(this.options[this.selectedIndex].value);';
143 } else {
144 $onchange = '';
148 echo '<select name="group" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
149 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
151 $groups = groups_get_all_groups($courseid);
153 $sel_groupid = 0;
155 if ($groups) {
156 // Print out the HTML
157 foreach ($groups as $group) {
158 $select = '';
159 if ($groupid == $group->id) {
160 $select = ' selected="selected"';
161 $sel_groupid = $group->id;
163 $usercount = (int)count_records('groups_members', 'groupid', $group->id);
164 $groupname = format_string($group->name).' ('.$usercount.')';
166 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
168 } else {
169 // Print an empty option to avoid the XHTML error of having an empty select element
170 echo '<option>&nbsp;</option>';
173 echo '</select>'."\n";
174 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
175 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
176 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
177 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
178 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
179 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
181 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
182 . get_string('creategroup', 'group') . '" /></p>'."\n";
184 echo '</td>'."\n";
185 echo '<td>'."\n";
186 echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup">&nbsp;</span></label></p>'."\n";
187 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
188 echo '<select name="user" id="members" size="15" class="select"'."\n";
189 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
191 $member_names = array();
193 if ($sel_groupid) {
194 if ($members = groups_get_members($groupid)) {
195 foreach($members as $member) {
196 $member_names[$member->id] = fullname($member, true);
201 if ($member_names) {
202 // Put the groupings into a hash and sort them
203 foreach ($member_names as $userid=>$username) {
204 echo "<option value=\"{$userid}\" title=\"{$username}\">{$username}</option>\n";
207 } else {
208 // Print an empty option to avoid the XHTML error of having an empty select element
209 echo '<option>&nbsp;</option>';
212 echo '</select>'."\n";
214 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
215 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
216 echo '</td>'."\n";
217 echo '</tr>'."\n";
218 echo '</table>'."\n";
220 //<input type="hidden" name="rand" value="om" />
221 echo '</div>'."\n";
222 echo '</form>'."\n";
224 if (ajaxenabled()) {
225 echo '<script type="text/javascript">'."\n";
226 echo '//<![CDATA['."\n";
227 echo 'var groupsCombo = new UpdatableGroupsCombo("'.$CFG->wwwroot.'", '.$course->id.');'."\n";
228 echo 'var membersCombo = new UpdatableMembersCombo("'.$CFG->wwwroot.'", '.$course->id.');'."\n";
229 echo '//]]>'."\n";
230 echo '</script>'."\n";
233 print_footer($course);
236 * Returns the first button action with the given prefix, taken from
237 * POST or GET, otherwise returns false.
238 * See /lib/moodlelib.php function optional_param.
239 * @param $prefix 'act_' as in 'action'.
240 * @return string The action without the prefix, or false if no action found.
242 function groups_param_action($prefix = 'act_') {
243 $action = false;
244 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
246 if ($_POST) {
247 $form_vars = $_POST;
249 elseif ($_GET) {
250 $form_vars = $_GET;
252 if ($form_vars) {
253 foreach ($form_vars as $key => $value) {
254 if (preg_match("/$prefix(.+)/", $key, $matches)) {
255 $action = $matches[1];
256 break;
260 if ($action && !preg_match('/^\w+$/', $action)) {
261 $action = false;
262 error('Action had wrong type.');
264 ///if (debugging()) echo 'Debug: '.$action;
265 return $action;