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->idnumber
= $data->idnumber
;
78 $newcategory->description_editor
= $data->description_editor
;
79 $newcategory->parent
= $data->parent
; // if $data->parent = 0, the new category will be a top-level category
81 if (isset($data->theme
) && !empty($CFG->allowcategorythemes
)) {
82 $newcategory->theme
= $data->theme
;
86 // Update an existing category.
87 $newcategory->id
= $category->id
;
88 if ($newcategory->parent
!= $category->parent
) {
89 // check category manage capability if parent changed
90 require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent
));
91 $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent
));
92 move_category($newcategory, $parent_cat);
95 // Create a new category.
96 $newcategory->description
= $data->description_editor
['text'];
97 $newcategory->sortorder
= 999;
98 $newcategory->id
= $DB->insert_record('course_categories', $newcategory);
99 $newcategory->context
= get_context_instance(CONTEXT_COURSECAT
, $newcategory->id
);
100 $categorycontext = $newcategory->context
;
101 mark_context_dirty($newcategory->context
->path
);
104 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
105 $DB->update_record('course_categories', $newcategory);
106 fix_course_sortorder();
108 redirect('category.php?id='.$newcategory->id
.'&categoryedit=on');
111 $PAGE->set_title($title);
112 $PAGE->set_heading($fullname);
113 echo $OUTPUT->header();
114 echo $OUTPUT->heading($strtitle);
116 echo $OUTPUT->footer();