MDL-21781 fixed incorrect options in user selectors
[moodle.git] / cohort / assign.php
CommitLineData
b980c56e
PS
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
18/**
19 * Cohort related management functions, this file needs to be included manually.
20 *
21 * @package moodlecore
22 * @subpackage cohort
23 * @copyright 2010 Petr Skoda (info@skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
b980c56e
PS
27require_once('../config.php');
28require_once($CFG->dirroot.'/cohort/lib.php');
29
30$id = required_param('id', PARAM_INT);
31
32require_login();
33
34$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
35$context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
36
c7d5ed1c 37require_capability('moodle/cohort:assign', $context);
b980c56e
PS
38
39$PAGE->set_url('/cohort/assign.php', array('id'=>$id));
40$PAGE->set_Context($context);
41
b980c56e
PS
42if ($context->contextlevel == CONTEXT_COURSECAT) {
43 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
44 $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
45}
46$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
47$PAGE->navbar->add(get_string('assign', 'cohort'));
48
49echo $OUTPUT->header();
50echo $OUTPUT->heading(get_string('assignto', 'cohort', format_string($cohort->name)));
51
52/// Get the user_selector we will need.
53$potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id));
54$existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id));
55
56/// Process incoming user assignments to cohorts
57 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
58 $userstoassign = $potentialuserselector->get_selected_users();
59 if (!empty($userstoassign)) {
60
61 foreach ($userstoassign as $adduser) {
62 // no duplicates please
63 if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
64 cohort_add_member($cohort->id, $adduser->id);
65 }
66 }
67
68 $potentialuserselector->invalidate_selected_users();
69 $existinguserselector->invalidate_selected_users();
70 }
71 }
72
73/// Process removing user assignments to the service
74 if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
75 $userstoremove = $existinguserselector->get_selected_users();
76 if (!empty($userstoremove)) {
77
78 foreach ($userstoremove as $removeuser) {
79 cohort_remove_member($cohort->id, $removeuser->id);
80 }
81
82 $potentialuserselector->invalidate_selected_users();
83 $existinguserselector->invalidate_selected_users();
84 }
85 }
86/// Print the form.
87/// display the UI
88
89?>
90<form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
91 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
92
93 <table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
94 <tr>
95 <td id="existingcell">
96 <p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p>
97 <?php $existinguserselector->display() ?>
98 </td>
99 <td id="buttonscell">
100 <div id="addcontrols">
101 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
102 </div>
103
104 <div id="removecontrols">
105 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
106 </div>
107 </td>
108 <td id="potentialcell">
109 <p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p>
110 <?php $potentialuserselector->display() ?>
111 </td>
112 </tr>
113 </table>
114</div></form>
115
116<?php
117
118
119echo $OUTPUT->footer();