Merge branch 'm25_MDL-40200_Notices_When_Viewing_Profile_Invalid_UserId' of https...
[moodle.git] / cohort / edit.php
blob3a18ddb932781dd2e9ac366a2e9c00daedf05674
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);
66 if ($context->contextlevel == CONTEXT_COURSECAT) {
67 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
68 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
69 $PAGE->set_pagelayout('report');
71 } else {
72 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
73 $PAGE->set_pagelayout('admin');
76 if ($delete and $cohort->id) {
77 $PAGE->url->param('delete', 1);
78 if ($confirm and confirm_sesskey()) {
79 cohort_delete_cohort($cohort);
80 redirect($returnurl);
82 $strheading = get_string('delcohort', 'cohort');
83 $PAGE->navbar->add($strheading);
84 $PAGE->set_title($strheading);
85 $PAGE->set_heading($COURSE->fullname);
86 echo $OUTPUT->header();
87 echo $OUTPUT->heading($strheading);
88 $yesurl = new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1, 'confirm'=>1,'sesskey'=>sesskey()));
89 $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
90 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
91 echo $OUTPUT->footer();
92 die;
95 $editoroptions = array('maxfiles'=>0, 'context'=>$context);
96 if ($cohort->id) {
97 // Edit existing.
98 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
99 $strheading = get_string('editcohort', 'cohort');
101 } else {
102 // Add new.
103 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
104 $strheading = get_string('addcohort', 'cohort');
107 $PAGE->set_title($strheading);
108 $PAGE->set_heading($COURSE->fullname);
109 $PAGE->navbar->add($strheading);
111 $editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
113 if ($editform->is_cancelled()) {
114 redirect($returnurl);
116 } else if ($data = $editform->get_data()) {
117 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
119 if ($data->id) {
120 cohort_update_cohort($data);
121 } else {
122 cohort_add_cohort($data);
125 // Use new context id, it could have been changed.
126 redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
129 echo $OUTPUT->header();
130 echo $OUTPUT->heading($strheading);
131 echo $editform->display();
132 echo $OUTPUT->footer();