18d607a58fd0d3ed97af94840edbbee98c0e0121
[moodle.git] / group / members.php
1 <?php // $Id$
2 /**
3  * Add/remove members from group.
4  *
5  * @copyright &copy; 2006 The Open University and others
6  * @author N.D.Freear AT open.ac.uk
7  * @author J.White AT open.ac.uk and others
8  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9  * @package groups
10  */
11 require_once(dirname(__FILE__) . '/../config.php');
12 require_once(dirname(__FILE__) . '/lib.php');
13 require_once($CFG->dirroot . '/user/selector/lib.php');
14 require_once($CFG->dirroot . '/course/lib.php');
16 $groupid = required_param('group', PARAM_INT);
18 if (!$group = $DB->get_record('groups', array('id'=>$groupid))) {
19     print_error('invalidgroupid');
20 }
22 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
23     print_error('invalidcourse');
24 }
25 $courseid = $course->id;
27 require_login($course);
28 $context = get_context_instance(CONTEXT_COURSE, $courseid);
29 require_capability('moodle/course:managegroups', $context);
31 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$group->id;
33 if (optional_param('cancel', false, PARAM_BOOL)) {
34     redirect($returnurl);
35 }
37 $groupmembersselector = new group_members_selector('removeselect',
38         array('groupid' => $groupid, 'courseid' => $course->id));
39 $groupmembersselector->set_extra_fields(array());
40 $potentialmembersselector = new group_non_members_selector('addselect',
41         array('groupid' => $groupid, 'courseid' => $course->id));
42 $potentialmembersselector->set_extra_fields(array());
43         
44 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
45     $userstoadd = $potentialmembersselector->get_selected_users();
46     if (!empty($userstoadd)) {
47         foreach ($userstoadd as $user) {
48             if (!groups_add_member($groupid, $user->id)) {
49                 print_error('erroraddremoveuser', 'group', $returnurl);
50             }
51             $groupmembersselector->invalidate_selected_users();
52             $potentialmembersselector->invalidate_selected_users();
53         }
54     }
55 }
57 if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
58     $userstoremove = $groupmembersselector->get_selected_users();
59     if (!empty($userstoremove)) {
60         foreach ($userstoremove as $user) {
61             if (!groups_remove_member($groupid, $user->id)) {
62                 print_error('erroraddremoveuser', 'group', $returnurl);
63             }
64             $groupmembersselector->invalidate_selected_users();
65             $potentialmembersselector->invalidate_selected_users();
66         }
67     }
68 }
70 // Print the page and form
71 $strgroups = get_string('groups');
72 $strparticipants = get_string('participants');
73 $stradduserstogroup = get_string('adduserstogroup', 'group');
74 $strusergroupmembership = get_string('usergroupmembership', 'group');
76 $groupname = format_string($group->name);
78 $navlinks = array();
79 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
80 $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
81 $navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
82 $navigation = build_navigation($navlinks);
84 $PAGE->requires->js('group/clientlib.js');
85 $PAGE->requires->js_function_call('init_add_remove_members_page');
86 print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
87 check_theme_arrows();
88 ?>
90 <div id="addmembersform">
91     <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo ": $groupname"; ?></h3>
93     <form id="assignform" method="post" action="<?php echo $CFG->wwwroot; ?>/group/members.php?group=<?php echo $groupid; ?>">
94     <div>
95     <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
97     <table class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
98     <tr>
99       <td id='existingcell'>
100           <p>
101             <label for="removeselect"><?php print_string('groupmembers', 'group'); ?></label>
102           </p>
103           <?php $groupmembersselector->display(); ?>
104           </td>
105       <td id='buttonscell'>
106         <p class="arrow_button">
107             <input name="add" id="add" type="submit" value="<?php echo $THEME->larrow.'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
108             <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$THEME->rarrow; ?>" title="<?php print_string('remove'); ?>" />
109         </p>
110       </td>
111       <td id='potentialcell'>
112           <p>
113             <label for="addselect"><?php print_string('potentialmembs', 'group'); ?></label>
114           </p>
115           <?php $potentialmembersselector->display(); ?>
116       </td>
117     </tr>
118     <tr><td colspan="3" id='backcell'>
119         <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
120     </td></tr>
121     </table>
122     </div>
123     </form>
124 </div>
126 <?php
127     echo $OUTPUT->footer();
128 ?>