MDL-35465 move duplicate check to cohort_add_member()
[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);
d197ea43 43 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
b980c56e 44} else {
d197ea43 45 $context = context::instance_by_id($contextid, MUST_EXIST);
b980c56e
PS
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);
21c8d60d
PS
71 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
72 $PAGE->set_pagelayout('report');
73
74} else {
75 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
76 $PAGE->set_pagelayout('admin');
b980c56e 77}
b980c56e
PS
78
79if ($delete and $cohort->id) {
80 $PAGE->url->param('delete', 1);
81 if ($confirm and confirm_sesskey()) {
82 cohort_delete_cohort($cohort);
83 redirect($returnurl);
84 }
85 $strheading = get_string('delcohort', 'cohort');
86 $PAGE->navbar->add($strheading);
87 $PAGE->set_title($strheading);
bd08a24a 88 $PAGE->set_heading($COURSE->fullname);
b980c56e
PS
89 echo $OUTPUT->header();
90 echo $OUTPUT->heading($strheading);
91 $yesurl = new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1, 'confirm'=>1,'sesskey'=>sesskey()));
92 $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
93 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
94 echo $OUTPUT->footer();
95 die;
96}
97
98$editoroptions = array('maxfiles'=>0, 'context'=>$context);
99if ($cohort->id) {
100 // edit existing
e9de1cf4 101 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
102 $strheading = get_string('editcohort', 'cohort');
103
104} else {
105 // add new
e9de1cf4 106 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
b980c56e
PS
107 $strheading = get_string('addcohort', 'cohort');
108}
109
110$PAGE->set_title($strheading);
bd08a24a 111$PAGE->set_heading($COURSE->fullname);
b980c56e
PS
112$PAGE->navbar->add($strheading);
113
9e1065a8 114$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
b980c56e
PS
115
116if ($editform->is_cancelled()) {
117 redirect($returnurl);
118
119} else if ($data = $editform->get_data()) {
120 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
121
122 if ($data->id) {
123 cohort_update_cohort($data);
124 } else {
125 cohort_add_cohort($data);
126 }
127
9e1065a8
PS
128 // use new context id, it could have been changed
129 redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
b980c56e
PS
130}
131
132echo $OUTPUT->header();
133echo $OUTPUT->heading($strheading);
134echo $editform->display();
135echo $OUTPUT->footer();
136