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, $cm, $data) = prepare_new_moduleinfo_data($course, $add, $section);
65 $data->sr
= $sectionreturn;
67 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
71 $sectionname = get_section_name($course, $cw);
72 $fullmodulename = get_string('modulename', $module->name
);
74 if ($data->section
&& $course->format
!= 'site') {
75 $heading = new stdClass();
76 $heading->what
= $fullmodulename;
77 $heading->to
= $sectionname;
78 $pageheading = get_string('addinganewto', 'moodle', $heading);
80 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
82 $navbaraddition = $pageheading;
84 } else if (!empty($update)) {
86 $url->param('update', $update);
89 // Select the "Edit settings" from navigation.
90 navigation_node
::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
92 // Check the course module exists.
93 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST
);
95 // Check the course exists.
96 $course = $DB->get_record('course', array('id'=>$cm->course
), '*', MUST_EXIST
);
99 require_login($course, false, $cm); // needed to setup proper $COURSE
101 list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
102 $data->return = $return;
103 $data->sr
= $sectionreturn;
104 $data->update
= $update;
106 $sectionname = get_section_name($course, $cw);
107 $fullmodulename = get_string('modulename', $module->name
);
109 if ($data->section
&& $course->format
!= 'site') {
110 $heading = new stdClass();
111 $heading->what
= $fullmodulename;
112 $heading->in
= $sectionname;
113 $pageheading = get_string('updatingain', 'moodle', $heading);
115 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
117 $navbaraddition = null;
121 print_error('invalidaction');
124 $pagepath = 'mod-' . $module->name
. '-';
125 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
130 $PAGE->set_pagetype($pagepath);
131 $PAGE->set_pagelayout('admin');
133 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
134 if (file_exists($modmoodleform)) {
135 require_once($modmoodleform);
137 print_error('noformdesc');
140 $mformclassname = 'mod_'.$module->name
.'_mod_form';
141 $mform = new $mformclassname($data, $cw->section
, $cm, $course);
142 $mform->set_data($data);
144 if ($mform->is_cancelled()) {
145 if ($return && !empty($cm->id
)) {
146 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
148 redirect(course_get_url($course, $cw->section
, array('sr' => $sectionreturn)));
150 } else if ($fromform = $mform->get_data()) {
151 // Convert the grade pass value - we may be using a language which uses commas,
152 // rather than decimal points, in numbers. These need to be converted so that
153 // they can be added to the DB.
154 if (isset($fromform->gradepass
)) {
155 $fromform->gradepass
= unformat_float($fromform->gradepass
);
158 if (!empty($fromform->update
)) {
159 list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
160 } else if (!empty($fromform->add
)) {
161 $fromform = add_moduleinfo($fromform, $course, $mform);
163 print_error('invaliddata');
166 if (isset($fromform->submitbutton
)) {
167 if (empty($fromform->showgradingmanagement
)) {
168 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
170 $returnurl = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule
));
171 redirect($fromform->gradingman
->get_management_url($returnurl));
174 redirect(course_get_url($course, $cw->section
, array('sr' => $sectionreturn)));
180 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
181 $strmodulenameplural = get_string('modulenameplural', $module->name
);
183 if (!empty($cm->id
)) {
184 $context = context_module
::instance($cm->id
);
186 $context = context_course
::instance($course->id
);
189 $PAGE->set_heading($course->fullname
);
190 $PAGE->set_title($streditinga);
191 $PAGE->set_cacheable(false);
193 if (isset($navbaraddition)) {
194 $PAGE->navbar
->add($navbaraddition);
197 echo $OUTPUT->header();
199 if (get_string_manager()->string_exists('modulename_help', $module->name
)) {
200 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name
, 'icon');
202 echo $OUTPUT->heading_with_help($pageheading, '', $module->name
, 'icon');
207 echo $OUTPUT->footer();