0bd3d7628f1ae945eb451b971321f834e1849167
[moodle.git] / group / assign.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 }
25 require_login();
26 if (!$group = get_record('groups', 'id', $groupid)) {
27     error('Incorrect group id');
28 }
30 if (! $course = get_record('course', 'id', $group->courseid)) {
31     print_error('invalidcourse');
32 }
34 require_login($course);
35 $courseid = $course->id;
37 $strsearch = get_string('search');
38 $strshowall = get_string('showall');
39 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$groupid;
41 $context = get_context_instance(CONTEXT_COURSE, $courseid);
42 require_capability('moodle/course:managegroups', $context);
44     if ($frm = data_submitted() and confirm_sesskey()) {
46         if (isset($frm->cancel)) {
47             redirect($returnurl);
49         } else if (isset($frm->add) and !empty($frm->addselect)) {
51             foreach ($frm->addselect as $userid) {
52                 if (! $userid = clean_param($userid, PARAM_INT)) {
53                     continue;
54                 }
55                 if (!groups_add_member($groupid, $userid)) {
56                     print_error('erroraddremoveuser', 'group', $returnurl);
57                 }
58             }
60         } else if (isset($frm->remove) and !empty($frm->removeselect)) {
62             foreach ($frm->removeselect as $userid) {
63                 if (! $userid = clean_param($userid, PARAM_INT)) {
64                     continue;
65                 }
66                 if (!groups_remove_member($groupid, $userid)) {
67                     print_error('erroraddremoveuser', 'group', $returnurl);
68                 }
70                 // MDL-9983
71                 $eventdata = new object();
72                 $eventdata -> groupid = $groupid;
73                 $eventdata -> userid = $userid;
74                 events_trigger('group_user_removed', $eventdata);
75             }
76         }
77     }
79     $groupmembers = groups_get_members($groupid);
80     $groupmembersoptions = '';
81     $groupmemberscount = 0;
82     if ($groupmembers != false) {
83         // Put the groupings into a hash and sorts them
84         foreach ($groupmembers as $userid) {
85             $listmembers[$userid] = groups_get_user_displayname($userid, $courseid);
86             $groupmemberscount ++;
87         }
88         natcasesort($listmembers);
90         // Print out the HTML
91         foreach($listmembers as $id => $name) {
92             $groupmembersoptions .= "<option value=\"$id\">$name</option>\n";
93         }
94     } else {
95         $groupmembersoptions .= '<option>&nbsp;</option>';
96     }
98     $potentialmembers = array();
99     $potentialmembersoptions = '';
100     $potentialmemberscount = 0;
102     $potentialmembers = groups_get_users_not_in_group($courseid, $groupid, $searchtext);
103     if (!empty($potentialmembers)) {
104         $potentialmemberscount = count($potentialmembers);
105     } else {
106         $potentialmemberscount = 0;
107     }
108     if ($potentialmemberscount <=  MAX_USERS_PER_PAGE) {
110         if ($potentialmembers != false) {
111             // Put the groupings into a hash and sorts them
112             foreach ($potentialmembers as $userid => $user) {
113                 $nonmembers[$userid] = fullname($user);
114                 //$nonmembers[$userid] = groups_get_user_displayname($userid, $courseid);
115             }
116             natcasesort($nonmembers);
118             // Print out the HTML
119             foreach($nonmembers as $id => $name) {
120                 $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
121             }
122         } else {
123             $potentialmembersoptions .= '<option>&nbsp;</option>';
124         }
125     }
127     // Print the page and form
128     $strgroups = get_string('groups');
129     $strparticipants = get_string('participants');
131     $groupname = groups_get_group_displayname($groupid);
133     print_header("$course->shortname: $strgroups",
134                  $course->fullname,
135                  "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
136                  "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
137                  "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
138                  '-> '. get_string('adduserstogroup', 'group'), '', '', true, '', user_login_string($course, $USER));
140 ?>
141 <div id="addmembersform">
142     <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo " $groupname"; ?></h3>
144     <form id="assignform" method="post" action="">
145     <div>
146     <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
147     <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
149     <table summary="" cellpadding="5" cellspacing="0">
150     <tr>
151       <td valign="top">
152           <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
153           <br />
154           <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
155                   onfocus="document.getElementById('assignform').add.disabled=true;
156                            document.getElementById('assignform').remove.disabled=false;
157                            document.getElementById('assignform').addselect.selectedIndex=-1;">
158           <?php echo $groupmembersoptions ?>
159           </select></td>
160       <td valign="top">
161 <?php // Hidden assignment? ?>
163         <?php check_theme_arrows(); ?>
164         <p class="arrow_button">
165             <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
166             <br />
167             <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
168         </p>
169       </td>
170       <td valign="top">
171           <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
172           <br />
173           <select name="addselect[]" size="20" id="addselect" multiple="multiple"
174                   onfocus="document.getElementById('assignform').add.disabled=false;
175                            document.getElementById('assignform').remove.disabled=true;
176                            document.getElementById('assignform').removeselect.selectedIndex=-1;">
177           <?php
178             if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
179                 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
180                         .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
181             } else {                           
182                 echo $potentialmembersoptions;
183             }              
184           ?>
185          </select>
186          <br />
187          <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
188          <input type="text" name="searchtext" id="searchtext" size="30" value="<?php p($searchtext, true) ?>"
189                   onfocus ="getElementById('assignform').add.disabled=true;
190                             getElementById('assignform').remove.disabled=true;
191                             getElementById('assignform').removeselect.selectedIndex=-1;
192                             getElementById('assignform').addselect.selectedIndex=-1;"
193                   onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
194                                if (keyCode == 13) {
195                                     getElementById('assignform').previoussearch.value=1;
196                                     getElementById('assignform').submit();
197                                } " />
198          <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
199          <?php
200               if (!empty($searchtext)) {
201                   echo '<input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
202               }
203          ?>
204        </td>
205     </tr>
206     <tr><td>
207         <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
208     </td></tr>
209     </table>
210     </div>
211     </form>
212 </div>
214 <?php
215     print_footer($course);
216 ?>