Merge branch 'MDL-39054' of git://github.com/jacks92/moodle
[moodle.git] / cohort / assign.php
CommitLineData
b980c56e 1<?php
b980c56e
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Cohort related management functions, this file needs to be included manually.
19 *
6eb8bf7f 20 * @package core_cohort
707f4136 21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
6eb8bf7f 25require('../config.php');
607fdf63 26require_once($CFG->dirroot.'/cohort/locallib.php');
b980c56e
PS
27
28$id = required_param('id', PARAM_INT);
29
30require_login();
31
32$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
d197ea43 33$context = context::instance_by_id($cohort->contextid, MUST_EXIST);
b980c56e 34
c7d5ed1c 35require_capability('moodle/cohort:assign', $context);
b980c56e 36
62bc5019 37$PAGE->set_context($context);
f6eb428e 38$PAGE->set_url('/cohort/assign.php', array('id'=>$id));
b980c56e 39
4a0ef03e
PS
40$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid));
41
42if (!empty($cohort->component)) {
6eb8bf7f 43 // We can not manually edit cohorts that were created by external systems, sorry.
4a0ef03e
PS
44 redirect($returnurl);
45}
46
c5070d06 47if (optional_param('cancel', false, PARAM_BOOL)) {
4a0ef03e 48 redirect($returnurl);
c5070d06
PS
49}
50
b980c56e
PS
51if ($context->contextlevel == CONTEXT_COURSECAT) {
52 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
21c8d60d
PS
53 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
54 $PAGE->set_pagelayout('report');
55
56} else {
57 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
58 $PAGE->set_pagelayout('admin');
b980c56e 59}
b980c56e 60$PAGE->navbar->add(get_string('assign', 'cohort'));
21c8d60d 61
bd08a24a
RW
62$PAGE->set_title(get_string('cohort:assign', 'cohort'));
63$PAGE->set_heading($COURSE->fullname);
b980c56e
PS
64
65echo $OUTPUT->header();
66echo $OUTPUT->heading(get_string('assignto', 'cohort', format_string($cohort->name)));
67
5121a95d
PS
68echo $OUTPUT->notification(get_string('removeuserwarning', 'core_cohort'));
69
c5070d06 70// Get the user_selector we will need.
d86b7de7
PS
71$potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id, 'accesscontext'=>$context));
72$existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id, 'accesscontext'=>$context));
b980c56e 73
c5070d06 74// Process incoming user assignments to the cohort
b980c56e 75
c5070d06
PS
76if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
77 $userstoassign = $potentialuserselector->get_selected_users();
78 if (!empty($userstoassign)) {
b980c56e 79
c5070d06 80 foreach ($userstoassign as $adduser) {
2f8c69e1 81 cohort_add_member($cohort->id, $adduser->id);
b980c56e
PS
82 }
83
c5070d06
PS
84 $potentialuserselector->invalidate_selected_users();
85 $existinguserselector->invalidate_selected_users();
86 }
87}
b980c56e 88
c5070d06
PS
89// Process removing user assignments to the cohort
90if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
91 $userstoremove = $existinguserselector->get_selected_users();
92 if (!empty($userstoremove)) {
93 foreach ($userstoremove as $removeuser) {
94 cohort_remove_member($cohort->id, $removeuser->id);
b980c56e 95 }
c5070d06
PS
96 $potentialuserselector->invalidate_selected_users();
97 $existinguserselector->invalidate_selected_users();
98 }
99}
b980c56e 100
c5070d06 101// Print the form.
b980c56e
PS
102?>
103<form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
104 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
105
106 <table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
107 <tr>
108 <td id="existingcell">
109 <p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p>
110 <?php $existinguserselector->display() ?>
111 </td>
112 <td id="buttonscell">
113 <div id="addcontrols">
c5070d06 114 <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
115 </div>
116
117 <div id="removecontrols">
c5070d06 118 <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
119 </div>
120 </td>
121 <td id="potentialcell">
122 <p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p>
123 <?php $potentialuserselector->display() ?>
124 </td>
125 </tr>
c5070d06
PS
126 <tr><td colspan="3" id='backcell'>
127 <input type="submit" name="cancel" value="<?php p(get_string('backtocohorts', 'cohort')); ?>" />
128 </td></tr>
b980c56e
PS
129 </table>
130</div></form>
131
132<?php
133
b980c56e 134echo $OUTPUT->footer();