Merge branch 'wip_master_mdl-42723' of https://github.com/iarenaza/moodle
[moodle.git] / course / edit.php
blobf257f9100186c16ba5015baec130b9ac710c9f6f
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 $course = course_get_format($id)->get_course();
49 require_login($course);
50 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
51 $coursecontext = context_course::instance($course->id);
52 require_capability('moodle/course:update', $coursecontext);
54 } else if ($categoryid) {
55 // Creating new course in this category.
56 $course = null;
57 require_login();
58 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
59 $catcontext = context_coursecat::instance($category->id);
60 require_capability('moodle/course:create', $catcontext);
61 $PAGE->set_context($catcontext);
63 } else {
64 require_login();
65 print_error('needcoursecategroyid');
68 // Prepare course and the editor.
69 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
70 $overviewfilesoptions = course_overviewfiles_options($course);
71 if (!empty($course)) {
72 // Add context for editor.
73 $editoroptions['context'] = $coursecontext;
74 $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
75 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
76 if ($overviewfilesoptions) {
77 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
80 // Inject current aliases.
81 $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
82 foreach($aliases as $alias) {
83 $course->{'role_'.$alias->roleid} = $alias->name;
86 } else {
87 // Editor should respect category context if course context is not set.
88 $editoroptions['context'] = $catcontext;
89 $editoroptions['subdirs'] = 0;
90 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
91 if ($overviewfilesoptions) {
92 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
96 // First create the form.
97 $editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto));
98 if ($editform->is_cancelled()) {
99 switch ($returnto) {
100 case 'category':
101 $url = new moodle_url($CFG->wwwroot.'/course/index.php', array('categoryid' => $categoryid));
102 break;
103 case 'catmanage':
104 $url = new moodle_url($CFG->wwwroot.'/course/management.php', array('categoryid' => $categoryid));
105 break;
106 case 'topcatmanage':
107 $url = new moodle_url($CFG->wwwroot.'/course/management.php');
108 break;
109 case 'topcat':
110 $url = new moodle_url($CFG->wwwroot.'/course/');
111 break;
112 default:
113 if (!empty($course->id)) {
114 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
115 } else {
116 $url = new moodle_url($CFG->wwwroot.'/course/');
118 break;
120 redirect($url);
122 } else if ($data = $editform->get_data()) {
123 // Process data if submitted.
124 if (empty($course->id)) {
125 // In creating the course.
126 $course = create_course($data, $editoroptions);
128 // Get the context of the newly created course.
129 $context = context_course::instance($course->id, MUST_EXIST);
131 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
132 // Deal with course creators - enrol them internally with default role.
133 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
135 if (!is_enrolled($context)) {
136 // Redirect to manual enrolment page if possible.
137 $instances = enrol_get_instances($course->id, true);
138 foreach($instances as $instance) {
139 if ($plugin = enrol_get_plugin($instance->enrol)) {
140 if ($plugin->get_manual_enrol_link($instance)) {
141 // We know that the ajax enrol UI will have an option to enrol.
142 redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
147 } else {
148 // Save any changes to the files used in the editor.
149 update_course($data, $editoroptions);
152 // Redirect user to newly created/updated course.
153 redirect(new moodle_url('/course/view.php', array('id' => $course->id)));
156 // Print the form.
158 $site = get_site();
160 $streditcoursesettings = get_string("editcoursesettings");
161 $straddnewcourse = get_string("addnewcourse");
162 $stradministration = get_string("administration");
163 $strcategories = get_string("categories");
165 if (!empty($course->id)) {
166 $PAGE->navbar->add($streditcoursesettings);
167 $title = $streditcoursesettings;
168 $fullname = $course->fullname;
169 } else {
170 $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
171 $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
172 $PAGE->navbar->add($straddnewcourse);
173 $title = "$site->shortname: $straddnewcourse";
174 $fullname = $site->fullname;
177 $PAGE->set_title($title);
178 $PAGE->set_heading($fullname);
180 echo $OUTPUT->header();
181 echo $OUTPUT->heading($streditcoursesettings);
183 $editform->display();
185 echo $OUTPUT->footer();