MDL-21887 fixed borked JS, but still the groups ajax needs a major cleanup
[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 $PAGE->requires->yui2_lib('connection');
15 $PAGE->requires->js('/group/clientlib.js');
17 $courseid = required_param('id', PARAM_INT);
18 $groupid  = optional_param('group', false, PARAM_INT);
19 $userid   = optional_param('user', false, PARAM_INT);
20 $action   = groups_param_action();
21 // Support either single group= parameter, or array groups[]
22 if ($groupid) {
23     $groupids=array($groupid);
24 } else {
25     $groupids = optional_param('groups', array(), PARAM_INT);
26 }
27 $singlegroup=count($groupids) == 1;
29 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
31 // Get the course information so we can print the header and
32 // check the course id is valid
34 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
35     $success = false;
36     print_error('invalidcourse'); //'The course ID is invalid'
37 }
39 $url = new moodle_url('/group/index.php', array('id'=>$courseid));
40 if ($userid) {
41     $url->param('user', $userid);
42 }
43 if ($groupid) {
44     $url->param('group', $groupid);
45 }
46 $PAGE->set_url($url);
48 // Make sure that the user has permissions to manage groups.
49 require_login($course);
51 $context = get_context_instance(CONTEXT_COURSE, $courseid);
52 if (! has_capability('moodle/course:managegroups', $context)) {
53     redirect(); //"group.php?id=$course->id");   // Not allowed to see all groups
54 }
56 // Check for multiple/no group errors
57 if(!$singlegroup) {
58     switch($action) {
59         case 'ajax_getmembersingroup':
60         case 'showgroupsettingsform':
61         case 'showaddmembersform':
62         case 'updatemembers':
63             print_error('errorselectone','group',$returnurl);
64     }
65 }
67 switch ($action) {
68     case false: //OK, display form.
69         break;
71     case 'ajax_getmembersingroup':
72         $roles = array();
73         if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
74             foreach($groupmemberroles as $roleid=>$roledata) {
75                 $shortroledata=new StdClass;
76                 $shortroledata->name=$roledata->name;
77                 $shortroledata->users=array();
78                 foreach($roledata->users as $member) {
79                     $shortmember=new StdClass;
80                     $shortmember->id=$member->id;
81                     $shortmember->name=fullname($member, true);
82                     $shortroledata->users[]=$shortmember;
83                 }
84                 $roles[]=$shortroledata;
85             }
86         }
87         echo json_encode($roles);
88         die;  // Client side JavaScript takes it from here.
90     case 'deletegroup':
91         if(count($groupids)==0) {
92             print_error('errorselectsome','group',$returnurl);
93         }
94         $groupidlist=implode(',',$groupids);
95         redirect('delete.php?courseid='.$courseid.'&groups='.$groupidlist);
96         break;
98     case 'showcreateorphangroupform':
99         redirect('group.php?courseid='.$courseid);
100         break;
102     case 'showautocreategroupsform':
103         redirect('autogroup.php?courseid='.$courseid);
104         break;
106     case 'showgroupsettingsform':
107         redirect('group.php?courseid='.$courseid.'&amp;id='.$groupids[0]);
108         break;
110     case 'updategroups': //Currently reloading.
111         break;
113     case 'removemembers':
114         break;
116     case 'showaddmembersform':
117         redirect('members.php?group='.$groupids[0]);
118         break;
120     case 'updatemembers': //Currently reloading.
121         break;
123     default: //ERROR.
124         if (debugging()) {
125             print_error('unknowaction', '', $returnurl);
126         break;
127     }
130 // Print the page and form
131 $strgroups = get_string('groups');
132 $strparticipants = get_string('participants');
134 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
135 $PAGE->navbar->add($strgroups);
137 /// Print header
138 $PAGE->set_title($strgroups);
139 $PAGE->set_heading(': '.$strgroups);
140 echo $OUTPUT->header();
142 // Add tabs
143 $currenttab = 'groups';
144 require('tabs.php');
146 $disabled = 'disabled="disabled"';
147 if (ajaxenabled()) {
148     // Some buttons are enabled if single group selected
149     $showaddmembersform_disabled = $singlegroup ? '' : $disabled;
150     $showeditgroupsettingsform_disabled = $singlegroup ? '' : $disabled;
151     $deletegroup_disabled = count($groupids)>0 ? '' : $disabled;
152 } else {
153     // Do not disable buttons. The buttons work based on the selected group,
154     // which you can change without reloading the page, so it is not appropriate
155     // to disable them if no group is selected.
156     $showaddmembersform_disabled = '';
157     $showeditgroupsettingsform_disabled = '';
158     $deletegroup_disabled = '';
161 echo $OUTPUT->heading(format_string($course->shortname) .' '.$strgroups, 3);
162 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
163 echo '<div>'."\n";
164 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
166 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
167 echo '<tr>'."\n";
170 echo "<td>\n";
171 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
173 if (ajaxenabled()) { // TODO: move this to JS init!
174     $onchange = 'M.core_group.membersCombo.refreshMembers();';
175 } else {
176     $onchange = '';
179 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
180 echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
182 $groups = groups_get_all_groups($courseid);
183 $selectedname = '&nbsp;';
185 if ($groups) {
186     // Print out the HTML
187     foreach ($groups as $group) {
188         $select = '';
189         $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id));
190         $groupname = format_string($group->name).' ('.$usercount.')';
191         if (in_array($group->id,$groupids)) {
192             $select = ' selected="selected"';
193             if ($singlegroup) {
194                 // Only keep selected name if there is one group selected
195                 $selectedname = $groupname;
196             }
197         }
199         echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
200     }
201 } else {
202     // Print an empty option to avoid the XHTML error of having an empty select element
203     echo '<option>&nbsp;</option>';
206 echo '</select>'."\n";
207 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
208         . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
209 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
210         . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
211 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
212         . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
214 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
215         . get_string('creategroup', 'group') . '" /></p>'."\n";
217 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
218         . get_string('autocreategroups', 'group') . '" /></p>'."\n";
220 echo '</td>'."\n";
221 echo '<td>'."\n";
223 echo '<p><label for="members"><span id="memberslabel">'.
224     get_string('membersofselectedgroup', 'group').
225     ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
226 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
227 echo '<select name="user" id="members" size="15" class="select"'."\n";
228 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
230 $member_names = array();
232 $atleastonemember = false;
233 if ($singlegroup) {
234     if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
235         foreach($groupmemberroles as $roleid=>$roledata) {
236             echo '<optgroup label="'.s($roledata->name).'">';
237             foreach($roledata->users as $member) {
238                 echo '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
239                 $atleastonemember = true;
240             }
241             echo '</optgroup>';
242         }
243     }
246 if (!$atleastonemember) {
247     // Print an empty option to avoid the XHTML error of having an empty select element
248     echo '<option>&nbsp;</option>';
251 echo '</select>'."\n";
253 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
254         . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
255 echo '</td>'."\n";
256 echo '</tr>'."\n";
257 echo '</table>'."\n";
259 //<input type="hidden" name="rand" value="om" />
260 echo '</div>'."\n";
261 echo '</form>'."\n";
263 if (ajaxenabled()) {
264     $PAGE->requires->js_init_call('M.core_group.init_index');
267 echo $OUTPUT->footer();
269 /**
270  * Returns the first button action with the given prefix, taken from
271  * POST or GET, otherwise returns false.
272  * See /lib/moodlelib.php function optional_param.
273  * @param $prefix 'act_' as in 'action'.
274  * @return string The action without the prefix, or false if no action found.
275  */
276 function groups_param_action($prefix = 'act_') {
277     $action = false;
278 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
280     if ($_POST) {
281         $form_vars = $_POST;
282     }
283     elseif ($_GET) {
284         $form_vars = $_GET;
285     }
286     if ($form_vars) {
287         foreach ($form_vars as $key => $value) {
288             if (preg_match("/$prefix(.+)/", $key, $matches)) {
289                 $action = $matches[1];
290                 break;
291             }
292         }
293     }
294     if ($action && !preg_match('/^\w+$/', $action)) {
295         $action = false;
296         print_error('unknowaction');
297     }
298     ///if (debugging()) echo 'Debug: '.$action;
299     return $action;