Merge branch 'MDL-61484-master' of git://github.com/junpataleta/moodle
[moodle.git] / course / editcategory.php
blobb45792059d438e6b2a9f014c0faff78309cf4150
1 <?php
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.
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/>.
17 /**
18 * Page for creating or editing course category name/parent/description.
20 * When called with an id parameter, edits the category with that id.
21 * Otherwise it creates a new category with default parent from the parent
22 * parameter, which may be 0.
24 * @package core_course
25 * @copyright 2007 Nicolas Connault
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once('../config.php');
30 require_once($CFG->dirroot.'/course/lib.php');
31 require_once($CFG->libdir.'/coursecatlib.php');
33 require_login();
35 $id = optional_param('id', 0, PARAM_INT);
37 $url = new moodle_url('/course/editcategory.php');
38 if ($id) {
39 $coursecat = coursecat::get($id, MUST_EXIST, true);
40 $category = $coursecat->get_db_record();
41 $context = context_coursecat::instance($id);
43 $url->param('id', $id);
44 $strtitle = new lang_string('editcategorysettings');
45 $itemid = 0; // Initialise itemid, as all files in category description has item id 0.
46 $title = $strtitle;
47 $fullname = $coursecat->get_formatted_name();
49 } else {
50 $parent = required_param('parent', PARAM_INT);
51 $url->param('parent', $parent);
52 if ($parent) {
53 $DB->record_exists('course_categories', array('id' => $parent), '*', MUST_EXIST);
54 $context = context_coursecat::instance($parent);
55 } else {
56 $context = context_system::instance();
58 navigation_node::override_active_url(new moodle_url('/course/editcategory.php', array('parent' => $parent)));
60 $category = new stdClass();
61 $category->id = 0;
62 $category->parent = $parent;
63 $strtitle = new lang_string("addnewcategory");
64 $itemid = null; // Set this explicitly, so files for parent category should not get loaded in draft area.
65 $title = "$SITE->shortname: ".get_string('addnewcategory');
66 $fullname = $SITE->fullname;
69 require_capability('moodle/category:manage', $context);
71 $PAGE->set_context($context);
72 $PAGE->set_url($url);
73 $PAGE->set_pagelayout('admin');
74 $PAGE->set_title($title);
75 $PAGE->set_heading($fullname);
77 $mform = new core_course_editcategory_form(null, array(
78 'categoryid' => $id,
79 'parent' => $category->parent,
80 'context' => $context,
81 'itemid' => $itemid
82 ));
83 $mform->set_data(file_prepare_standard_editor(
84 $category,
85 'description',
86 $mform->get_description_editor_options(),
87 $context,
88 'coursecat',
89 'description',
90 $itemid
91 ));
93 $manageurl = new moodle_url('/course/management.php');
94 if ($mform->is_cancelled()) {
95 if ($id) {
96 $manageurl->param('categoryid', $id);
97 } else if ($parent) {
98 $manageurl->param('categoryid', $parent);
100 redirect($manageurl);
101 } else if ($data = $mform->get_data()) {
102 if (isset($coursecat)) {
103 if ((int)$data->parent !== (int)$coursecat->parent && !$coursecat->can_change_parent($data->parent)) {
104 print_error('cannotmovecategory');
106 $coursecat->update($data, $mform->get_description_editor_options());
107 } else {
108 $category = coursecat::create($data, $mform->get_description_editor_options());
110 $manageurl->param('categoryid', $category->id);
111 redirect($manageurl);
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading($strtitle);
116 $mform->display();
117 echo $OUTPUT->footer();