MDL-21781 fixed some more cohort assign issues
[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
c5070d06
PS
42if (optional_param('cancel', false, PARAM_BOOL)) {
43 redirect(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
44}
45
b980c56e
PS
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
c5070d06 56// Get the user_selector we will need.
b980c56e
PS
57$potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id));
58$existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id));
59
c5070d06 60// Process incoming user assignments to the cohort
b980c56e 61
c5070d06
PS
62if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
63 $userstoassign = $potentialuserselector->get_selected_users();
64 if (!empty($userstoassign)) {
b980c56e 65
c5070d06
PS
66 foreach ($userstoassign as $adduser) {
67 // no duplicates please
68 if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
69 cohort_add_member($cohort->id, $adduser->id);
b980c56e
PS
70 }
71 }
72
c5070d06
PS
73 $potentialuserselector->invalidate_selected_users();
74 $existinguserselector->invalidate_selected_users();
75 }
76}
b980c56e 77
c5070d06
PS
78// Process removing user assignments to the cohort
79if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
80 $userstoremove = $existinguserselector->get_selected_users();
81 if (!empty($userstoremove)) {
82 foreach ($userstoremove as $removeuser) {
83 cohort_remove_member($cohort->id, $removeuser->id);
b980c56e 84 }
c5070d06
PS
85 $potentialuserselector->invalidate_selected_users();
86 $existinguserselector->invalidate_selected_users();
87 }
88}
b980c56e 89
c5070d06 90// Print the form.
b980c56e
PS
91?>
92<form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
93 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
94
95 <table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
96 <tr>
97 <td id="existingcell">
98 <p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p>
99 <?php $existinguserselector->display() ?>
100 </td>
101 <td id="buttonscell">
102 <div id="addcontrols">
c5070d06 103 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.s(get_string('add')); ?>" title="<?php p(get_string('add')); ?>" /><br />
b980c56e
PS
104 </div>
105
106 <div id="removecontrols">
c5070d06 107 <input name="remove" id="remove" type="submit" value="<?php echo s(get_string('remove')).'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php p(get_string('remove')); ?>" />
b980c56e
PS
108 </div>
109 </td>
110 <td id="potentialcell">
111 <p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p>
112 <?php $potentialuserselector->display() ?>
113 </td>
114 </tr>
c5070d06
PS
115 <tr><td colspan="3" id='backcell'>
116 <input type="submit" name="cancel" value="<?php p(get_string('backtocohorts', 'cohort')); ?>" />
117 </td></tr>
b980c56e
PS
118 </table>
119</div></form>
120
121<?php
122
b980c56e 123echo $OUTPUT->footer();