Merge branch 'MDL-64695-37' of https://github.com/paulholden/moodle into MOODLE_37_STABLE
[moodle.git] / course / modedit.php
blobf048bcd25e43a8e0867cd60c11b1e1e98b7384f2
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_ALPHA); // 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);
40 $url = new moodle_url('/course/modedit.php');
41 $url->param('sr', $sectionreturn);
42 if (!empty($return)) {
43 $url->param('return', $return);
46 if (!empty($add)) {
47 $section = required_param('section', PARAM_INT);
48 $course = required_param('course', PARAM_INT);
50 $url->param('add', $add);
51 $url->param('section', $section);
52 $url->param('course', $course);
53 $PAGE->set_url($url);
55 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
56 require_login($course);
58 // There is no page for this in the navigation. The closest we'll have is the course section.
59 // If the course section isn't displayed on the navigation this will fall back to the course which
60 // will be the closest match we have.
61 navigation_node::override_active_url(course_get_url($course, $section));
63 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $add, $section);
64 $data->return = 0;
65 $data->sr = $sectionreturn;
66 $data->add = $add;
67 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
68 $data->type = $type;
71 $sectionname = get_section_name($course, $cw);
72 $fullmodulename = get_string('modulename', $module->name);
74 if ($data->section && $course->format != 'site') {
75 $heading = new stdClass();
76 $heading->what = $fullmodulename;
77 $heading->to = $sectionname;
78 $pageheading = get_string('addinganewto', 'moodle', $heading);
79 } else {
80 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
82 $navbaraddition = $pageheading;
84 } else if (!empty($update)) {
86 $url->param('update', $update);
87 $PAGE->set_url($url);
89 // Select the "Edit settings" from navigation.
90 navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
92 // Check the course module exists.
93 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
95 // Check the course exists.
96 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
98 // require_login
99 require_login($course, false, $cm); // needed to setup proper $COURSE
101 list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
102 $data->return = $return;
103 $data->sr = $sectionreturn;
104 $data->update = $update;
106 $sectionname = get_section_name($course, $cw);
107 $fullmodulename = get_string('modulename', $module->name);
109 if ($data->section && $course->format != 'site') {
110 $heading = new stdClass();
111 $heading->what = $fullmodulename;
112 $heading->in = $sectionname;
113 $pageheading = get_string('updatingain', 'moodle', $heading);
114 } else {
115 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
117 $navbaraddition = null;
119 } else {
120 require_login();
121 print_error('invalidaction');
124 $pagepath = 'mod-' . $module->name . '-';
125 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
126 $pagepath .= $type;
127 } else {
128 $pagepath .= 'mod';
130 $PAGE->set_pagetype($pagepath);
131 $PAGE->set_pagelayout('admin');
133 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
134 if (file_exists($modmoodleform)) {
135 require_once($modmoodleform);
136 } else {
137 print_error('noformdesc');
140 $mformclassname = 'mod_'.$module->name.'_mod_form';
141 $mform = new $mformclassname($data, $cw->section, $cm, $course);
142 $mform->set_data($data);
144 if ($mform->is_cancelled()) {
145 if ($return && !empty($cm->id)) {
146 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
147 } else {
148 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
150 } else if ($fromform = $mform->get_data()) {
151 if (!empty($fromform->update)) {
152 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
153 } else if (!empty($fromform->add)) {
154 $fromform = add_moduleinfo($fromform, $course, $mform);
155 } else {
156 print_error('invaliddata');
159 if (isset($fromform->submitbutton)) {
160 $url = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule, 'forceview' => 1));
161 if (empty($fromform->showgradingmanagement)) {
162 redirect($url);
163 } else {
164 redirect($fromform->gradingman->get_management_url($url));
166 } else {
167 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
169 exit;
171 } else {
173 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
174 $strmodulenameplural = get_string('modulenameplural', $module->name);
176 if (!empty($cm->id)) {
177 $context = context_module::instance($cm->id);
178 } else {
179 $context = context_course::instance($course->id);
182 $PAGE->set_heading($course->fullname);
183 $PAGE->set_title($streditinga);
184 $PAGE->set_cacheable(false);
186 if (isset($navbaraddition)) {
187 $PAGE->navbar->add($navbaraddition);
190 echo $OUTPUT->header();
192 if (get_string_manager()->string_exists('modulename_help', $module->name)) {
193 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
194 } else {
195 echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon');
198 $mform->display();
200 echo $OUTPUT->footer();