MDL-36014 cohorts: fix string names
[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);
274dc068 34$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
b980c56e
PS
35
36require_login();
37
38$category = null;
39if ($id) {
40 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
d197ea43 41 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
b980c56e 42} else {
d197ea43 43 $context = context::instance_by_id($contextid, MUST_EXIST);
b980c56e
PS
44 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
45 print_error('invalidcontext');
46 }
fbaea88f 47 $cohort = new stdClass();
b980c56e
PS
48 $cohort->id = 0;
49 $cohort->contextid = $context->id;
50 $cohort->name = '';
51 $cohort->description = '';
52}
53
c7d5ed1c 54require_capability('moodle/cohort:manage', $context);
b980c56e 55
274dc068
MG
56if ($returnurl) {
57 $returnurl = new moodle_url($returnurl);
58} else {
59 $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
60}
b980c56e 61
4a0ef03e 62if (!empty($cohort->component)) {
6eb8bf7f 63 // We can not manually edit cohorts that were created by external systems, sorry.
4a0ef03e
PS
64 redirect($returnurl);
65}
66
b980c56e 67$PAGE->set_context($context);
274dc068
MG
68$baseurl = new moodle_url('/cohort/edit.php', array('contextid' => $context->id, 'id' => $cohort->id));
69$PAGE->set_url($baseurl);
b980c56e 70$PAGE->set_context($context);
d5814f4e 71$PAGE->set_pagelayout('admin');
b980c56e 72
b980c56e
PS
73if ($context->contextlevel == CONTEXT_COURSECAT) {
74 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
21c8d60d 75 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
21c8d60d
PS
76
77} else {
78 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
b980c56e 79}
b980c56e
PS
80
81if ($delete and $cohort->id) {
82 $PAGE->url->param('delete', 1);
83 if ($confirm and confirm_sesskey()) {
84 cohort_delete_cohort($cohort);
85 redirect($returnurl);
86 }
87 $strheading = get_string('delcohort', 'cohort');
88 $PAGE->navbar->add($strheading);
89 $PAGE->set_title($strheading);
bd08a24a 90 $PAGE->set_heading($COURSE->fullname);
b980c56e
PS
91 echo $OUTPUT->header();
92 echo $OUTPUT->heading($strheading);
274dc068
MG
93 $yesurl = new moodle_url('/cohort/edit.php', array('id' => $cohort->id, 'delete' => 1,
94 'confirm' => 1, 'sesskey' => sesskey(), 'returnurl' => $returnurl->out_as_local_url()));
b980c56e
PS
95 $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
96 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
97 echo $OUTPUT->footer();
98 die;
99}
100
101$editoroptions = array('maxfiles'=>0, 'context'=>$context);
102if ($cohort->id) {
6eb8bf7f 103 // Edit existing.
e9de1cf4 104 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
105 $strheading = get_string('editcohort', 'cohort');
106
107} else {
6eb8bf7f 108 // Add new.
e9de1cf4 109 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
110 $strheading = get_string('addcohort', 'cohort');
111}
112
113$PAGE->set_title($strheading);
bd08a24a 114$PAGE->set_heading($COURSE->fullname);
b980c56e
PS
115$PAGE->navbar->add($strheading);
116
274dc068 117$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort, 'returnurl'=>$returnurl));
b980c56e
PS
118
119if ($editform->is_cancelled()) {
120 redirect($returnurl);
121
122} else if ($data = $editform->get_data()) {
123 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
124
125 if ($data->id) {
126 cohort_update_cohort($data);
127 } else {
128 cohort_add_cohort($data);
129 }
130
274dc068
MG
131 if ($returnurl->get_param('showall') || $returnurl->get_param('contextid') == $data->contextid) {
132 // Redirect to where we were before.
133 redirect($returnurl);
134 } else {
135 // Use new context id, it has been changed.
136 redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid)));
137 }
b980c56e
PS
138}
139
140echo $OUTPUT->header();
141echo $OUTPUT->heading($strheading);
274dc068
MG
142
143if (!$id && ($editcontrols = cohort_edit_controls($context, $baseurl))) {
144 echo $OUTPUT->render($editcontrols);
145}
146
b980c56e
PS
147echo $editform->display();
148echo $OUTPUT->footer();
149