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 ($groupmemberroles = groups_get_members_by_role($groupid,$courseid,'u.id,u.firstname,u.lastname')) {
51 foreach($groupmemberroles as $roleid=>$roledata) {
52 $shortroledata=new StdClass
;
53 $shortroledata->name
=$roledata->name
;
54 $shortroledata->users
=array();
55 foreach($roledata->users
as $member) {
56 $shortmember=new StdClass
;
57 $shortmember->id
=$member->id
;
58 $shortmember->name
=fullname($member, true);
59 $shortroledata->users
[]=$shortmember;
61 $roles[]=$shortroledata;
64 $json = new Services_JSON();
65 echo $json->encode($roles);
66 die; // Client side JavaScript takes it from here.
69 redirect('group.php?delete=1&courseid='.$courseid.'&id='.$groupid);
72 case 'showcreateorphangroupform':
73 redirect('group.php?courseid='.$courseid);
76 case 'showautocreategroupsform':
77 redirect('autogroup.php?courseid='.$courseid);
80 case 'showgroupsettingsform':
81 redirect('group.php?courseid='.$courseid.'&id='.$groupid);
84 case 'updategroups': //Currently reloading.
90 case 'showaddmembersform':
91 redirect('members.php?group='.$groupid);
94 case 'updatemembers': //Currently reloading.
99 error('Error, unknown button/action. Probably a user-interface bug!', $returnurl);
104 // Print the page and form
105 $strgroups = get_string('groups');
106 $strparticipants = get_string('participants');
108 $navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot
.'/user/index.php?id='.$courseid, 'type'=>'misc'),
109 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
110 $navigation = build_navigation($navlinks);
113 print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
115 $currenttab = 'groups';
118 $disabled = 'disabled="disabled"';
120 $showeditgroupsettingsform_disabled = $disabled;
121 $deletegroup_disabled = $disabled;
122 $showcreategroupform_disabled = $disabled;
124 if (!empty($groupid)) {
125 $showaddmembersform_disabled = '';
126 $showeditgroupsettingsform_disabled = '';
127 $deletegroup_disabled = '';
129 $deletegroup_disabled = $disabled;
130 $showeditgroupsettingsform_disabled = $disabled;
131 $showaddmembersform_disabled = $disabled;
134 print_heading(format_string($course->shortname
) .' '.$strgroups, 'center', 3);
135 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
137 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
139 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
145 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping"> </span></label></p>'."\n";
148 $onchange = 'membersCombo.refreshMembers(this.options[this.selectedIndex].value);';
154 echo '<select name="group" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
155 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
157 $groups = groups_get_all_groups($courseid);
162 // Print out the HTML
163 foreach ($groups as $group) {
165 if ($groupid == $group->id
) {
166 $select = ' selected="selected"';
167 $sel_groupid = $group->id
;
169 $usercount = (int)count_records('groups_members', 'groupid', $group->id
);
170 $groupname = format_string($group->name
).' ('.$usercount.')';
172 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
175 // Print an empty option to avoid the XHTML error of having an empty select element
176 echo '<option> </option>';
179 echo '</select>'."\n";
180 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
181 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
182 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
183 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
184 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
185 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
187 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
188 . get_string('creategroup', 'group') . '" /></p>'."\n";
190 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
191 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
195 echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup"> </span></label></p>'."\n";
196 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
197 echo '<select name="user" id="members" size="15" class="select"'."\n";
198 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
200 $member_names = array();
202 $atleastonemember = false;
205 if ($groupmemberroles = groups_get_members_by_role($groupid,$courseid,'u.id,u.firstname,u.lastname')) {
206 foreach($groupmemberroles as $roleid=>$roledata) {
207 echo '<optgroup label="'.htmlspecialchars($roledata->name
).'">';
208 foreach($roledata->users
as $member) {
209 echo '<option value="'.$member->id
.'">'.fullname($member, true).'</option>';
210 $atleastonemember = true;
217 if (!$atleastonemember) {
218 // Print an empty option to avoid the XHTML error of having an empty select element
219 echo '<option> </option>';
222 echo '</select>'."\n";
224 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
225 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
228 echo '</table>'."\n";
230 //<input type="hidden" name="rand" value="om" />
235 echo '<script type="text/javascript">'."\n";
236 echo '//<![CDATA['."\n";
237 echo 'var groupsCombo = new UpdatableGroupsCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
238 echo 'var membersCombo = new UpdatableMembersCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
240 echo '</script>'."\n";
243 print_footer($course);
246 * Returns the first button action with the given prefix, taken from
247 * POST or GET, otherwise returns false.
248 * See /lib/moodlelib.php function optional_param.
249 * @param $prefix 'act_' as in 'action'.
250 * @return string The action without the prefix, or false if no action found.
252 function groups_param_action($prefix = 'act_') {
254 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
263 foreach ($form_vars as $key => $value) {
264 if (preg_match("/$prefix(.+)/", $key, $matches)) {
265 $action = $matches[1];
270 if ($action && !preg_match('/^\w+$/', $action)) {
272 error('Action had wrong type.');
274 ///if (debugging()) echo 'Debug: '.$action;