MDL-25755 enrol ldap - avoid reserved keyword. (k) goes to Chris Myers
[moodle.git] / group / index.php
blob8eac097162a49f27d97e2274dd68c0cacffcc443
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 ini_set('include_path', $CFG->libdir.'/pear'.PATH_SEPARATOR.ini_get('include_path'));
14 require_once('HTML/AJAX/JSON.php');
16 require_js('yui_yahoo');
17 require_js('yui_dom');
18 require_js('yui_utilities');
19 require_js('yui_connection');
20 require_js($CFG->wwwroot.'/group/clientlib.js');
22 $courseid = required_param('id', PARAM_INT);
23 $groupid = optional_param('group', false, PARAM_INT);
24 $userid = optional_param('user', false, PARAM_INT);
25 $action = groups_param_action();
26 // Support either single group= parameter, or array groups[]
27 if ($groupid) {
28 $groupids=array($groupid);
29 } else {
30 $groupids=array();
31 if (isset($_REQUEST['groups'])) {
32 foreach ($_REQUEST['groups'] as $groupid) {
33 if ($groupid = clean_param($groupid, PARAM_INT)) {
34 $groupids[]=$groupid;
39 $singlegroup=count($groupids) == 1;
41 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
43 // Get the course information so we can print the header and
44 // check the course id is valid
46 if (!$course = get_record('course', 'id',$courseid)) {
47 $success = false;
48 print_error('invalidcourse'); //'The course ID is invalid'
51 // Make sure that the user has permissions to manage groups.
52 require_login($course);
54 $context = get_context_instance(CONTEXT_COURSE, $courseid);
55 if (! has_capability('moodle/course:managegroups', $context)) {
56 redirect(); //"group.php?id=$course->id"); // Not allowed to see all groups
59 // Check for multiple/no group errors
60 if(!$singlegroup) {
61 switch($action) {
62 case 'ajax_getmembersingroup':
63 case 'showgroupsettingsform':
64 case 'showaddmembersform':
65 case 'updatemembers':
66 print_error('errorselectone','group',$returnurl);
70 switch ($action) {
71 case false: //OK, display form.
72 break;
74 case 'ajax_getmembersingroup':
75 $roles = array();
76 if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
77 foreach($groupmemberroles as $roleid=>$roledata) {
78 $shortroledata=new StdClass;
79 $shortroledata->name=$roledata->name;
80 $shortroledata->users=array();
81 foreach($roledata->users as $member) {
82 $shortmember=new StdClass;
83 $shortmember->id=$member->id;
84 $shortmember->name=fullname($member, true);
85 $shortroledata->users[]=$shortmember;
87 $roles[]=$shortroledata;
90 echo json_encode($roles);
91 die; // Client side JavaScript takes it from here.
93 case 'deletegroup':
94 if(count($groupids)==0) {
95 print_error('errorselectsome','group',$returnurl);
97 $groupidlist=implode(',',$groupids);
98 redirect('delete.php?courseid='.$courseid.'&groups='.$groupidlist);
99 break;
101 case 'showcreateorphangroupform':
102 redirect('group.php?courseid='.$courseid);
103 break;
105 case 'showautocreategroupsform':
106 redirect('autogroup.php?courseid='.$courseid);
107 break;
109 case 'showgroupsettingsform':
110 redirect('group.php?courseid='.$courseid.'&amp;id='.$groupids[0]);
111 break;
113 case 'updategroups': //Currently reloading.
114 break;
116 case 'removemembers':
117 break;
119 case 'showaddmembersform':
120 redirect('members.php?group='.$groupids[0]);
121 break;
123 case 'updatemembers': //Currently reloading.
124 break;
126 default: //ERROR.
127 if (debugging()) {
128 error('Error, unknown button/action. Probably a user-interface bug!', $returnurl);
129 break;
133 // Print the page and form
134 $strgroups = get_string('groups');
135 $strparticipants = get_string('participants');
137 $navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
138 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
139 $navigation = build_navigation($navlinks);
141 /// Print header
142 print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
143 // Add tabs
144 $currenttab = 'groups';
145 require('tabs.php');
147 $disabled = 'disabled="disabled"';
148 if (ajaxenabled()) {
149 // Some buttons are enabled if single group selected
150 $showaddmembersform_disabled = $singlegroup ? '' : $disabled;
151 $showeditgroupsettingsform_disabled = $singlegroup ? '' : $disabled;
152 $deletegroup_disabled = count($groupids)>0 ? '' : $disabled;
153 } else {
154 // Do not disable buttons. The buttons work based on the selected group,
155 // which you can change without reloading the page, so it is not appropriate
156 // to disable them if no group is selected.
157 $showaddmembersform_disabled = '';
158 $showeditgroupsettingsform_disabled = '';
159 $deletegroup_disabled = '';
162 print_heading(format_string($course->shortname) .' '.$strgroups, 'center', 3);
163 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
164 echo '<div>'."\n";
165 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
167 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
168 echo '<tr>'."\n";
171 echo "<td>\n";
172 // NO GROUPINGS YET!
173 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
175 if (ajaxenabled()) {
176 $onchange = 'membersCombo.refreshMembers();';
177 } else {
178 $onchange = '';
182 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
183 echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
185 $groups = groups_get_all_groups($courseid);
186 $selectedname = '&nbsp;';
188 if ($groups) {
189 // Print out the HTML
190 foreach ($groups as $group) {
191 $select = '';
192 $usercount = (int)count_records('groups_members', 'groupid', $group->id);
193 $groupname = format_string($group->name).' ('.$usercount.')';
194 if (in_array($group->id,$groupids)) {
195 $select = ' selected="selected"';
196 if ($singlegroup) {
197 // Only keep selected name if there is one group selected
198 $selectedname = $groupname;
202 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
204 } else {
205 // Print an empty option to avoid the XHTML error of having an empty select element
206 echo '<option>&nbsp;</option>';
209 echo '</select>'."\n";
210 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
211 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
212 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
213 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
214 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
215 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
217 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
218 . get_string('creategroup', 'group') . '" /></p>'."\n";
220 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
221 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
223 echo '</td>'."\n";
224 echo '<td>'."\n";
226 echo '<p><label for="members"><span id="memberslabel">'.
227 get_string('membersofselectedgroup', 'group').
228 ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
229 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
230 echo '<select name="user" id="members" size="15" class="select"'."\n";
231 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
233 $member_names = array();
235 $atleastonemember = false;
236 if ($singlegroup) {
237 if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
238 foreach($groupmemberroles as $roleid=>$roledata) {
239 echo '<optgroup label="'.htmlspecialchars($roledata->name).'">';
240 foreach($roledata->users as $member) {
241 echo '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
242 $atleastonemember = true;
244 echo '</optgroup>';
249 if (!$atleastonemember) {
250 // Print an empty option to avoid the XHTML error of having an empty select element
251 echo '<option>&nbsp;</option>';
254 echo '</select>'."\n";
256 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
257 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
258 echo '</td>'."\n";
259 echo '</tr>'."\n";
260 echo '</table>'."\n";
262 //<input type="hidden" name="rand" value="om" />
263 echo '</div>'."\n";
264 echo '</form>'."\n";
266 if (ajaxenabled()) {
267 echo '<script type="text/javascript">'."\n";
268 echo '//<![CDATA['."\n";
269 echo 'var groupsCombo = new UpdatableGroupsCombo("'.$CFG->wwwroot.'", '.$course->id.');'."\n";
270 echo 'var membersCombo = new UpdatableMembersCombo("'.$CFG->wwwroot.'", '.$course->id.');'."\n";
271 echo '//]]>'."\n";
272 echo '</script>'."\n";
275 print_footer($course);
278 * Returns the first button action with the given prefix, taken from
279 * POST or GET, otherwise returns false.
280 * See /lib/moodlelib.php function optional_param.
281 * @param $prefix 'act_' as in 'action'.
282 * @return string The action without the prefix, or false if no action found.
284 function groups_param_action($prefix = 'act_') {
285 $action = false;
286 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
288 if ($_POST) {
289 $form_vars = $_POST;
291 elseif ($_GET) {
292 $form_vars = $_GET;
294 if ($form_vars) {
295 foreach ($form_vars as $key => $value) {
296 if (preg_match("/$prefix(.+)/", $key, $matches)) {
297 $action = $matches[1];
298 break;
302 if ($action && !preg_match('/^\w+$/', $action)) {
303 $action = false;
304 error('Action had wrong type.');
306 ///if (debugging()) echo 'Debug: '.$action;
307 return $action;