Merge branch 'MDL-29528_20' of git://github.com/timhunt/moodle into MOODLE_20_STABLE
[moodle.git] / course / mod.php
blob062760f08ba9fe40610176f7fdf8e0c7d55025dc
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 * 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
23 * @package course
26 require("../config.php");
27 require_once("lib.php");
29 require_login();
31 $sectionreturn = optional_param('sr', '', PARAM_INT);
32 $add = optional_param('add', '', PARAM_ALPHA);
33 $type = optional_param('type', '', PARAM_ALPHA);
34 $indent = optional_param('indent', 0, PARAM_INT);
35 $update = optional_param('update', 0, PARAM_INT);
36 $hide = optional_param('hide', 0, PARAM_INT);
37 $show = optional_param('show', 0, PARAM_INT);
38 $copy = optional_param('copy', 0, PARAM_INT);
39 $moveto = optional_param('moveto', 0, PARAM_INT);
40 $movetosection = optional_param('movetosection', 0, PARAM_INT);
41 $delete = optional_param('delete', 0, PARAM_INT);
42 $course = optional_param('course', 0, PARAM_INT);
43 $groupmode = optional_param('groupmode', -1, PARAM_INT);
44 $cancelcopy = optional_param('cancelcopy', 0, PARAM_BOOL);
45 $confirm = optional_param('confirm', 0, PARAM_BOOL);
47 // This page should always redirect
48 $url = new moodle_url('/course/mod.php');
49 foreach (compact('indent','update','hide','show','copy','moveto','movetosection','delete','course','cancelcopy','confirm') as $key=>$value) {
50 if ($value !== 0) {
51 $url->param($key, $value);
54 if ($sectionreturn !== '') {
55 $url->param('sr', $sectionreturn);
57 if ($add !== '') {
58 $url->param('add', $add);
60 if ($type !== '') {
61 $url->param('type', $type);
63 if ($groupmode !== '') {
64 $url->param('groupmode', $groupmode);
66 $PAGE->set_url($url);
68 //check if we are adding / editing a module that has new forms using formslib
69 if (!empty($add)) {
70 $id = required_param('id', PARAM_INT);
71 $section = required_param('section', PARAM_INT);
72 $type = optional_param('type', '', PARAM_ALPHA);
73 $returntomod = optional_param('return', 0, PARAM_BOOL);
75 redirect("$CFG->wwwroot/course/modedit.php?add=$add&type=$type&course=$id&section=$section&return=$returntomod");
77 } else if (!empty($update)) {
78 if (!$cm = get_coursemodule_from_id('', $update, 0, true)) {
79 print_error('invalidcoursemodule');
81 $returntomod = optional_param('return', 0, PARAM_BOOL);
82 redirect("$CFG->wwwroot/course/modedit.php?update=$update&return=$returntomod");
84 } else if (!empty($delete)) {
85 if (!$cm = get_coursemodule_from_id('', $delete, 0, true)) {
86 print_error('invalidcoursemodule');
89 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
90 print_error('invalidcourseid');
92 require_login($course->id); // needed to setup proper $COURSE
93 $context = get_context_instance(CONTEXT_COURSE, $course->id);
94 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
95 require_capability('moodle/course:manageactivities', $context);
97 $return = "$CFG->wwwroot/course/view.php?id=$cm->course#section-$cm->sectionnum";
99 if (!$confirm or !confirm_sesskey()) {
100 $fullmodulename = get_string('modulename', $cm->modname);
102 $optionsyes = array('confirm'=>1, 'delete'=>$cm->id, 'sesskey'=>sesskey());
103 $optionsno = array('id'=>$cm->course);
105 $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
106 $strdeletecheckfull = get_string('deletecheckfull', '', "$fullmodulename '$cm->name'");
108 $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
109 $PAGE->set_title($strdeletecheck);
110 $PAGE->set_heading($course->fullname);
111 $PAGE->navbar->add($strdeletecheck);
112 echo $OUTPUT->header();
114 // print_simple_box_start('center', '60%', '#FFAAAA', 20, 'noticebox');
115 echo $OUTPUT->box_start('noticebox');
116 $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
117 $formcancel = new single_button(new moodle_url($return, $optionsno), get_string('no'), 'get');
118 echo $OUTPUT->confirm($strdeletecheckfull, $formcontinue, $formcancel);
119 echo $OUTPUT->box_end();
120 echo $OUTPUT->footer();
122 exit;
125 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
127 if (file_exists($modlib)) {
128 require_once($modlib);
129 } else {
130 print_error('modulemissingcode', '', '', $modlib);
133 $deleteinstancefunction = $cm->modname."_delete_instance";
135 if (!$deleteinstancefunction($cm->instance)) {
136 echo $OUTPUT->notification("Could not delete the $cm->modname (instance)");
139 // remove all module files in case modules forget to do that
140 $fs = get_file_storage();
141 $fs->delete_area_files($modcontext->id);
143 if (!delete_course_module($cm->id)) {
144 echo $OUTPUT->notification("Could not delete the $cm->modname (coursemodule)");
146 if (!delete_mod_from_section($cm->id, $cm->section)) {
147 echo $OUTPUT->notification("Could not delete the $cm->modname from that section");
150 // Trigger a mod_deleted event with information about this module.
151 $eventdata = new stdClass();
152 $eventdata->modulename = $cm->modname;
153 $eventdata->cmid = $cm->id;
154 $eventdata->courseid = $course->id;
155 $eventdata->userid = $USER->id;
156 events_trigger('mod_deleted', $eventdata);
158 add_to_log($course->id, 'course', "delete mod",
159 "view.php?id=$cm->course",
160 "$cm->modname $cm->instance", $cm->id);
162 rebuild_course_cache($course->id);
164 redirect($return);
168 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
169 if (!$cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true)) {
170 print_error('invalidcoursemodule');
173 if (!empty($movetosection)) {
174 if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
175 print_error('sectionnotexist');
177 $beforecm = NULL;
179 } else { // normal moveto
180 if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
181 print_error('invalidcoursemodule');
183 if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
184 print_error('sectionnotexist');
188 require_login($section->course); // needed to setup proper $COURSE
189 $context = get_context_instance(CONTEXT_COURSE, $section->course);
190 require_capability('moodle/course:manageactivities', $context);
192 if (!ismoving($section->course)) {
193 print_error('needcopy', '', "view.php?id=$section->course");
196 moveto_module($cm, $section, $beforecm);
198 unset($USER->activitycopy);
199 unset($USER->activitycopycourse);
200 unset($USER->activitycopyname);
202 rebuild_course_cache($section->course);
204 if (SITEID == $section->course) {
205 redirect($CFG->wwwroot);
206 } else {
207 redirect("view.php?id=$section->course#section-$sectionreturn");
210 } else if (!empty($indent) and confirm_sesskey()) {
211 $id = required_param('id', PARAM_INT);
212 if (!$cm = get_coursemodule_from_id('', $id, 0, true)) {
213 print_error('invalidcoursemodule');
216 require_login($cm->course); // needed to setup proper $COURSE
217 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
218 require_capability('moodle/course:manageactivities', $context);
220 $cm->indent += $indent;
222 if ($cm->indent < 0) {
223 $cm->indent = 0;
226 $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
228 rebuild_course_cache($cm->course);
230 if (SITEID == $cm->course) {
231 redirect($CFG->wwwroot);
232 } else {
233 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
236 } else if (!empty($hide) and confirm_sesskey()) {
237 if (!$cm = get_coursemodule_from_id('', $hide, 0, true)) {
238 print_error('invalidcoursemodule');
241 require_login($cm->course); // needed to setup proper $COURSE
242 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
243 require_capability('moodle/course:activityvisibility', $context);
245 set_coursemodule_visible($cm->id, 0);
247 rebuild_course_cache($cm->course);
249 if (SITEID == $cm->course) {
250 redirect($CFG->wwwroot);
251 } else {
252 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
255 } else if (!empty($show) and confirm_sesskey()) {
256 if (!$cm = get_coursemodule_from_id('', $show, 0, true)) {
257 print_error('invalidcoursemodule');
260 require_login($cm->course); // needed to setup proper $COURSE
261 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
262 require_capability('moodle/course:activityvisibility', $context);
264 if (!$section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
265 print_error('sectionnotexist');
268 if (!$module = $DB->get_record('modules', array('id'=>$cm->module))) {
269 print_error('moduledoesnotexist');
272 if ($module->visible and ($section->visible or (SITEID == $cm->course))) {
273 set_coursemodule_visible($cm->id, 1);
274 rebuild_course_cache($cm->course);
277 if (SITEID == $cm->course) {
278 redirect($CFG->wwwroot);
279 } else {
280 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
283 } else if ($groupmode > -1 and confirm_sesskey()) {
284 $id = required_param('id', PARAM_INT);
285 if (!$cm = get_coursemodule_from_id('', $id, 0, true)) {
286 print_error('invalidcoursemodule');
289 require_login($cm->course); // needed to setup proper $COURSE
290 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
291 require_capability('moodle/course:manageactivities', $context);
293 set_coursemodule_groupmode($cm->id, $groupmode);
295 rebuild_course_cache($cm->course);
297 if (SITEID == $cm->course) {
298 redirect($CFG->wwwroot);
299 } else {
300 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
303 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
304 if (!$cm = get_coursemodule_from_id('', $copy, 0, true)) {
305 print_error('invalidcoursemodule');
308 require_login($cm->course); // needed to setup proper $COURSE
309 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
310 require_capability('moodle/course:manageactivities', $context);
312 if (!$section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
313 print_error('sectionnotexist');
316 $USER->activitycopy = $copy;
317 $USER->activitycopycourse = $cm->course;
318 $USER->activitycopyname = $cm->name;
320 redirect("view.php?id=$cm->course#section-$sectionreturn");
322 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
324 $courseid = $USER->activitycopycourse;
326 unset($USER->activitycopy);
327 unset($USER->activitycopycourse);
328 unset($USER->activitycopyname);
330 redirect("view.php?id=$courseid");
332 } else {
333 print_error('unknowaction');