MDL-11419 - groups: interface enhancements + new features:
[moodle.git] / group / members.php
1 <?php // $Id$
2 /**
3  * Add/remove members from group.
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 define("MAX_USERS_PER_PAGE", 5000);
16 $groupid    = required_param('group', PARAM_INT);
17 $searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
18 $showall    = optional_param('showall', 0, PARAM_BOOL);
20 if ($showall) {
21     $searchtext = '';
22 }
24 if (!$group = get_record('groups', 'id', $groupid)) {
25     error('Incorrect group id');
26 }
28 if (!$course = get_record('course', 'id', $group->courseid)) {
29     print_error('invalidcourse');
30 }
31 $courseid = $course->id;
33 require_login($course);
34 $context = get_context_instance(CONTEXT_COURSE, $courseid);
35 require_capability('moodle/course:managegroups', $context);
37 $strsearch = get_string('search');
38 $strshowall = get_string('showall');
39 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$group->id;
42 if ($frm = data_submitted() and confirm_sesskey()) {
44     if (isset($frm->cancel)) {
45         redirect($returnurl);
47     } else if (isset($frm->add) and !empty($frm->addselect)) {
49         foreach ($frm->addselect as $userid) {
50             if (! $userid = clean_param($userid, PARAM_INT)) {
51                 continue;
52             }
53             if (!groups_add_member($groupid, $userid)) {
54                 print_error('erroraddremoveuser', 'group', $returnurl);
55             }
56         }
58     } else if (isset($frm->remove) and !empty($frm->removeselect)) {
60         foreach ($frm->removeselect as $userid) {
61             if (! $userid = clean_param($userid, PARAM_INT)) {
62                 continue;
63             }
64             if (!groups_remove_member($groupid, $userid)) {
65                 print_error('erroraddremoveuser', 'group', $returnurl);
66             }
68             // MDL-9983
69             $eventdata = new object();
70             $eventdata -> groupid = $groupid;
71             $eventdata -> userid = $userid;
72             events_trigger('group_user_removed', $eventdata);
73         }
74     }
75 }
77 $groupmembersoptions = '';
78 $groupmemberscount = 0;
80 if ($groupmembers = groups_get_members($groupid)) {
81     foreach($groupmembers as $member) {
82         $groupmembersoptions .= '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
83         $groupmemberscount ++;
84     }
86 } else {
87     $groupmembersoptions .= '<option>&nbsp;</option>';
88 }
90 $potentialmembers = array();
91 $potentialmembersoptions = '';
92 $potentialmemberscount = 0;
94 $potentialmembers = groups_get_users_not_in_group($courseid, $groupid, $searchtext);
95 if (!empty($potentialmembers)) {
96     $potentialmemberscount = count($potentialmembers);
97 } else {
98     $potentialmemberscount = 0;
99 }
100 if ($potentialmemberscount <=  MAX_USERS_PER_PAGE) {
102     if ($potentialmembers != false) {
103         // Get other groups user already belongs to
104         $sql = "SELECT u.id AS userid, g.* FROM {$CFG->prefix}user u " .
105                     "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid " .
106                     "INNER JOIN {$CFG->prefix}groups g ON gm.groupid = g.id " .
107                "WHERE u.id IN (".implode(',',array_keys($potentialmembers)).") AND g.courseid = {$course->id} ";
108         $rs = get_recordset_sql($sql);
109         $groups = array();
110         $usergroups = array();
111         while ($usergroup =  rs_fetch_next_record($rs)) {
112             $usergroups[$usergroup->userid][$usergroup->id] = $usergroup;
113         }
114         
115         // Put the groupings into a hash and sorts them
116         foreach ($potentialmembers as $userid => $user) {
117             $nonmembers[$userid] = fullname($user)." (".@count($usergroups[$userid]).")";
118         }
119         natcasesort($nonmembers);
121         // Print out the HTML
122         foreach($nonmembers as $id => $name) {
123             $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
124         }
125     } else {
126         $potentialmembersoptions .= '<option>&nbsp;</option>';
127     }
130 // Print the page and form
131 $strgroups = get_string('groups');
132 $strparticipants = get_string('participants');
133 $stradduserstogroup = get_string('adduserstogroup', 'group');
134 $strusergroupmembership = get_string('usergroupmembership', 'group');
136 $groupname = format_string($group->name);
138 $navlinks = array();
139 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
140 $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
141 $navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
142 $navigation = build_navigation($navlinks);
144 print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
146 // Print Javascript for showing the selected users group membership
147 ?>
148 <script type="text/javascript">
149 //<![CDATA[
150 var userSummaries = Array( 
151 <?php
152 $membercnt = count($nonmembers);
153 $i=1;
154 foreach ($nonmembers as $userid => $potentalmember) {
156     if (isset($usergroups[$userid])) {
157         $usergrouplist = '<ul>';
158     
159         foreach ($usergroups[$userid] as $groupitem) {
160             $usergrouplist .= '<li>'.addslashes(format_string($groupitem->name)).'</li>';
161         }
162         $usergrouplist .= '</ul>';
163     }
164     else {
165         $usergrouplist = '';
166     }
167     echo "'$usergrouplist'";
168     if ($i < $membercnt) {
169         echo ', ';
170     }
171     $i++;
173 ?>
174 );
176 function updateUserSummary() {
178     var selectEl = document.getElementById('addselect');
179     var summaryDiv = document.getElementById('group-usersummary');
180     var length = selectEl.length;
181     var selectCnt = 0;
182     var selectIdx = -1;
183     
184     for(i=0;i<length;i++) {
185         if (selectEl.options[i].selected) {
186                 selectCnt++;
187             selectIdx = i;
188         }
189     }
191     if (selectCnt == 1 && userSummaries[selectIdx]) {
192         summaryDiv.innerHTML = userSummaries[selectIdx];
193     } else {
194         summaryDiv.innerHTML = '';
195     }
196     
197     return(true);
199 //]]>
200 </script>
202 <div id="addmembersform">
203     <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo ": $groupname"; ?></h3>
205     <form id="assignform" method="post" action="members.php">
206     <div>
207     <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
208     <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
210     <table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
211     <tr>
212       <td valign="top">
213           <p>
214             <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
215           </p>
216           <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
217                   onfocus="document.getElementById('assignform').add.disabled=true;
218                            document.getElementById('assignform').remove.disabled=false;
219                            document.getElementById('assignform').addselect.selectedIndex=-1;"
220                   onclick="this.focus();updateUserSummary();">
221           <?php echo $groupmembersoptions ?>
222           </select></td>
223       <td valign="top">
224 <?php // Hidden assignment? ?>
226         <?php check_theme_arrows(); ?>
227         <p class="arrow_button">
228             <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
229             <br />
230             <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
231         </p>
232       </td>
233       <td valign="top">
234           <p>
235             <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
236           </p>
237           <select name="addselect[]" size="20" id="addselect" multiple="multiple"
238                   onfocus="updateUserSummary();document.getElementById('assignform').add.disabled=false;
239                            document.getElementById('assignform').remove.disabled=true;
240                            document.getElementById('assignform').removeselect.selectedIndex=-1;"
241                   onclick="this.focus();updateUserSummary();">
242           <?php
243             if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
244                 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
245                         .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
246             } else {
247                 echo $potentialmembersoptions;
248             }
249           ?>
250          </select>
251          <br />
252          <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
253          <input type="text" name="searchtext" id="searchtext" size="21" value="<?php p($searchtext, true) ?>"
254                   onfocus ="getElementById('assignform').add.disabled=true;
255                             getElementById('assignform').remove.disabled=true;
256                             getElementById('assignform').removeselect.selectedIndex=-1;
257                             getElementById('assignform').addselect.selectedIndex=-1;"
258                   onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
259                                if (keyCode == 13) {
260                                     getElementById('assignform').previoussearch.value=1;
261                                     getElementById('assignform').submit();
262                                } " />
263          <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
264          <?php
265               if (!empty($searchtext)) {
266                   echo '<br /><input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
267               }
268          ?>
269        </td>
270        <td valign="top">
271         <p><?php p($strusergroupmembership) ?></p>
272         <div id="group-usersummary"></div>
273        </td>
274     </tr>
275     <tr><td>
276         <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
277     </td></tr>
278     </table>
279     </div>
280     </form>
281 </div>
283 <?php
284     print_footer($course);
285 ?>