MDL-24321 switching to stdClass in /c*/
[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 *
707f4136 21 * @package core
b980c56e 22 * @subpackage cohort
707f4136 23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
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 38
62bc5019 39$PAGE->set_context($context);
f6eb428e 40$PAGE->set_url('/cohort/assign.php', array('id'=>$id));
b980c56e 41
4a0ef03e
PS
42$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid));
43
44if (!empty($cohort->component)) {
45 // we can not manually edit cohorts that were created by external systems, sorry
46 redirect($returnurl);
47}
48
c5070d06 49if (optional_param('cancel', false, PARAM_BOOL)) {
4a0ef03e 50 redirect($returnurl);
c5070d06
PS
51}
52
b980c56e
PS
53if ($context->contextlevel == CONTEXT_COURSECAT) {
54 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
55 $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
56}
57$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
58$PAGE->navbar->add(get_string('assign', 'cohort'));
bd08a24a
RW
59$PAGE->set_title(get_string('cohort:assign', 'cohort'));
60$PAGE->set_heading($COURSE->fullname);
b980c56e
PS
61
62echo $OUTPUT->header();
63echo $OUTPUT->heading(get_string('assignto', 'cohort', format_string($cohort->name)));
64
c5070d06 65// Get the user_selector we will need.
b980c56e
PS
66$potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id));
67$existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id));
68
c5070d06 69// Process incoming user assignments to the cohort
b980c56e 70
c5070d06
PS
71if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
72 $userstoassign = $potentialuserselector->get_selected_users();
73 if (!empty($userstoassign)) {
b980c56e 74
c5070d06
PS
75 foreach ($userstoassign as $adduser) {
76 // no duplicates please
77 if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
78 cohort_add_member($cohort->id, $adduser->id);
b980c56e
PS
79 }
80 }
81
c5070d06
PS
82 $potentialuserselector->invalidate_selected_users();
83 $existinguserselector->invalidate_selected_users();
84 }
85}
b980c56e 86
c5070d06
PS
87// Process removing user assignments to the cohort
88if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
89 $userstoremove = $existinguserselector->get_selected_users();
90 if (!empty($userstoremove)) {
91 foreach ($userstoremove as $removeuser) {
92 cohort_remove_member($cohort->id, $removeuser->id);
b980c56e 93 }
c5070d06
PS
94 $potentialuserselector->invalidate_selected_users();
95 $existinguserselector->invalidate_selected_users();
96 }
97}
b980c56e 98
c5070d06 99// Print the form.
b980c56e
PS
100?>
101<form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
102 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
103
104 <table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
105 <tr>
106 <td id="existingcell">
107 <p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p>
108 <?php $existinguserselector->display() ?>
109 </td>
110 <td id="buttonscell">
111 <div id="addcontrols">
c5070d06 112 <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
113 </div>
114
115 <div id="removecontrols">
c5070d06 116 <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
117 </div>
118 </td>
119 <td id="potentialcell">
120 <p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p>
121 <?php $potentialuserselector->display() ?>
122 </td>
123 </tr>
c5070d06
PS
124 <tr><td colspan="3" id='backcell'>
125 <input type="submit" name="cancel" value="<?php p(get_string('backtocohorts', 'cohort')); ?>" />
126 </td></tr>
b980c56e
PS
127 </table>
128</div></form>
129
130<?php
131
b980c56e 132echo $OUTPUT->footer();