Updated the 19 build version to 20100904
[moodle.git] / course / editcategory.php
blobe0b940dc4beb06074db48c7651bce7f3cd996768
1 <?php // $Id$
2 /**
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.
7 */
9 require_once('../config.php');
10 require_once('lib.php');
11 require_once('editcategory_form.php');
13 require_login();
15 $id = optional_param('id', 0, PARAM_INT);
16 if ($id) {
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');
22 } else {
23 $parent = required_param('parent', PARAM_INT);
24 if ($parent) {
25 if (!record_exists('course_categories', 'id', $parent)) {
26 error('Unknown parent category ' . $parent);
28 $context = get_context_instance(CONTEXT_COURSECAT, $parent);
29 } else {
30 $context = get_system_context();
32 $category = new stdClass();
33 $category->id = 0;
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()) {
43 if ($id) {
44 redirect($CFG->wwwroot . '/course/category.php?id=' . $id . '&categoryedit=on');
45 } else if ($parent) {
46 redirect($CFG->wwwroot .'/course/category.php?id=' . $parent . '&categoryedit=on');
47 } else {
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;
60 if ($id) {
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();
72 } else {
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');
85 // Print the form
86 $straddnewcategory = get_string('addnewcategory');
87 $stradministration = get_string('administration');
88 $strcategories = get_string('categories');
89 $navlinks = array();
91 if ($id) {
92 $navlinks[] = array('name' => $strtitle,
93 'link' => null,
94 'type' => 'misc');
95 $title = $strtitle;
96 $fullname = $category->name;
97 } else {
98 $navlinks[] = array('name' => $stradministration,
99 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
100 'type' => 'misc');
101 $navlinks[] = array('name' => $strcategories,
102 'link' => 'index.php',
103 'type' => 'misc');
104 $navlinks[] = array('name' => $straddnewcategory,
105 'link' => null,
106 'type' => 'misc');
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);
115 $mform->display();
117 print_footer();