MDL-46503 behat: cohort generator allows to create cohort in category
[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);
33$confirm = optional_param('confirm', 0, PARAM_BOOL);
34
35require_login();
36
37$category = null;
38if ($id) {
39 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
d197ea43 40 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
b980c56e 41} else {
d197ea43 42 $context = context::instance_by_id($contextid, MUST_EXIST);
b980c56e
PS
43 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
44 print_error('invalidcontext');
45 }
fbaea88f 46 $cohort = new stdClass();
b980c56e
PS
47 $cohort->id = 0;
48 $cohort->contextid = $context->id;
49 $cohort->name = '';
50 $cohort->description = '';
51}
52
c7d5ed1c 53require_capability('moodle/cohort:manage', $context);
b980c56e
PS
54
55$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
56
4a0ef03e 57if (!empty($cohort->component)) {
6eb8bf7f 58 // We can not manually edit cohorts that were created by external systems, sorry.
4a0ef03e
PS
59 redirect($returnurl);
60}
61
b980c56e
PS
62$PAGE->set_context($context);
63$PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
64$PAGE->set_context($context);
d5814f4e 65$PAGE->set_pagelayout('admin');
b980c56e 66
b980c56e
PS
67if ($context->contextlevel == CONTEXT_COURSECAT) {
68 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
21c8d60d 69 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
21c8d60d
PS
70
71} else {
72 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
b980c56e 73}
b980c56e
PS
74
75if ($delete and $cohort->id) {
76 $PAGE->url->param('delete', 1);
77 if ($confirm and confirm_sesskey()) {
78 cohort_delete_cohort($cohort);
79 redirect($returnurl);
80 }
81 $strheading = get_string('delcohort', 'cohort');
82 $PAGE->navbar->add($strheading);
83 $PAGE->set_title($strheading);
bd08a24a 84 $PAGE->set_heading($COURSE->fullname);
b980c56e
PS
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;
92}
93
94$editoroptions = array('maxfiles'=>0, 'context'=>$context);
95if ($cohort->id) {
6eb8bf7f 96 // Edit existing.
e9de1cf4 97 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
98 $strheading = get_string('editcohort', 'cohort');
99
100} else {
6eb8bf7f 101 // Add new.
e9de1cf4 102 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
103 $strheading = get_string('addcohort', 'cohort');
104}
105
106$PAGE->set_title($strheading);
bd08a24a 107$PAGE->set_heading($COURSE->fullname);
b980c56e
PS
108$PAGE->navbar->add($strheading);
109
9e1065a8 110$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
b980c56e
PS
111
112if ($editform->is_cancelled()) {
113 redirect($returnurl);
114
115} else if ($data = $editform->get_data()) {
116 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
117
118 if ($data->id) {
119 cohort_update_cohort($data);
120 } else {
121 cohort_add_cohort($data);
122 }
123
6eb8bf7f 124 // Use new context id, it could have been changed.
9e1065a8 125 redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
b980c56e
PS
126}
127
128echo $OUTPUT->header();
129echo $OUTPUT->heading($strheading);
130echo $editform->display();
131echo $OUTPUT->footer();
132