MDL-16553 Assignment, student view: whitespace fix
[moodle.git] / course / editcategory.php
blobcc384ce3a6c90ab281796f93621560916ac0681e
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 // 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();
74 } else {
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');
87 // Print the form
88 $straddnewcategory = get_string('addnewcategory');
89 $stradministration = get_string('administration');
90 $strcategories = get_string('categories');
91 $navlinks = array();
93 if ($id) {
94 $navlinks[] = array('name' => $strtitle,
95 'link' => null,
96 'type' => 'misc');
97 $title = $strtitle;
98 $fullname = $category->name;
99 } else {
100 $navlinks[] = array('name' => $stradministration,
101 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
102 'type' => 'misc');
103 $navlinks[] = array('name' => $strcategories,
104 'link' => 'index.php',
105 'type' => 'misc');
106 $navlinks[] = array('name' => $straddnewcategory,
107 'link' => null,
108 'type' => 'misc');
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);
117 $mform->display();
119 print_footer();