Merge branch 'MDL-38424_m23' of git://github.com/kordan/moodle into MOODLE_23_STABLE
[moodle.git] / course / edit.php
blob3d4c6b4c560f2d967d35f9e9bcce4e084301e222
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 $pageparams = array('id'=>$id);
36 if (empty($id)) {
37 $pageparams = array('category'=>$categoryid);
39 $PAGE->set_url('/course/edit.php', $pageparams);
41 // basic access control checks
42 if ($id) { // editing course
43 if ($id == SITEID){
44 // don't allow editing of 'site course' using this from
45 print_error('cannoteditsiteform');
48 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
49 require_login($course);
50 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
51 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
52 require_capability('moodle/course:update', $coursecontext);
54 } else if ($categoryid) { // creating new course in this category
55 $course = null;
56 require_login();
57 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
58 $catcontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
59 require_capability('moodle/course:create', $catcontext);
60 $PAGE->set_context($catcontext);
62 } else {
63 require_login();
64 print_error('needcoursecategroyid');
67 // Prepare course and the editor
68 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
69 if (!empty($course)) {
70 //add context for editor
71 $editoroptions['context'] = $coursecontext;
72 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
74 } else {
75 //editor should respect category context if course context is not set.
76 $editoroptions['context'] = $catcontext;
77 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
80 // first create the form
81 $editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto));
82 if ($editform->is_cancelled()) {
83 switch ($returnto) {
84 case 'category':
85 $url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$categoryid));
86 break;
87 case 'topcat':
88 $url = new moodle_url($CFG->wwwroot.'/course/');
89 break;
90 default:
91 if (!empty($course->id)) {
92 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
93 } else {
94 $url = new moodle_url($CFG->wwwroot.'/course/');
96 break;
98 redirect($url);
100 } else if ($data = $editform->get_data()) {
101 // process data if submitted
103 if (empty($course->id)) {
104 // In creating the course
105 $course = create_course($data, $editoroptions);
107 // Get the context of the newly created course
108 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
110 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
111 // deal with course creators - enrol them internally with default role
112 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
115 if (!is_enrolled($context)) {
116 // Redirect to manual enrolment page if possible
117 $instances = enrol_get_instances($course->id, true);
118 foreach($instances as $instance) {
119 if ($plugin = enrol_get_plugin($instance->enrol)) {
120 if ($plugin->get_manual_enrol_link($instance)) {
121 // we know that the ajax enrol UI will have an option to enrol
122 redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
127 } else {
128 // Save any changes to the files used in the editor
129 update_course($data, $editoroptions);
131 rebuild_course_cache($course->id);
133 // Redirect user to newly created/updated course.
134 redirect(new moodle_url('/course/view.php', array('id' => $course->id)));
138 // Print the form
140 $site = get_site();
142 $streditcoursesettings = get_string("editcoursesettings");
143 $straddnewcourse = get_string("addnewcourse");
144 $stradministration = get_string("administration");
145 $strcategories = get_string("categories");
147 if (!empty($course->id)) {
148 $PAGE->navbar->add($streditcoursesettings);
149 $title = $streditcoursesettings;
150 $fullname = $course->fullname;
151 } else {
152 $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
153 $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
154 $PAGE->navbar->add($straddnewcourse);
155 $title = "$site->shortname: $straddnewcourse";
156 $fullname = $site->fullname;
159 $PAGE->set_title($title);
160 $PAGE->set_heading($fullname);
162 echo $OUTPUT->header();
163 echo $OUTPUT->heading($streditcoursesettings);
165 $editform->display();
167 echo $OUTPUT->footer();