weekly release 3.10.1+
[moodle.git] / group / assign.php
CommitLineData
bbe0bd98 1<?php
2
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.
14//
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/>.
17
f3f7610c 18/**
62d63838 19 * Add/remove group from grouping.
bbe0bd98 20 *
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
4d8e2417
AG
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package core_group
f3f7610c 24 */
bbe0bd98 25
f3f7610c
ML
26require_once('../config.php');
27require_once('lib.php');
f3f7610c 28
62d63838 29$groupingid = required_param('id', PARAM_INT);
23431567 30
a6855934 31$PAGE->set_url('/group/assign.php', array('id'=>$groupingid));
bbe0bd98 32
dfdaabd6 33if (!$grouping = $DB->get_record('groupings', array('id'=>$groupingid))) {
b121a4ee 34 print_error('invalidgroupid');
2524b0f2 35}
f3f7610c 36
dfdaabd6 37if (!$course = $DB->get_record('course', array('id'=>$grouping->courseid))) {
b39552af 38 print_error('invalidcourse');
39}
2524b0f2 40$courseid = $course->id;
f3f7610c 41
62d63838 42require_login($course);
bf0f06b1 43$context = context_course::instance($courseid);
2524b0f2 44require_capability('moodle/course:managegroups', $context);
45
62d63838 46$returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$courseid;
f3f7610c 47
2524b0f2 48
294ce987 49if ($frm = data_submitted() and confirm_sesskey()) {
f3f7610c 50
62d63838 51 if (isset($frm->cancel)) {
52 redirect($returnurl);
2524b0f2 53
62d63838 54 } else if (isset($frm->add) and !empty($frm->addselect)) {
55 foreach ($frm->addselect as $groupid) {
e17dbeeb
SH
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);
f3f7610c 58 }
e17dbeeb
SH
59 // Invalidate the course groups cache seeing as we've changed it.
60 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
2524b0f2 61
fd6d98cc
JD
62 // Invalidate the user_group_groupings cache, too.
63 cache_helper::purge_by_definition('core', 'user_group_groupings');
62d63838 64 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
62d63838 65 foreach ($frm->removeselect as $groupid) {
e17dbeeb
SH
66 // Ask this method not to purge the cache, we'll do it ourselves afterwards.
67 groups_unassign_grouping($grouping->id, (int)$groupid, false);
27c69abe 68 }
e17dbeeb
SH
69 // Invalidate the course groups cache seeing as we've changed it.
70 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
fd6d98cc
JD
71
72 // Invalidate the user_group_groupings cache, too.
73 cache_helper::purge_by_definition('core', 'user_group_groupings');
27c69abe 74 }
62d63838 75}
2524b0f2 76
2524b0f2 77
62d63838 78$currentmembers = array();
79$potentialmembers = array();
80
6edfb4c8 81if ($groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name')) {
dfdaabd6 82 if ($assignment = $DB->get_records('groupings_groups', array('groupingid'=>$grouping->id))) {
62d63838 83 foreach ($assignment as $ass) {
84 $currentmembers[$ass->groupid] = $groups[$ass->groupid];
85 unset($groups[$ass->groupid]);
86 }
23431567 87 }
62d63838 88 $potentialmembers = $groups;
89}
27c69abe 90
62d63838 91$currentmembersoptions = '';
92$currentmemberscount = 0;
93if ($currentmembers) {
94 foreach($currentmembers as $group) {
83f9f83a
HN
95 $currentmembersoptions .= '<option value="' . $group->id . '." title="' . format_string($group->name) . '">' .
96 format_string($group->name) . '</option>';
62d63838 97 $currentmemberscount ++;
98 }
47b18c1c 99
aa2f6604 100 // Get course managers so they can be highlighted in the list
df997f84 101 if ($managerroles = get_config('', 'coursecontact')) {
c71f3265 102 $coursecontactroles = explode(',', $managerroles);
df997f84 103 foreach ($coursecontactroles as $roleid) {
dfdaabd6 104 $role = $DB->get_record('role', array('id'=>$roleid));
4f0c2d00 105 $managers = get_role_users($roleid, $context, true, 'u.id', 'u.id ASC');
47b18c1c 106 }
107 }
62d63838 108} else {
109 $currentmembersoptions .= '<option>&nbsp;</option>';
110}
23431567 111
62d63838 112$potentialmembersoptions = '';
113$potentialmemberscount = 0;
114if ($potentialmembers) {
115 foreach($potentialmembers as $group) {
83f9f83a
HN
116 $potentialmembersoptions .= '<option value="' . $group->id . '." title="' . format_string($group->name) . '">' .
117 format_string($group->name) . '</option>';
62d63838 118 $potentialmemberscount ++;
27c69abe 119 }
62d63838 120} else {
121 $potentialmembersoptions .= '<option>&nbsp;</option>';
122}
f3f7610c 123
62d63838 124// Print the page and form
125$strgroups = get_string('groups');
126$strparticipants = get_string('participants');
0be6f678 127$straddgroupstogroupings = get_string('addgroupstogroupings', 'group');
f3f7610c 128
62d63838 129$groupingname = format_string($grouping->name);
f3f7610c 130
7c725a24 131navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$course->id)));
566889aa 132$PAGE->set_pagelayout('admin');
7c725a24 133
a6855934
PS
134$PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
135$PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
b87573d7 136$PAGE->navbar->add($straddgroupstogroupings);
137
138/// Print header
139$PAGE->set_title("$course->shortname: $strgroups");
140$PAGE->set_heading($course->fullname);
b87573d7 141echo $OUTPUT->header();
f3f7610c 142
f3f7610c
ML
143?>
144<div id="addmembersform">
62d63838 145 <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
b4e80a8b 146 <form id="assignform" method="post" action="">
147 <div>
f3f7610c 148 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
f181e4ac 149 <table summary="" class="generaltable generalbox groupmanagementtable boxaligncenter">
f3f7610c 150 <tr>
f181e4ac 151 <td id="existingcell">
dfdaabd6 152 <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); ?></label>
f181e4ac 153 <div class="userselector" id="removeselect_wrapper">
f3f7610c 154 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
b4e80a8b 155 onfocus="document.getElementById('assignform').add.disabled=true;
156 document.getElementById('assignform').remove.disabled=false;
157 document.getElementById('assignform').addselect.selectedIndex=-1;">
62d63838 158 <?php echo $currentmembersoptions ?>
f181e4ac
DB
159 </select></div></td>
160 <td id="buttonscell">
f3f7610c 161 <p class="arrow_button">
09028f32 162 <input class="btn btn-secondary" name="add" id="add" type="submit"
fad1a02d 163 value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
f181e4ac 164 title="<?php print_string('add'); ?>" /><br>
09028f32 165 <input class="btn btn-secondary" name="remove" id="remove" type="submit"
fad1a02d 166 value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
f181e4ac 167 title="<?php print_string('remove'); ?>" />
f3f7610c
ML
168 </p>
169 </td>
f181e4ac 170 <td id="potentialcell">
dfdaabd6 171 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); ?></label>
f181e4ac 172 <div class="userselector" id="addselect_wrapper">
f3f7610c 173 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
b4e80a8b 174 onfocus="document.getElementById('assignform').add.disabled=false;
175 document.getElementById('assignform').remove.disabled=true;
176 document.getElementById('assignform').removeselect.selectedIndex=-1;">
62d63838 177 <?php echo $potentialmembersoptions ?>
f3f7610c 178 </select>
f181e4ac 179 </div>
f3f7610c
ML
180 </td>
181 </tr>
f181e4ac 182 <tr><td colspan="3" id="backcell">
09028f32
AA
183 <input class="btn btn-secondary" type="submit" name="cancel"
184 value="<?php print_string('backtogroupings', 'group'); ?>" />
f3f7610c
ML
185 </td></tr>
186 </table>
b4e80a8b 187 </div>
f3f7610c
ML
188 </form>
189</div>
190
191<?php
653468d4 192 echo $OUTPUT->footer();