calendar/lib: calendar_set_filters() use pre-fetched context and course recs
[moodle-pu.git] / group / assign.php
blob9cfff8dacd6986d045f061e19312de4b3eb76400
1 <?php
2 /**
3 * Add/remove group from grouping.
4 * @package groups
5 */
6 require_once('../config.php');
7 require_once('lib.php');
9 $groupingid = required_param('id', PARAM_INT);
11 if (!$grouping = get_record('groupings', 'id', $groupingid)) {
12 error('Incorrect group id');
15 if (! $course = get_record('course', 'id', $grouping->courseid)) {
16 print_error('invalidcourse');
18 $courseid = $course->id;
20 require_login($course);
21 $context = get_context_instance(CONTEXT_COURSE, $courseid);
22 require_capability('moodle/course:managegroups', $context);
24 $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$courseid;
27 if ($frm = data_submitted() and confirm_sesskey()) {
29 if (isset($frm->cancel)) {
30 redirect($returnurl);
32 } else if (isset($frm->add) and !empty($frm->addselect)) {
33 foreach ($frm->addselect as $groupid) {
34 $groupid = (int)$groupid;
35 if (record_exists('groupings_groups', 'groupingid', $grouping->id, 'groupid', $groupid)) {
36 continue;
38 $assign = new object();
39 $assign->groupingid = $grouping->id;
40 $assign->groupid = $groupid;
41 $assign->timeadded = time();
42 insert_record('groupings_groups', $assign);
45 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
47 foreach ($frm->removeselect as $groupid) {
48 $groupid = (int)$groupid;
49 delete_records('groupings_groups', 'groupingid', $grouping->id, 'groupid', $groupid);
55 $currentmembers = array();
56 $potentialmembers = array();
58 if ($groups = get_records('groups', 'courseid', $courseid, 'name')) {
59 if ($assignment = get_records('groupings_groups', 'groupingid', $grouping->id)) {
60 foreach ($assignment as $ass) {
61 $currentmembers[$ass->groupid] = $groups[$ass->groupid];
62 unset($groups[$ass->groupid]);
65 $potentialmembers = $groups;
68 $currentmembersoptions = '';
69 $currentmemberscount = 0;
70 if ($currentmembers) {
71 foreach($currentmembers as $group) {
72 $currentmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
73 $currentmemberscount ++;
76 // Get course managers so they can be hilited in the list
77 if ($managerroles = get_config('', 'coursemanager')) {
78 $coursemanagerroles = split(',', $managerroles);
79 foreach ($coursemanagerroles as $roleid) {
80 $role = get_record('role','id',$roleid);
81 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
82 $managers = get_role_users($roleid, $context, true, 'u.id', 'u.id ASC', $canseehidden);
86 if ($potentialmembers != false) {
87 // Put the groupings into a hash and sorts them
88 foreach ($potentialmembers as $user) {
89 if(!empty($managers[$user->id])) {
90 $nonmembers[$user->id] = '#'.$user->firstname.' '.$user->lastname;
92 else {
93 $nonmembers[$user->id] = $user->firstname.' '.$user->lastname;
95 $potentialmemberscount++;
97 natcasesort($nonmembers);
98 } else {
99 $currentmembersoptions .= '<option>&nbsp;</option>';
102 $potentialmembersoptions = '';
103 $potentialmemberscount = 0;
104 if ($potentialmembers) {
105 foreach($potentialmembers as $group) {
106 $potentialmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
107 $potentialmemberscount ++;
109 } else {
110 $potentialmembersoptions .= '<option>&nbsp;</option>';
113 // Print the page and form
114 $strgroups = get_string('groups');
115 $strparticipants = get_string('participants');
116 $straddgroupstogroupings = get_string('addgroupstogroupings', 'group');
118 $groupingname = format_string($grouping->name);
120 $navlinks = array();
121 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
122 $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
123 $navlinks[] = array('name' => $straddgroupstogroupings, 'link' => null, 'type' => 'misc');
124 $navigation = build_navigation($navlinks);
126 print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
129 <div id="addmembersform">
130 <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
132 <form id="assignform" method="post" action="">
133 <div>
134 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
135 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
137 <table summary="" cellpadding="5" cellspacing="0">
138 <tr>
139 <td valign="top">
140 <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); //count($contextusers) ?></label>
141 <br />
142 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
143 onfocus="document.getElementById('assignform').add.disabled=true;
144 document.getElementById('assignform').remove.disabled=false;
145 document.getElementById('assignform').addselect.selectedIndex=-1;">
146 <?php echo $currentmembersoptions ?>
147 </select></td>
148 <td valign="top">
149 <?php // Hidden assignment? ?>
151 <?php check_theme_arrows(); ?>
152 <p class="arrow_button">
153 <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
154 <br />
155 <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
156 </p>
157 </td>
158 <td valign="top">
159 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
160 <br />
161 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
162 onfocus="document.getElementById('assignform').add.disabled=false;
163 document.getElementById('assignform').remove.disabled=true;
164 document.getElementById('assignform').removeselect.selectedIndex=-1;">
165 <?php echo $potentialmembersoptions ?>
166 </select>
167 <br />
168 </td>
169 </tr>
170 <tr><td>
171 <input type="submit" name="cancel" value="<?php print_string('backtogroupings', 'group'); ?>" />
172 </td></tr>
173 </table>
174 </div>
175 </form>
176 </div>
178 <?php
179 print_footer($course);