Merge branch 'MDL-48639_27' of git://github.com/timhunt/moodle into MOODLE_27_STABLE
[moodle.git] / cohort / edit.php
bloba585f35cda1038856fbe8c457b57d84864d74f74
1 <?php
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.
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/>.
17 /**
18 * Cohort related management functions, this file needs to be included manually.
20 * @package core_cohort
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../config.php');
26 require($CFG->dirroot.'/course/lib.php');
27 require($CFG->dirroot.'/cohort/lib.php');
28 require($CFG->dirroot.'/cohort/edit_form.php');
30 $id = optional_param('id', 0, PARAM_INT);
31 $contextid = optional_param('contextid', 0, PARAM_INT);
32 $delete = optional_param('delete', 0, PARAM_BOOL);
33 $confirm = optional_param('confirm', 0, PARAM_BOOL);
35 require_login();
37 $category = null;
38 if ($id) {
39 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
40 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
41 } else {
42 $context = context::instance_by_id($contextid, MUST_EXIST);
43 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
44 print_error('invalidcontext');
46 $cohort = new stdClass();
47 $cohort->id = 0;
48 $cohort->contextid = $context->id;
49 $cohort->name = '';
50 $cohort->description = '';
53 require_capability('moodle/cohort:manage', $context);
55 $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
57 if (!empty($cohort->component)) {
58 // We can not manually edit cohorts that were created by external systems, sorry.
59 redirect($returnurl);
62 $PAGE->set_context($context);
63 $PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
64 $PAGE->set_context($context);
65 $PAGE->set_pagelayout('admin');
67 if ($context->contextlevel == CONTEXT_COURSECAT) {
68 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
69 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
71 } else {
72 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
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();