Merge branch 'install_25_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / course / modedit.php
blob3352fb07e6b4ade9c7b9efcde8247901baa9faae
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.'/conditionlib.php');
32 require_once($CFG->libdir.'/plagiarismlib.php');
33 require_once($CFG->dirroot . '/course/modlib.php');
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
39 $sectionreturn = optional_param('sr', null, PARAM_INT);
41 $url = new moodle_url('/course/modedit.php');
42 $url->param('sr', $sectionreturn);
43 if (!empty($return)) {
44 $url->param('return', $return);
47 if (!empty($add)) {
48 $section = required_param('section', PARAM_INT);
49 $course = required_param('course', PARAM_INT);
51 $url->param('add', $add);
52 $url->param('section', $section);
53 $url->param('course', $course);
54 $PAGE->set_url($url);
56 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
57 require_login($course);
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));
64 list($module, $context, $cw) = can_add_moduleinfo($course, $add, $section);
66 $cm = null;
68 $data = new stdClass();
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
82 $data->sr = $sectionreturn;
84 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
85 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
86 file_prepare_draft_area($draftid_editor, null, null, null, null);
87 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
90 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
91 and has_capability('moodle/grade:managegradingforms', $context)) {
92 require_once($CFG->dirroot.'/grade/grading/lib.php');
94 $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
95 $areas = grading_manager::available_areas('mod_'.$module->name);
97 foreach ($areas as $areaname => $areatitle) {
98 $data->_advancedgradingdata['areas'][$areaname] = array(
99 'title' => $areatitle,
100 'method' => '',
102 $formfield = 'advancedgradingmethod_'.$areaname;
103 $data->{$formfield} = '';
107 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
108 $data->type = $type;
111 $sectionname = get_section_name($course, $cw);
112 $fullmodulename = get_string('modulename', $module->name);
114 if ($data->section && $course->format != 'site') {
115 $heading = new stdClass();
116 $heading->what = $fullmodulename;
117 $heading->to = $sectionname;
118 $pageheading = get_string('addinganewto', 'moodle', $heading);
119 } else {
120 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
122 $navbaraddition = $pageheading;
124 } else if (!empty($update)) {
126 $url->param('update', $update);
127 $PAGE->set_url($url);
129 // Check the course module exists.
130 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
132 // Check the course exists.
133 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
135 // require_login
136 require_login($course, false, $cm); // needed to setup proper $COURSE
138 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
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;
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->availablefrom = $cm->availablefrom;
161 $data->availableuntil = $cm->availableuntil;
162 $data->showavailability = $cm->showavailability;
165 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
166 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
167 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
168 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
171 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
172 and has_capability('moodle/grade:managegradingforms', $context)) {
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();
178 foreach ($areas as $areaname => $areatitle) {
179 $gradingman->set_area($areaname);
180 $method = $gradingman->get_active_method();
181 $data->_advancedgradingdata['areas'][$areaname] = array(
182 'title' => $areatitle,
183 'method' => $method,
185 $formfield = 'advancedgradingmethod_'.$areaname;
186 $data->{$formfield} = $method;
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;
199 // set category if present
200 $gradecat = false;
201 foreach ($items as $item) {
202 if ($gradecat === false) {
203 $gradecat = $item->categoryid;
204 continue;
206 if ($gradecat != $item->categoryid) {
207 //mixed categories
208 $gradecat = false;
209 break;
212 if ($gradecat !== false) {
213 // do not set if mixed categories present
214 $data->gradecat = $gradecat;
218 $sectionname = get_section_name($course, $cw);
219 $fullmodulename = get_string('modulename', $module->name);
221 if ($data->section && $course->format != 'site') {
222 $heading = new stdClass();
223 $heading->what = $fullmodulename;
224 $heading->in = $sectionname;
225 $pageheading = get_string('updatingain', 'moodle', $heading);
226 } else {
227 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
229 $navbaraddition = null;
231 } else {
232 require_login();
233 print_error('invalidaction');
236 $pagepath = 'mod-' . $module->name . '-';
237 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
238 $pagepath .= $type;
239 } else {
240 $pagepath .= 'mod';
242 $PAGE->set_pagetype($pagepath);
243 $PAGE->set_pagelayout('admin');
245 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
246 if (file_exists($modmoodleform)) {
247 require_once($modmoodleform);
248 } else {
249 print_error('noformdesc');
252 include_modulelib($module->name);
254 $mformclassname = 'mod_'.$module->name.'_mod_form';
255 $mform = new $mformclassname($data, $cw->section, $cm, $course);
256 $mform->set_data($data);
258 if ($mform->is_cancelled()) {
259 if ($return && !empty($cm->id)) {
260 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
261 } else {
262 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
264 } else if ($fromform = $mform->get_data()) {
266 if (!empty($fromform->update)) {
267 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
268 } else if (!empty($fromform->add)) {
269 $fromform = add_moduleinfo($fromform, $course, $mform);
270 } else {
271 print_error('invaliddata');
274 if (isset($fromform->submitbutton)) {
275 if (empty($fromform->showgradingmanagement)) {
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));
279 redirect($fromform->gradingman->get_management_url($returnurl));
281 } else {
282 redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
284 exit;
286 } else {
288 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
289 $strmodulenameplural = get_string('modulenameplural', $module->name);
291 if (!empty($cm->id)) {
292 $context = context_module::instance($cm->id);
293 } else {
294 $context = context_course::instance($course->id);
297 $PAGE->set_heading($course->fullname);
298 $PAGE->set_title($streditinga);
299 $PAGE->set_cacheable(false);
301 if (isset($navbaraddition)) {
302 $PAGE->navbar->add($navbaraddition);
305 echo $OUTPUT->header();
307 if (get_string_manager()->string_exists('modulename_help', $module->name)) {
308 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
309 } else {
310 echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon');
313 $mform->display();
315 echo $OUTPUT->footer();