3 * Add/remove members from group.
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');
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
);
24 if (!$group = get_record('groups', 'id', $groupid)) {
25 error('Incorrect group id');
28 if (!$course = get_record('course', 'id', $group->courseid
)) {
29 print_error('invalidcourse');
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
)) {
47 } else if (isset($frm->add
) and !empty($frm->addselect
)) {
49 foreach ($frm->addselect
as $userid) {
50 if (! $userid = clean_param($userid, PARAM_INT
)) {
53 if (!groups_add_member($groupid, $userid)) {
54 print_error('erroraddremoveuser', 'group', $returnurl);
58 } else if (isset($frm->remove
) and !empty($frm->removeselect
)) {
60 foreach ($frm->removeselect
as $userid) {
61 if (! $userid = clean_param($userid, PARAM_INT
)) {
64 if (!groups_remove_member($groupid, $userid)) {
65 print_error('erroraddremoveuser', 'group', $returnurl);
69 $eventdata = new object();
70 $eventdata -> groupid
= $groupid;
71 $eventdata -> userid
= $userid;
72 events_trigger('group_user_removed', $eventdata);
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 ++
;
87 $groupmembersoptions .= '<option> </option>';
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);
98 $potentialmemberscount = 0;
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);
107 natcasesort($nonmembers);
109 // Print out the HTML
110 foreach($nonmembers as $id => $name) {
111 $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
114 $potentialmembersoptions .= '<option> </option>';
118 // Print the page and form
119 $strgroups = get_string('groups');
120 $strparticipants = get_string('participants');
122 $groupname = format_string($group->name
);
124 print_header("$course->shortname: $strgroups",
126 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
127 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
128 "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
129 '-> '. get_string('adduserstogroup', 'group'), '', '', true, '', user_login_string($course, $USER));
132 <div id
="addmembersform">
133 <h3
class="main"><?php
print_string('adduserstogroup', 'group'); echo ": $groupname"; ?
></h3
>
135 <form id
="assignform" method
="post" action
="members.php">
137 <input type
="hidden" name
="sesskey" value
="<?php p(sesskey()); ?>" />
138 <input type
="hidden" name
="group" value
="<?php echo $groupid; ?>" />
140 <table summary
="" cellpadding
="5" cellspacing
="0">
143 <label
for="removeselect"><?php
print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
145 <select name
="removeselect[]" size
="20" id
="removeselect" multiple
="multiple"
146 onfocus
="document.getElementById('assignform').add.disabled=true;
147 document.getElementById('assignform').remove.disabled=false;
148 document.getElementById('assignform').addselect.selectedIndex=-1;">
149 <?php
echo $groupmembersoptions ?
>
152 <?php
// Hidden assignment? ?>
154 <?php
check_theme_arrows(); ?
>
155 <p
class="arrow_button">
156 <input name
="add" id
="add" type
="submit" value
="<?php echo ' '.$THEME->larrow.' '.get_string('add'); ?>" title
="<?php print_string('add'); ?>" />
158 <input name
="remove" id
="remove" type
="submit" value
="<?php echo ' '.$THEME->rarrow.' '.get_string('remove'); ?>" title
="<?php print_string('remove'); ?>" />
162 <label
for="addselect"><?php
print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
164 <select name
="addselect[]" size
="20" id
="addselect" multiple
="multiple"
165 onfocus
="document.getElementById('assignform').add.disabled=false;
166 document.getElementById('assignform').remove.disabled=true;
167 document.getElementById('assignform').removeselect.selectedIndex=-1;">
169 if ($potentialmemberscount > MAX_USERS_PER_PAGE
) {
170 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
171 .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
173 echo $potentialmembersoptions;
178 <label
for="searchtext" class="accesshide"><?php
p($strsearch) ?
></label
>
179 <input type
="text" name
="searchtext" id
="searchtext" size
="30" value
="<?php p($searchtext, true) ?>"
180 onfocus
="getElementById('assignform').add.disabled=true;
181 getElementById('assignform').remove.disabled=true;
182 getElementById('assignform').removeselect.selectedIndex=-1;
183 getElementById('assignform').addselect.selectedIndex=-1;"
184 onkeydown
= "var keyCode = event.which ? event.which : event.keyCode;
186 getElementById('assignform').previoussearch.value=1;
187 getElementById('assignform').submit();
189 <input name
="search" id
="search" type
="submit" value
="<?php p($strsearch) ?>" />
191 if (!empty($searchtext)) {
192 echo '<input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
198 <input type
="submit" name
="cancel" value
="<?php print_string('backtogroups', 'group'); ?>" />
206 print_footer($course);