MDL-81035 core: Fix redirect with sub-path
[moodle.git] / course / modedit.php
blob5e8a611f080d6edf7ac9b19d63cc64cc5b6153af
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 * Adds or updates modules in a course using new formslib
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($CFG->libdir.'/filelib.php');
29 require_once($CFG->libdir.'/gradelib.php');
30 require_once($CFG->libdir.'/completionlib.php');
31 require_once($CFG->libdir.'/plagiarismlib.php');
32 require_once($CFG->dirroot . '/course/modlib.php');
34 $add = optional_param('add', '', PARAM_ALPHANUM); // Module name.
35 $update = optional_param('update', 0, PARAM_INT);
36 $return = optional_param('return', 0, PARAM_BOOL); //return to course/view.php if false or mod/modname/view.php if true
37 $type = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
38 $sectionreturn = optional_param('sr', null, PARAM_INT);
39 $beforemod = optional_param('beforemod', 0, PARAM_INT);
40 $showonly = optional_param('showonly', '', PARAM_TAGLIST); // Settings group to show expanded and hide the rest.
42 // Force it to be null if it's not a valid section number.
43 if ($sectionreturn < 0) {
44 $sectionreturn = null;
47 $url = new moodle_url('/course/modedit.php');
48 if (!is_null($sectionreturn)) {
49 $url->param('sr', $sectionreturn);
51 if (!empty($return)) {
52 $url->param('return', $return);
54 if (!empty($showonly)) {
55 $url->param('showonly', $showonly);
58 if (!empty($add)) {
59 $section = required_param('section', PARAM_INT);
60 $course = required_param('course', PARAM_INT);
62 $url->param('add', $add);
63 $url->param('section', $section);
64 $url->param('course', $course);
65 $PAGE->set_url($url);
67 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
68 require_login($course);
70 // There is no page for this in the navigation. The closest we'll have is the course section.
71 // If the course section isn't displayed on the navigation this will fall back to the course which
72 // will be the closest match we have.
73 navigation_node::override_active_url(course_get_url($course, $section));
75 // MDL-69431 Validate that $section (url param) does not exceed the maximum for this course / format.
76 // If too high (e.g. section *id* not number) non-sequential sections inserted in course_sections table.
77 // Then on import, backup fills 'gap' with empty sections (see restore_rebuild_course_cache). Avoid this.
78 $courseformat = course_get_format($course);
79 $maxsections = $courseformat->get_max_sections();
80 if ($section > $maxsections) {
81 throw new \moodle_exception('maxsectionslimit', 'moodle', '', $maxsections);
84 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $add, $section);
85 $data->return = 0;
86 if (!is_null($sectionreturn)) {
87 $data->sr = $sectionreturn;
89 $data->add = $add;
90 $data->beforemod = $beforemod;
91 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
92 $data->type = $type;
95 $sectionname = get_section_name($course, $cw);
96 $fullmodulename = get_string('modulename', $module->name);
97 $pageheading = $pagetitle = get_string('addinganew', 'moodle', $fullmodulename);
98 $navbaraddition = $pageheading;
100 } else if (!empty($update)) {
102 $url->param('update', $update);
103 $PAGE->set_url($url);
105 // Select the "Edit settings" from navigation.
106 navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
108 // Check the course module exists.
109 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
111 // Check the course exists.
112 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
114 // require_login
115 require_login($course, false, $cm); // needed to setup proper $COURSE
117 list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
118 $data->return = $return;
119 if (!is_null($sectionreturn)) {
120 $data->sr = $sectionreturn;
122 $data->update = $update;
123 if (!empty($showonly)) {
124 $data->showonly = $showonly;
127 $sectionname = get_section_name($course, $cw);
128 $fullmodulename = get_string('modulename', $module->name);
129 $pageheading = get_string('editsettings', 'moodle');
130 $pagetitle = get_string('edita', 'moodle', $fullmodulename) . ': ' . $cm->name;
131 $navbaraddition = null;
133 } else {
134 require_login();
135 throw new \moodle_exception('invalidaction');
138 $pagepath = 'mod-' . $module->name . '-';
139 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
140 $pagepath .= $type;
141 } else {
142 $pagepath .= 'mod';
144 $PAGE->set_pagetype($pagepath);
145 $PAGE->set_pagelayout('admin');
146 $PAGE->add_body_class('limitedwidth');
149 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
150 if (file_exists($modmoodleform)) {
151 require_once($modmoodleform);
152 } else {
153 throw new \moodle_exception('noformdesc');
156 $mformclassname = 'mod_'.$module->name.'_mod_form';
157 $mform = new $mformclassname($data, $cw->section, $cm, $course);
158 $mform->set_data($data);
159 if (!empty($showonly)) {
160 $mform->filter_shown_headers(explode(',', $showonly));
163 if ($mform->is_cancelled()) {
164 if ($return && !empty($cm->id)) {
165 $urlparams = [
166 'id' => $cm->id, // We always need the activity id.
167 'forceview' => 1, // Stop file downloads in resources.
169 $activityurl = new moodle_url("/mod/$module->name/view.php", $urlparams);
170 redirect($activityurl);
171 } else {
172 $options = [];
173 if (!is_null($sectionreturn)) {
174 $options['sr'] = $sectionreturn;
176 redirect(course_get_url($course, $cw->section, $options));
178 } else if ($fromform = $mform->get_data()) {
179 // Mark that this is happening in the front-end UI. This is used to indicate that we are able to
180 // do regrading with a progress bar and redirect, if necessary.
181 $fromform->frontend = true;
182 if (!empty($fromform->update)) {
183 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
184 } else if (!empty($fromform->add)) {
185 $fromform = add_moduleinfo($fromform, $course, $mform);
186 } else {
187 throw new \moodle_exception('invaliddata');
190 if (isset($fromform->submitbutton)) {
191 $url = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule, 'forceview' => 1));
192 if (!empty($fromform->showgradingmanagement)) {
193 $url = $fromform->gradingman->get_management_url($url);
195 } else {
196 $options = [];
197 if (!is_null($sectionreturn)) {
198 $options['sr'] = $sectionreturn;
200 $url = course_get_url($course, $cw->section, $options);
203 // If we need to regrade the course with a progress bar as a result of updating this module,
204 // redirect first to the page that will do this.
205 if (isset($fromform->needsfrontendregrade)) {
206 $url = new moodle_url('/course/modregrade.php', ['id' => $fromform->coursemodule,
207 'url' => $url->out_as_local_url(false)]);
210 redirect($url);
211 exit;
213 } else {
214 if (!empty($cm->id)) {
215 $context = context_module::instance($cm->id);
216 } else {
217 $context = context_course::instance($course->id);
220 $PAGE->set_heading($course->fullname);
221 if ($course->id !== $SITE->id) {
222 $pagetitle = $pagetitle . moodle_page::TITLE_SEPARATOR . $course->shortname;
224 $PAGE->set_title($pagetitle);
225 $PAGE->set_cacheable(false);
227 if (isset($navbaraddition)) {
228 $PAGE->navbar->add($navbaraddition);
230 $PAGE->activityheader->disable();
232 echo $OUTPUT->header();
233 echo $OUTPUT->heading_with_help($pageheading, '', $module->name);
235 $mform->display();
237 echo $OUTPUT->footer();