2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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.
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');
35 $id = optional_param('id', 0, PARAM_INT
);
36 $itemid = 0; //initalise itemid, as all files in category description has item id 0
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 = context_coursecat
::instance($id);
44 $PAGE->set_context($categorycontext);
45 require_capability('moodle/category:manage', $categorycontext);
46 $strtitle = get_string('editcategorysettings');
47 $editorcontext = $categorycontext;
49 $fullname = $category->name
;
51 $parent = required_param('parent', PARAM_INT
);
52 $PAGE->set_url('/course/editcategory.php', array('parent' => $parent));
54 if (!$DB->record_exists('course_categories', array('id' => $parent))) {
55 print_error('unknowcategory');
57 $context = context_coursecat
::instance($parent);
59 $context = get_system_context();
61 $PAGE->set_context($context);
62 $category = new stdClass();
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
,
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()) {
88 redirect($CFG->wwwroot
. '/course/category.php?id=' . $id . '&categoryedit=on');
90 redirect($CFG->wwwroot
.'/course/category.php?id=' . $parent . '&categoryedit=on');
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 $logaction = 'update';
107 // Update an existing category.
108 $newcategory->id
= $category->id
;
109 if ($newcategory->parent
!= $category->parent
) {
110 // check category manage capability if parent changed
111 require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent
));
112 $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent
));
113 move_category($newcategory, $parent_cat);
116 // Create a new category.
117 $newcategory->description
= $data->description_editor
['text'];
119 // Don't overwrite the $newcategory object as it'll be processed by file_postupdate_standard_editor in a moment
120 $category = create_course_category($newcategory);
121 $newcategory->id
= $category->id
;
122 $categorycontext = $category->context
;
126 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
127 $DB->update_record('course_categories', $newcategory);
128 add_to_log(SITEID
, "category", $logaction, "editcategory.php?id=$newcategory->id", $newcategory->id
);
129 fix_course_sortorder();
131 redirect('category.php?id='.$newcategory->id
.'&categoryedit=on');
134 // Unfortunately the navigation never generates correctly for this page because technically this page doesn't actually
135 // exist on the navigation; you get here through the course management page.
136 // First up we'll try to make the course management page active seeing as that is where the user thinks they are.
137 // The big prolem here is that the course management page is a common page for both editing users and common users and
138 // is only added to the admin tree if the user has permission to edit at the system level.
139 $node = $PAGE->settingsnav
->get('root');
141 $node = $node->get('courses');
143 $node = $node->get('coursemgmt');
147 // The course management page exists so make that active.
148 $node->make_active();
150 // Failing that we'll override the URL, not as accurate and chances are things
151 // won't be 100% correct all the time but should work most times.
152 // A common reason to arrive here is having the management capability within only a particular category (not at system level).
153 navigation_node
::override_active_url(new moodle_url('/course/index.php', array('categoryedit' => 'on')));
156 $PAGE->set_title($title);
157 $PAGE->set_heading($fullname);
158 echo $OUTPUT->header();
159 echo $OUTPUT->heading($strtitle);
161 echo $OUTPUT->footer();