MDL-67264 core_course: Begin set up for Activity chooser
[moodle.git] / course / edit.php
CommitLineData
8bdc9cac 1<?php
df997f84
PS
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
17/**
18 * Edit course settings
19 *
5dc361e1 20 * @package core_course
df997f84
PS
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once('../config.php');
26require_once('lib.php');
27require_once('edit_form.php');
28
5dc361e1
SH
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.
24d0d813
SH
32$returnurl = optional_param('returnurl', '', PARAM_LOCALURL); // A return URL. returnto must also be set to 'url'.
33
34if ($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);
38} else {
c62d077b
SH
39 if (!empty($id)) {
40 $returnurl = new moodle_url($CFG->wwwroot . '/course/view.php', array('id' => $id));
41 } else {
42 $returnurl = new moodle_url($CFG->wwwroot . '/course/');
43 }
44 if ($returnto !== 0) {
45 switch ($returnto) {
46 case 'category':
47 $returnurl = new moodle_url($CFG->wwwroot . '/course/index.php', array('categoryid' => $categoryid));
48 break;
49 case 'catmanage':
50 $returnurl = new moodle_url($CFG->wwwroot . '/course/management.php', array('categoryid' => $categoryid));
51 break;
52 case 'topcatmanage':
53 $returnurl = new moodle_url($CFG->wwwroot . '/course/management.php');
54 break;
55 case 'topcat':
56 $returnurl = new moodle_url($CFG->wwwroot . '/course/');
57 break;
1b276bfc
A
58 case 'pending':
59 $returnurl = new moodle_url($CFG->wwwroot . '/course/pending.php');
60 break;
c62d077b 61 }
24d0d813
SH
62 }
63}
df997f84
PS
64
65$PAGE->set_pagelayout('admin');
b9f4c378
SH
66if ($id) {
67 $pageparams = array('id' => $id);
68} else {
69 $pageparams = array('category' => $categoryid);
736a6551 70}
24d0d813
SH
71if ($returnto !== 0) {
72 $pageparams['returnto'] = $returnto;
73 if ($returnto === 'url' && $returnurl) {
74 $pageparams['returnurl'] = $returnurl;
75 }
76}
736a6551 77$PAGE->set_url('/course/edit.php', $pageparams);
df997f84 78
5dc361e1
SH
79// Basic access control checks.
80if ($id) {
81 // Editing course.
df997f84 82 if ($id == SITEID){
5dc361e1 83 // Don't allow editing of 'site course' using this from.
df997f84 84 print_error('cannoteditsiteform');
fa040aac 85 }
86
7456c967
MG
87 // Login to the course and retrieve also all fields defined by course format.
88 $course = get_course($id);
dba37691 89 require_login($course);
7456c967
MG
90 $course = course_get_format($course)->get_course();
91
df997f84 92 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
9a5e297b 93 $coursecontext = context_course::instance($course->id);
df997f84 94 require_capability('moodle/course:update', $coursecontext);
df997f84 95
5dc361e1
SH
96} else if ($categoryid) {
97 // Creating new course in this category.
df997f84
PS
98 $course = null;
99 require_login();
100 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
9a5e297b 101 $catcontext = context_coursecat::instance($category->id);
dba37691 102 require_capability('moodle/course:create', $catcontext);
dba37691 103 $PAGE->set_context($catcontext);
df997f84
PS
104
105} else {
28af88f3
DNA
106 // Creating new course in default category.
107 $course = null;
df997f84 108 require_login();
28af88f3
DNA
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);
df997f84
PS
113}
114
5dc361e1 115// Prepare course and the editor.
df997f84 116$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
d1f8c1bd 117$overviewfilesoptions = course_overviewfiles_options($course);
df997f84 118if (!empty($course)) {
5dc361e1 119 // Add context for editor.
e9de1cf4 120 $editoroptions['context'] = $coursecontext;
5e95223e 121 $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
64f93798 122 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
d1f8c1bd
MG
123 if ($overviewfilesoptions) {
124 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
125 }
5e69314d 126
5dc361e1 127 // Inject current aliases.
c52551dc
PS
128 $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
129 foreach($aliases as $alias) {
130 $course->{'role_'.$alias->roleid} = $alias->name;
131 }
132
0d1e5456 133 // Populate course tags.
74fa9f76 134 $course->tags = core_tag_tag::get_item_tags_array('core', 'course', $course->id);
0d1e5456 135
df997f84 136} else {
5dc361e1 137 // Editor should respect category context if course context is not set.
dedb69a2 138 $editoroptions['context'] = $catcontext;
5e95223e 139 $editoroptions['subdirs'] = 0;
64f93798 140 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
d1f8c1bd
MG
141 if ($overviewfilesoptions) {
142 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
143 }
df997f84 144}
f9903ed0 145
5dc361e1 146// First create the form.
24d0d813
SH
147$args = array(
148 'course' => $course,
149 'category' => $category,
150 'editoroptions' => $editoroptions,
151 'returnto' => $returnto,
152 'returnurl' => $returnurl
153);
154$editform = new course_edit_form(null, $args);
df997f84 155if ($editform->is_cancelled()) {
24d0d813
SH
156 // The form has been cancelled, take them back to what ever the return to is.
157 redirect($returnurl);
df997f84 158} else if ($data = $editform->get_data()) {
5dc361e1 159 // Process data if submitted.
df997f84 160 if (empty($course->id)) {
5dc361e1 161 // In creating the course.
df997f84 162 $course = create_course($data, $editoroptions);
bfefa87e 163
5dc361e1 164 // Get the context of the newly created course.
9a5e297b 165 $context = context_course::instance($course->id, MUST_EXIST);
bfefa87e 166
df997f84 167 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
5dc361e1 168 // Deal with course creators - enrol them internally with default role.
5d69038b 169 // Note: This does not respect capabilities, the creator will be assigned the default role.
170 // This is an expected behaviour. See MDL-66683 for further details.
171 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
eef400a4 172 }
24d0d813
SH
173
174 // The URL to take them to if they chose save and display.
175 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
176
177 // If they choose to save and display, and they are not enrolled take them to the enrolments page instead.
178 if (!is_enrolled($context) && isset($data->saveanddisplay)) {
5dc361e1 179 // Redirect to manual enrolment page if possible.
eef400a4
PS
180 $instances = enrol_get_instances($course->id, true);
181 foreach($instances as $instance) {
182 if ($plugin = enrol_get_plugin($instance->enrol)) {
183 if ($plugin->get_manual_enrol_link($instance)) {
5dc361e1 184 // We know that the ajax enrol UI will have an option to enrol.
4b50765b 185 $courseurl = new moodle_url('/user/index.php', array('id' => $course->id, 'newcourse' => 1));
24d0d813 186 break;
eef400a4 187 }
df997f84 188 }
73c24ef8 189 }
df997f84 190 }
df997f84 191 } else {
5dc361e1 192 // Save any changes to the files used in the editor.
df997f84 193 update_course($data, $editoroptions);
24d0d813
SH
194 // Set the URL to take them too if they choose save and display.
195 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
f9903ed0 196 }
eef400a4 197
24d0d813
SH
198 if (isset($data->saveanddisplay)) {
199 // Redirect user to newly created/updated course.
200 redirect($courseurl);
201 } else {
202 // Save and return. Take them back to wherever.
203 redirect($returnurl);
204 }
df997f84 205}
f9903ed0 206
5dc361e1 207// Print the form.
6049fb78 208
df997f84 209$site = get_site();
f374fb10 210
df997f84
PS
211$streditcoursesettings = get_string("editcoursesettings");
212$straddnewcourse = get_string("addnewcourse");
213$stradministration = get_string("administration");
214$strcategories = get_string("categories");
af978a6d 215
df997f84 216if (!empty($course->id)) {
b9f4c378
SH
217 // Navigation note: The user is editing a course, the course will exist within the navigation and settings.
218 // The navigation will automatically find the Edit settings page under course navigation.
219 $pagedesc = $streditcoursesettings;
df997f84
PS
220 $title = $streditcoursesettings;
221 $fullname = $course->fullname;
222} else {
b9f4c378
SH
223 // The user is adding a course, this page isn't presented in the site navigation/admin.
224 // Adding a new course is part of course category management territory.
225 // We'd prefer to use the management interface URL without args.
226 $managementurl = new moodle_url('/course/management.php');
227 // These are the caps required in order to see the management interface.
228 $managementcaps = array('moodle/category:manage', 'moodle/course:create');
229 if ($categoryid && !has_any_capability($managementcaps, context_system::instance())) {
230 // If the user doesn't have either manage caps then they can only manage within the given category.
231 $managementurl->param('categoryid', $categoryid);
232 }
233 // Because the course category management interfaces are buried in the admin tree and that is loaded by ajax
234 // we need to manually tell the navigation we need it loaded. The second arg does this.
235 navigation_node::override_active_url($managementurl, true);
236
237 $pagedesc = $straddnewcourse;
df997f84
PS
238 $title = "$site->shortname: $straddnewcourse";
239 $fullname = $site->fullname;
b9f4c378 240 $PAGE->navbar->add($pagedesc);
df997f84
PS
241}
242
243$PAGE->set_title($title);
244$PAGE->set_heading($fullname);
f9903ed0 245
df997f84 246echo $OUTPUT->header();
b9f4c378 247echo $OUTPUT->heading($pagedesc);
f89033b1 248
df997f84 249$editform->display();
2378265e 250
df997f84 251echo $OUTPUT->footer();