3 * The main group management user interface.
5 * @copyright © 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
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)) {
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
45 case false: //OK, display form.
48 case 'ajax_getmembersingroup':
50 if ($members = groups_get_members($groupid)) {
51 $member_names = array();
52 foreach($members as $member) {
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.
64 redirect('group.php?delete=1&courseid='.$courseid.'&id='.$groupid);
67 case 'showcreateorphangroupform':
68 redirect('group.php?courseid='.$courseid);
71 case 'showgroupsettingsform':
72 redirect('group.php?courseid='.$courseid.'&id='.$groupid);
75 case 'updategroups': //Currently reloading.
81 case 'showaddmembersform':
82 redirect('members.php?group='.$groupid);
85 case 'updatemembers': //Currently reloading.
90 error('Error, unknown button/action. Probably a user-interface bug!', $returnurl);
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);
104 print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
106 if (!empty($CFG->enablegroupings
)) {
108 $currenttab = 'groups';
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 = '';
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";
131 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
133 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
139 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping"> </span></label></p>'."\n";
142 $onchange = 'membersCombo.refreshMembers(this.options[this.selectedIndex].value);';
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);
156 // Print out the HTML
157 foreach ($groups as $group) {
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";
169 // Print an empty option to avoid the XHTML error of having an empty select element
170 echo '<option> </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";
186 echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup"> </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();
194 if ($members = groups_get_members($groupid)) {
195 foreach($members as $member) {
196 $member_names[$member->id
] = fullname($member, true);
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";
208 // Print an empty option to avoid the XHTML error of having an empty select element
209 echo '<option> </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";
218 echo '</table>'."\n";
220 //<input type="hidden" name="rand" value="om" />
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";
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_') {
244 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
253 foreach ($form_vars as $key => $value) {
254 if (preg_match("/$prefix(.+)/", $key, $matches)) {
255 $action = $matches[1];
260 if ($action && !preg_match('/^\w+$/', $action)) {
262 error('Action had wrong type.');
264 ///if (debugging()) echo 'Debug: '.$action;