MDL-26257 Shibboleth auth: run logout handler only if logged in via Shibboleth
[moodle.git] / course / mod.php
blobd6b9e05b78699ce751bf573df815e23f409bc804
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 $duplicate = optional_param('duplicate', 0, PARAM_INT);
37 $hide = optional_param('hide', 0, PARAM_INT);
38 $show = optional_param('show', 0, PARAM_INT);
39 $copy = optional_param('copy', 0, PARAM_INT);
40 $moveto = optional_param('moveto', 0, PARAM_INT);
41 $movetosection = optional_param('movetosection', 0, PARAM_INT);
42 $delete = optional_param('delete', 0, PARAM_INT);
43 $course = optional_param('course', 0, PARAM_INT);
44 $groupmode = optional_param('groupmode', -1, PARAM_INT);
45 $cancelcopy = optional_param('cancelcopy', 0, PARAM_BOOL);
46 $confirm = optional_param('confirm', 0, PARAM_BOOL);
48 // This page should always redirect
49 $url = new moodle_url('/course/mod.php');
50 foreach (compact('indent','update','hide','show','copy','moveto','movetosection','delete','course','cancelcopy','confirm') as $key=>$value) {
51 if ($value !== 0) {
52 $url->param($key, $value);
55 if ($sectionreturn !== '') {
56 $url->param('sr', $sectionreturn);
58 if ($add !== '') {
59 $url->param('add', $add);
61 if ($type !== '') {
62 $url->param('type', $type);
64 if ($groupmode !== '') {
65 $url->param('groupmode', $groupmode);
67 $PAGE->set_url($url);
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 if (!$cm = get_coursemodule_from_id('', $update, 0, true)) {
80 print_error('invalidcoursemodule');
82 $returntomod = optional_param('return', 0, PARAM_BOOL);
83 redirect("$CFG->wwwroot/course/modedit.php?update=$update&return=$returntomod");
85 } else if (!empty($duplicate)) {
86 $cm = get_coursemodule_from_id('', $duplicate, 0, true, MUST_EXIST);
87 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
89 require_login($course->id);
90 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
91 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
92 require_capability('moodle/course:manageactivities', $coursecontext);
94 if (!$confirm or !confirm_sesskey()) {
95 $PAGE->set_title(get_string('duplicate'));
96 $PAGE->set_heading($course->fullname);
97 $PAGE->navbar->add(get_string('duplicatinga', 'core', format_string($cm->name)));
98 $PAGE->set_pagelayout('incourse');
100 $a = new stdClass();
101 $a->modtype = get_string('modulename', $cm->modname);
102 $a->modname = format_string($cm->name);
103 $a->modid = $cm->id;
105 echo $OUTPUT->header();
106 echo $OUTPUT->confirm(
107 get_string('duplicateconfirm', 'core', $a),
108 new single_button(
109 new moodle_url('/course/modduplicate.php', array('cmid' => $cm->id, 'course' => $course->id)),
110 get_string('continue'),
111 'post'),
112 new single_button(
113 new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $cm->course)),
114 get_string('cancel'),
115 'get')
117 echo $OUTPUT->footer();
118 die();
121 } else if (!empty($delete)) {
122 if (!$cm = get_coursemodule_from_id('', $delete, 0, true)) {
123 print_error('invalidcoursemodule');
126 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
127 print_error('invalidcourseid');
129 require_login($course->id); // needed to setup proper $COURSE
130 $context = get_context_instance(CONTEXT_COURSE, $course->id);
131 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
132 require_capability('moodle/course:manageactivities', $context);
134 $return = "$CFG->wwwroot/course/view.php?id=$cm->course#section-$cm->sectionnum";
136 if (!$confirm or !confirm_sesskey()) {
137 $fullmodulename = get_string('modulename', $cm->modname);
139 $optionsyes = array('confirm'=>1, 'delete'=>$cm->id, 'sesskey'=>sesskey());
140 $optionsno = array('id'=>$cm->course);
142 $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
143 $strdeletecheckfull = get_string('deletecheckfull', '', "$fullmodulename '$cm->name'");
145 $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
146 $PAGE->set_title($strdeletecheck);
147 $PAGE->set_heading($course->fullname);
148 $PAGE->navbar->add($strdeletecheck);
149 echo $OUTPUT->header();
151 // print_simple_box_start('center', '60%', '#FFAAAA', 20, 'noticebox');
152 echo $OUTPUT->box_start('noticebox');
153 $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
154 $formcancel = new single_button(new moodle_url($return, $optionsno), get_string('no'), 'get');
155 echo $OUTPUT->confirm($strdeletecheckfull, $formcontinue, $formcancel);
156 echo $OUTPUT->box_end();
157 echo $OUTPUT->footer();
159 exit;
162 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
164 if (file_exists($modlib)) {
165 require_once($modlib);
166 } else {
167 print_error('modulemissingcode', '', '', $modlib);
170 $deleteinstancefunction = $cm->modname."_delete_instance";
172 if (!$deleteinstancefunction($cm->instance)) {
173 echo $OUTPUT->notification("Could not delete the $cm->modname (instance)");
176 // remove all module files in case modules forget to do that
177 $fs = get_file_storage();
178 $fs->delete_area_files($modcontext->id);
180 if (!delete_course_module($cm->id)) {
181 echo $OUTPUT->notification("Could not delete the $cm->modname (coursemodule)");
183 if (!delete_mod_from_section($cm->id, $cm->section)) {
184 echo $OUTPUT->notification("Could not delete the $cm->modname from that section");
187 // Trigger a mod_deleted event with information about this module.
188 $eventdata = new stdClass();
189 $eventdata->modulename = $cm->modname;
190 $eventdata->cmid = $cm->id;
191 $eventdata->courseid = $course->id;
192 $eventdata->userid = $USER->id;
193 events_trigger('mod_deleted', $eventdata);
195 add_to_log($course->id, 'course', "delete mod",
196 "view.php?id=$cm->course",
197 "$cm->modname $cm->instance", $cm->id);
199 rebuild_course_cache($course->id);
201 redirect($return);
205 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
206 if (!$cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true)) {
207 print_error('invalidcoursemodule');
210 if (!empty($movetosection)) {
211 if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
212 print_error('sectionnotexist');
214 $beforecm = NULL;
216 } else { // normal moveto
217 if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
218 print_error('invalidcoursemodule');
220 if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
221 print_error('sectionnotexist');
225 require_login($section->course); // needed to setup proper $COURSE
226 $context = get_context_instance(CONTEXT_COURSE, $section->course);
227 require_capability('moodle/course:manageactivities', $context);
229 if (!ismoving($section->course)) {
230 print_error('needcopy', '', "view.php?id=$section->course");
233 moveto_module($cm, $section, $beforecm);
235 unset($USER->activitycopy);
236 unset($USER->activitycopycourse);
237 unset($USER->activitycopyname);
239 rebuild_course_cache($section->course);
241 if (SITEID == $section->course) {
242 redirect($CFG->wwwroot);
243 } else {
244 redirect("view.php?id=$section->course#section-$sectionreturn");
247 } else if (!empty($indent) and confirm_sesskey()) {
248 $id = required_param('id', PARAM_INT);
249 if (!$cm = get_coursemodule_from_id('', $id, 0, true)) {
250 print_error('invalidcoursemodule');
253 require_login($cm->course); // needed to setup proper $COURSE
254 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
255 require_capability('moodle/course:manageactivities', $context);
257 $cm->indent += $indent;
259 if ($cm->indent < 0) {
260 $cm->indent = 0;
263 $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
265 rebuild_course_cache($cm->course);
267 if (SITEID == $cm->course) {
268 redirect($CFG->wwwroot);
269 } else {
270 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
273 } else if (!empty($hide) and confirm_sesskey()) {
274 if (!$cm = get_coursemodule_from_id('', $hide, 0, true)) {
275 print_error('invalidcoursemodule');
278 require_login($cm->course); // needed to setup proper $COURSE
279 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
280 require_capability('moodle/course:activityvisibility', $context);
282 set_coursemodule_visible($cm->id, 0);
284 rebuild_course_cache($cm->course);
286 if (SITEID == $cm->course) {
287 redirect($CFG->wwwroot);
288 } else {
289 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
292 } else if (!empty($show) and confirm_sesskey()) {
293 if (!$cm = get_coursemodule_from_id('', $show, 0, true)) {
294 print_error('invalidcoursemodule');
297 require_login($cm->course); // needed to setup proper $COURSE
298 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
299 require_capability('moodle/course:activityvisibility', $context);
301 if (!$section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
302 print_error('sectionnotexist');
305 if (!$module = $DB->get_record('modules', array('id'=>$cm->module))) {
306 print_error('moduledoesnotexist');
309 if ($module->visible and ($section->visible or (SITEID == $cm->course))) {
310 set_coursemodule_visible($cm->id, 1);
311 rebuild_course_cache($cm->course);
314 if (SITEID == $cm->course) {
315 redirect($CFG->wwwroot);
316 } else {
317 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
320 } else if ($groupmode > -1 and confirm_sesskey()) {
321 $id = required_param('id', PARAM_INT);
322 if (!$cm = get_coursemodule_from_id('', $id, 0, true)) {
323 print_error('invalidcoursemodule');
326 require_login($cm->course); // needed to setup proper $COURSE
327 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
328 require_capability('moodle/course:manageactivities', $context);
330 set_coursemodule_groupmode($cm->id, $groupmode);
332 rebuild_course_cache($cm->course);
334 if (SITEID == $cm->course) {
335 redirect($CFG->wwwroot);
336 } else {
337 redirect("view.php?id=$cm->course#section-$cm->sectionnum");
340 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
341 if (!$cm = get_coursemodule_from_id('', $copy, 0, true)) {
342 print_error('invalidcoursemodule');
345 require_login($cm->course); // needed to setup proper $COURSE
346 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
347 require_capability('moodle/course:manageactivities', $context);
349 if (!$section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
350 print_error('sectionnotexist');
353 $USER->activitycopy = $copy;
354 $USER->activitycopycourse = $cm->course;
355 $USER->activitycopyname = $cm->name;
357 redirect("view.php?id=$cm->course#section-$sectionreturn");
359 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
361 $courseid = $USER->activitycopycourse;
363 unset($USER->activitycopy);
364 unset($USER->activitycopycourse);
365 unset($USER->activitycopyname);
367 redirect("view.php?id=$courseid");
369 } else {
370 print_error('unknowaction');