~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to course/editcategory.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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