MDL-41828 mod: fixed navbar when adding modules to a course
[moodle.git] / course / modedit.php
CommitLineData
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
26require_once("../config.php");
27require_once("lib.php");
28require_once($CFG->libdir.'/filelib.php');
29require_once($CFG->libdir.'/gradelib.php');
30require_once($CFG->libdir.'/completionlib.php');
31require_once($CFG->libdir.'/conditionlib.php');
bce59524 32require_once($CFG->libdir.'/plagiarismlib.php');
80fe0c19 33require_once($CFG->dirroot . '/course/modlib.php');
ba9dc077
SH
34
35$add = optional_param('add', '', PARAM_ALPHA); // module name
36$update = optional_param('update', 0, PARAM_INT);
37$return = optional_param('return', 0, PARAM_BOOL); //return to course/view.php if false or mod/modname/view.php if true
38$type = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
923451c5 39$sectionreturn = optional_param('sr', null, PARAM_INT);
ba9dc077
SH
40
41$url = new moodle_url('/course/modedit.php');
a41b1d96 42$url->param('sr', $sectionreturn);
ba9dc077
SH
43if (!empty($return)) {
44 $url->param('return', $return);
45}
46
47if (!empty($add)) {
ba9dc077
SH
48 $section = required_param('section', PARAM_INT);
49 $course = required_param('course', PARAM_INT);
2b9e3bac 50
ebb32067
SV
51 $url->param('add', $add);
52 $url->param('section', $section);
53 $url->param('course', $course);
54 $PAGE->set_url($url);
55
74df2951 56 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
ba9dc077 57 require_login($course);
264867fd 58
0af463d4
SH
59 // There is no page for this in the navigation. The closest we'll have is the course section.
60 // If the course section isn't displayed on the navigation this will fall back to the course which
61 // will be the closest match we have.
62 navigation_node::override_active_url(course_get_url($course, $section));
63
dd5d933f 64 list($module, $context, $cw) = can_add_moduleinfo($course, $add, $section);
52273766 65
ba9dc077
SH
66 $cm = null;
67
fbaea88f 68 $data = new stdClass();
ba9dc077
SH
69 $data->section = $section; // The section number itself - relative!!! (section column in course_sections)
70 $data->visible = $cw->visible;
71 $data->course = $course->id;
72 $data->module = $module->id;
73 $data->modulename = $module->name;
74 $data->groupmode = $course->groupmode;
75 $data->groupingid = $course->defaultgroupingid;
76 $data->groupmembersonly = 0;
77 $data->id = '';
78 $data->instance = '';
79 $data->coursemodule = '';
80 $data->add = $add;
81 $data->return = 0; //must be false if this is an add, go back to course view on cancel
a41b1d96 82 $data->sr = $sectionreturn;
ba9dc077
SH
83
84 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
85 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
f9beaf44 86 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
ba9dc077
SH
87 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
88 }
c18269c7 89
1dea75b8
DM
90 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
91 and has_capability('moodle/grade:managegradingforms', $context)) {
bb50c376
DM
92 require_once($CFG->dirroot.'/grade/grading/lib.php');
93
94 $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
95 $areas = grading_manager::available_areas('mod_'.$module->name);
96
97 foreach ($areas as $areaname => $areatitle) {
98 $data->_advancedgradingdata['areas'][$areaname] = array(
99 'title' => $areatitle,
100 'method' => '',
101 );
102 $formfield = 'advancedgradingmethod_'.$areaname;
103 $data->{$formfield} = '';
104 }
105 }
106
ba9dc077
SH
107 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
108 $data->type = $type;
109 }
264867fd 110
7487c856 111 $sectionname = get_section_name($course, $cw);
ba9dc077 112 $fullmodulename = get_string('modulename', $module->name);
264867fd 113
ba9dc077 114 if ($data->section && $course->format != 'site') {
fbaea88f 115 $heading = new stdClass();
ba9dc077 116 $heading->what = $fullmodulename;
7487c856 117 $heading->to = $sectionname;
ba9dc077
SH
118 $pageheading = get_string('addinganewto', 'moodle', $heading);
119 } else {
120 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
121 }
0af463d4 122 $navbaraddition = $pageheading;
264867fd 123
ba9dc077 124} else if (!empty($update)) {
cb141e5a 125
ba9dc077
SH
126 $url->param('update', $update);
127 $PAGE->set_url($url);
264867fd 128
dd5d933f 129 // Check the course module exists.
ba9dc077 130 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
dd5d933f
JM
131
132 // Check the course exists.
74df2951 133 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
ba9dc077 134
dd5d933f 135 // require_login
ba9dc077 136 require_login($course, false, $cm); // needed to setup proper $COURSE
ba9dc077 137
dd5d933f 138 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
ba9dc077
SH
139
140 $data->coursemodule = $cm->id;
141 $data->section = $cw->section; // The section number itself - relative!!! (section column in course_sections)
142 $data->visible = $cm->visible; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
143 $data->cmidnumber = $cm->idnumber; // The cm IDnumber
144 $data->groupmode = groups_get_activity_groupmode($cm); // locked later if forced
145 $data->groupingid = $cm->groupingid;
146 $data->groupmembersonly = $cm->groupmembersonly;
147 $data->course = $course->id;
148 $data->module = $module->id;
149 $data->modulename = $module->name;
150 $data->instance = $cm->instance;
151 $data->return = $return;
a41b1d96 152 $data->sr = $sectionreturn;
ba9dc077
SH
153 $data->update = $update;
154 $data->completion = $cm->completion;
155 $data->completionview = $cm->completionview;
156 $data->completionexpected = $cm->completionexpected;
157 $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
8c40662e 158 $data->showdescription = $cm->showdescription;
ba9dc077
SH
159 if (!empty($CFG->enableavailability)) {
160 $data->availablefrom = $cm->availablefrom;
161 $data->availableuntil = $cm->availableuntil;
162 $data->showavailability = $cm->showavailability;
163 }
aa54ed7b 164
ba9dc077
SH
165 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
166 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
64f93798 167 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
ba9dc077
SH
168 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
169 }
dc5c2bd9 170
1dea75b8
DM
171 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
172 and has_capability('moodle/grade:managegradingforms', $context)) {
b11f9da6
DM
173 require_once($CFG->dirroot.'/grade/grading/lib.php');
174 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename);
175 $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods();
176 $areas = $gradingman->get_available_areas();
177
178 foreach ($areas as $areaname => $areatitle) {
8a4acb3a
DM
179 $gradingman->set_area($areaname);
180 $method = $gradingman->get_active_method();
b11f9da6
DM
181 $data->_advancedgradingdata['areas'][$areaname] = array(
182 'title' => $areatitle,
183 'method' => $method,
184 );
185 $formfield = 'advancedgradingmethod_'.$areaname;
186 $data->{$formfield} = $method;
187 }
188 }
189
ba9dc077
SH
190 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
191 'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
192 // add existing outcomes
193 foreach ($items as $item) {
194 if (!empty($item->outcomeid)) {
195 $data->{'outcome_'.$item->outcomeid} = 1;
71ee4471 196 }
ba9dc077 197 }
4b86bb08 198
ba9dc077
SH
199 // set category if present
200 $gradecat = false;
201 foreach ($items as $item) {
202 if ($gradecat === false) {
203 $gradecat = $item->categoryid;
204 continue;
4b86bb08 205 }
ba9dc077
SH
206 if ($gradecat != $item->categoryid) {
207 //mixed categories
208 $gradecat = false;
209 break;
4b86bb08 210 }
71ee4471 211 }
ba9dc077
SH
212 if ($gradecat !== false) {
213 // do not set if mixed categories present
214 $data->gradecat = $gradecat;
264867fd 215 }
ba9dc077 216 }
61defed9 217
7487c856 218 $sectionname = get_section_name($course, $cw);
ba9dc077 219 $fullmodulename = get_string('modulename', $module->name);
61defed9 220
ba9dc077 221 if ($data->section && $course->format != 'site') {
fbaea88f 222 $heading = new stdClass();
ba9dc077 223 $heading->what = $fullmodulename;
7487c856 224 $heading->in = $sectionname;
ba9dc077 225 $pageheading = get_string('updatingain', 'moodle', $heading);
52273766 226 } else {
ba9dc077 227 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
264867fd 228 }
0af463d4 229 $navbaraddition = null;
264867fd 230
ba9dc077
SH
231} else {
232 require_login();
233 print_error('invalidaction');
234}
235
236$pagepath = 'mod-' . $module->name . '-';
237if (!empty($type)) { //TODO: hopefully will be removed in 2.0
238 $pagepath .= $type;
239} else {
240 $pagepath .= 'mod';
241}
242$PAGE->set_pagetype($pagepath);
ae2fa74f 243$PAGE->set_pagelayout('admin');
ba9dc077
SH
244
245$modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
246if (file_exists($modmoodleform)) {
247 require_once($modmoodleform);
248} else {
249 print_error('noformdesc');
250}
251
dd5d933f 252include_modulelib($module->name);
ba9dc077
SH
253
254$mformclassname = 'mod_'.$module->name.'_mod_form';
255$mform = new $mformclassname($data, $cw->section, $cm, $course);
256$mform->set_data($data);
257
258if ($mform->is_cancelled()) {
259 if ($return && !empty($cm->id)) {
260 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
52273766 261 } else {
923451c5 262 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
ba9dc077
SH
263 }
264} else if ($fromform = $mform->get_data()) {
c85cf7df 265
ba9dc077 266 if (!empty($fromform->update)) {
dd5d933f 267 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
ba9dc077 268 } else if (!empty($fromform->add)) {
dd5d933f 269 $fromform = add_moduleinfo($fromform, $course, $mform);
ba9dc077
SH
270 } else {
271 print_error('invaliddata');
272 }
61defed9 273
ba9dc077 274 if (isset($fromform->submitbutton)) {
7bf4f6e9 275 if (empty($fromform->showgradingmanagement)) {
03d448e5
DM
276 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
277 } else {
278 $returnurl = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule));
7bf4f6e9 279 redirect($fromform->gradingman->get_management_url($returnurl));
03d448e5 280 }
52273766 281 } else {
923451c5 282 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
ba9dc077
SH
283 }
284 exit;
61defed9 285
ba9dc077 286} else {
aa6c1ced 287
ba9dc077
SH
288 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
289 $strmodulenameplural = get_string('modulenameplural', $module->name);
aa6c1ced 290
ba9dc077 291 if (!empty($cm->id)) {
9a5e297b 292 $context = context_module::instance($cm->id);
ba9dc077 293 } else {
9a5e297b 294 $context = context_course::instance($course->id);
ba9dc077
SH
295 }
296
297 $PAGE->set_heading($course->fullname);
c20b001b 298 $PAGE->set_title($streditinga);
ba9dc077 299 $PAGE->set_cacheable(false);
0af463d4
SH
300
301 if (isset($navbaraddition)) {
302 $PAGE->navbar->add($navbaraddition);
303 }
304
ba9dc077
SH
305 echo $OUTPUT->header();
306
5435c9dc 307 if (get_string_manager()->string_exists('modulename_help', $module->name)) {
ba9dc077
SH
308 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
309 } else {
e1401974 310 echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon');
264867fd 311 }
ba9dc077
SH
312
313 $mform->display();
314
315 echo $OUTPUT->footer();
5435c9dc 316}