weekly release 2.2.4+
[moodle.git] / course / editcategory.php
blobc475b2a34581b7e1fbb09ca86066e8a23a6a600f
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.
19 * When called with an id parameter, edits the category with that id.
20 * Otherwise it creates a new category with default parent from the parent
21 * parameter, which may be 0.
23 * @package core
24 * @subpackage 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('lib.php');
31 require_once('editcategory_form.php');
33 require_login();
35 $id = optional_param('id', 0, PARAM_INT);
36 $itemid = 0; //initalise itemid, as all files in category description has item id 0
38 if ($id) {
39 if (!$category = $DB->get_record('course_categories', array('id' => $id))) {
40 print_error('unknowcategory');
42 $PAGE->set_url('/course/editcategory.php', array('id' => $id));
43 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $id);
44 $PAGE->set_context($categorycontext);
45 require_capability('moodle/category:manage', $categorycontext);
46 $strtitle = get_string('editcategorysettings');
47 $editorcontext = $categorycontext;
48 $title = $strtitle;
49 $fullname = $category->name;
50 } else {
51 $parent = required_param('parent', PARAM_INT);
52 $PAGE->set_url('/course/editcategory.php', array('parent' => $parent));
53 if ($parent) {
54 if (!$DB->record_exists('course_categories', array('id' => $parent))) {
55 print_error('unknowcategory');
57 $context = get_context_instance(CONTEXT_COURSECAT, $parent);
58 } else {
59 $context = get_system_context();
61 $PAGE->set_context($context);
62 $category = new stdClass();
63 $category->id = 0;
64 $category->parent = $parent;
65 require_capability('moodle/category:manage', $context);
66 $strtitle = get_string("addnewcategory");
67 $editorcontext = $context;
68 $itemid = null; //set this explicitly, so files for parent category should not get loaded in draft area.
69 $title = "$SITE->shortname: ".get_string('addnewcategory');
70 $fullname = $SITE->fullname;
73 $PAGE->set_pagelayout('admin');
75 $editoroptions = array(
76 'maxfiles' => EDITOR_UNLIMITED_FILES,
77 'maxbytes' => $CFG->maxbytes,
78 'trusttext' => true,
79 'context' => $editorcontext
81 $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', $itemid);
83 $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions'));
84 $mform->set_data($category);
86 if ($mform->is_cancelled()) {
87 if ($id) {
88 redirect($CFG->wwwroot . '/course/category.php?id=' . $id . '&categoryedit=on');
89 } else if ($parent) {
90 redirect($CFG->wwwroot .'/course/category.php?id=' . $parent . '&categoryedit=on');
91 } else {
92 redirect($CFG->wwwroot .'/course/index.php?categoryedit=on');
94 } else if ($data = $mform->get_data()) {
95 $newcategory = new stdClass();
96 $newcategory->name = $data->name;
97 $newcategory->idnumber = $data->idnumber;
98 $newcategory->description_editor = $data->description_editor;
99 $newcategory->parent = $data->parent; // if $data->parent = 0, the new category will be a top-level category
101 if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
102 $newcategory->theme = $data->theme;
105 if ($id) {
106 // Update an existing category.
107 $newcategory->id = $category->id;
108 if ($newcategory->parent != $category->parent) {
109 // check category manage capability if parent changed
110 require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent));
111 $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent));
112 move_category($newcategory, $parent_cat);
114 } else {
115 // Create a new category.
116 $newcategory->description = $data->description_editor['text'];
117 $newcategory->sortorder = 999;
118 $newcategory->id = $DB->insert_record('course_categories', $newcategory);
119 $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
120 $categorycontext = $newcategory->context;
121 mark_context_dirty($newcategory->context->path);
124 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
125 $DB->update_record('course_categories', $newcategory);
126 fix_course_sortorder();
128 redirect('category.php?id='.$newcategory->id.'&categoryedit=on');
131 // Unfortunately the navigation never generates correctly for this page because technically this page doesn't actually
132 // exist on the navigation; you get here through the course management page.
133 // First up we'll try to make the course management page active seeing as that is where the user thinks they are.
134 // The big prolem here is that the course management page is a common page for both editing users and common users and
135 // is only added to the admin tree if the user has permission to edit at the system level.
136 $node = $PAGE->settingsnav->get('root');
137 if ($node) {
138 $node = $node->get('courses');
139 if ($node) {
140 $node = $node->get('coursemgmt');
143 if ($node) {
144 // The course management page exists so make that active.
145 $node->make_active();
146 } else {
147 // Failing that we'll override the URL, not as accurate and chances are things
148 // won't be 100% correct all the time but should work most times.
149 // A common reason to arrive here is having the management capability within only a particular category (not at system level).
150 navigation_node::override_active_url(new moodle_url('/course/index.php', array('categoryedit' => 'on')));
153 $PAGE->set_title($title);
154 $PAGE->set_heading($fullname);
155 echo $OUTPUT->header();
156 echo $OUTPUT->heading($strtitle);
157 $mform->display();
158 echo $OUTPUT->footer();