Merge branch 'wip-MDL-26781' of git://github.com/sammarshallou/moodle
[moodle.git] / course / editcategory.php
blob3cff561935cb46f74d34e5958dcc0bfa520a6715
1 <?php
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 = $DB->get_record('course_categories', array('id' => $id))) {
18 print_error('unknowcategory');
20 $PAGE->set_url('/course/editcategory.php', array('id' => $id));
21 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $id);
22 $PAGE->set_context($categorycontext);
23 require_capability('moodle/category:manage', $categorycontext);
24 $strtitle = get_string('editcategorysettings');
25 $editorcontext = $categorycontext;
26 $title = $strtitle;
27 $fullname = $category->name;
28 } else {
29 $parent = required_param('parent', PARAM_INT);
30 $PAGE->set_url('/course/editcategory.php', array('parent' => $parent));
31 if ($parent) {
32 if (!$DB->record_exists('course_categories', array('id' => $parent))) {
33 print_error('unknowcategory');
35 $context = get_context_instance(CONTEXT_COURSECAT, $parent);
36 } else {
37 $context = get_system_context();
39 $PAGE->set_context($context);
40 $category = new stdClass();
41 $category->id = 0;
42 $category->parent = $parent;
43 require_capability('moodle/category:manage', $context);
44 $strtitle = get_string("addnewcategory");
45 $editorcontext = null;
46 $title = "$SITE->shortname: ".get_string('addnewcategory');
47 $fullname = $SITE->fullname;
50 $PAGE->set_pagelayout('admin');
52 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true);
53 $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', 0);
55 $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions'));
56 $mform->set_data($category);
58 if ($mform->is_cancelled()) {
59 if ($id) {
60 redirect($CFG->wwwroot . '/course/category.php?id=' . $id . '&categoryedit=on');
61 } else if ($parent) {
62 redirect($CFG->wwwroot .'/course/category.php?id=' . $parent . '&categoryedit=on');
63 } else {
64 redirect($CFG->wwwroot .'/course/index.php?categoryedit=on');
66 } else if ($data = $mform->get_data()) {
67 $newcategory = new stdClass();
68 $newcategory->name = $data->name;
69 $newcategory->description_editor = $data->description_editor;
70 $newcategory->parent = $data->parent; // if $data->parent = 0, the new category will be a top-level category
72 if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
73 $newcategory->theme = $data->theme;
76 if ($id) {
77 // Update an existing category.
78 $newcategory->id = $category->id;
79 if ($newcategory->parent != $category->parent) {
80 // check category manage capability if parent changed
81 require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent));
82 $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent));
83 move_category($newcategory, $parent_cat);
85 } else {
86 // Create a new category.
87 $newcategory->description = $data->description_editor['text'];
88 $newcategory->sortorder = 999;
89 $newcategory->id = $DB->insert_record('course_categories', $newcategory);
90 $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
91 $categorycontext = $newcategory->context;
92 mark_context_dirty($newcategory->context->path);
95 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
96 $DB->update_record('course_categories', $newcategory);
97 fix_course_sortorder();
99 redirect('category.php?id='.$newcategory->id.'&categoryedit=on');
102 $PAGE->set_title($title);
103 $PAGE->set_heading($fullname);
104 echo $OUTPUT->header();
105 echo $OUTPUT->heading($strtitle);
106 $mform->display();
107 echo $OUTPUT->footer();