Moodle release 2.8.1
[moodle.git] / course / edit.php
blobf21ca884276a5d3f23a858ac82836c0b3b7cb83c
1 <?php
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.
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/>.
17 /**
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.
33 $PAGE->set_pagelayout('admin');
34 if ($id) {
35 $pageparams = array('id' => $id);
36 } else {
37 $pageparams = array('category' => $categoryid);
39 $PAGE->set_url('/course/edit.php', $pageparams);
41 // Basic access control checks.
42 if ($id) {
43 // Editing course.
44 if ($id == SITEID){
45 // Don't allow editing of 'site course' using this from.
46 print_error('cannoteditsiteform');
49 // Login to the course and retrieve also all fields defined by course format.
50 $course = get_course($id);
51 require_login($course);
52 $course = course_get_format($course)->get_course();
54 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
55 $coursecontext = context_course::instance($course->id);
56 require_capability('moodle/course:update', $coursecontext);
58 } else if ($categoryid) {
59 // Creating new course in this category.
60 $course = null;
61 require_login();
62 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
63 $catcontext = context_coursecat::instance($category->id);
64 require_capability('moodle/course:create', $catcontext);
65 $PAGE->set_context($catcontext);
67 } else {
68 require_login();
69 print_error('needcoursecategroyid');
72 // Prepare course and the editor.
73 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
74 $overviewfilesoptions = course_overviewfiles_options($course);
75 if (!empty($course)) {
76 // Add context for editor.
77 $editoroptions['context'] = $coursecontext;
78 $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
79 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
80 if ($overviewfilesoptions) {
81 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
84 // Inject current aliases.
85 $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
86 foreach($aliases as $alias) {
87 $course->{'role_'.$alias->roleid} = $alias->name;
90 } else {
91 // Editor should respect category context if course context is not set.
92 $editoroptions['context'] = $catcontext;
93 $editoroptions['subdirs'] = 0;
94 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
95 if ($overviewfilesoptions) {
96 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
100 // First create the form.
101 $editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto));
102 if ($editform->is_cancelled()) {
103 switch ($returnto) {
104 case 'category':
105 $url = new moodle_url($CFG->wwwroot.'/course/index.php', array('categoryid' => $categoryid));
106 break;
107 case 'catmanage':
108 $url = new moodle_url($CFG->wwwroot.'/course/management.php', array('categoryid' => $categoryid));
109 break;
110 case 'topcatmanage':
111 $url = new moodle_url($CFG->wwwroot.'/course/management.php');
112 break;
113 case 'topcat':
114 $url = new moodle_url($CFG->wwwroot.'/course/');
115 break;
116 default:
117 if (!empty($course->id)) {
118 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
119 } else {
120 $url = new moodle_url($CFG->wwwroot.'/course/');
122 break;
124 redirect($url);
126 } else if ($data = $editform->get_data()) {
127 // Process data if submitted.
128 if (empty($course->id)) {
129 // In creating the course.
130 $course = create_course($data, $editoroptions);
132 // Get the context of the newly created course.
133 $context = context_course::instance($course->id, MUST_EXIST);
135 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
136 // Deal with course creators - enrol them internally with default role.
137 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
139 if (!is_enrolled($context)) {
140 // Redirect to manual enrolment page if possible.
141 $instances = enrol_get_instances($course->id, true);
142 foreach($instances as $instance) {
143 if ($plugin = enrol_get_plugin($instance->enrol)) {
144 if ($plugin->get_manual_enrol_link($instance)) {
145 // We know that the ajax enrol UI will have an option to enrol.
146 redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
151 } else {
152 // Save any changes to the files used in the editor.
153 update_course($data, $editoroptions);
156 // Redirect user to newly created/updated course.
157 redirect(new moodle_url('/course/view.php', array('id' => $course->id)));
160 // Print the form.
162 $site = get_site();
164 $streditcoursesettings = get_string("editcoursesettings");
165 $straddnewcourse = get_string("addnewcourse");
166 $stradministration = get_string("administration");
167 $strcategories = get_string("categories");
169 if (!empty($course->id)) {
170 // Navigation note: The user is editing a course, the course will exist within the navigation and settings.
171 // The navigation will automatically find the Edit settings page under course navigation.
172 $pagedesc = $streditcoursesettings;
173 $title = $streditcoursesettings;
174 $fullname = $course->fullname;
175 } else {
176 // The user is adding a course, this page isn't presented in the site navigation/admin.
177 // Adding a new course is part of course category management territory.
178 // We'd prefer to use the management interface URL without args.
179 $managementurl = new moodle_url('/course/management.php');
180 // These are the caps required in order to see the management interface.
181 $managementcaps = array('moodle/category:manage', 'moodle/course:create');
182 if ($categoryid && !has_any_capability($managementcaps, context_system::instance())) {
183 // If the user doesn't have either manage caps then they can only manage within the given category.
184 $managementurl->param('categoryid', $categoryid);
186 // Because the course category management interfaces are buried in the admin tree and that is loaded by ajax
187 // we need to manually tell the navigation we need it loaded. The second arg does this.
188 navigation_node::override_active_url($managementurl, true);
190 $pagedesc = $straddnewcourse;
191 $title = "$site->shortname: $straddnewcourse";
192 $fullname = $site->fullname;
193 $PAGE->navbar->add($pagedesc);
196 $PAGE->set_title($title);
197 $PAGE->set_heading($fullname);
199 echo $OUTPUT->header();
200 echo $OUTPUT->heading($pagedesc);
202 $editform->display();
204 echo $OUTPUT->footer();