MDL-53758 search: Better results with low hit rates, improve performance
[moodle.git] / course / edit.php
blobd79c0d6ae6fb5893e85bb7965d54a067e67f6f61
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.
32 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL); // A return URL. returnto must also be set to 'url'.
34 if ($returnto === 'url' && confirm_sesskey() && $returnurl) {
35 // If returnto is 'url' then $returnurl may be used as the destination to return to after saving or cancelling.
36 // Sesskey must be specified, and would be set by the form anyway.
37 $returnurl = new moodle_url($returnurl);
38 } else {
39 if (!empty($id)) {
40 $returnurl = new moodle_url($CFG->wwwroot . '/course/view.php', array('id' => $id));
41 } else {
42 $returnurl = new moodle_url($CFG->wwwroot . '/course/');
44 if ($returnto !== 0) {
45 switch ($returnto) {
46 case 'category':
47 $returnurl = new moodle_url($CFG->wwwroot . '/course/index.php', array('categoryid' => $categoryid));
48 break;
49 case 'catmanage':
50 $returnurl = new moodle_url($CFG->wwwroot . '/course/management.php', array('categoryid' => $categoryid));
51 break;
52 case 'topcatmanage':
53 $returnurl = new moodle_url($CFG->wwwroot . '/course/management.php');
54 break;
55 case 'topcat':
56 $returnurl = new moodle_url($CFG->wwwroot . '/course/');
57 break;
62 $PAGE->set_pagelayout('admin');
63 if ($id) {
64 $pageparams = array('id' => $id);
65 } else {
66 $pageparams = array('category' => $categoryid);
68 if ($returnto !== 0) {
69 $pageparams['returnto'] = $returnto;
70 if ($returnto === 'url' && $returnurl) {
71 $pageparams['returnurl'] = $returnurl;
74 $PAGE->set_url('/course/edit.php', $pageparams);
76 // Basic access control checks.
77 if ($id) {
78 // Editing course.
79 if ($id == SITEID){
80 // Don't allow editing of 'site course' using this from.
81 print_error('cannoteditsiteform');
84 // Login to the course and retrieve also all fields defined by course format.
85 $course = get_course($id);
86 require_login($course);
87 $course = course_get_format($course)->get_course();
89 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
90 $coursecontext = context_course::instance($course->id);
91 require_capability('moodle/course:update', $coursecontext);
93 } else if ($categoryid) {
94 // Creating new course in this category.
95 $course = null;
96 require_login();
97 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
98 $catcontext = context_coursecat::instance($category->id);
99 require_capability('moodle/course:create', $catcontext);
100 $PAGE->set_context($catcontext);
102 } else {
103 require_login();
104 print_error('needcoursecategroyid');
107 // Prepare course and the editor.
108 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
109 $overviewfilesoptions = course_overviewfiles_options($course);
110 if (!empty($course)) {
111 // Add context for editor.
112 $editoroptions['context'] = $coursecontext;
113 $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
114 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
115 if ($overviewfilesoptions) {
116 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
119 // Inject current aliases.
120 $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
121 foreach($aliases as $alias) {
122 $course->{'role_'.$alias->roleid} = $alias->name;
125 // Populate course tags.
126 $course->tags = core_tag_tag::get_item_tags_array('core', 'course', $course->id);
128 } else {
129 // Editor should respect category context if course context is not set.
130 $editoroptions['context'] = $catcontext;
131 $editoroptions['subdirs'] = 0;
132 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
133 if ($overviewfilesoptions) {
134 file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
138 // First create the form.
139 $args = array(
140 'course' => $course,
141 'category' => $category,
142 'editoroptions' => $editoroptions,
143 'returnto' => $returnto,
144 'returnurl' => $returnurl
146 $editform = new course_edit_form(null, $args);
147 if ($editform->is_cancelled()) {
148 // The form has been cancelled, take them back to what ever the return to is.
149 redirect($returnurl);
150 } else if ($data = $editform->get_data()) {
151 // Process data if submitted.
152 if (empty($course->id)) {
153 // In creating the course.
154 $course = create_course($data, $editoroptions);
156 // Get the context of the newly created course.
157 $context = context_course::instance($course->id, MUST_EXIST);
159 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
160 // Deal with course creators - enrol them internally with default role.
161 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
164 // The URL to take them to if they chose save and display.
165 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
167 // If they choose to save and display, and they are not enrolled take them to the enrolments page instead.
168 if (!is_enrolled($context) && isset($data->saveanddisplay)) {
169 // Redirect to manual enrolment page if possible.
170 $instances = enrol_get_instances($course->id, true);
171 foreach($instances as $instance) {
172 if ($plugin = enrol_get_plugin($instance->enrol)) {
173 if ($plugin->get_manual_enrol_link($instance)) {
174 // We know that the ajax enrol UI will have an option to enrol.
175 $courseurl = new moodle_url('/enrol/users.php', array('id' => $course->id, 'newcourse' => 1));
176 break;
181 } else {
182 // Save any changes to the files used in the editor.
183 update_course($data, $editoroptions);
184 // Set the URL to take them too if they choose save and display.
185 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
188 if (isset($data->saveanddisplay)) {
189 // Redirect user to newly created/updated course.
190 redirect($courseurl);
191 } else {
192 // Save and return. Take them back to wherever.
193 redirect($returnurl);
197 // Print the form.
199 $site = get_site();
201 $streditcoursesettings = get_string("editcoursesettings");
202 $straddnewcourse = get_string("addnewcourse");
203 $stradministration = get_string("administration");
204 $strcategories = get_string("categories");
206 if (!empty($course->id)) {
207 // Navigation note: The user is editing a course, the course will exist within the navigation and settings.
208 // The navigation will automatically find the Edit settings page under course navigation.
209 $pagedesc = $streditcoursesettings;
210 $title = $streditcoursesettings;
211 $fullname = $course->fullname;
212 } else {
213 // The user is adding a course, this page isn't presented in the site navigation/admin.
214 // Adding a new course is part of course category management territory.
215 // We'd prefer to use the management interface URL without args.
216 $managementurl = new moodle_url('/course/management.php');
217 // These are the caps required in order to see the management interface.
218 $managementcaps = array('moodle/category:manage', 'moodle/course:create');
219 if ($categoryid && !has_any_capability($managementcaps, context_system::instance())) {
220 // If the user doesn't have either manage caps then they can only manage within the given category.
221 $managementurl->param('categoryid', $categoryid);
223 // Because the course category management interfaces are buried in the admin tree and that is loaded by ajax
224 // we need to manually tell the navigation we need it loaded. The second arg does this.
225 navigation_node::override_active_url($managementurl, true);
227 $pagedesc = $straddnewcourse;
228 $title = "$site->shortname: $straddnewcourse";
229 $fullname = $site->fullname;
230 $PAGE->navbar->add($pagedesc);
233 $PAGE->set_title($title);
234 $PAGE->set_heading($fullname);
236 echo $OUTPUT->header();
237 echo $OUTPUT->heading($pagedesc);
239 $editform->display();
241 echo $OUTPUT->footer();