MDL-24321 switching to stdClass in /c*/
[moodle.git] / cohort / edit.php
CommitLineData
b980c56e
PS
1<?php
2
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.
14//
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/>.
17
18
19/**
20 * Cohort related management functions, this file needs to be included manually.
21 *
707f4136 22 * @package core
b980c56e 23 * @subpackage cohort
707f4136 24 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28require('../config.php');
df997f84 29require($CFG->dirroot.'/course/lib.php');
b980c56e
PS
30require($CFG->dirroot.'/cohort/lib.php');
31require($CFG->dirroot.'/cohort/edit_form.php');
32
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);
37
38require_login();
39
40$category = null;
41if ($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');
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
PS
57
58$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
59
4a0ef03e
PS
60if (!empty($cohort->component)) {
61 // we can not manually edit cohorts that were created by external systems, sorry
62 redirect($returnurl);
63}
64
b980c56e
PS
65$PAGE->set_context($context);
66$PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
67$PAGE->set_context($context);
68
b980c56e
PS
69if ($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')));
72}
73$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
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) {
96 // edit existing
97 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
98 $strheading = get_string('editcohort', 'cohort');
99
100} else {
101 // add new
102 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
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
9e1065a8
PS
124 // use new context id, it could have been changed
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