Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / group / assign.php
blobfe3ec490352e52adb006d8bb52e5c46c381e9143
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 // Invalidate the user_group_groupings cache, too.
63 cache_helper::purge_by_definition('core', 'user_group_groupings');
64 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
65 foreach ($frm->removeselect as $groupid) {
66 // Ask this method not to purge the cache, we'll do it ourselves afterwards.
67 groups_unassign_grouping($grouping->id, (int)$groupid, false);
69 // Invalidate the course groups cache seeing as we've changed it.
70 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
72 // Invalidate the user_group_groupings cache, too.
73 cache_helper::purge_by_definition('core', 'user_group_groupings');
78 $currentmembers = array();
79 $potentialmembers = array();
81 if ($groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name')) {
82 if ($assignment = $DB->get_records('groupings_groups', array('groupingid'=>$grouping->id))) {
83 foreach ($assignment as $ass) {
84 $currentmembers[$ass->groupid] = $groups[$ass->groupid];
85 unset($groups[$ass->groupid]);
88 $potentialmembers = $groups;
91 $currentmembersoptions = '';
92 $currentmemberscount = 0;
93 if ($currentmembers) {
94 foreach($currentmembers as $group) {
95 $currentmembersoptions .= '<option value="' . $group->id . '." title="' . format_string($group->name) . '">' .
96 format_string($group->name) . '</option>';
97 $currentmemberscount ++;
100 // Get course managers so they can be highlighted in the list
101 if ($managerroles = get_config('', 'coursecontact')) {
102 $coursecontactroles = explode(',', $managerroles);
103 foreach ($coursecontactroles as $roleid) {
104 $role = $DB->get_record('role', array('id'=>$roleid));
105 $managers = get_role_users($roleid, $context, true, 'u.id', 'u.id ASC');
108 } else {
109 $currentmembersoptions .= '<option>&nbsp;</option>';
112 $potentialmembersoptions = '';
113 $potentialmemberscount = 0;
114 if ($potentialmembers) {
115 foreach($potentialmembers as $group) {
116 $potentialmembersoptions .= '<option value="' . $group->id . '." title="' . format_string($group->name) . '">' .
117 format_string($group->name) . '</option>';
118 $potentialmemberscount ++;
120 } else {
121 $potentialmembersoptions .= '<option>&nbsp;</option>';
124 // Print the page and form
125 $strgroups = get_string('groups');
126 $strparticipants = get_string('participants');
127 $straddgroupstogroupings = get_string('addgroupstogroupings', 'group');
129 $groupingname = format_string($grouping->name);
131 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$course->id)));
132 $PAGE->set_pagelayout('admin');
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)));
136 $PAGE->navbar->add($straddgroupstogroupings);
138 /// Print header
139 $PAGE->set_title("$course->shortname: $strgroups");
140 $PAGE->set_heading($course->fullname);
141 echo $OUTPUT->header();
144 <div id="addmembersform">
145 <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
146 <form id="assignform" method="post" action="">
147 <div>
148 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
149 <table summary="" class="generaltable generalbox groupmanagementtable boxaligncenter">
150 <tr>
151 <td id="existingcell">
152 <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); ?></label>
153 <div class="userselector" id="removeselect_wrapper">
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 $currentmembersoptions ?>
159 </select></div></td>
160 <td id="buttonscell">
161 <p class="arrow_button">
162 <input class="btn btn-secondary" name="add" id="add" type="submit"
163 value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
164 title="<?php print_string('add'); ?>" /><br>
165 <input class="btn btn-secondary" name="remove" id="remove" type="submit"
166 value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
167 title="<?php print_string('remove'); ?>" />
168 </p>
169 </td>
170 <td id="potentialcell">
171 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); ?></label>
172 <div class="userselector" id="addselect_wrapper">
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 echo $potentialmembersoptions ?>
178 </select>
179 </div>
180 </td>
181 </tr>
182 <tr><td colspan="3" id="backcell">
183 <input class="btn btn-secondary" type="submit" name="cancel"
184 value="<?php print_string('backtogroupings', 'group'); ?>" />
185 </td></tr>
186 </table>
187 </div>
188 </form>
189 </div>
191 <?php
192 echo $OUTPUT->footer();