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
);
17 if (!$category = get_record('course_categories', 'id', $id)) {
18 error("Category not known!");
20 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT
, $id));
21 $strtitle = get_string('editcategorysettings');
23 $parent = required_param('parent', PARAM_INT
);
25 if (!record_exists('course_categories', 'id', $parent)) {
26 error('Unknown parent category ' . $parent);
28 $context = get_context_instance(CONTEXT_COURSECAT
, $parent);
30 $context = get_system_context();
32 $category = new stdClass();
34 $category->parent
= $parent;
35 require_capability('moodle/category:manage', $context);
36 $strtitle = get_string("addnewcategory");
39 $mform = new editcategory_form('editcategory.php', $category);
40 $mform->set_data($category);
42 if ($mform->is_cancelled()) {
44 redirect($CFG->wwwroot
. '/course/category.php?id=' . $id . '&categoryedit=on');
46 redirect($CFG->wwwroot
.'/course/category.php?id=' . $parent . '&categoryedit=on');
48 redirect($CFG->wwwroot
.'/course/index.php?categoryedit=on');
50 } else if ($data = $mform->get_data()) {
51 $newcategory = new stdClass();
52 $newcategory->name
= $data->name
;
53 $newcategory->description
= $data->description
;
54 $newcategory->parent
= $data->parent
; // if $data->parent = 0, the new category will be a top-level category
56 if (isset($data->theme
) && !empty($CFG->allowcategorythemes
)) {
57 $newcategory->theme
= $data->theme
;
61 // Update an existing category.
62 $newcategory->id
= $category->id
;
63 if ($newcategory->parent
!= $category->parent
) {
64 // check category manage capability if parent changed
65 require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent
));
66 $parent_cat = get_record('course_categories', 'id', $newcategory->parent
);
67 move_category($newcategory, $parent_cat);
69 if (!update_record('course_categories', $newcategory)) {
70 error( "Could not update the category '$newcategory->name' ");
72 fix_course_sortorder();
75 // Create a new category.
76 $newcategory->sortorder
= 999;
77 if (!$newcategory->id
= insert_record('course_categories', $newcategory)) {
78 error("Could not insert the new category '$newcategory->name' ");
80 $newcategory->context
= get_context_instance(CONTEXT_COURSECAT
, $newcategory->id
);
81 mark_context_dirty($newcategory->context
->path
);
82 fix_course_sortorder(); // Required to build course_categories.depth and .path.
84 redirect('category.php?id='.$newcategory->id
.'&categoryedit=on');
88 $straddnewcategory = get_string('addnewcategory');
89 $stradministration = get_string('administration');
90 $strcategories = get_string('categories');
94 $navlinks[] = array('name' => $strtitle,
98 $fullname = $category->name
;
100 $navlinks[] = array('name' => $stradministration,
101 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
103 $navlinks[] = array('name' => $strcategories,
104 'link' => 'index.php',
106 $navlinks[] = array('name' => $straddnewcategory,
109 $title = "$SITE->shortname: $straddnewcategory";
110 $fullname = $SITE->fullname
;
113 $navigation = build_navigation($navlinks);
114 print_header($title, $fullname, $navigation, $mform->focus());
115 print_heading($strtitle);