Merge branch 'MDL-53674-30-eventmsg' of git://github.com/mudrd8mz/moodle into MOODLE_...
[moodle.git] / group / assign.php
blob0218022b69673e0b7398e252a0d252ffd514350a
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Add/remove group from grouping.
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package core_group
26 require_once('../config.php');
27 require_once('lib.php');
29 $groupingid = required_param('id', PARAM_INT);
31 $PAGE->set_url('/group/assign.php', array('id'=>$groupingid));
33 if (!$grouping = $DB->get_record('groupings', array('id'=>$groupingid))) {
34 print_error('invalidgroupid');
37 if (!$course = $DB->get_record('course', array('id'=>$grouping->courseid))) {
38 print_error('invalidcourse');
40 $courseid = $course->id;
42 require_login($course);
43 $context = context_course::instance($courseid);
44 require_capability('moodle/course:managegroups', $context);
46 $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$courseid;
49 if ($frm = data_submitted() and confirm_sesskey()) {
51 if (isset($frm->cancel)) {
52 redirect($returnurl);
54 } else if (isset($frm->add) and !empty($frm->addselect)) {
55 foreach ($frm->addselect as $groupid) {
56 // Ask this method not to purge the cache, we'll do it ourselves afterwards.
57 groups_assign_grouping($grouping->id, (int)$groupid, null, false);
59 // Invalidate the course groups cache seeing as we've changed it.
60 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
62 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
63 foreach ($frm->removeselect as $groupid) {
64 // Ask this method not to purge the cache, we'll do it ourselves afterwards.
65 groups_unassign_grouping($grouping->id, (int)$groupid, false);
67 // Invalidate the course groups cache seeing as we've changed it.
68 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
73 $currentmembers = array();
74 $potentialmembers = array();
76 if ($groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name')) {
77 if ($assignment = $DB->get_records('groupings_groups', array('groupingid'=>$grouping->id))) {
78 foreach ($assignment as $ass) {
79 $currentmembers[$ass->groupid] = $groups[$ass->groupid];
80 unset($groups[$ass->groupid]);
83 $potentialmembers = $groups;
86 $currentmembersoptions = '';
87 $currentmemberscount = 0;
88 if ($currentmembers) {
89 foreach($currentmembers as $group) {
90 $currentmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
91 $currentmemberscount ++;
94 // Get course managers so they can be highlighted in the list
95 if ($managerroles = get_config('', 'coursecontact')) {
96 $coursecontactroles = explode(',', $managerroles);
97 foreach ($coursecontactroles as $roleid) {
98 $role = $DB->get_record('role', array('id'=>$roleid));
99 $managers = get_role_users($roleid, $context, true, 'u.id', 'u.id ASC');
102 } else {
103 $currentmembersoptions .= '<option>&nbsp;</option>';
106 $potentialmembersoptions = '';
107 $potentialmemberscount = 0;
108 if ($potentialmembers) {
109 foreach($potentialmembers as $group) {
110 $potentialmembersoptions .= '<option value="'.$group->id.'.">'.format_string($group->name).'</option>';
111 $potentialmemberscount ++;
113 } else {
114 $potentialmembersoptions .= '<option>&nbsp;</option>';
117 // Print the page and form
118 $strgroups = get_string('groups');
119 $strparticipants = get_string('participants');
120 $straddgroupstogroupings = get_string('addgroupstogroupings', 'group');
122 $groupingname = format_string($grouping->name);
124 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$course->id)));
125 $PAGE->set_pagelayout('admin');
127 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
128 $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
129 $PAGE->navbar->add($straddgroupstogroupings);
131 /// Print header
132 $PAGE->set_title("$course->shortname: $strgroups");
133 $PAGE->set_heading($course->fullname);
134 echo $OUTPUT->header();
137 <div id="addmembersform">
138 <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
139 <form id="assignform" method="post" action="">
140 <div>
141 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
142 <table summary="" class="generaltable generalbox groupmanagementtable boxaligncenter">
143 <tr>
144 <td id="existingcell">
145 <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); ?></label>
146 <div class="userselector" id="removeselect_wrapper">
147 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
148 onfocus="document.getElementById('assignform').add.disabled=true;
149 document.getElementById('assignform').remove.disabled=false;
150 document.getElementById('assignform').addselect.selectedIndex=-1;">
151 <?php echo $currentmembersoptions ?>
152 </select></div></td>
153 <td id="buttonscell">
154 <p class="arrow_button">
155 <input name="add" id="add" type="submit"
156 value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
157 title="<?php print_string('add'); ?>" /><br>
158 <input name="remove" id="remove" type="submit"
159 value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
160 title="<?php print_string('remove'); ?>" />
161 </p>
162 </td>
163 <td id="potentialcell">
164 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); ?></label>
165 <div class="userselector" id="addselect_wrapper">
166 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
167 onfocus="document.getElementById('assignform').add.disabled=false;
168 document.getElementById('assignform').remove.disabled=true;
169 document.getElementById('assignform').removeselect.selectedIndex=-1;">
170 <?php echo $potentialmembersoptions ?>
171 </select>
172 </div>
173 </td>
174 </tr>
175 <tr><td colspan="3" id="backcell">
176 <input type="submit" name="cancel" value="<?php print_string('backtogroupings', 'group'); ?>" />
177 </td></tr>
178 </table>
179 </div>
180 </form>
181 </div>
183 <?php
184 echo $OUTPUT->footer();