Merge branch 'MDL-29847_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / course / mod.php
blob365c7e65182b326ed0810020dcccd4a598eefc05
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 $sectionreturn = optional_param('sr', '', 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) {
49 if ($value !== 0) {
50 $url->param($key, $value);
53 if ($sectionreturn !== '') {
54 $url->param('sr', $sectionreturn);
56 if ($add !== '') {
57 $url->param('add', $add);
59 if ($type !== '') {
60 $url->param('type', $type);
62 if ($groupmode !== '') {
63 $url->param('groupmode', $groupmode);
65 $PAGE->set_url($url);
67 require_login();
69 //check if we are adding / editing a module that has new forms using formslib
70 if (!empty($add)) {
71 $id = required_param('id', PARAM_INT);
72 $section = required_param('section', PARAM_INT);
73 $type = optional_param('type', '', PARAM_ALPHA);
74 $returntomod = optional_param('return', 0, PARAM_BOOL);
76 redirect("$CFG->wwwroot/course/modedit.php?add=$add&type=$type&course=$id&section=$section&return=$returntomod");
78 } else if (!empty($update)) {
79 $cm = get_coursemodule_from_id('', $update, 0, true, MUST_EXIST);
80 $returntomod = optional_param('return', 0, PARAM_BOOL);
81 redirect("$CFG->wwwroot/course/modedit.php?update=$update&return=$returntomod");
83 } else if (!empty($duplicate)) {
84 $cm = get_coursemodule_from_id('', $duplicate, 0, true, MUST_EXIST);
85 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
87 require_login($course, false, $cm);
88 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
89 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
90 require_capability('moodle/course:manageactivities', $coursecontext);
92 if (!$confirm or !confirm_sesskey()) {
93 $PAGE->set_title(get_string('duplicate'));
94 $PAGE->set_heading($course->fullname);
95 $PAGE->navbar->add(get_string('duplicatinga', 'core', format_string($cm->name)));
96 $PAGE->set_pagelayout('incourse');
98 $a = new stdClass();
99 $a->modtype = get_string('modulename', $cm->modname);
100 $a->modname = format_string($cm->name);
101 $a->modid = $cm->id;
103 echo $OUTPUT->header();
104 echo $OUTPUT->confirm(
105 get_string('duplicateconfirm', 'core', $a),
106 new single_button(
107 new moodle_url('/course/modduplicate.php', array('cmid' => $cm->id, 'course' => $course->id)),
108 get_string('continue'),
109 'post'),
110 new single_button(
111 new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $cm->course)),
112 get_string('cancel'),
113 'get')
115 echo $OUTPUT->footer();
116 die();
119 } else if (!empty($delete)) {
120 $cm = get_coursemodule_from_id('', $delete, 0, true, MUST_EXIST);
121 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
123 require_login($course, false, $cm);
124 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
125 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
126 require_capability('moodle/course:manageactivities', $modcontext);
128 $return = "$CFG->wwwroot/course/view.php?id=$cm->course#section-$cm->sectionnum";
130 if (!$confirm or !confirm_sesskey()) {
131 $fullmodulename = get_string('modulename', $cm->modname);
133 $optionsyes = array('confirm'=>1, 'delete'=>$cm->id, 'sesskey'=>sesskey());
134 $optionsno = array('id'=>$cm->course);
136 $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
137 $strdeletecheckfull = get_string('deletecheckfull', '', "$fullmodulename '$cm->name'");
139 $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
140 $PAGE->set_title($strdeletecheck);
141 $PAGE->set_heading($course->fullname);
142 $PAGE->navbar->add($strdeletecheck);
143 echo $OUTPUT->header();
145 // print_simple_box_start('center', '60%', '#FFAAAA', 20, 'noticebox');
146 echo $OUTPUT->box_start('noticebox');
147 $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
148 $formcancel = new single_button(new moodle_url($return, $optionsno), get_string('no'), 'get');
149 echo $OUTPUT->confirm($strdeletecheckfull, $formcontinue, $formcancel);
150 echo $OUTPUT->box_end();
151 echo $OUTPUT->footer();
153 exit;
156 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
158 if (file_exists($modlib)) {
159 require_once($modlib);
160 } else {
161 print_error('modulemissingcode', '', '', $modlib);
164 $deleteinstancefunction = $cm->modname."_delete_instance";
166 if (!$deleteinstancefunction($cm->instance)) {
167 echo $OUTPUT->notification("Could not delete the $cm->modname (instance)");
170 // remove all module files in case modules forget to do that
171 $fs = get_file_storage();
172 $fs->delete_area_files($modcontext->id);
174 if (!delete_course_module($cm->id)) {
175 echo $OUTPUT->notification("Could not delete the $cm->modname (coursemodule)");
177 if (!delete_mod_from_section($cm->id, $cm->section)) {
178 echo $OUTPUT->notification("Could not delete the $cm->modname from that section");
181 // Trigger a mod_deleted event with information about this module.
182 $eventdata = new stdClass();
183 $eventdata->modulename = $cm->modname;
184 $eventdata->cmid = $cm->id;
185 $eventdata->courseid = $course->id;
186 $eventdata->userid = $USER->id;
187 events_trigger('mod_deleted', $eventdata);
189 add_to_log($course->id, 'course', "delete mod",
190 "view.php?id=$cm->course",
191 "$cm->modname $cm->instance", $cm->id);
193 rebuild_course_cache($course->id);
195 redirect($return);
199 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
200 $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, MUST_EXIST);
201 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
203 require_login($course, false, $cm);
204 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
205 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
206 require_capability('moodle/course:manageactivities', $modcontext);
208 if (!empty($movetosection)) {
209 if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
210 print_error('sectionnotexist');
212 $beforecm = NULL;
214 } else { // normal moveto
215 if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
216 print_error('invalidcoursemodule');
218 if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
219 print_error('sectionnotexist');
223 if (!ismoving($section->course)) {
224 print_error('needcopy', '', "view.php?id=$section->course");
227 moveto_module($cm, $section, $beforecm);
229 unset($USER->activitycopy);
230 unset($USER->activitycopycourse);
231 unset($USER->activitycopyname);
233 rebuild_course_cache($section->course);
235 if (SITEID == $section->course) {
236 redirect($CFG->wwwroot);
237 } else {
238 redirect("view.php?id=$section->course#section-$sectionreturn");
241 } else if (!empty($indent) and confirm_sesskey()) {
242 $id = required_param('id', PARAM_INT);
244 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
245 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
247 require_login($course, false, $cm);
248 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
249 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
250 require_capability('moodle/course:manageactivities', $modcontext);
252 $cm->indent += $indent;
254 if ($cm->indent < 0) {
255 $cm->indent = 0;
258 $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
260 rebuild_course_cache($cm->course);
262 if (SITEID == $cm->course) {
263 redirect($CFG->wwwroot);
264 } else {
265 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
268 } else if (!empty($hide) and confirm_sesskey()) {
269 $cm = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST);
270 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
272 require_login($course, false, $cm);
273 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
274 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
275 require_capability('moodle/course:activityvisibility', $modcontext);
277 set_coursemodule_visible($cm->id, 0);
279 rebuild_course_cache($cm->course);
281 if (SITEID == $cm->course) {
282 redirect($CFG->wwwroot);
283 } else {
284 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
287 } else if (!empty($show) and confirm_sesskey()) {
288 $cm = get_coursemodule_from_id('', $show, 0, true, MUST_EXIST);
289 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
291 require_login($course, false, $cm);
292 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
293 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
294 require_capability('moodle/course:activityvisibility', $modcontext);
296 $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
298 $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
300 if ($module->visible and ($section->visible or (SITEID == $cm->course))) {
301 set_coursemodule_visible($cm->id, 1);
302 rebuild_course_cache($cm->course);
305 if (SITEID == $cm->course) {
306 redirect($CFG->wwwroot);
307 } else {
308 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
311 } else if ($groupmode > -1 and confirm_sesskey()) {
312 $id = required_param('id', PARAM_INT);
314 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
315 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
317 require_login($course, false, $cm);
318 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
319 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
320 require_capability('moodle/course:manageactivities', $modcontext);
322 set_coursemodule_groupmode($cm->id, $groupmode);
324 rebuild_course_cache($cm->course);
326 if (SITEID == $cm->course) {
327 redirect($CFG->wwwroot);
328 } else {
329 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
332 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
333 $cm = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST);
334 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
336 require_login($course, false, $cm);
337 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
338 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
339 require_capability('moodle/course:manageactivities', $modcontext);
341 $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
343 $USER->activitycopy = $copy;
344 $USER->activitycopycourse = $cm->course;
345 $USER->activitycopyname = $cm->name;
347 redirect("view.php?id=$cm->course#section-$sectionreturn");
349 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
351 $courseid = $USER->activitycopycourse;
353 unset($USER->activitycopy);
354 unset($USER->activitycopycourse);
355 unset($USER->activitycopyname);
357 redirect("view.php?id=$courseid");
359 } else {
360 print_error('unknowaction');