3 * Page for creating or editing course category name/parent/description.
4 * When called with an id parameter, edits the category with that id.
5 * Otherwise it creates a new category with default parent from the parent
6 * parameter, which may be 0.
9 require_once('../config.php');
10 require_once('lib.php');
11 require_once('editcategory_form.php');
15 $id = optional_param('id', 0, PARAM_INT
);
16 $itemid = 0; //initalise itemid, as all files in category description has item id 0
19 if (!$category = $DB->get_record('course_categories', array('id' => $id))) {
20 print_error('unknowcategory');
22 $PAGE->set_url('/course/editcategory.php', array('id' => $id));
23 $categorycontext = get_context_instance(CONTEXT_COURSECAT
, $id);
24 $PAGE->set_context($categorycontext);
25 require_capability('moodle/category:manage', $categorycontext);
26 $strtitle = get_string('editcategorysettings');
27 $editorcontext = $categorycontext;
29 $fullname = $category->name
;
31 $parent = required_param('parent', PARAM_INT
);
32 $PAGE->set_url('/course/editcategory.php', array('parent' => $parent));
34 if (!$DB->record_exists('course_categories', array('id' => $parent))) {
35 print_error('unknowcategory');
37 $context = get_context_instance(CONTEXT_COURSECAT
, $parent);
39 $context = get_system_context();
41 $PAGE->set_context($context);
42 $category = new stdClass();
44 $category->parent
= $parent;
45 require_capability('moodle/category:manage', $context);
46 $strtitle = get_string("addnewcategory");
47 $editorcontext = $context;
48 $itemid = null; //set this explicitly, so files for parent category should not get loaded in draft area.
49 $title = "$SITE->shortname: ".get_string('addnewcategory');
50 $fullname = $SITE->fullname
;
53 $PAGE->set_pagelayout('admin');
55 $editoroptions = array(
56 'maxfiles' => EDITOR_UNLIMITED_FILES
,
57 'maxbytes' => $CFG->maxbytes
,
59 'context' => $editorcontext
61 $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', $itemid);
63 $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions'));
64 $mform->set_data($category);
66 if ($mform->is_cancelled()) {
68 redirect($CFG->wwwroot
. '/course/category.php?id=' . $id . '&categoryedit=on');
70 redirect($CFG->wwwroot
.'/course/category.php?id=' . $parent . '&categoryedit=on');
72 redirect($CFG->wwwroot
.'/course/index.php?categoryedit=on');
74 } else if ($data = $mform->get_data()) {
75 $newcategory = new stdClass();
76 $newcategory->name
= $data->name
;
77 $newcategory->description_editor
= $data->description_editor
;
78 $newcategory->parent
= $data->parent
; // if $data->parent = 0, the new category will be a top-level category
80 if (isset($data->theme
) && !empty($CFG->allowcategorythemes
)) {
81 $newcategory->theme
= $data->theme
;
85 // Update an existing category.
86 $newcategory->id
= $category->id
;
87 if ($newcategory->parent
!= $category->parent
) {
88 // check category manage capability if parent changed
89 require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent
));
90 $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent
));
91 move_category($newcategory, $parent_cat);
94 // Create a new category.
95 $newcategory->description
= $data->description_editor
['text'];
96 $newcategory->sortorder
= 999;
97 $newcategory->id
= $DB->insert_record('course_categories', $newcategory);
98 $newcategory->context
= get_context_instance(CONTEXT_COURSECAT
, $newcategory->id
);
99 $categorycontext = $newcategory->context
;
100 mark_context_dirty($newcategory->context
->path
);
103 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
104 $DB->update_record('course_categories', $newcategory);
105 fix_course_sortorder();
107 redirect('category.php?id='.$newcategory->id
.'&categoryedit=on');
110 $PAGE->set_title($title);
111 $PAGE->set_heading($fullname);
112 echo $OUTPUT->header();
113 echo $OUTPUT->heading($strtitle);
115 echo $OUTPUT->footer();