Merge branch 'install_21_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_21...
[moodle.git] / course / edit.php
blobe3688a18a5441477555c102f45f28ce7d4ae2850
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Edit course settings
21 * @package moodlecore
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once('lib.php');
28 require_once('edit_form.php');
30 $id = optional_param('id', 0, PARAM_INT); // course id
31 $categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
32 $returnto = optional_param('returnto', 0, PARAM_ALPHANUM); // generic navigation return page switch
34 $PAGE->set_pagelayout('admin');
35 $PAGE->set_url('/course/edit.php');
37 // basic access control checks
38 if ($id) { // editing course
39 if ($id == SITEID){
40 // don't allow editing of 'site course' using this from
41 print_error('cannoteditsiteform');
44 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
45 require_login($course);
46 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
47 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
48 require_capability('moodle/course:update', $coursecontext);
49 $PAGE->url->param('id',$id);
51 } else if ($categoryid) { // creating new course in this category
52 $course = null;
53 require_login();
54 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
55 $catcontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
56 require_capability('moodle/course:create', $catcontext);
57 $PAGE->url->param('category',$categoryid);
58 $PAGE->set_context($catcontext);
60 } else {
61 require_login();
62 print_error('needcoursecategroyid');
65 // Prepare course and the editor
66 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
67 if (!empty($course)) {
68 $allowedmods = array();
69 if ($am = $DB->get_records('course_allowed_modules', array('course'=>$course->id))) {
70 foreach ($am as $m) {
71 $allowedmods[] = $m->module;
73 } else {
74 // this happens in case we edit course created before enabling module restrictions or somebody disabled everything :-(
75 if (empty($course->restrictmodules) and !empty($CFG->defaultallowedmodules)) {
76 $allowedmods = explode(',', $CFG->defaultallowedmodules);
79 $course->allowedmods = $allowedmods;
80 //add context for editor
81 $editoroptions['context'] = $coursecontext;
82 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
84 } else {
85 //editor should respect category context if course context is not set.
86 $editoroptions['context'] = $catcontext;
87 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
89 // Set up the default restricted module list
90 if (!empty($CFG->restrictbydefault)) {
91 if (!empty($CFG->defaultallowedmodules)) {
92 $course->allowedmods = explode(',', $CFG->defaultallowedmodules);
97 // first create the form
98 $editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto));
99 if ($editform->is_cancelled()) {
100 switch ($returnto) {
101 case 'category':
102 $url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$categoryid));
103 break;
104 case 'topcat':
105 $url = new moodle_url($CFG->wwwroot.'/course/');
106 break;
107 default:
108 if (!empty($course->id)) {
109 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
110 } else {
111 $url = new moodle_url($CFG->wwwroot.'/course/');
113 break;
115 redirect($url);
117 } else if ($data = $editform->get_data()) {
118 // process data if submitted
120 if (empty($course->id)) {
121 // In creating the course
122 $course = create_course($data, $editoroptions);
124 // Get the context of the newly created course
125 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
127 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
128 // deal with course creators - enrol them internally with default role
129 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
132 if (!is_enrolled($context)) {
133 // Redirect to manual enrolment page if possible
134 $instances = enrol_get_instances($course->id, true);
135 foreach($instances as $instance) {
136 if ($plugin = enrol_get_plugin($instance->enrol)) {
137 if ($plugin->get_manual_enrol_link($instance)) {
138 // we know that the ajax enrol UI will have an option to enrol
139 redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
144 } else {
145 // Save any changes to the files used in the editor
146 update_course($data, $editoroptions);
149 switch ($returnto) {
150 case 'category':
151 case 'topcat': //redirecting to where the new course was created by default.
152 $url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$categoryid));
153 break;
154 default:
155 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
156 break;
158 redirect($url);
162 // Print the form
164 $site = get_site();
166 $streditcoursesettings = get_string("editcoursesettings");
167 $straddnewcourse = get_string("addnewcourse");
168 $stradministration = get_string("administration");
169 $strcategories = get_string("categories");
171 if (!empty($course->id)) {
172 $PAGE->navbar->add($streditcoursesettings);
173 $title = $streditcoursesettings;
174 $fullname = $course->fullname;
175 } else {
176 $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
177 $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
178 $PAGE->navbar->add($straddnewcourse);
179 $title = "$site->shortname: $straddnewcourse";
180 $fullname = $site->fullname;
183 $PAGE->set_title($title);
184 $PAGE->set_heading($fullname);
186 echo $OUTPUT->header();
187 echo $OUTPUT->heading($streditcoursesettings);
189 $editform->display();
191 echo $OUTPUT->footer();