3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Cohort related management functions, this file needs to be included manually.
23 * @copyright 2010 Petr Skoda (info@skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once('../config.php');
28 require_once($CFG->dirroot.'/cohort/lib.php');
30 $id = required_param('id', PARAM_INT);
34 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
35 $context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
37 require_capability('moodle/cohort:assign', $context);
39 $PAGE->set_url('/cohort/assign.php', array('id'=>$id));
40 $PAGE->set_Context($context);
42 $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid));
44 if (!empty($cohort->component)) {
45 // we can not manually edit cohorts that were created by external systems, sorry
49 if (optional_param('cancel', false, PARAM_BOOL)) {
53 if ($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')));
57 $PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
58 $PAGE->navbar->add(get_string('assign', 'cohort'));
60 echo $OUTPUT->header();
61 echo $OUTPUT->heading(get_string('assignto', 'cohort', format_string($cohort->name)));
63 // Get the user_selector we will need.
64 $potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id));
65 $existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id));
67 // Process incoming user assignments to the cohort
69 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
70 $userstoassign = $potentialuserselector->get_selected_users();
71 if (!empty($userstoassign)) {
73 foreach ($userstoassign as $adduser) {
74 // no duplicates please
75 if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
76 cohort_add_member($cohort->id, $adduser->id);
80 $potentialuserselector->invalidate_selected_users();
81 $existinguserselector->invalidate_selected_users();
85 // Process removing user assignments to the cohort
86 if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
87 $userstoremove = $existinguserselector->get_selected_users();
88 if (!empty($userstoremove)) {
89 foreach ($userstoremove as $removeuser) {
90 cohort_remove_member($cohort->id, $removeuser->id);
92 $potentialuserselector->invalidate_selected_users();
93 $existinguserselector->invalidate_selected_users();
99 <form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
100 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
102 <table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
104 <td id="existingcell">
105 <p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p>
106 <?php $existinguserselector->display() ?>
108 <td id="buttonscell">
109 <div id="addcontrols">
110 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().' '.s(get_string('add')); ?>" title="<?php p(get_string('add')); ?>" /><br />
113 <div id="removecontrols">
114 <input name="remove" id="remove" type="submit" value="<?php echo s(get_string('remove')).' '.$OUTPUT->rarrow(); ?>" title="<?php p(get_string('remove')); ?>" />
117 <td id="potentialcell">
118 <p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p>
119 <?php $potentialuserselector->display() ?>
122 <tr><td colspan="3" id='backcell'>
123 <input type="submit" name="cancel" value="<?php p(get_string('backtocohorts', 'cohort')); ?>" />
130 echo $OUTPUT->footer();