MDL-52252 Tags: Add tags to modules (Resources and Activities)
[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');
bce59524 31require_once($CFG->libdir.'/plagiarismlib.php');
80fe0c19 32require_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
42if (!empty($return)) {
43 $url->param('return', $return);
44}
45
46if (!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
dd5d933f 63 list($module, $context, $cw) = can_add_moduleinfo($course, $add, $section);
52273766 64
ba9dc077
SH
65 $cm = null;
66
fbaea88f 67 $data = new stdClass();
ba9dc077
SH
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;
ba9dc077
SH
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
a41b1d96 80 $data->sr = $sectionreturn;
ba9dc077
SH
81
82 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
83 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
f9beaf44 84 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
ba9dc077
SH
85 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
86 }
c18269c7 87
1dea75b8
DM
88 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
89 and has_capability('moodle/grade:managegradingforms', $context)) {
bb50c376
DM
90 require_once($CFG->dirroot.'/grade/grading/lib.php');
91
92 $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
93 $areas = grading_manager::available_areas('mod_'.$module->name);
94
95 foreach ($areas as $areaname => $areatitle) {
96 $data->_advancedgradingdata['areas'][$areaname] = array(
97 'title' => $areatitle,
98 'method' => '',
99 );
100 $formfield = 'advancedgradingmethod_'.$areaname;
101 $data->{$formfield} = '';
102 }
103 }
104
ba9dc077
SH
105 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
106 $data->type = $type;
107 }
264867fd 108
7487c856 109 $sectionname = get_section_name($course, $cw);
ba9dc077 110 $fullmodulename = get_string('modulename', $module->name);
264867fd 111
ba9dc077 112 if ($data->section && $course->format != 'site') {
fbaea88f 113 $heading = new stdClass();
ba9dc077 114 $heading->what = $fullmodulename;
7487c856 115 $heading->to = $sectionname;
ba9dc077
SH
116 $pageheading = get_string('addinganewto', 'moodle', $heading);
117 } else {
118 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
119 }
0af463d4 120 $navbaraddition = $pageheading;
264867fd 121
ba9dc077 122} else if (!empty($update)) {
cb141e5a 123
ba9dc077
SH
124 $url->param('update', $update);
125 $PAGE->set_url($url);
264867fd 126
d5814f4e
PS
127 // Select the "Edit settings" from navigation.
128 navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
129
dd5d933f 130 // Check the course module exists.
ba9dc077 131 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
dd5d933f
JM
132
133 // Check the course exists.
74df2951 134 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
ba9dc077 135
dd5d933f 136 // require_login
ba9dc077 137 require_login($course, false, $cm); // needed to setup proper $COURSE
ba9dc077 138
dd5d933f 139 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
ba9dc077
SH
140
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;
ba9dc077
SH
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;
dffcf46f 159 $data->tags = core_tag_tag::get_item_tags_array('core', 'course_modules', $cm->id);
ba9dc077 160 if (!empty($CFG->enableavailability)) {
400c0fd2 161 $data->availabilityconditionsjson = $cm->availability;
ba9dc077 162 }
aa54ed7b 163
ba9dc077
SH
164 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
165 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
64f93798 166 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
ba9dc077
SH
167 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
168 }
dc5c2bd9 169
1dea75b8
DM
170 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
171 and has_capability('moodle/grade:managegradingforms', $context)) {
b11f9da6
DM
172 require_once($CFG->dirroot.'/grade/grading/lib.php');
173 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename);
174 $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods();
175 $areas = $gradingman->get_available_areas();
176
177 foreach ($areas as $areaname => $areatitle) {
8a4acb3a
DM
178 $gradingman->set_area($areaname);
179 $method = $gradingman->get_active_method();
b11f9da6
DM
180 $data->_advancedgradingdata['areas'][$areaname] = array(
181 'title' => $areatitle,
182 'method' => $method,
183 );
184 $formfield = 'advancedgradingmethod_'.$areaname;
185 $data->{$formfield} = $method;
186 }
187 }
188
ba9dc077
SH
189 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
190 'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
5d0aa9dc 191 // Add existing outcomes.
ba9dc077 192 foreach ($items as $item) {
5d0aa9dc
MN
193 if (!empty($item->outcomeid)) {
194 $data->{'outcome_' . $item->outcomeid} = 1;
195 } else if (!empty($item->gradepass)) {
8164fad4
GF
196 $decimalpoints = $item->get_decimals();
197 $data->gradepass = format_float($item->gradepass, $decimalpoints);
198 }
ba9dc077 199 }
4b86bb08 200
ba9dc077
SH
201 // set category if present
202 $gradecat = false;
203 foreach ($items as $item) {
204 if ($gradecat === false) {
205 $gradecat = $item->categoryid;
206 continue;
4b86bb08 207 }
ba9dc077
SH
208 if ($gradecat != $item->categoryid) {
209 //mixed categories
210 $gradecat = false;
211 break;
4b86bb08 212 }
71ee4471 213 }
ba9dc077
SH
214 if ($gradecat !== false) {
215 // do not set if mixed categories present
216 $data->gradecat = $gradecat;
264867fd 217 }
ba9dc077 218 }
61defed9 219
7487c856 220 $sectionname = get_section_name($course, $cw);
ba9dc077 221 $fullmodulename = get_string('modulename', $module->name);
61defed9 222
ba9dc077 223 if ($data->section && $course->format != 'site') {
fbaea88f 224 $heading = new stdClass();
ba9dc077 225 $heading->what = $fullmodulename;
7487c856 226 $heading->in = $sectionname;
ba9dc077 227 $pageheading = get_string('updatingain', 'moodle', $heading);
52273766 228 } else {
ba9dc077 229 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
264867fd 230 }
0af463d4 231 $navbaraddition = null;
264867fd 232
ba9dc077
SH
233} else {
234 require_login();
235 print_error('invalidaction');
236}
237
238$pagepath = 'mod-' . $module->name . '-';
239if (!empty($type)) { //TODO: hopefully will be removed in 2.0
240 $pagepath .= $type;
241} else {
242 $pagepath .= 'mod';
243}
244$PAGE->set_pagetype($pagepath);
ae2fa74f 245$PAGE->set_pagelayout('admin');
ba9dc077
SH
246
247$modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
248if (file_exists($modmoodleform)) {
249 require_once($modmoodleform);
250} else {
251 print_error('noformdesc');
252}
253
ba9dc077
SH
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()) {
9758da6e
MN
265 // Convert the grade pass value - we may be using a language which uses commas,
266 // rather than decimal points, in numbers. These need to be converted so that
267 // they can be added to the DB.
268 if (isset($fromform->gradepass)) {
269 $fromform->gradepass = unformat_float($fromform->gradepass);
270 }
c85cf7df 271
ba9dc077 272 if (!empty($fromform->update)) {
dd5d933f 273 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
ba9dc077 274 } else if (!empty($fromform->add)) {
dd5d933f 275 $fromform = add_moduleinfo($fromform, $course, $mform);
ba9dc077
SH
276 } else {
277 print_error('invaliddata');
278 }
61defed9 279
ba9dc077 280 if (isset($fromform->submitbutton)) {
7bf4f6e9 281 if (empty($fromform->showgradingmanagement)) {
03d448e5
DM
282 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
283 } else {
284 $returnurl = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule));
7bf4f6e9 285 redirect($fromform->gradingman->get_management_url($returnurl));
03d448e5 286 }
52273766 287 } else {
923451c5 288 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
ba9dc077
SH
289 }
290 exit;
61defed9 291
ba9dc077 292} else {
aa6c1ced 293
ba9dc077
SH
294 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
295 $strmodulenameplural = get_string('modulenameplural', $module->name);
aa6c1ced 296
ba9dc077 297 if (!empty($cm->id)) {
9a5e297b 298 $context = context_module::instance($cm->id);
ba9dc077 299 } else {
9a5e297b 300 $context = context_course::instance($course->id);
ba9dc077
SH
301 }
302
303 $PAGE->set_heading($course->fullname);
c20b001b 304 $PAGE->set_title($streditinga);
ba9dc077 305 $PAGE->set_cacheable(false);
0af463d4
SH
306
307 if (isset($navbaraddition)) {
308 $PAGE->navbar->add($navbaraddition);
309 }
310
ba9dc077
SH
311 echo $OUTPUT->header();
312
5435c9dc 313 if (get_string_manager()->string_exists('modulename_help', $module->name)) {
ba9dc077
SH
314 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
315 } else {
e1401974 316 echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon');
264867fd 317 }
ba9dc077
SH
318
319 $mform->display();
320
321 echo $OUTPUT->footer();
5435c9dc 322}