MDL-36302 course: Set correct URL on settings page for navigation.
[moodle.git] / course / edit.php
CommitLineData
8bdc9cac 1<?php
f9903ed0 2
df997f84
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Edit course settings
20 *
21 * @package moodlecore
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once('../config.php');
27require_once('lib.php');
28require_once('edit_form.php');
29
30$id = optional_param('id', 0, PARAM_INT); // course id
31$categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
7feebf6f 32$returnto = optional_param('returnto', 0, PARAM_ALPHANUM); // generic navigation return page switch
df997f84
PS
33
34$PAGE->set_pagelayout('admin');
736a6551
DW
35$pageparams = array('id'=>$id);
36if (empty($id)) {
37 $pageparams = array('category'=>$categoryid);
38}
39$PAGE->set_url('/course/edit.php', $pageparams);
df997f84
PS
40
41// basic access control checks
42if ($id) { // editing course
43 if ($id == SITEID){
44 // don't allow editing of 'site course' using this from
45 print_error('cannoteditsiteform');
fa040aac 46 }
47
fc79ede5 48 $course = course_get_format($id)->get_course();
dba37691 49 require_login($course);
df997f84 50 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
9a5e297b 51 $coursecontext = context_course::instance($course->id);
df997f84 52 require_capability('moodle/course:update', $coursecontext);
df997f84
PS
53
54} else if ($categoryid) { // creating new course in this category
55 $course = null;
56 require_login();
57 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
9a5e297b 58 $catcontext = context_coursecat::instance($category->id);
dba37691 59 require_capability('moodle/course:create', $catcontext);
dba37691 60 $PAGE->set_context($catcontext);
df997f84
PS
61
62} else {
63 require_login();
64 print_error('needcoursecategroyid');
65}
66
67// Prepare course and the editor
68$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
69if (!empty($course)) {
e9de1cf4
RT
70 //add context for editor
71 $editoroptions['context'] = $coursecontext;
64f93798 72 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
5e69314d 73
c52551dc
PS
74 // Inject current aliases
75 $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
76 foreach($aliases as $alias) {
77 $course->{'role_'.$alias->roleid} = $alias->name;
78 }
79
df997f84 80} else {
dedb69a2
RT
81 //editor should respect category context if course context is not set.
82 $editoroptions['context'] = $catcontext;
64f93798 83 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
df997f84 84}
f9903ed0 85
df997f84 86// first create the form
7feebf6f 87$editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto));
df997f84 88if ($editform->is_cancelled()) {
7feebf6f 89 switch ($returnto) {
90 case 'category':
91 $url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$categoryid));
92 break;
93 case 'topcat':
94 $url = new moodle_url($CFG->wwwroot.'/course/');
95 break;
96 default:
97 if (!empty($course->id)) {
98 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
99 } else {
100 $url = new moodle_url($CFG->wwwroot.'/course/');
101 }
102 break;
103 }
104 redirect($url);
eef400a4 105
df997f84
PS
106} else if ($data = $editform->get_data()) {
107 // process data if submitted
f9903ed0 108
df997f84
PS
109 if (empty($course->id)) {
110 // In creating the course
111 $course = create_course($data, $editoroptions);
bfefa87e 112
df997f84 113 // Get the context of the newly created course
9a5e297b 114 $context = context_course::instance($course->id, MUST_EXIST);
bfefa87e 115
df997f84 116 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
eef400a4 117 // deal with course creators - enrol them internally with default role
df997f84 118 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
8bdc9cac 119
eef400a4 120 }
9eb7b1cb 121 if (!is_enrolled($context)) {
eef400a4
PS
122 // Redirect to manual enrolment page if possible
123 $instances = enrol_get_instances($course->id, true);
124 foreach($instances as $instance) {
125 if ($plugin = enrol_get_plugin($instance->enrol)) {
126 if ($plugin->get_manual_enrol_link($instance)) {
127 // we know that the ajax enrol UI will have an option to enrol
128 redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
129 }
df997f84 130 }
73c24ef8 131 }
df997f84 132 }
df997f84
PS
133 } else {
134 // Save any changes to the files used in the editor
135 update_course($data, $editoroptions);
f9903ed0 136 }
eef400a4 137
7feebf6f 138 switch ($returnto) {
139 case 'category':
140 case 'topcat': //redirecting to where the new course was created by default.
141 $url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$categoryid));
142 break;
143 default:
144 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
145 break;
146 }
eef400a4 147 redirect($url);
df997f84 148}
f9903ed0 149
f8a5159a 150
df997f84 151// Print the form
6049fb78 152
df997f84 153$site = get_site();
f374fb10 154
df997f84
PS
155$streditcoursesettings = get_string("editcoursesettings");
156$straddnewcourse = get_string("addnewcourse");
157$stradministration = get_string("administration");
158$strcategories = get_string("categories");
af978a6d 159
df997f84
PS
160if (!empty($course->id)) {
161 $PAGE->navbar->add($streditcoursesettings);
162 $title = $streditcoursesettings;
163 $fullname = $course->fullname;
164} else {
165 $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
166 $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
167 $PAGE->navbar->add($straddnewcourse);
168 $title = "$site->shortname: $straddnewcourse";
169 $fullname = $site->fullname;
170}
171
172$PAGE->set_title($title);
173$PAGE->set_heading($fullname);
f9903ed0 174
df997f84
PS
175echo $OUTPUT->header();
176echo $OUTPUT->heading($streditcoursesettings);
f89033b1 177
df997f84 178$editform->display();
2378265e 179
df997f84 180echo $OUTPUT->footer();
f9903ed0 181