MDL-12296:
[moodle-linuxchix.git] / group / index.php
blobea316c881fd8918db8c8ca421e73c21872ff6d77
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 'showautocreategroupsform':
72 redirect('autogroup.php?courseid='.$courseid);
73 break;
75 case 'showgroupsettingsform':
76 redirect('group.php?courseid='.$courseid.'&amp;id='.$groupid);
77 break;
79 case 'updategroups': //Currently reloading.
80 break;
82 case 'removemembers':
83 break;
85 case 'showaddmembersform':
86 redirect('members.php?group='.$groupid);
87 break;
89 case 'updatemembers': //Currently reloading.
90 break;
92 default: //ERROR.
93 if (debugging()) {
94 error('Error, unknown button/action. Probably a user-interface bug!', $returnurl);
95 break;
99 // Print the page and form
100 $strgroups = get_string('groups');
101 $strparticipants = get_string('participants');
103 $navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
104 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
105 $navigation = build_navigation($navlinks);
107 /// Print header
108 print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
109 // Add tabs
110 $currenttab = 'groups';
111 require('tabs.php');
113 $disabled = 'disabled="disabled"';
115 $showeditgroupsettingsform_disabled = $disabled;
116 $deletegroup_disabled = $disabled;
117 $showcreategroupform_disabled = $disabled;
119 if (!empty($groupid)) {
120 $showaddmembersform_disabled = '';
121 $showeditgroupsettingsform_disabled = '';
122 $deletegroup_disabled = '';
123 } else {
124 $deletegroup_disabled = $disabled;
125 $showeditgroupsettingsform_disabled = $disabled;
126 $showaddmembersform_disabled = $disabled;
129 print_heading(format_string($course->shortname) .' '.$strgroups, 'center', 3);
130 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
131 echo '<div>'."\n";
132 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
134 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
135 echo '<tr>'."\n";
138 echo "<td>\n";
139 // NO GROUPINGS YET!
140 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
142 if (ajaxenabled()) {
143 $onchange = 'membersCombo.refreshMembers(this.options[this.selectedIndex].value);';
144 } else {
145 $onchange = '';
149 echo '<select name="group" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
150 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
152 $groups = groups_get_all_groups($courseid);
154 $sel_groupid = 0;
156 if ($groups) {
157 // Print out the HTML
158 foreach ($groups as $group) {
159 $select = '';
160 if ($groupid == $group->id) {
161 $select = ' selected="selected"';
162 $sel_groupid = $group->id;
164 $usercount = (int)count_records('groups_members', 'groupid', $group->id);
165 $groupname = format_string($group->name).' ('.$usercount.')';
167 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
169 } else {
170 // Print an empty option to avoid the XHTML error of having an empty select element
171 echo '<option>&nbsp;</option>';
174 echo '</select>'."\n";
175 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
176 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
177 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
178 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
179 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
180 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
182 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
183 . get_string('creategroup', 'group') . '" /></p>'."\n";
185 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
186 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
188 echo '</td>'."\n";
189 echo '<td>'."\n";
190 echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup">&nbsp;</span></label></p>'."\n";
191 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
192 echo '<select name="user" id="members" size="15" class="select"'."\n";
193 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
195 $member_names = array();
197 if ($sel_groupid) {
198 if ($members = groups_get_members($groupid)) {
199 foreach($members as $member) {
200 $member_names[$member->id] = fullname($member, true);
205 if ($member_names) {
206 // Put the groupings into a hash and sort them
207 foreach ($member_names as $userid=>$username) {
208 echo "<option value=\"{$userid}\" title=\"{$username}\">{$username}</option>\n";
211 } else {
212 // Print an empty option to avoid the XHTML error of having an empty select element
213 echo '<option>&nbsp;</option>';
216 echo '</select>'."\n";
218 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
219 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
220 echo '</td>'."\n";
221 echo '</tr>'."\n";
222 echo '</table>'."\n";
224 //<input type="hidden" name="rand" value="om" />
225 echo '</div>'."\n";
226 echo '</form>'."\n";
228 if (ajaxenabled()) {
229 echo '<script type="text/javascript">'."\n";
230 echo '//<![CDATA['."\n";
231 echo 'var groupsCombo = new UpdatableGroupsCombo("'.$CFG->wwwroot.'", '.$course->id.');'."\n";
232 echo 'var membersCombo = new UpdatableMembersCombo("'.$CFG->wwwroot.'", '.$course->id.');'."\n";
233 echo '//]]>'."\n";
234 echo '</script>'."\n";
237 print_footer($course);
240 * Returns the first button action with the given prefix, taken from
241 * POST or GET, otherwise returns false.
242 * See /lib/moodlelib.php function optional_param.
243 * @param $prefix 'act_' as in 'action'.
244 * @return string The action without the prefix, or false if no action found.
246 function groups_param_action($prefix = 'act_') {
247 $action = false;
248 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
250 if ($_POST) {
251 $form_vars = $_POST;
253 elseif ($_GET) {
254 $form_vars = $_GET;
256 if ($form_vars) {
257 foreach ($form_vars as $key => $value) {
258 if (preg_match("/$prefix(.+)/", $key, $matches)) {
259 $action = $matches[1];
260 break;
264 if ($action && !preg_match('/^\w+$/', $action)) {
265 $action = false;
266 error('Action had wrong type.');
268 ///if (debugging()) echo 'Debug: '.$action;
269 return $action;