MDL-21781 cohort id duplicates detection
[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 *
22 * @package moodlecore
23 * @subpackage cohort
24 * @copyright 2010 Petr Skoda (info@skodak.org)
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28require('../config.php');
29require($CFG->dirroot.'/cohort/lib.php');
30require($CFG->dirroot.'/cohort/edit_form.php');
31
32$id = optional_param('id', 0, PARAM_INT);
33$contextid = optional_param('contextid', 0, PARAM_INT);
34$delete = optional_param('delete', 0, PARAM_BOOL);
35$confirm = optional_param('confirm', 0, PARAM_BOOL);
36
37require_login();
38
39$category = null;
40if ($id) {
41 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
42 $context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
43} else {
44 $context = get_context_instance_by_id($contextid, MUST_EXIST);
45 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
46 print_error('invalidcontext');
47 }
48 $cohort = new object();
49 $cohort->id = 0;
50 $cohort->contextid = $context->id;
51 $cohort->name = '';
52 $cohort->description = '';
53}
54
c7d5ed1c 55require_capability('moodle/cohort:manage', $context);
b980c56e
PS
56
57$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
58
59$PAGE->set_context($context);
60$PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
61$PAGE->set_context($context);
62
63// TODO: ohlala, the navbar is not doing what I would expect
64$PAGE->navbar->add(get_string('home'), new moodle_url('/'), navbar::TYPE_SYSTEM);
65if ($context->contextlevel == CONTEXT_COURSECAT) {
66 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
67 $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
68}
69$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
70
71if ($delete and $cohort->id) {
72 $PAGE->url->param('delete', 1);
73 if ($confirm and confirm_sesskey()) {
74 cohort_delete_cohort($cohort);
75 redirect($returnurl);
76 }
77 $strheading = get_string('delcohort', 'cohort');
78 $PAGE->navbar->add($strheading);
79 $PAGE->set_title($strheading);
80 echo $OUTPUT->header();
81 echo $OUTPUT->heading($strheading);
82 $yesurl = new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1, 'confirm'=>1,'sesskey'=>sesskey()));
83 $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
84 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
85 echo $OUTPUT->footer();
86 die;
87}
88
89$editoroptions = array('maxfiles'=>0, 'context'=>$context);
90if ($cohort->id) {
91 // edit existing
92 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
93 $strheading = get_string('editcohort', 'cohort');
94
95} else {
96 // add new
97 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
98 $strheading = get_string('addcohort', 'cohort');
99}
100
101$PAGE->set_title($strheading);
102$PAGE->navbar->add($strheading);
103
104$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions));
105$editform->set_data($cohort);
106
107if ($editform->is_cancelled()) {
108 redirect($returnurl);
109
110} else if ($data = $editform->get_data()) {
111 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
112
113 if ($data->id) {
114 cohort_update_cohort($data);
115 } else {
116 cohort_add_cohort($data);
117 }
118
119 redirect($returnurl);
120}
121
122echo $OUTPUT->header();
123echo $OUTPUT->heading($strheading);
124echo $editform->display();
125echo $OUTPUT->footer();
126