e675d24ce7f6a854bf2c039aff21ed6f1ebf0eea
[moodle.git] / group / members.php
1 <?php
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         // Put the groupings into a hash and sorts them
104         foreach ($potentialmembers as $userid => $user) {
105             $nonmembers[$userid] = fullname($user);
106         }
107         natcasesort($nonmembers);
109         // Print out the HTML
110         foreach($nonmembers as $id => $name) {
111             $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
112         }
113     } else {
114         $potentialmembersoptions .= '<option>&nbsp;</option>';
115     }
118 // Print the page and form
119 $strgroups = get_string('groups');
120 $strparticipants = get_string('participants');
121 $stradduserstogroup = get_string('adduserstogroup', 'group');
123 $groupname = format_string($group->name);
125 $navlinks = array();
126 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
127 $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
128 $navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
129 $navigation = build_navigation($navlinks);
131 print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
133 ?>
134 <div id="addmembersform">
135     <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo ": $groupname"; ?></h3>
137     <form id="assignform" method="post" action="members.php">
138     <div>
139     <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
140     <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
142     <table summary="" cellpadding="5" cellspacing="0">
143     <tr>
144       <td valign="top">
145           <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
146           <br />
147           <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
148                   onfocus="document.getElementById('assignform').add.disabled=true;
149                            document.getElementById('assignform').remove.disabled=false;
150                            document.getElementById('assignform').addselect.selectedIndex=-1;">
151           <?php echo $groupmembersoptions ?>
152           </select></td>
153       <td valign="top">
154 <?php // Hidden assignment? ?>
156         <?php check_theme_arrows(); ?>
157         <p class="arrow_button">
158             <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
159             <br />
160             <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
161         </p>
162       </td>
163       <td valign="top">
164           <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
165           <br />
166           <select name="addselect[]" size="20" id="addselect" multiple="multiple"
167                   onfocus="document.getElementById('assignform').add.disabled=false;
168                            document.getElementById('assignform').remove.disabled=true;
169                            document.getElementById('assignform').removeselect.selectedIndex=-1;">
170           <?php
171             if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
172                 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
173                         .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
174             } else {
175                 echo $potentialmembersoptions;
176             }
177           ?>
178          </select>
179          <br />
180          <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
181          <input type="text" name="searchtext" id="searchtext" size="30" value="<?php p($searchtext, true) ?>"
182                   onfocus ="getElementById('assignform').add.disabled=true;
183                             getElementById('assignform').remove.disabled=true;
184                             getElementById('assignform').removeselect.selectedIndex=-1;
185                             getElementById('assignform').addselect.selectedIndex=-1;"
186                   onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
187                                if (keyCode == 13) {
188                                     getElementById('assignform').previoussearch.value=1;
189                                     getElementById('assignform').submit();
190                                } " />
191          <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
192          <?php
193               if (!empty($searchtext)) {
194                   echo '<input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
195               }
196          ?>
197        </td>
198     </tr>
199     <tr><td>
200         <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
201     </td></tr>
202     </table>
203     </div>
204     </form>
205 </div>
207 <?php
208     print_footer($course);
209 ?>