MDL-31873 added mod_assign_get_grades web service function
[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);
65
b980c56e
PS
66if ($context->contextlevel == CONTEXT_COURSECAT) {
67 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
21c8d60d
PS
68 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
69 $PAGE->set_pagelayout('report');
70
71} else {
72 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
73 $PAGE->set_pagelayout('admin');
b980c56e 74}
b980c56e
PS
75
76if ($delete and $cohort->id) {
77 $PAGE->url->param('delete', 1);
78 if ($confirm and confirm_sesskey()) {
79 cohort_delete_cohort($cohort);
80 redirect($returnurl);
81 }
82 $strheading = get_string('delcohort', 'cohort');
83 $PAGE->navbar->add($strheading);
84 $PAGE->set_title($strheading);
bd08a24a 85 $PAGE->set_heading($COURSE->fullname);
b980c56e
PS
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;
93}
94
95$editoroptions = array('maxfiles'=>0, 'context'=>$context);
96if ($cohort->id) {
6eb8bf7f 97 // Edit existing.
e9de1cf4 98 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
99 $strheading = get_string('editcohort', 'cohort');
100
101} else {
6eb8bf7f 102 // Add new.
e9de1cf4 103 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
104 $strheading = get_string('addcohort', 'cohort');
105}
106
107$PAGE->set_title($strheading);
bd08a24a 108$PAGE->set_heading($COURSE->fullname);
b980c56e
PS
109$PAGE->navbar->add($strheading);
110
9e1065a8 111$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
b980c56e
PS
112
113if ($editform->is_cancelled()) {
114 redirect($returnurl);
115
116} else if ($data = $editform->get_data()) {
117 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
118
119 if ($data->id) {
120 cohort_update_cohort($data);
121 } else {
122 cohort_add_cohort($data);
123 }
124
6eb8bf7f 125 // Use new context id, it could have been changed.
9e1065a8 126 redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
b980c56e
PS
127}
128
129echo $OUTPUT->header();
130echo $OUTPUT->heading($strheading);
131echo $editform->display();
132echo $OUTPUT->footer();
133