MDL-67264 core_course: Begin set up for Activity chooser
[moodle.git] / course / editcategory.php
CommitLineData
8bdc9cac 1<?php
8486f207
SH
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.
13//
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/>.
16
1f682581 17/**
8ed5dd63 18 * Page for creating or editing course category name/parent/description.
5dc361e1 19 *
8ed5dd63 20 * When called with an id parameter, edits the category with that id.
21 * Otherwise it creates a new category with default parent from the parent
22 * parameter, which may be 0.
8486f207 23 *
5dc361e1 24 * @package core_course
8486f207
SH
25 * @copyright 2007 Nicolas Connault
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1f682581 27 */
28
8ed5dd63 29require_once('../config.php');
6e1d1ee0 30require_once($CFG->dirroot.'/course/lib.php');
1f682581 31
8ed5dd63 32require_login();
1f682581 33
8ed5dd63 34$id = optional_param('id', 0, PARAM_INT);
cec1d814 35
5dc361e1 36$url = new moodle_url('/course/editcategory.php');
8ed5dd63 37if ($id) {
442f12f8 38 $coursecat = core_course_category::get($id, MUST_EXIST, true);
5dc361e1
SH
39 $category = $coursecat->get_db_record();
40 $context = context_coursecat::instance($id);
41
42 $url->param('id', $id);
43 $strtitle = new lang_string('editcategorysettings');
44 $itemid = 0; // Initialise itemid, as all files in category description has item id 0.
f36b47ef 45 $title = $strtitle;
5dc361e1 46 $fullname = $coursecat->get_formatted_name();
d5814f4e 47
8ed5dd63 48} else {
49 $parent = required_param('parent', PARAM_INT);
5dc361e1 50 $url->param('parent', $parent);
8ed5dd63 51 if ($parent) {
5dc361e1 52 $DB->record_exists('course_categories', array('id' => $parent), '*', MUST_EXIST);
9a5e297b 53 $context = context_coursecat::instance($parent);
8ed5dd63 54 } else {
0601e0ee 55 $context = context_system::instance();
8ed5dd63 56 }
d5814f4e 57 navigation_node::override_active_url(new moodle_url('/course/editcategory.php', array('parent' => $parent)));
5dc361e1 58
8ed5dd63 59 $category = new stdClass();
60 $category->id = 0;
61 $category->parent = $parent;
5dc361e1
SH
62 $strtitle = new lang_string("addnewcategory");
63 $itemid = null; // Set this explicitly, so files for parent category should not get loaded in draft area.
f36b47ef
SH
64 $title = "$SITE->shortname: ".get_string('addnewcategory');
65 $fullname = $SITE->fullname;
9c536df7 66}
67
5dc361e1 68require_capability('moodle/category:manage', $context);
f36b47ef 69
5dc361e1
SH
70$PAGE->set_context($context);
71$PAGE->set_url($url);
72$PAGE->set_pagelayout('admin');
73$PAGE->set_title($title);
74$PAGE->set_heading($fullname);
9c536df7 75
5dc361e1
SH
76$mform = new core_course_editcategory_form(null, array(
77 'categoryid' => $id,
78 'parent' => $category->parent,
79 'context' => $context,
80 'itemid' => $itemid
81));
82$mform->set_data(file_prepare_standard_editor(
83 $category,
84 'description',
85 $mform->get_description_editor_options(),
86 $context,
87 'coursecat',
88 'description',
89 $itemid
90));
91
92$manageurl = new moodle_url('/course/management.php');
8ed5dd63 93if ($mform->is_cancelled()) {
94 if ($id) {
5dc361e1 95 $manageurl->param('categoryid', $id);
8ed5dd63 96 } else if ($parent) {
5dc361e1 97 $manageurl->param('categoryid', $parent);
8ed5dd63 98 }
5dc361e1 99 redirect($manageurl);
8ed5dd63 100} else if ($data = $mform->get_data()) {
5dc361e1
SH
101 if (isset($coursecat)) {
102 if ((int)$data->parent !== (int)$coursecat->parent && !$coursecat->can_change_parent($data->parent)) {
9bad61db 103 print_error('cannotmovecategory');
9c536df7 104 }
5dc361e1 105 $coursecat->update($data, $mform->get_description_editor_options());
1f682581 106 } else {
442f12f8 107 $category = core_course_category::create($data, $mform->get_description_editor_options());
1f682581 108 }
5dc361e1
SH
109 $manageurl->param('categoryid', $category->id);
110 redirect($manageurl);
8ed5dd63 111}
1f682581 112
837b38ee 113echo $OUTPUT->header();
7c5286cd 114echo $OUTPUT->heading($strtitle);
1f682581 115$mform->display();
d60c1124 116echo $OUTPUT->footer();