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 * Moves, adds, updates, duplicates or deletes modules in a course
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require("../config.php");
27 require_once("lib.php");
29 $sectionreturn = optional_param('sr', null, PARAM_INT
);
30 $add = optional_param('add', '', PARAM_ALPHA
);
31 $type = optional_param('type', '', PARAM_ALPHA
);
32 $indent = optional_param('indent', 0, PARAM_INT
);
33 $update = optional_param('update', 0, PARAM_INT
);
34 $duplicate = optional_param('duplicate', 0, PARAM_INT
);
35 $hide = optional_param('hide', 0, PARAM_INT
);
36 $show = optional_param('show', 0, PARAM_INT
);
37 $copy = optional_param('copy', 0, PARAM_INT
);
38 $moveto = optional_param('moveto', 0, PARAM_INT
);
39 $movetosection = optional_param('movetosection', 0, PARAM_INT
);
40 $delete = optional_param('delete', 0, PARAM_INT
);
41 $course = optional_param('course', 0, PARAM_INT
);
42 $groupmode = optional_param('groupmode', -1, PARAM_INT
);
43 $cancelcopy = optional_param('cancelcopy', 0, PARAM_BOOL
);
44 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
46 // This page should always redirect
47 $url = new moodle_url('/course/mod.php');
48 foreach (compact('indent','update','hide','show','copy','moveto','movetosection','delete','course','cancelcopy','confirm') as $key=>$value) {
50 $url->param($key, $value);
53 $url->param('sr', $sectionreturn);
55 $url->param('add', $add);
58 $url->param('type', $type);
60 if ($groupmode !== '') {
61 $url->param('groupmode', $groupmode);
67 //check if we are adding / editing a module that has new forms using formslib
69 $id = required_param('id', PARAM_INT
);
70 $section = required_param('section', PARAM_INT
);
71 $type = optional_param('type', '', PARAM_ALPHA
);
72 $returntomod = optional_param('return', 0, PARAM_BOOL
);
74 redirect("$CFG->wwwroot/course/modedit.php?add=$add&type=$type&course=$id§ion=$section&return=$returntomod&sr=$sectionreturn");
76 } else if (!empty($update)) {
77 $cm = get_coursemodule_from_id('', $update, 0, true, MUST_EXIST
);
78 $returntomod = optional_param('return', 0, PARAM_BOOL
);
79 redirect("$CFG->wwwroot/course/modedit.php?update=$update&return=$returntomod&sr=$sectionreturn");
81 } else if (!empty($duplicate)) {
82 $cm = get_coursemodule_from_id('', $duplicate, 0, true, MUST_EXIST
);
83 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
85 require_login($course, false, $cm);
86 $coursecontext = context_course
::instance($course->id
);
87 $modcontext = context_module
::instance($cm->id
);
88 require_capability('moodle/course:manageactivities', $coursecontext);
90 if (!$confirm or !confirm_sesskey()) {
91 $PAGE->set_title(get_string('duplicate'));
92 $PAGE->set_heading($course->fullname
);
93 $PAGE->navbar
->add(get_string('duplicatinga', 'core', format_string($cm->name
)));
94 $PAGE->set_pagelayout('incourse');
97 $a->modtype
= get_string('modulename', $cm->modname
);
98 $a->modname
= format_string($cm->name
);
101 echo $OUTPUT->header();
102 echo $OUTPUT->confirm(
103 get_string('duplicateconfirm', 'core', $a),
105 new moodle_url('/course/modduplicate.php', array(
106 'cmid' => $cm->id
, 'course' => $course->id
, 'sr' => $sectionreturn)),
107 get_string('continue'),
110 course_get_url($course, $cm->sectionnum
, array('sr' => $sectionreturn)),
111 get_string('cancel'),
114 echo $OUTPUT->footer();
118 } else if (!empty($delete)) {
119 $cm = get_coursemodule_from_id('', $delete, 0, true, MUST_EXIST
);
120 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
122 require_login($course, false, $cm);
123 $coursecontext = context_course
::instance($course->id
);
124 $modcontext = context_module
::instance($cm->id
);
125 require_capability('moodle/course:manageactivities', $modcontext);
127 $return = course_get_url($course, $cm->sectionnum
, array('sr' => $sectionreturn));
129 if (!$confirm or !confirm_sesskey()) {
130 $fullmodulename = get_string('modulename', $cm->modname
);
132 $optionsyes = array('confirm'=>1, 'delete'=>$cm->id
, 'sesskey'=>sesskey(), 'sr' => $sectionreturn);
134 $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
135 $strdeletecheckfull = get_string('deletecheckfull', '', "$fullmodulename '$cm->name'");
137 $PAGE->set_pagetype('mod-' . $cm->modname
. '-delete');
138 $PAGE->set_title($strdeletecheck);
139 $PAGE->set_heading($course->fullname
);
140 $PAGE->navbar
->add($strdeletecheck);
141 echo $OUTPUT->header();
143 // print_simple_box_start('center', '60%', '#FFAAAA', 20, 'noticebox');
144 echo $OUTPUT->box_start('noticebox');
145 $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
146 $formcancel = new single_button($return, get_string('no'), 'get');
147 echo $OUTPUT->confirm($strdeletecheckfull, $formcontinue, $formcancel);
148 echo $OUTPUT->box_end();
149 echo $OUTPUT->footer();
154 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
156 if (file_exists($modlib)) {
157 require_once($modlib);
159 print_error('modulemissingcode', '', '', $modlib);
162 $deleteinstancefunction = $cm->modname
."_delete_instance";
164 if (!$deleteinstancefunction($cm->instance
)) {
165 echo $OUTPUT->notification("Could not delete the $cm->modname (instance)");
168 // remove all module files in case modules forget to do that
169 $fs = get_file_storage();
170 $fs->delete_area_files($modcontext->id
);
172 if (!delete_course_module($cm->id
)) {
173 echo $OUTPUT->notification("Could not delete the $cm->modname (coursemodule)");
175 if (!delete_mod_from_section($cm->id
, $cm->section
)) {
176 echo $OUTPUT->notification("Could not delete the $cm->modname from that section");
179 // Trigger a mod_deleted event with information about this module.
180 $eventdata = new stdClass();
181 $eventdata->modulename
= $cm->modname
;
182 $eventdata->cmid
= $cm->id
;
183 $eventdata->courseid
= $course->id
;
184 $eventdata->userid
= $USER->id
;
185 events_trigger('mod_deleted', $eventdata);
187 add_to_log($course->id
, 'course', "delete mod",
188 "view.php?id=$cm->course",
189 "$cm->modname $cm->instance", $cm->id
);
195 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
196 $cm = get_coursemodule_from_id('', $USER->activitycopy
, 0, true, MUST_EXIST
);
197 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
199 require_login($course, false, $cm);
200 $coursecontext = context_course
::instance($course->id
);
201 $modcontext = context_module
::instance($cm->id
);
202 require_capability('moodle/course:manageactivities', $modcontext);
204 if (!empty($movetosection)) {
205 if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course
))) {
206 print_error('sectionnotexist');
210 } else { // normal moveto
211 if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course
, true)) {
212 print_error('invalidcoursemodule');
214 if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section
, 'course'=>$cm->course
))) {
215 print_error('sectionnotexist');
219 if (!ismoving($section->course
)) {
220 print_error('needcopy', '', "view.php?id=$section->course");
223 moveto_module($cm, $section, $beforecm);
225 $sectionreturn = $USER->activitycopysectionreturn
;
226 unset($USER->activitycopy
);
227 unset($USER->activitycopycourse
);
228 unset($USER->activitycopyname
);
229 unset($USER->activitycopysectionreturn
);
231 redirect(course_get_url($course, $section->section
, array('sr' => $sectionreturn)));
233 } else if (!empty($indent) and confirm_sesskey()) {
234 $id = required_param('id', PARAM_INT
);
236 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST
);
237 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
239 require_login($course, false, $cm);
240 $coursecontext = context_course
::instance($course->id
);
241 $modcontext = context_module
::instance($cm->id
);
242 require_capability('moodle/course:manageactivities', $modcontext);
244 $cm->indent +
= $indent;
246 if ($cm->indent
< 0) {
250 $DB->set_field('course_modules', 'indent', $cm->indent
, array('id'=>$cm->id
));
252 rebuild_course_cache($cm->course
);
254 redirect(course_get_url($course, $cm->sectionnum
, array('sr' => $sectionreturn)));
256 } else if (!empty($hide) and confirm_sesskey()) {
257 $cm = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST
);
258 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
260 require_login($course, false, $cm);
261 $coursecontext = context_course
::instance($course->id
);
262 $modcontext = context_module
::instance($cm->id
);
263 require_capability('moodle/course:activityvisibility', $modcontext);
265 set_coursemodule_visible($cm->id
, 0);
267 redirect(course_get_url($course, $cm->sectionnum
, array('sr' => $sectionreturn)));
269 } else if (!empty($show) and confirm_sesskey()) {
270 $cm = get_coursemodule_from_id('', $show, 0, true, MUST_EXIST
);
271 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
273 require_login($course, false, $cm);
274 $coursecontext = context_course
::instance($course->id
);
275 $modcontext = context_module
::instance($cm->id
);
276 require_capability('moodle/course:activityvisibility', $modcontext);
278 $section = $DB->get_record('course_sections', array('id'=>$cm->section
), '*', MUST_EXIST
);
280 $module = $DB->get_record('modules', array('id'=>$cm->module
), '*', MUST_EXIST
);
282 if ($module->visible
and ($section->visible
or (SITEID
== $cm->course
))) {
283 set_coursemodule_visible($cm->id
, 1);
286 redirect(course_get_url($course, $section->section
, array('sr' => $sectionreturn)));
288 } else if ($groupmode > -1 and confirm_sesskey()) {
289 $id = required_param('id', PARAM_INT
);
291 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST
);
292 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
294 require_login($course, false, $cm);
295 $coursecontext = context_course
::instance($course->id
);
296 $modcontext = context_module
::instance($cm->id
);
297 require_capability('moodle/course:manageactivities', $modcontext);
299 set_coursemodule_groupmode($cm->id
, $groupmode);
301 redirect(course_get_url($course, $cm->sectionnum
, array('sr' => $sectionreturn)));
303 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
304 $cm = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST
);
305 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
307 require_login($course, false, $cm);
308 $coursecontext = context_course
::instance($course->id
);
309 $modcontext = context_module
::instance($cm->id
);
310 require_capability('moodle/course:manageactivities', $modcontext);
312 $section = $DB->get_record('course_sections', array('id'=>$cm->section
), '*', MUST_EXIST
);
314 $USER->activitycopy
= $copy;
315 $USER->activitycopycourse
= $cm->course
;
316 $USER->activitycopyname
= $cm->name
;
317 $USER->activitycopysectionreturn
= $sectionreturn;
319 redirect(course_get_url($course, $section->section
, array('sr' => $sectionreturn)));
321 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
323 $courseid = $USER->activitycopycourse
;
324 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
326 $cm = get_coursemodule_from_id('', $USER->activitycopy
, 0, true, IGNORE_MISSING
);
327 $sectionreturn = $USER->activitycopysectionreturn
;
328 unset($USER->activitycopy
);
329 unset($USER->activitycopycourse
);
330 unset($USER->activitycopyname
);
331 unset($USER->activitycopysectionreturn
);
332 redirect(course_get_url($course, $cm->sectionnum
, array('sr' => $sectionreturn)));
334 print_error('unknowaction');