MDL-21781 cohort id duplicates detection
[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
27
28
29require_once('../config.php');
30require_once($CFG->dirroot.'/cohort/lib.php');
31
32$id = required_param('id', PARAM_INT);
33
34require_login();
35
36$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
37$context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
38
c7d5ed1c 39require_capability('moodle/cohort:assign', $context);
b980c56e
PS
40
41$PAGE->set_url('/cohort/assign.php', array('id'=>$id));
42$PAGE->set_Context($context);
43
44// TODO: ohlala, the navbar is not doing what I would expect
45$PAGE->navbar->add(get_string('home'), new moodle_url('/'), navbar::TYPE_SYSTEM);
46if ($context->contextlevel == CONTEXT_COURSECAT) {
47 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
48 $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
49}
50$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
51$PAGE->navbar->add(get_string('assign', 'cohort'));
52
53echo $OUTPUT->header();
54echo $OUTPUT->heading(get_string('assignto', 'cohort', format_string($cohort->name)));
55
56/// Get the user_selector we will need.
57$potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id));
58$existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id));
59
60/// Process incoming user assignments to cohorts
61 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
62 $userstoassign = $potentialuserselector->get_selected_users();
63 if (!empty($userstoassign)) {
64
65 foreach ($userstoassign as $adduser) {
66 // no duplicates please
67 if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
68 cohort_add_member($cohort->id, $adduser->id);
69 }
70 }
71
72 $potentialuserselector->invalidate_selected_users();
73 $existinguserselector->invalidate_selected_users();
74 }
75 }
76
77/// Process removing user assignments to the service
78 if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
79 $userstoremove = $existinguserselector->get_selected_users();
80 if (!empty($userstoremove)) {
81
82 foreach ($userstoremove as $removeuser) {
83 cohort_remove_member($cohort->id, $removeuser->id);
84 }
85
86 $potentialuserselector->invalidate_selected_users();
87 $existinguserselector->invalidate_selected_users();
88 }
89 }
90/// Print the form.
91/// display the UI
92
93?>
94<form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
95 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
96
97 <table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
98 <tr>
99 <td id="existingcell">
100 <p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p>
101 <?php $existinguserselector->display() ?>
102 </td>
103 <td id="buttonscell">
104 <div id="addcontrols">
105 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
106 </div>
107
108 <div id="removecontrols">
109 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
110 </div>
111 </td>
112 <td id="potentialcell">
113 <p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p>
114 <?php $potentialuserselector->display() ?>
115 </td>
116 </tr>
117 </table>
118</div></form>
119
120<?php
121
122
123echo $OUTPUT->footer();