Commit | Line | Data |
---|---|---|
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 | 25 | require('../config.php'); |
607fdf63 | 26 | require_once($CFG->dirroot.'/cohort/locallib.php'); |
b980c56e PS |
27 | |
28 | $id = required_param('id', PARAM_INT); | |
274dc068 | 29 | $returnurl = optional_param('returnurl', '', PARAM_LOCALURL); |
b980c56e PS |
30 | |
31 | require_login(); | |
32 | ||
33 | $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST); | |
d197ea43 | 34 | $context = context::instance_by_id($cohort->contextid, MUST_EXIST); |
b980c56e | 35 | |
c7d5ed1c | 36 | require_capability('moodle/cohort:assign', $context); |
b980c56e | 37 | |
62bc5019 | 38 | $PAGE->set_context($context); |
f6eb428e | 39 | $PAGE->set_url('/cohort/assign.php', array('id'=>$id)); |
d5814f4e | 40 | $PAGE->set_pagelayout('admin'); |
b980c56e | 41 | |
274dc068 MG |
42 | if ($returnurl) { |
43 | $returnurl = new moodle_url($returnurl); | |
44 | } else { | |
45 | $returnurl = new moodle_url('/cohort/index.php', array('contextid' => $cohort->contextid)); | |
46 | } | |
4a0ef03e PS |
47 | |
48 | if (!empty($cohort->component)) { | |
6eb8bf7f | 49 | // We can not manually edit cohorts that were created by external systems, sorry. |
4a0ef03e PS |
50 | redirect($returnurl); |
51 | } | |
52 | ||
c5070d06 | 53 | if (optional_param('cancel', false, PARAM_BOOL)) { |
4a0ef03e | 54 | redirect($returnurl); |
c5070d06 PS |
55 | } |
56 | ||
b980c56e PS |
57 | if ($context->contextlevel == CONTEXT_COURSECAT) { |
58 | $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST); | |
21c8d60d | 59 | navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid))); |
21c8d60d PS |
60 | } else { |
61 | navigation_node::override_active_url(new moodle_url('/cohort/index.php', array())); | |
b980c56e | 62 | } |
b980c56e | 63 | $PAGE->navbar->add(get_string('assign', 'cohort')); |
21c8d60d | 64 | |
78fb7110 | 65 | $PAGE->set_title(get_string('assigncohorts', 'cohort')); |
bd08a24a | 66 | $PAGE->set_heading($COURSE->fullname); |
b980c56e PS |
67 | |
68 | echo $OUTPUT->header(); | |
69 | echo $OUTPUT->heading(get_string('assignto', 'cohort', format_string($cohort->name))); | |
70 | ||
5121a95d PS |
71 | echo $OUTPUT->notification(get_string('removeuserwarning', 'core_cohort')); |
72 | ||
c5070d06 | 73 | // Get the user_selector we will need. |
d86b7de7 PS |
74 | $potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id, 'accesscontext'=>$context)); |
75 | $existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id, 'accesscontext'=>$context)); | |
b980c56e | 76 | |
c5070d06 | 77 | // Process incoming user assignments to the cohort |
b980c56e | 78 | |
c5070d06 PS |
79 | if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { |
80 | $userstoassign = $potentialuserselector->get_selected_users(); | |
81 | if (!empty($userstoassign)) { | |
b980c56e | 82 | |
c5070d06 | 83 | foreach ($userstoassign as $adduser) { |
2f8c69e1 | 84 | cohort_add_member($cohort->id, $adduser->id); |
b980c56e PS |
85 | } |
86 | ||
c5070d06 PS |
87 | $potentialuserselector->invalidate_selected_users(); |
88 | $existinguserselector->invalidate_selected_users(); | |
89 | } | |
90 | } | |
b980c56e | 91 | |
c5070d06 PS |
92 | // Process removing user assignments to the cohort |
93 | if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) { | |
94 | $userstoremove = $existinguserselector->get_selected_users(); | |
95 | if (!empty($userstoremove)) { | |
96 | foreach ($userstoremove as $removeuser) { | |
97 | cohort_remove_member($cohort->id, $removeuser->id); | |
b980c56e | 98 | } |
c5070d06 PS |
99 | $potentialuserselector->invalidate_selected_users(); |
100 | $existinguserselector->invalidate_selected_users(); | |
101 | } | |
102 | } | |
b980c56e | 103 | |
c5070d06 | 104 | // Print the form. |
b980c56e PS |
105 | ?> |
106 | <form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div> | |
107 | <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" /> | |
274dc068 | 108 | <input type="hidden" name="returnurl" value="<?php echo $returnurl->out_as_local_url() ?>" /> |
b980c56e PS |
109 | |
110 | <table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0"> | |
111 | <tr> | |
112 | <td id="existingcell"> | |
113 | <p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p> | |
114 | <?php $existinguserselector->display() ?> | |
115 | </td> | |
116 | <td id="buttonscell"> | |
117 | <div id="addcontrols"> | |
c5070d06 | 118 | <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().' '.s(get_string('add')); ?>" title="<?php p(get_string('add')); ?>" /><br /> |
b980c56e PS |
119 | </div> |
120 | ||
121 | <div id="removecontrols"> | |
c5070d06 | 122 | <input name="remove" id="remove" type="submit" value="<?php echo s(get_string('remove')).' '.$OUTPUT->rarrow(); ?>" title="<?php p(get_string('remove')); ?>" /> |
b980c56e PS |
123 | </div> |
124 | </td> | |
125 | <td id="potentialcell"> | |
126 | <p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p> | |
127 | <?php $potentialuserselector->display() ?> | |
128 | </td> | |
129 | </tr> | |
c5070d06 PS |
130 | <tr><td colspan="3" id='backcell'> |
131 | <input type="submit" name="cancel" value="<?php p(get_string('backtocohorts', 'cohort')); ?>" /> | |
132 | </td></tr> | |
b980c56e PS |
133 | </table> |
134 | </div></form> | |
135 | ||
136 | <?php | |
137 | ||
b980c56e | 138 | echo $OUTPUT->footer(); |