SCORM AICC MDL-30223 invalid call to update_record - very hard to test/reproduce.
[moodle.git] / course / editcategory.php
blob9752cce1713a5614b4d4e47a2647b0f2bde07fdd
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 $itemid = 0; //initalise itemid, as all files in category description has item id 0
18 if ($id) {
19 if (!$category = $DB->get_record('course_categories', array('id' => $id))) {
20 print_error('unknowcategory');
22 $PAGE->set_url('/course/editcategory.php', array('id' => $id));
23 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $id);
24 $PAGE->set_context($categorycontext);
25 require_capability('moodle/category:manage', $categorycontext);
26 $strtitle = get_string('editcategorysettings');
27 $editorcontext = $categorycontext;
28 $title = $strtitle;
29 $fullname = $category->name;
30 } else {
31 $parent = required_param('parent', PARAM_INT);
32 $PAGE->set_url('/course/editcategory.php', array('parent' => $parent));
33 if ($parent) {
34 if (!$DB->record_exists('course_categories', array('id' => $parent))) {
35 print_error('unknowcategory');
37 $context = get_context_instance(CONTEXT_COURSECAT, $parent);
38 } else {
39 $context = get_system_context();
41 $PAGE->set_context($context);
42 $category = new stdClass();
43 $category->id = 0;
44 $category->parent = $parent;
45 require_capability('moodle/category:manage', $context);
46 $strtitle = get_string("addnewcategory");
47 $editorcontext = $context;
48 $itemid = null; //set this explicitly, so files for parent category should not get loaded in draft area.
49 $title = "$SITE->shortname: ".get_string('addnewcategory');
50 $fullname = $SITE->fullname;
53 $PAGE->set_pagelayout('admin');
55 $editoroptions = array(
56 'maxfiles' => EDITOR_UNLIMITED_FILES,
57 'maxbytes' => $CFG->maxbytes,
58 'trusttext' => true,
59 'context' => $editorcontext
61 $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', $itemid);
63 $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions'));
64 $mform->set_data($category);
66 if ($mform->is_cancelled()) {
67 if ($id) {
68 redirect($CFG->wwwroot . '/course/category.php?id=' . $id . '&categoryedit=on');
69 } else if ($parent) {
70 redirect($CFG->wwwroot .'/course/category.php?id=' . $parent . '&categoryedit=on');
71 } else {
72 redirect($CFG->wwwroot .'/course/index.php?categoryedit=on');
74 } else if ($data = $mform->get_data()) {
75 $newcategory = new stdClass();
76 $newcategory->name = $data->name;
77 $newcategory->idnumber = $data->idnumber;
78 $newcategory->description_editor = $data->description_editor;
79 $newcategory->parent = $data->parent; // if $data->parent = 0, the new category will be a top-level category
81 if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
82 $newcategory->theme = $data->theme;
85 if ($id) {
86 // Update an existing category.
87 $newcategory->id = $category->id;
88 if ($newcategory->parent != $category->parent) {
89 // check category manage capability if parent changed
90 require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent));
91 $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent));
92 move_category($newcategory, $parent_cat);
94 } else {
95 // Create a new category.
96 $newcategory->description = $data->description_editor['text'];
97 $newcategory->sortorder = 999;
98 $newcategory->id = $DB->insert_record('course_categories', $newcategory);
99 $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
100 $categorycontext = $newcategory->context;
101 mark_context_dirty($newcategory->context->path);
104 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
105 $DB->update_record('course_categories', $newcategory);
106 fix_course_sortorder();
108 redirect('category.php?id='.$newcategory->id.'&categoryedit=on');
111 $PAGE->set_title($title);
112 $PAGE->set_heading($fullname);
113 echo $OUTPUT->header();
114 echo $OUTPUT->heading($strtitle);
115 $mform->display();
116 echo $OUTPUT->footer();