MDL-10383 - groups/groupings refactoring and improvements - basic UI for groupings...
[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');
122 $groupname = format_string($group->name);
124 print_header("$course->shortname: $strgroups",
125              $course->fullname,
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));
131 ?>
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">
136     <div>
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">
141     <tr>
142       <td valign="top">
143           <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
144           <br />
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 ?>
150           </select></td>
151       <td valign="top">
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 '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
157             <br />
158             <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
159         </p>
160       </td>
161       <td valign="top">
162           <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
163           <br />
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;">
168           <?php
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";
172             } else {
173                 echo $potentialmembersoptions;
174             }
175           ?>
176          </select>
177          <br />
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;
185                                if (keyCode == 13) {
186                                     getElementById('assignform').previoussearch.value=1;
187                                     getElementById('assignform').submit();
188                                } " />
189          <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
190          <?php
191               if (!empty($searchtext)) {
192                   echo '<input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
193               }
194          ?>
195        </td>
196     </tr>
197     <tr><td>
198         <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
199     </td></tr>
200     </table>
201     </div>
202     </form>
203 </div>
205 <?php
206     print_footer($course);
207 ?>