MDL-21781 fixed some more cohort assign issues
[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
b980c56e
PS
63if ($context->contextlevel == CONTEXT_COURSECAT) {
64 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
65 $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
66}
67$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
68
69if ($delete and $cohort->id) {
70 $PAGE->url->param('delete', 1);
71 if ($confirm and confirm_sesskey()) {
72 cohort_delete_cohort($cohort);
73 redirect($returnurl);
74 }
75 $strheading = get_string('delcohort', 'cohort');
76 $PAGE->navbar->add($strheading);
77 $PAGE->set_title($strheading);
78 echo $OUTPUT->header();
79 echo $OUTPUT->heading($strheading);
80 $yesurl = new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1, 'confirm'=>1,'sesskey'=>sesskey()));
81 $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
82 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
83 echo $OUTPUT->footer();
84 die;
85}
86
87$editoroptions = array('maxfiles'=>0, 'context'=>$context);
88if ($cohort->id) {
89 // edit existing
90 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
91 $strheading = get_string('editcohort', 'cohort');
92
93} else {
94 // add new
95 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
96 $strheading = get_string('addcohort', 'cohort');
97}
98
99$PAGE->set_title($strheading);
100$PAGE->navbar->add($strheading);
101
102$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions));
103$editform->set_data($cohort);
104
105if ($editform->is_cancelled()) {
106 redirect($returnurl);
107
108} else if ($data = $editform->get_data()) {
109 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
110
111 if ($data->id) {
112 cohort_update_cohort($data);
113 } else {
114 cohort_add_cohort($data);
115 }
116
117 redirect($returnurl);
118}
119
120echo $OUTPUT->header();
121echo $OUTPUT->heading($strheading);
122echo $editform->display();
123echo $OUTPUT->footer();
124