Commit | Line | Data |
---|---|---|
aa54ed7b | 1 | <?php |
2 | ||
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. | |
14 | // | |
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/>. | |
17 | ||
18 | /** | |
ba9dc077 SH |
19 | * Adds or updates modules in a course using new formslib |
20 | * | |
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 | |
24 | */ | |
25 | ||
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'); | |
bce59524 | 31 | require_once($CFG->libdir.'/plagiarismlib.php'); |
80fe0c19 | 32 | require_once($CFG->dirroot . '/course/modlib.php'); |
ba9dc077 SH |
33 | |
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 | |
923451c5 | 38 | $sectionreturn = optional_param('sr', null, PARAM_INT); |
ba9dc077 SH |
39 | |
40 | $url = new moodle_url('/course/modedit.php'); | |
a41b1d96 | 41 | $url->param('sr', $sectionreturn); |
ba9dc077 SH |
42 | if (!empty($return)) { |
43 | $url->param('return', $return); | |
44 | } | |
45 | ||
46 | if (!empty($add)) { | |
ba9dc077 SH |
47 | $section = required_param('section', PARAM_INT); |
48 | $course = required_param('course', PARAM_INT); | |
2b9e3bac | 49 | |
ebb32067 SV |
50 | $url->param('add', $add); |
51 | $url->param('section', $section); | |
52 | $url->param('course', $course); | |
53 | $PAGE->set_url($url); | |
54 | ||
74df2951 | 55 | $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST); |
ba9dc077 | 56 | require_login($course); |
264867fd | 57 | |
0af463d4 SH |
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)); | |
62 | ||
796876b0 JL |
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; | |
ba9dc077 SH |
67 | if (!empty($type)) { //TODO: hopefully will be removed in 2.0 |
68 | $data->type = $type; | |
69 | } | |
264867fd | 70 | |
7487c856 | 71 | $sectionname = get_section_name($course, $cw); |
ba9dc077 | 72 | $fullmodulename = get_string('modulename', $module->name); |
264867fd | 73 | |
ba9dc077 | 74 | if ($data->section && $course->format != 'site') { |
fbaea88f | 75 | $heading = new stdClass(); |
ba9dc077 | 76 | $heading->what = $fullmodulename; |
7487c856 | 77 | $heading->to = $sectionname; |
ba9dc077 SH |
78 | $pageheading = get_string('addinganewto', 'moodle', $heading); |
79 | } else { | |
80 | $pageheading = get_string('addinganew', 'moodle', $fullmodulename); | |
81 | } | |
0af463d4 | 82 | $navbaraddition = $pageheading; |
264867fd | 83 | |
ba9dc077 | 84 | } else if (!empty($update)) { |
cb141e5a | 85 | |
ba9dc077 SH |
86 | $url->param('update', $update); |
87 | $PAGE->set_url($url); | |
264867fd | 88 | |
d5814f4e PS |
89 | // Select the "Edit settings" from navigation. |
90 | navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1))); | |
91 | ||
dd5d933f | 92 | // Check the course module exists. |
ba9dc077 | 93 | $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST); |
dd5d933f JM |
94 | |
95 | // Check the course exists. | |
74df2951 | 96 | $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); |
ba9dc077 | 97 | |
dd5d933f | 98 | // require_login |
ba9dc077 | 99 | require_login($course, false, $cm); // needed to setup proper $COURSE |
ba9dc077 | 100 | |
796876b0 JL |
101 | list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course); |
102 | $data->return = $return; | |
103 | $data->sr = $sectionreturn; | |
104 | $data->update = $update; | |
61defed9 | 105 | |
7487c856 | 106 | $sectionname = get_section_name($course, $cw); |
ba9dc077 | 107 | $fullmodulename = get_string('modulename', $module->name); |
61defed9 | 108 | |
ba9dc077 | 109 | if ($data->section && $course->format != 'site') { |
fbaea88f | 110 | $heading = new stdClass(); |
ba9dc077 | 111 | $heading->what = $fullmodulename; |
7487c856 | 112 | $heading->in = $sectionname; |
ba9dc077 | 113 | $pageheading = get_string('updatingain', 'moodle', $heading); |
52273766 | 114 | } else { |
ba9dc077 | 115 | $pageheading = get_string('updatinga', 'moodle', $fullmodulename); |
264867fd | 116 | } |
0af463d4 | 117 | $navbaraddition = null; |
264867fd | 118 | |
ba9dc077 SH |
119 | } else { |
120 | require_login(); | |
121 | print_error('invalidaction'); | |
122 | } | |
123 | ||
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'; | |
129 | } | |
130 | $PAGE->set_pagetype($pagepath); | |
ae2fa74f | 131 | $PAGE->set_pagelayout('admin'); |
ba9dc077 SH |
132 | |
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'); | |
138 | } | |
139 | ||
ba9dc077 SH |
140 | $mformclassname = 'mod_'.$module->name.'_mod_form'; |
141 | $mform = new $mformclassname($data, $cw->section, $cm, $course); | |
142 | $mform->set_data($data); | |
143 | ||
144 | if ($mform->is_cancelled()) { | |
145 | if ($return && !empty($cm->id)) { | |
146 | redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id"); | |
52273766 | 147 | } else { |
923451c5 | 148 | redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn))); |
ba9dc077 SH |
149 | } |
150 | } else if ($fromform = $mform->get_data()) { | |
ba9dc077 | 151 | if (!empty($fromform->update)) { |
dd5d933f | 152 | list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform); |
ba9dc077 | 153 | } else if (!empty($fromform->add)) { |
dd5d933f | 154 | $fromform = add_moduleinfo($fromform, $course, $mform); |
ba9dc077 SH |
155 | } else { |
156 | print_error('invaliddata'); | |
157 | } | |
61defed9 | 158 | |
ba9dc077 | 159 | if (isset($fromform->submitbutton)) { |
1678181a | 160 | $url = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule, 'forceview' => 1)); |
7bf4f6e9 | 161 | if (empty($fromform->showgradingmanagement)) { |
1678181a | 162 | redirect($url); |
03d448e5 | 163 | } else { |
1678181a | 164 | redirect($fromform->gradingman->get_management_url($url)); |
03d448e5 | 165 | } |
52273766 | 166 | } else { |
923451c5 | 167 | redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn))); |
ba9dc077 SH |
168 | } |
169 | exit; | |
61defed9 | 170 | |
ba9dc077 | 171 | } else { |
aa6c1ced | 172 | |
ba9dc077 SH |
173 | $streditinga = get_string('editinga', 'moodle', $fullmodulename); |
174 | $strmodulenameplural = get_string('modulenameplural', $module->name); | |
aa6c1ced | 175 | |
ba9dc077 | 176 | if (!empty($cm->id)) { |
9a5e297b | 177 | $context = context_module::instance($cm->id); |
ba9dc077 | 178 | } else { |
9a5e297b | 179 | $context = context_course::instance($course->id); |
ba9dc077 SH |
180 | } |
181 | ||
182 | $PAGE->set_heading($course->fullname); | |
c20b001b | 183 | $PAGE->set_title($streditinga); |
ba9dc077 | 184 | $PAGE->set_cacheable(false); |
0af463d4 SH |
185 | |
186 | if (isset($navbaraddition)) { | |
187 | $PAGE->navbar->add($navbaraddition); | |
188 | } | |
189 | ||
ba9dc077 SH |
190 | echo $OUTPUT->header(); |
191 | ||
5435c9dc | 192 | if (get_string_manager()->string_exists('modulename_help', $module->name)) { |
ba9dc077 SH |
193 | echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon'); |
194 | } else { | |
e1401974 | 195 | echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon'); |
264867fd | 196 | } |
ba9dc077 SH |
197 | |
198 | $mform->display(); | |
199 | ||
200 | echo $OUTPUT->footer(); | |
5435c9dc | 201 | } |