3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Adds or updates modules in a course using new formslib
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);
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);
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);
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
;
77 $data->coursemodule
= '';
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,
100 $formfield = 'advancedgradingmethod_'.$areaname;
101 $data->{$formfield} = '';
105 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
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);
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
);
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,
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->gradepass
)) {
193 $decimalpoints = $item->get_decimals();
194 $data->gradepass
= format_float($item->gradepass
, $decimalpoints);
196 if (!empty($item->outcomeid
)) {
197 $data->{'outcome_'.$item->outcomeid
} = 1;
201 // set category if present
203 foreach ($items as $item) {
204 if ($gradecat === false) {
205 $gradecat = $item->categoryid
;
208 if ($gradecat != $item->categoryid
) {
214 if ($gradecat !== false) {
215 // do not set if mixed categories present
216 $data->gradecat
= $gradecat;
220 $sectionname = get_section_name($course, $cw);
221 $fullmodulename = get_string('modulename', $module->name
);
223 if ($data->section
&& $course->format
!= 'site') {
224 $heading = new stdClass();
225 $heading->what
= $fullmodulename;
226 $heading->in
= $sectionname;
227 $pageheading = get_string('updatingain', 'moodle', $heading);
229 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
231 $navbaraddition = null;
235 print_error('invalidaction');
238 $pagepath = 'mod-' . $module->name
. '-';
239 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
244 $PAGE->set_pagetype($pagepath);
245 $PAGE->set_pagelayout('admin');
247 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
248 if (file_exists($modmoodleform)) {
249 require_once($modmoodleform);
251 print_error('noformdesc');
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");
262 redirect(course_get_url($course, $cw->section
, array('sr' => $sectionreturn)));
264 } else if ($fromform = $mform->get_data()) {
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
);
272 if (!empty($fromform->update
)) {
273 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
274 } else if (!empty($fromform->add
)) {
275 $fromform = add_moduleinfo($fromform, $course, $mform);
277 print_error('invaliddata');
280 if (isset($fromform->submitbutton
)) {
281 if (empty($fromform->showgradingmanagement
)) {
282 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
284 $returnurl = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule
));
285 redirect($fromform->gradingman
->get_management_url($returnurl));
288 redirect(course_get_url($course, $cw->section
, array('sr' => $sectionreturn)));
294 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
295 $strmodulenameplural = get_string('modulenameplural', $module->name
);
297 if (!empty($cm->id
)) {
298 $context = context_module
::instance($cm->id
);
300 $context = context_course
::instance($course->id
);
303 $PAGE->set_heading($course->fullname
);
304 $PAGE->set_title($streditinga);
305 $PAGE->set_cacheable(false);
307 if (isset($navbaraddition)) {
308 $PAGE->navbar
->add($navbaraddition);
311 echo $OUTPUT->header();
313 if (get_string_manager()->string_exists('modulename_help', $module->name
)) {
314 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name
, 'icon');
316 echo $OUTPUT->heading_with_help($pageheading, '', $module->name
, 'icon');
321 echo $OUTPUT->footer();