weekly release 3.0dev
[moodle.git] / cohort / edit.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
b980c56e
PS
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
25require('../config.php');
df997f84 26require($CFG->dirroot.'/course/lib.php');
b980c56e
PS
27require($CFG->dirroot.'/cohort/lib.php');
28require($CFG->dirroot.'/cohort/edit_form.php');
29
30$id = optional_param('id', 0, PARAM_INT);
31$contextid = optional_param('contextid', 0, PARAM_INT);
32$delete = optional_param('delete', 0, PARAM_BOOL);
80f98467
MG
33$show = optional_param('show', 0, PARAM_BOOL);
34$hide = optional_param('hide', 0, PARAM_BOOL);
b980c56e 35$confirm = optional_param('confirm', 0, PARAM_BOOL);
274dc068 36$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
b980c56e
PS
37
38require_login();
39
40$category = null;
41if ($id) {
42 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
d197ea43 43 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
b980c56e 44} else {
d197ea43 45 $context = context::instance_by_id($contextid, MUST_EXIST);
b980c56e
PS
46 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
47 print_error('invalidcontext');
48 }
fbaea88f 49 $cohort = new stdClass();
b980c56e
PS
50 $cohort->id = 0;
51 $cohort->contextid = $context->id;
52 $cohort->name = '';
53 $cohort->description = '';
54}
55
c7d5ed1c 56require_capability('moodle/cohort:manage', $context);
b980c56e 57
274dc068
MG
58if ($returnurl) {
59 $returnurl = new moodle_url($returnurl);
60} else {
61 $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
62}
b980c56e 63
4a0ef03e 64if (!empty($cohort->component)) {
6eb8bf7f 65 // We can not manually edit cohorts that were created by external systems, sorry.
4a0ef03e
PS
66 redirect($returnurl);
67}
68
b980c56e 69$PAGE->set_context($context);
274dc068
MG
70$baseurl = new moodle_url('/cohort/edit.php', array('contextid' => $context->id, 'id' => $cohort->id));
71$PAGE->set_url($baseurl);
b980c56e 72$PAGE->set_context($context);
d5814f4e 73$PAGE->set_pagelayout('admin');
b980c56e 74
b980c56e
PS
75if ($context->contextlevel == CONTEXT_COURSECAT) {
76 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
21c8d60d 77 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
21c8d60d
PS
78
79} else {
80 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
b980c56e 81}
b980c56e
PS
82
83if ($delete and $cohort->id) {
84 $PAGE->url->param('delete', 1);
85 if ($confirm and confirm_sesskey()) {
86 cohort_delete_cohort($cohort);
87 redirect($returnurl);
88 }
89 $strheading = get_string('delcohort', 'cohort');
90 $PAGE->navbar->add($strheading);
91 $PAGE->set_title($strheading);
bd08a24a 92 $PAGE->set_heading($COURSE->fullname);
b980c56e
PS
93 echo $OUTPUT->header();
94 echo $OUTPUT->heading($strheading);
274dc068
MG
95 $yesurl = new moodle_url('/cohort/edit.php', array('id' => $cohort->id, 'delete' => 1,
96 'confirm' => 1, 'sesskey' => sesskey(), 'returnurl' => $returnurl->out_as_local_url()));
b980c56e
PS
97 $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
98 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
99 echo $OUTPUT->footer();
100 die;
101}
102
80f98467
MG
103if ($show && $cohort->id && confirm_sesskey()) {
104 if (!$cohort->visible) {
105 $record = (object)array('id' => $cohort->id, 'visible' => 1, 'contextid' => $cohort->contextid);
106 cohort_update_cohort($record);
107 }
108 redirect($returnurl);
109}
110
111if ($hide && $cohort->id && confirm_sesskey()) {
112 if ($cohort->visible) {
113 $record = (object)array('id' => $cohort->id, 'visible' => 0, 'contextid' => $cohort->contextid);
114 cohort_update_cohort($record);
115 }
116 redirect($returnurl);
117}
118
b980c56e
PS
119$editoroptions = array('maxfiles'=>0, 'context'=>$context);
120if ($cohort->id) {
6eb8bf7f 121 // Edit existing.
e9de1cf4 122 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
123 $strheading = get_string('editcohort', 'cohort');
124
125} else {
6eb8bf7f 126 // Add new.
e9de1cf4 127 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
128 $strheading = get_string('addcohort', 'cohort');
129}
130
131$PAGE->set_title($strheading);
bd08a24a 132$PAGE->set_heading($COURSE->fullname);
b980c56e
PS
133$PAGE->navbar->add($strheading);
134
274dc068 135$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort, 'returnurl'=>$returnurl));
b980c56e
PS
136
137if ($editform->is_cancelled()) {
138 redirect($returnurl);
139
140} else if ($data = $editform->get_data()) {
141 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
142
143 if ($data->id) {
144 cohort_update_cohort($data);
145 } else {
146 cohort_add_cohort($data);
147 }
148
274dc068
MG
149 if ($returnurl->get_param('showall') || $returnurl->get_param('contextid') == $data->contextid) {
150 // Redirect to where we were before.
151 redirect($returnurl);
152 } else {
153 // Use new context id, it has been changed.
154 redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid)));
155 }
b980c56e
PS
156}
157
158echo $OUTPUT->header();
159echo $OUTPUT->heading($strheading);
274dc068
MG
160
161if (!$id && ($editcontrols = cohort_edit_controls($context, $baseurl))) {
162 echo $OUTPUT->render($editcontrols);
163}
164
b980c56e
PS
165echo $editform->display();
166echo $OUTPUT->footer();
167