weekly release 2.1.6+
[moodle.git] / cohort / edit.php
blobf092247f0b7011c3f7bcb0846f3f39430e9cb815
1 <?php
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.
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 /**
20 * Cohort related management functions, this file needs to be included manually.
22 * @package core
23 * @subpackage cohort
24 * @copyright 2010 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require('../config.php');
29 require($CFG->dirroot.'/course/lib.php');
30 require($CFG->dirroot.'/cohort/lib.php');
31 require($CFG->dirroot.'/cohort/edit_form.php');
33 $id = optional_param('id', 0, PARAM_INT);
34 $contextid = optional_param('contextid', 0, PARAM_INT);
35 $delete = optional_param('delete', 0, PARAM_BOOL);
36 $confirm = optional_param('confirm', 0, PARAM_BOOL);
38 require_login();
40 $category = null;
41 if ($id) {
42 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
43 $context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
44 } else {
45 $context = get_context_instance_by_id($contextid, MUST_EXIST);
46 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
47 print_error('invalidcontext');
49 $cohort = new stdClass();
50 $cohort->id = 0;
51 $cohort->contextid = $context->id;
52 $cohort->name = '';
53 $cohort->description = '';
56 require_capability('moodle/cohort:manage', $context);
58 $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
60 if (!empty($cohort->component)) {
61 // we can not manually edit cohorts that were created by external systems, sorry
62 redirect($returnurl);
65 $PAGE->set_context($context);
66 $PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
67 $PAGE->set_context($context);
69 if ($context->contextlevel == CONTEXT_COURSECAT) {
70 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
71 $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
73 $PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
75 if ($delete and $cohort->id) {
76 $PAGE->url->param('delete', 1);
77 if ($confirm and confirm_sesskey()) {
78 cohort_delete_cohort($cohort);
79 redirect($returnurl);
81 $strheading = get_string('delcohort', 'cohort');
82 $PAGE->navbar->add($strheading);
83 $PAGE->set_title($strheading);
84 $PAGE->set_heading($COURSE->fullname);
85 echo $OUTPUT->header();
86 echo $OUTPUT->heading($strheading);
87 $yesurl = new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1, 'confirm'=>1,'sesskey'=>sesskey()));
88 $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
89 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
90 echo $OUTPUT->footer();
91 die;
94 $editoroptions = array('maxfiles'=>0, 'context'=>$context);
95 if ($cohort->id) {
96 // edit existing
97 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
98 $strheading = get_string('editcohort', 'cohort');
100 } else {
101 // add new
102 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
103 $strheading = get_string('addcohort', 'cohort');
106 $PAGE->set_title($strheading);
107 $PAGE->set_heading($COURSE->fullname);
108 $PAGE->navbar->add($strheading);
110 $editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
112 if ($editform->is_cancelled()) {
113 redirect($returnurl);
115 } else if ($data = $editform->get_data()) {
116 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
118 if ($data->id) {
119 cohort_update_cohort($data);
120 } else {
121 cohort_add_cohort($data);
124 // use new context id, it could have been changed
125 redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
128 echo $OUTPUT->header();
129 echo $OUTPUT->heading($strheading);
130 echo $editform->display();
131 echo $OUTPUT->footer();