MDL-78632 core: Update upgrade versions
[moodle.git] / group / assign.php
blobd3c37c7cc098664d1eca9b4557e04ab244dff308
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 throw new \moodle_exception('invalidgroupid');
37 if (!$course = $DB->get_record('course', array('id'=>$grouping->courseid))) {
38 throw new \moodle_exception('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(get_string('groupings', 'group'),
136 new moodle_url('/group/groupings.php', ['id' => $courseid]));
137 $PAGE->navbar->add($straddgroupstogroupings);
139 /// Print header
140 $PAGE->set_title("$course->shortname: $strgroups");
141 $PAGE->set_heading($course->fullname);
142 echo $OUTPUT->header();
145 <div id="addmembersform">
146 <h3 class="main"><?php print_string('addgroupstogroupings', 'group'); echo ": $groupingname"; ?></h3>
147 <form id="assignform" method="post" action="">
148 <div>
149 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
150 <table summary="" class="generaltable generalbox groupmanagementtable boxaligncenter">
151 <tr>
152 <td id="existingcell">
153 <label for="removeselect"><?php print_string('existingmembers', 'group', $currentmemberscount); ?></label>
154 <div class="userselector" id="removeselect_wrapper">
155 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
156 onfocus="document.getElementById('assignform').add.disabled=true;
157 document.getElementById('assignform').remove.disabled=false;
158 document.getElementById('assignform').addselect.selectedIndex=-1;">
159 <?php echo $currentmembersoptions ?>
160 </select></div></td>
161 <td id="buttonscell">
162 <p class="arrow_button">
163 <input class="btn btn-secondary" name="add" id="add" type="submit"
164 value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
165 title="<?php print_string('add'); ?>" /><br>
166 <input class="btn btn-secondary" name="remove" id="remove" type="submit"
167 value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
168 title="<?php print_string('remove'); ?>" />
169 </p>
170 </td>
171 <td id="potentialcell">
172 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); ?></label>
173 <div class="userselector" id="addselect_wrapper">
174 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
175 onfocus="document.getElementById('assignform').add.disabled=false;
176 document.getElementById('assignform').remove.disabled=true;
177 document.getElementById('assignform').removeselect.selectedIndex=-1;">
178 <?php echo $potentialmembersoptions ?>
179 </select>
180 </div>
181 </td>
182 </tr>
183 <tr><td colspan="3" id="backcell">
184 <input class="btn btn-secondary" type="submit" name="cancel"
185 value="<?php print_string('backtogroupings', 'group'); ?>" />
186 </td></tr>
187 </table>
188 </div>
189 </form>
190 </div>
192 <?php
193 echo $OUTPUT->footer();