group MDL-19798 Updated print_header and build_navigation to OUTPUT and PAGE equivalents
[moodle.git] / group / index.php
1 <?php
2 /**
3  * The main group management user interface.
4  *
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
10  */
11 require_once('../config.php');
12 require_once('lib.php');
14 ini_set('include_path', $CFG->libdir.'/pear'.PATH_SEPARATOR.ini_get('include_path'));
16 $PAGE->requires->yui_lib('connection');
17 $PAGE->requires->js('group/clientlib.js');
19 $courseid = required_param('id', PARAM_INT);
20 $groupid  = optional_param('group', false, PARAM_INT);
21 $userid   = optional_param('user', false, PARAM_INT);
22 $action   = groups_param_action();
23 // Support either single group= parameter, or array groups[]
24 if ($groupid) {
25     $groupids=array($groupid);
26 } else {
27     $groupids = optional_param('groups', array(), PARAM_INT);
28 }
29 $singlegroup=count($groupids) == 1;
31 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
33 // Get the course information so we can print the header and
34 // check the course id is valid
36 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
37     $success = false;
38     print_error('invalidcourse'); //'The course ID is invalid'
39 }
41 // Make sure that the user has permissions to manage groups.
42 require_login($course);
44 $context = get_context_instance(CONTEXT_COURSE, $courseid);
45 if (! has_capability('moodle/course:managegroups', $context)) {
46     redirect(); //"group.php?id=$course->id");   // Not allowed to see all groups
47 }
49 // Check for multiple/no group errors
50 if(!$singlegroup) {
51     switch($action) {
52         case 'ajax_getmembersingroup':
53         case 'showgroupsettingsform':
54         case 'showaddmembersform':
55         case 'updatemembers':
56             print_error('errorselectone','group',$returnurl);
57     }
58 }
60 switch ($action) {
61     case false: //OK, display form.
62         break;
64     case 'ajax_getmembersingroup':
65         $roles = array();
66         if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
67             foreach($groupmemberroles as $roleid=>$roledata) {
68                 $shortroledata=new StdClass;
69                 $shortroledata->name=$roledata->name;
70                 $shortroledata->users=array();
71                 foreach($roledata->users as $member) {
72                     $shortmember=new StdClass;
73                     $shortmember->id=$member->id;
74                     $shortmember->name=fullname($member, true);
75                     $shortroledata->users[]=$shortmember;
76                 }
77                 $roles[]=$shortroledata;
78             }
79         }
80         echo json_encode($roles);
81         die;  // Client side JavaScript takes it from here.
83     case 'deletegroup':
84         if(count($groupids)==0) {
85             print_error('errorselectsome','group',$returnurl);
86         }
87         $groupidlist=implode(',',$groupids);
88         redirect('delete.php?courseid='.$courseid.'&groups='.$groupidlist);
89         break;
91     case 'showcreateorphangroupform':
92         redirect('group.php?courseid='.$courseid);
93         break;
95     case 'showautocreategroupsform':
96         redirect('autogroup.php?courseid='.$courseid);
97         break;
99     case 'showgroupsettingsform':
100         redirect('group.php?courseid='.$courseid.'&amp;id='.$groupids[0]);
101         break;
103     case 'updategroups': //Currently reloading.
104         break;
106     case 'removemembers':
107         break;
109     case 'showaddmembersform':
110         redirect('members.php?group='.$groupids[0]);
111         break;
113     case 'updatemembers': //Currently reloading.
114         break;
116     default: //ERROR.
117         if (debugging()) {
118             print_error('unknowaction', '', $returnurl);
119         break;
120     }
123 // Print the page and form
124 $strgroups = get_string('groups');
125 $strparticipants = get_string('participants');
127 $PAGE->navbar->add($strparticipants, new moodle_url($CFG->wwwroot.'/user/index.php', array('id'=>$courseid)));
128 $PAGE->navbar->add($strgroups);
130 /// Print header
131 $PAGE->set_title($strgroups);
132 $PAGE->set_heading(': '.$strgroups);
133 echo $OUTPUT->header();
135 // Add tabs
136 $currenttab = 'groups';
137 require('tabs.php');
139 $disabled = 'disabled="disabled"';
140 if (ajaxenabled()) {
141     // Some buttons are enabled if single group selected
142     $showaddmembersform_disabled = $singlegroup ? '' : $disabled;
143     $showeditgroupsettingsform_disabled = $singlegroup ? '' : $disabled;
144     $deletegroup_disabled = count($groupids)>0 ? '' : $disabled;
145 } else {
146     // Do not disable buttons. The buttons work based on the selected group,
147     // which you can change without reloading the page, so it is not appropriate
148     // to disable them if no group is selected.
149     $showaddmembersform_disabled = '';
150     $showeditgroupsettingsform_disabled = '';
151     $deletegroup_disabled = '';
154 echo $OUTPUT->heading(format_string($course->shortname) .' '.$strgroups, 3);
155 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
156 echo '<div>'."\n";
157 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
159 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
160 echo '<tr>'."\n";
163 echo "<td>\n";
164 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
166 if (ajaxenabled()) {
167     $onchange = 'membersCombo.refreshMembers();';
168 } else {
169     $onchange = '';
172 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
173 echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
175 $groups = groups_get_all_groups($courseid);
176 $selectedname = '&nbsp;';
178 if ($groups) {
179     // Print out the HTML
180     foreach ($groups as $group) {
181         $select = '';
182         $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id));
183         $groupname = format_string($group->name).' ('.$usercount.')';
184         if (in_array($group->id,$groupids)) {
185             $select = ' selected="selected"';
186             if ($singlegroup) {
187                 // Only keep selected name if there is one group selected
188                 $selectedname = $groupname;
189             }
190         }
191         
192         echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
193     }
194 } else {
195     // Print an empty option to avoid the XHTML error of having an empty select element
196     echo '<option>&nbsp;</option>';
199 echo '</select>'."\n";
200 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
201         . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
202 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
203         . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
204 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
205         . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
207 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
208         . get_string('creategroup', 'group') . '" /></p>'."\n";
210 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
211         . get_string('autocreategroups', 'group') . '" /></p>'."\n";
213 echo '</td>'."\n";
214 echo '<td>'."\n";
216 echo '<p><label for="members"><span id="memberslabel">'.
217     get_string('membersofselectedgroup', 'group').
218     ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
219 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
220 echo '<select name="user" id="members" size="15" class="select"'."\n";
221 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
223 $member_names = array();
225 $atleastonemember = false;
226 if ($singlegroup) {
227     if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
228         foreach($groupmemberroles as $roleid=>$roledata) {
229             echo '<optgroup label="'.s($roledata->name).'">';
230             foreach($roledata->users as $member) {
231                 echo '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
232                 $atleastonemember = true;
233             }
234             echo '</optgroup>';
235         }
236     }
239 if (!$atleastonemember) {
240     // Print an empty option to avoid the XHTML error of having an empty select element
241     echo '<option>&nbsp;</option>';
244 echo '</select>'."\n";
246 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
247         . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
248 echo '</td>'."\n";
249 echo '</tr>'."\n";
250 echo '</table>'."\n";
252 //<input type="hidden" name="rand" value="om" />
253 echo '</div>'."\n";
254 echo '</form>'."\n";
256 if (ajaxenabled()) {
257     $PAGE->requires->js_function_call('var groupsCombo = new UpdatableGroupsCombo',
258             array($CFG->httpswwwroot, $course->id));
259     $PAGE->requires->js_function_call('var membersCombo = new UpdatableMembersCombo',
260             array($CFG->httpswwwroot, $course->id));
263 echo $OUTPUT->footer();
265 /**
266  * Returns the first button action with the given prefix, taken from
267  * POST or GET, otherwise returns false.
268  * See /lib/moodlelib.php function optional_param.
269  * @param $prefix 'act_' as in 'action'.
270  * @return string The action without the prefix, or false if no action found.
271  */
272 function groups_param_action($prefix = 'act_') {
273     $action = false;
274 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
276     if ($_POST) {
277         $form_vars = $_POST;
278     }
279     elseif ($_GET) {
280         $form_vars = $_GET;
281     }
282     if ($form_vars) {
283         foreach ($form_vars as $key => $value) {
284             if (preg_match("/$prefix(.+)/", $key, $matches)) {
285                 $action = $matches[1];
286                 break;
287             }
288         }
289     }
290     if ($action && !preg_match('/^\w+$/', $action)) {
291         $action = false;
292         print_error('unknowaction');
293     }
294     ///if (debugging()) echo 'Debug: '.$action;
295     return $action;
298 ?>