Merge branch 'MDL-54892-MOODLE_30_STABLE' of https://github.com/tobiasreischmann...
[moodle.git] / course / modedit.php
blob65880f9bcf189b543d4f5017d671169f73e55336
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) = can_add_moduleinfo($course, $add, $section);
65 $cm = null;
67 $data = new stdClass();
68 $data->section = $section; // The section number itself - relative!!! (section column in course_sections)
69 $data->visible = $cw->visible;
70 $data->course = $course->id;
71 $data->module = $module->id;
72 $data->modulename = $module->name;
73 $data->groupmode = $course->groupmode;
74 $data->groupingid = $course->defaultgroupingid;
75 $data->id = '';
76 $data->instance = '';
77 $data->coursemodule = '';
78 $data->add = $add;
79 $data->return = 0; //must be false if this is an add, go back to course view on cancel
80 $data->sr = $sectionreturn;
82 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
83 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
84 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
85 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
88 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
89 and has_capability('moodle/grade:managegradingforms', $context)) {
90 require_once($CFG->dirroot.'/grade/grading/lib.php');
92 $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
93 $areas = grading_manager::available_areas('mod_'.$module->name);
95 foreach ($areas as $areaname => $areatitle) {
96 $data->_advancedgradingdata['areas'][$areaname] = array(
97 'title' => $areatitle,
98 'method' => '',
100 $formfield = 'advancedgradingmethod_'.$areaname;
101 $data->{$formfield} = '';
105 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
106 $data->type = $type;
109 $sectionname = get_section_name($course, $cw);
110 $fullmodulename = get_string('modulename', $module->name);
112 if ($data->section && $course->format != 'site') {
113 $heading = new stdClass();
114 $heading->what = $fullmodulename;
115 $heading->to = $sectionname;
116 $pageheading = get_string('addinganewto', 'moodle', $heading);
117 } else {
118 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
120 $navbaraddition = $pageheading;
122 } else if (!empty($update)) {
124 $url->param('update', $update);
125 $PAGE->set_url($url);
127 // Select the "Edit settings" from navigation.
128 navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
130 // Check the course module exists.
131 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
133 // Check the course exists.
134 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
136 // require_login
137 require_login($course, false, $cm); // needed to setup proper $COURSE
139 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
141 $data->coursemodule = $cm->id;
142 $data->section = $cw->section; // The section number itself - relative!!! (section column in course_sections)
143 $data->visible = $cm->visible; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
144 $data->cmidnumber = $cm->idnumber; // The cm IDnumber
145 $data->groupmode = groups_get_activity_groupmode($cm); // locked later if forced
146 $data->groupingid = $cm->groupingid;
147 $data->course = $course->id;
148 $data->module = $module->id;
149 $data->modulename = $module->name;
150 $data->instance = $cm->instance;
151 $data->return = $return;
152 $data->sr = $sectionreturn;
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;
158 $data->showdescription = $cm->showdescription;
159 if (!empty($CFG->enableavailability)) {
160 $data->availabilityconditionsjson = $cm->availability;
163 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
164 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
165 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
166 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
169 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
170 and has_capability('moodle/grade:managegradingforms', $context)) {
171 require_once($CFG->dirroot.'/grade/grading/lib.php');
172 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename);
173 $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods();
174 $areas = $gradingman->get_available_areas();
176 foreach ($areas as $areaname => $areatitle) {
177 $gradingman->set_area($areaname);
178 $method = $gradingman->get_active_method();
179 $data->_advancedgradingdata['areas'][$areaname] = array(
180 'title' => $areatitle,
181 'method' => $method,
183 $formfield = 'advancedgradingmethod_'.$areaname;
184 $data->{$formfield} = $method;
188 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
189 'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
190 // Add existing outcomes.
191 foreach ($items as $item) {
192 if (!empty($item->outcomeid)) {
193 $data->{'outcome_' . $item->outcomeid} = 1;
194 } else if (!empty($item->gradepass)) {
195 $decimalpoints = $item->get_decimals();
196 $data->gradepass = format_float($item->gradepass, $decimalpoints);
200 // set category if present
201 $gradecat = false;
202 foreach ($items as $item) {
203 if ($gradecat === false) {
204 $gradecat = $item->categoryid;
205 continue;
207 if ($gradecat != $item->categoryid) {
208 //mixed categories
209 $gradecat = false;
210 break;
213 if ($gradecat !== false) {
214 // do not set if mixed categories present
215 $data->gradecat = $gradecat;
219 $sectionname = get_section_name($course, $cw);
220 $fullmodulename = get_string('modulename', $module->name);
222 if ($data->section && $course->format != 'site') {
223 $heading = new stdClass();
224 $heading->what = $fullmodulename;
225 $heading->in = $sectionname;
226 $pageheading = get_string('updatingain', 'moodle', $heading);
227 } else {
228 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
230 $navbaraddition = null;
232 } else {
233 require_login();
234 print_error('invalidaction');
237 $pagepath = 'mod-' . $module->name . '-';
238 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
239 $pagepath .= $type;
240 } else {
241 $pagepath .= 'mod';
243 $PAGE->set_pagetype($pagepath);
244 $PAGE->set_pagelayout('admin');
246 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
247 if (file_exists($modmoodleform)) {
248 require_once($modmoodleform);
249 } else {
250 print_error('noformdesc');
253 $mformclassname = 'mod_'.$module->name.'_mod_form';
254 $mform = new $mformclassname($data, $cw->section, $cm, $course);
255 $mform->set_data($data);
257 if ($mform->is_cancelled()) {
258 if ($return && !empty($cm->id)) {
259 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
260 } else {
261 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
263 } else if ($fromform = $mform->get_data()) {
264 // Convert the grade pass value - we may be using a language which uses commas,
265 // rather than decimal points, in numbers. These need to be converted so that
266 // they can be added to the DB.
267 if (isset($fromform->gradepass)) {
268 $fromform->gradepass = unformat_float($fromform->gradepass);
271 if (!empty($fromform->update)) {
272 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
273 } else if (!empty($fromform->add)) {
274 $fromform = add_moduleinfo($fromform, $course, $mform);
275 } else {
276 print_error('invaliddata');
279 if (isset($fromform->submitbutton)) {
280 if (empty($fromform->showgradingmanagement)) {
281 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
282 } else {
283 $returnurl = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule));
284 redirect($fromform->gradingman->get_management_url($returnurl));
286 } else {
287 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
289 exit;
291 } else {
293 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
294 $strmodulenameplural = get_string('modulenameplural', $module->name);
296 if (!empty($cm->id)) {
297 $context = context_module::instance($cm->id);
298 } else {
299 $context = context_course::instance($course->id);
302 $PAGE->set_heading($course->fullname);
303 $PAGE->set_title($streditinga);
304 $PAGE->set_cacheable(false);
306 if (isset($navbaraddition)) {
307 $PAGE->navbar->add($navbaraddition);
310 echo $OUTPUT->header();
312 if (get_string_manager()->string_exists('modulename_help', $module->name)) {
313 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
314 } else {
315 echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon');
318 $mform->display();
320 echo $OUTPUT->footer();