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 $parent_cat = get_record('course_categories', 'id', $newcategory->parent
);
65 move_category($newcategory, $parent_cat);
67 if (!update_record('course_categories', $newcategory)) {
68 error( "Could not update the category '$newcategory->name' ");
70 fix_course_sortorder();
73 // Create a new category.
74 $newcategory->sortorder
= 999;
75 if (!$newcategory->id
= insert_record('course_categories', $newcategory)) {
76 error("Could not insert the new category '$newcategory->name' ");
78 $newcategory->context
= get_context_instance(CONTEXT_COURSECAT
, $newcategory->id
);
79 mark_context_dirty($newcategory->context
->path
);
80 fix_course_sortorder(); // Required to build course_categories.depth and .path.
82 redirect('category.php?id='.$newcategory->id
.'&categoryedit=on');
86 $straddnewcategory = get_string('addnewcategory');
87 $stradministration = get_string('administration');
88 $strcategories = get_string('categories');
92 $navlinks[] = array('name' => $strtitle,
96 $fullname = $category->name
;
98 $navlinks[] = array('name' => $stradministration,
99 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
101 $navlinks[] = array('name' => $strcategories,
102 'link' => 'index.php',
104 $navlinks[] = array('name' => $straddnewcategory,
107 $title = "$SITE->shortname: $straddnewcategory";
108 $fullname = $SITE->fullname
;
111 $navigation = build_navigation($navlinks);
112 print_header($title, $fullname, $navigation, $mform->focus());
113 print_heading($strtitle);