MDL-34045 fix invalid idnumber field type in cohort form
[moodle.git] / cohort / edit.php
blobdc1bd08bb04187d15cca8dbf3dcf3b70403ce806
1 <?php
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.
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/>.
19 /**
20 * Cohort related management functions, this file needs to be included manually.
22 * @package core
23 * @subpackage cohort
24 * @copyright 2010 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require('../config.php');
29 require($CFG->dirroot.'/course/lib.php');
30 require($CFG->dirroot.'/cohort/lib.php');
31 require($CFG->dirroot.'/cohort/edit_form.php');
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);
38 require_login();
40 $category = null;
41 if ($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');
49 $cohort = new stdClass();
50 $cohort->id = 0;
51 $cohort->contextid = $context->id;
52 $cohort->name = '';
53 $cohort->description = '';
56 require_capability('moodle/cohort:manage', $context);
58 $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
60 if (!empty($cohort->component)) {
61 // we can not manually edit cohorts that were created by external systems, sorry
62 redirect($returnurl);
65 $PAGE->set_context($context);
66 $PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
67 $PAGE->set_context($context);
69 if ($context->contextlevel == CONTEXT_COURSECAT) {
70 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
71 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
72 $PAGE->set_pagelayout('report');
74 } else {
75 navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
76 $PAGE->set_pagelayout('admin');
79 if ($delete and $cohort->id) {
80 $PAGE->url->param('delete', 1);
81 if ($confirm and confirm_sesskey()) {
82 cohort_delete_cohort($cohort);
83 redirect($returnurl);
85 $strheading = get_string('delcohort', 'cohort');
86 $PAGE->navbar->add($strheading);
87 $PAGE->set_title($strheading);
88 $PAGE->set_heading($COURSE->fullname);
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;
98 $editoroptions = array('maxfiles'=>0, 'context'=>$context);
99 if ($cohort->id) {
100 // edit existing
101 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
102 $strheading = get_string('editcohort', 'cohort');
104 } else {
105 // add new
106 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
107 $strheading = get_string('addcohort', 'cohort');
110 $PAGE->set_title($strheading);
111 $PAGE->set_heading($COURSE->fullname);
112 $PAGE->navbar->add($strheading);
114 $editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
116 if ($editform->is_cancelled()) {
117 redirect($returnurl);
119 } else if ($data = $editform->get_data()) {
120 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
122 if ($data->id) {
123 cohort_update_cohort($data);
124 } else {
125 cohort_add_cohort($data);
128 // use new context id, it could have been changed
129 redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
132 echo $OUTPUT->header();
133 echo $OUTPUT->heading($strheading);
134 echo $editform->display();
135 echo $OUTPUT->footer();