Merge branch 'MDL-44806-master' of git://github.com/phalacee/moodle
[moodle.git] / course / edit.php
blobb88ab774f765cd915ef5fa0aad3f738b8d41ad62
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 $pageparams = array('id'=>$id);
35 if (empty($id)) {
36 $pageparams = array('category'=>$categoryid);
38 $PAGE->set_url('/course/edit.php', $pageparams);
40 // Basic access control checks.
41 if ($id) {
42 // Editing course.
43 if ($id == SITEID){
44 // Don't allow editing of 'site course' using this from.
45 print_error('cannoteditsiteform');
48 // Login to the course and retrieve also all fields defined by course format.
49 $course = get_course($id);
50 require_login($course);
51 $course = course_get_format($course)->get_course();
53 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
54 $coursecontext = context_course::instance($course->id);
55 require_capability('moodle/course:update', $coursecontext);
57 } else if ($categoryid) {
58 // Creating new course in this category.
59 $course = null;
60 require_login();
61 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
62 $catcontext = context_coursecat::instance($category->id);
63 require_capability('moodle/course:create', $catcontext);
64 $PAGE->set_context($catcontext);
66 } else {
67 require_login();
68 print_error('needcoursecategroyid');
71 // Prepare course and the editor.
72 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
73 $overviewfilesoptions = course_overviewfiles_options($course);
74 if (!empty($course)) {
75 // Add context for editor.
76 $editoroptions['context'] = $coursecontext;
77 $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
78 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
79 if ($overviewfilesoptions) {
80 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
83 // Inject current aliases.
84 $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
85 foreach($aliases as $alias) {
86 $course->{'role_'.$alias->roleid} = $alias->name;
89 } else {
90 // Editor should respect category context if course context is not set.
91 $editoroptions['context'] = $catcontext;
92 $editoroptions['subdirs'] = 0;
93 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
94 if ($overviewfilesoptions) {
95 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
99 // First create the form.
100 $editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto));
101 if ($editform->is_cancelled()) {
102 switch ($returnto) {
103 case 'category':
104 $url = new moodle_url($CFG->wwwroot.'/course/index.php', array('categoryid' => $categoryid));
105 break;
106 case 'catmanage':
107 $url = new moodle_url($CFG->wwwroot.'/course/management.php', array('categoryid' => $categoryid));
108 break;
109 case 'topcatmanage':
110 $url = new moodle_url($CFG->wwwroot.'/course/management.php');
111 break;
112 case 'topcat':
113 $url = new moodle_url($CFG->wwwroot.'/course/');
114 break;
115 default:
116 if (!empty($course->id)) {
117 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
118 } else {
119 $url = new moodle_url($CFG->wwwroot.'/course/');
121 break;
123 redirect($url);
125 } else if ($data = $editform->get_data()) {
126 // Process data if submitted.
127 if (empty($course->id)) {
128 // In creating the course.
129 $course = create_course($data, $editoroptions);
131 // Get the context of the newly created course.
132 $context = context_course::instance($course->id, MUST_EXIST);
134 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
135 // Deal with course creators - enrol them internally with default role.
136 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
138 if (!is_enrolled($context)) {
139 // Redirect to manual enrolment page if possible.
140 $instances = enrol_get_instances($course->id, true);
141 foreach($instances as $instance) {
142 if ($plugin = enrol_get_plugin($instance->enrol)) {
143 if ($plugin->get_manual_enrol_link($instance)) {
144 // We know that the ajax enrol UI will have an option to enrol.
145 redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
150 } else {
151 // Save any changes to the files used in the editor.
152 update_course($data, $editoroptions);
155 // Redirect user to newly created/updated course.
156 redirect(new moodle_url('/course/view.php', array('id' => $course->id)));
159 // Print the form.
161 $site = get_site();
163 $streditcoursesettings = get_string("editcoursesettings");
164 $straddnewcourse = get_string("addnewcourse");
165 $stradministration = get_string("administration");
166 $strcategories = get_string("categories");
168 if (!empty($course->id)) {
169 $PAGE->navbar->add($streditcoursesettings);
170 $title = $streditcoursesettings;
171 $fullname = $course->fullname;
172 } else {
173 $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
174 $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
175 $PAGE->navbar->add($straddnewcourse);
176 $title = "$site->shortname: $straddnewcourse";
177 $fullname = $site->fullname;
180 $PAGE->set_title($title);
181 $PAGE->set_heading($fullname);
183 echo $OUTPUT->header();
184 echo $OUTPUT->heading($streditcoursesettings);
186 $editform->display();
188 echo $OUTPUT->footer();