Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / course / editcategory.php
blob355aca195ee0b2e3c5d9c512881ad8f91a0e9972
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Page for creating or editing course category name/parent/description.
19 * When called with an id parameter, edits the category with that id.
20 * Otherwise it creates a new category with default parent from the parent
21 * parameter, which may be 0.
23 * @package core
24 * @subpackage course
25 * @copyright 2007 Nicolas Connault
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once('../config.php');
30 require_once($CFG->dirroot.'/course/lib.php');
31 require_once($CFG->dirroot.'/course/editcategory_form.php');
32 require_once($CFG->libdir.'/coursecatlib.php');
34 require_login();
36 $id = optional_param('id', 0, PARAM_INT);
37 $itemid = 0; //initalise itemid, as all files in category description has item id 0
39 if ($id) {
40 if (!$category = $DB->get_record('course_categories', array('id' => $id))) {
41 print_error('unknowcategory');
43 $PAGE->set_url('/course/editcategory.php', array('id' => $id));
44 $categorycontext = context_coursecat::instance($id);
45 $PAGE->set_context($categorycontext);
46 require_capability('moodle/category:manage', $categorycontext);
47 $strtitle = get_string('editcategorysettings');
48 $editorcontext = $categorycontext;
49 $title = $strtitle;
50 $fullname = $category->name;
51 } else {
52 $parent = required_param('parent', PARAM_INT);
53 $PAGE->set_url('/course/editcategory.php', array('parent' => $parent));
54 if ($parent) {
55 if (!$DB->record_exists('course_categories', array('id' => $parent))) {
56 print_error('unknowcategory');
58 $context = context_coursecat::instance($parent);
59 } else {
60 $context = get_system_context();
62 $PAGE->set_context($context);
63 $category = new stdClass();
64 $category->id = 0;
65 $category->parent = $parent;
66 require_capability('moodle/category:manage', $context);
67 $strtitle = get_string("addnewcategory");
68 $editorcontext = $context;
69 $itemid = null; //set this explicitly, so files for parent category should not get loaded in draft area.
70 $title = "$SITE->shortname: ".get_string('addnewcategory');
71 $fullname = $SITE->fullname;
74 $PAGE->set_pagelayout('admin');
76 $editoroptions = array(
77 'maxfiles' => EDITOR_UNLIMITED_FILES,
78 'maxbytes' => $CFG->maxbytes,
79 'trusttext' => true,
80 'context' => $editorcontext
82 $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', $itemid);
84 $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions'));
85 $mform->set_data($category);
87 if ($mform->is_cancelled()) {
88 if ($id) {
89 redirect($CFG->wwwroot . '/course/manage.php?id=' . $id);
90 } else if ($parent) {
91 redirect($CFG->wwwroot .'/course/manage.php?id=' . $parent);
92 } else {
93 redirect($CFG->wwwroot .'/course/manage.php');
95 } else if ($data = $mform->get_data()) {
96 if ($id) {
97 $newcategory = coursecat::get($id);
98 if ($data->parent != $category->parent && !$newcategory->can_change_parent($data->parent)) {
99 print_error('cannotmovecategory');
101 $newcategory->update($data, $editoroptions);
102 } else {
103 $newcategory = coursecat::create($data, $editoroptions);
106 redirect('manage.php?id='.$newcategory->id);
109 // Page "Add new category" (with "Top" as a parent) does not exist in navigation.
110 // We pretend we are on course management page.
111 if (empty($id) && empty($parent)) {
112 navigation_node::override_active_url(new moodle_url('/course/manage.php'));
115 $PAGE->set_title($title);
116 $PAGE->set_heading($fullname);
117 echo $OUTPUT->header();
118 echo $OUTPUT->heading($strtitle);
119 $mform->display();
120 echo $OUTPUT->footer();