2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Edit course settings
20 * @package core_course
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once('lib.php');
27 require_once('edit_form.php');
29 $id = optional_param('id', 0, PARAM_INT
); // Course id.
30 $categoryid = optional_param('category', 0, PARAM_INT
); // Course category - can be changed in edit form.
31 $returnto = optional_param('returnto', 0, PARAM_ALPHANUM
); // Generic navigation return page switch.
32 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL
); // A return URL. returnto must also be set to 'url'.
34 if ($returnto === 'url' && confirm_sesskey() && $returnurl) {
35 // If returnto is 'url' then $returnurl may be used as the destination to return to after saving or cancelling.
36 // Sesskey must be specified, and would be set by the form anyway.
37 $returnurl = new moodle_url($returnurl);
40 $returnurl = new moodle_url($CFG->wwwroot
. '/course/view.php', array('id' => $id));
42 $returnurl = new moodle_url($CFG->wwwroot
. '/course/');
44 if ($returnto !== 0) {
47 $returnurl = new moodle_url($CFG->wwwroot
. '/course/index.php', array('categoryid' => $categoryid));
50 $returnurl = new moodle_url($CFG->wwwroot
. '/course/management.php', array('categoryid' => $categoryid));
53 $returnurl = new moodle_url($CFG->wwwroot
. '/course/management.php');
56 $returnurl = new moodle_url($CFG->wwwroot
. '/course/');
59 $returnurl = new moodle_url($CFG->wwwroot
. '/course/pending.php');
65 $PAGE->set_pagelayout('admin');
67 $pageparams = array('id' => $id);
69 $pageparams = array('category' => $categoryid);
71 if ($returnto !== 0) {
72 $pageparams['returnto'] = $returnto;
73 if ($returnto === 'url' && $returnurl) {
74 $pageparams['returnurl'] = $returnurl;
77 $PAGE->set_url('/course/edit.php', $pageparams);
79 // Basic access control checks.
83 // Don't allow editing of 'site course' using this from.
84 throw new \
moodle_exception('cannoteditsiteform');
87 // Login to the course and retrieve also all fields defined by course format.
88 $course = get_course($id);
89 require_login($course);
90 $course = course_get_format($course)->get_course();
92 $category = $DB->get_record('course_categories', array('id'=>$course->category
), '*', MUST_EXIST
);
93 $coursecontext = context_course
::instance($course->id
);
94 require_capability('moodle/course:update', $coursecontext);
96 } else if ($categoryid) {
97 // Creating new course in this category.
100 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST
);
101 $catcontext = context_coursecat
::instance($category->id
);
102 require_capability('moodle/course:create', $catcontext);
103 $PAGE->set_context($catcontext);
106 // Creating new course in default category.
109 $category = core_course_category
::get_default();
110 $catcontext = context_coursecat
::instance($category->id
);
111 require_capability('moodle/course:create', $catcontext);
112 $PAGE->set_context($catcontext);
115 // We are adding a new course and have a category context.
116 if (isset($catcontext)) {
117 $PAGE->set_secondary_active_tab('categorymain');
120 // Prepare course and the editor.
121 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES
, 'maxbytes'=>$CFG->maxbytes
, 'trusttext'=>false, 'noclean'=>true);
122 $overviewfilesoptions = course_overviewfiles_options($course);
123 if (!empty($course)) {
124 // Add context for editor.
125 $editoroptions['context'] = $coursecontext;
126 $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
127 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
128 if ($overviewfilesoptions) {
129 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
132 // Inject current aliases.
133 $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id
));
134 foreach($aliases as $alias) {
135 $course->{'role_'.$alias->roleid
} = $alias->name
;
138 // Populate course tags.
139 $course->tags
= core_tag_tag
::get_item_tags_array('core', 'course', $course->id
);
142 // Editor should respect category context if course context is not set.
143 $editoroptions['context'] = $catcontext;
144 $editoroptions['subdirs'] = 0;
145 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
146 if ($overviewfilesoptions) {
147 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
151 // First create the form.
154 'category' => $category,
155 'editoroptions' => $editoroptions,
156 'returnto' => $returnto,
157 'returnurl' => $returnurl
159 $editform = new course_edit_form(null, $args);
160 if ($editform->is_cancelled()) {
161 // The form has been cancelled, take them back to what ever the return to is.
162 redirect($returnurl);
163 } else if ($data = $editform->get_data()) {
164 // Process data if submitted.
165 if (empty($course->id
)) {
166 // In creating the course.
167 $course = create_course($data, $editoroptions);
169 // Get the context of the newly created course.
170 $context = context_course
::instance($course->id
, MUST_EXIST
);
172 // Admins have all capabilities, so is_viewing is returning true for admins.
173 // We are checking 'enroladminnewcourse' setting to decide to enrol them or not.
174 if (is_siteadmin($USER->id
)) {
175 $enroluser = $CFG->enroladminnewcourse
;
177 $enroluser = !is_viewing($context, null, 'moodle/role:assign');
180 if (!empty($CFG->creatornewroleid
) and $enroluser and !is_enrolled($context, null, 'moodle/role:assign')) {
181 // Deal with course creators - enrol them internally with default role.
182 // Note: This does not respect capabilities, the creator will be assigned the default role.
183 // This is an expected behaviour. See MDL-66683 for further details.
184 enrol_try_internal_enrol($course->id
, $USER->id
, $CFG->creatornewroleid
);
187 // The URL to take them to if they chose save and display.
188 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id
));
190 // Save any changes to the files used in the editor.
191 update_course($data, $editoroptions);
192 // Set the URL to take them too if they choose save and display.
193 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id
));
196 if (isset($data->saveanddisplay
)) {
197 // Redirect user to newly created/updated course.
198 redirect($courseurl);
200 // Save and return. Take them back to wherever.
201 redirect($returnurl);
209 $streditcoursesettings = get_string("editcoursesettings");
210 $straddnewcourse = get_string("addnewcourse");
211 $stradministration = get_string("administration");
212 $strcategories = get_string("categories");
214 if (!empty($course->id
)) {
215 // Navigation note: The user is editing a course, the course will exist within the navigation and settings.
216 // The navigation will automatically find the Edit settings page under course navigation.
217 $pagedesc = $streditcoursesettings;
218 $title = $streditcoursesettings;
219 $fullname = $course->fullname
;
221 // The user is adding a course, this page isn't presented in the site navigation/admin.
222 // Adding a new course is part of course category management territory.
223 // We'd prefer to use the management interface URL without args.
224 $managementurl = new moodle_url('/course/management.php');
225 // These are the caps required in order to see the management interface.
226 $managementcaps = array('moodle/category:manage', 'moodle/course:create');
227 if ($categoryid && !has_any_capability($managementcaps, context_system
::instance())) {
228 // If the user doesn't have either manage caps then they can only manage within the given category.
229 $managementurl->param('categoryid', $categoryid);
231 // Because the course category interfaces are buried in the admin tree and that is loaded by ajax
232 // we need to manually tell the navigation we need it loaded. The second arg does this.
233 navigation_node
::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $category->id
]), true);
234 $PAGE->set_primary_active_tab('home');
235 $PAGE->navbar
->add(get_string('coursemgmt', 'admin'), $managementurl);
237 $pagedesc = $straddnewcourse;
238 $title = "$site->shortname: $straddnewcourse";
239 $fullname = format_string($category->name
);
240 $PAGE->navbar
->add($pagedesc);
243 $PAGE->set_title($title);
244 $PAGE->add_body_class('limitedwidth');
245 $PAGE->set_heading($fullname);
247 echo $OUTPUT->header();
248 echo $OUTPUT->heading($pagedesc);
250 $editform->display();
252 echo $OUTPUT->footer();