MDL-67264 core_course: Activity chooser new feature
[moodle.git] / course / mod.php
CommitLineData
d9cb06dc 1<?php
2
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.
14//
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/>.
17
18/**
19 * Moves, adds, updates, duplicates or deletes modules in a course
20 *
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
24 */
25
26require("../config.php");
27require_once("lib.php");
28
923451c5 29$sectionreturn = optional_param('sr', null, PARAM_INT);
d9cb06dc 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);
fa820563 34$duplicate = optional_param('duplicate', 0, PARAM_INT);
d9cb06dc 35$hide = optional_param('hide', 0, PARAM_INT);
8341055e 36$stealth = optional_param('stealth', 0, PARAM_INT);
d9cb06dc 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);
46
47// This page should always redirect
a6855934 48$url = new moodle_url('/course/mod.php');
7f093351 49foreach (compact('indent','update','hide','show','copy','moveto','movetosection','delete','course','cancelcopy','confirm') as $key=>$value) {
50 if ($value !== 0) {
51 $url->param($key, $value);
52 }
53}
923451c5 54$url->param('sr', $sectionreturn);
7f093351 55if ($add !== '') {
56 $url->param('add', $add);
57}
58if ($type !== '') {
59 $url->param('type', $type);
60}
61if ($groupmode !== '') {
62 $url->param('groupmode', $groupmode);
63}
64$PAGE->set_url($url);
d9cb06dc 65
af189935
PS
66require_login();
67
d9cb06dc 68//check if we are adding / editing a module that has new forms using formslib
69if (!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);
74
a41b1d96 75 redirect("$CFG->wwwroot/course/modedit.php?add=$add&type=$type&course=$id&section=$section&return=$returntomod&sr=$sectionreturn");
d9cb06dc 76
77} else if (!empty($update)) {
af189935 78 $cm = get_coursemodule_from_id('', $update, 0, true, MUST_EXIST);
d9cb06dc 79 $returntomod = optional_param('return', 0, PARAM_BOOL);
a41b1d96 80 redirect("$CFG->wwwroot/course/modedit.php?update=$update&return=$returntomod&sr=$sectionreturn");
1adf55c5 81
f31d5641 82} else if (!empty($duplicate) and confirm_sesskey()) {
60df6787
S
83 $cm = get_coursemodule_from_id('', $duplicate, 0, true, MUST_EXIST);
84 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
fa820563 85
af189935 86 require_login($course, false, $cm);
9a5e297b 87 $modcontext = context_module::instance($cm->id);
60df6787 88 require_capability('moodle/course:manageactivities', $modcontext);
fa820563 89
60df6787
S
90 // Duplicate the module.
91 $newcm = duplicate_module($course, $cm);
92 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
fa820563 93
d9cb06dc 94} else if (!empty($delete)) {
af189935 95 $cm = get_coursemodule_from_id('', $delete, 0, true, MUST_EXIST);
74df2951 96 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
41b94a40 97
af189935 98 require_login($course, false, $cm);
9a5e297b 99 $modcontext = context_module::instance($cm->id);
af189935 100 require_capability('moodle/course:manageactivities', $modcontext);
264867fd 101
923451c5 102 $return = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
caa65d9b 103
d9cb06dc 104 if (!$confirm or !confirm_sesskey()) {
105 $fullmodulename = get_string('modulename', $cm->modname);
3ba70534 106
76055f5d 107 $optionsyes = array('confirm'=>1, 'delete'=>$cm->id, 'sesskey'=>sesskey(), 'sr' => $sectionreturn);
3ba70534 108
d9cb06dc 109 $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
33905518
AH
110 $strparams = (object)array('type' => $fullmodulename, 'name' => $cm->name);
111 $strdeletechecktypename = get_string('deletechecktypename', '', $strparams);
3ba70534 112
d9cb06dc 113 $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
114 $PAGE->set_title($strdeletecheck);
b36781d7 115 $PAGE->set_heading($course->fullname);
d9cb06dc 116 $PAGE->navbar->add($strdeletecheck);
3ba70534 117
a347aee3 118 echo $OUTPUT->header();
d9cb06dc 119 echo $OUTPUT->box_start('noticebox');
dc6896ef 120 $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
76055f5d 121 $formcancel = new single_button($return, get_string('no'), 'get');
33905518 122 echo $OUTPUT->confirm($strdeletechecktypename, $formcontinue, $formcancel);
d9cb06dc 123 echo $OUTPUT->box_end();
124 echo $OUTPUT->footer();
3ba70534 125
d9cb06dc 126 exit;
127 }
caa65d9b 128
a347aee3
MN
129 // Delete the module.
130 course_delete_module($cm->id);
caa65d9b 131
d9cb06dc 132 redirect($return);
133}
d897cae4 134
d9cb06dc 135
136if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
af189935 137 $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, MUST_EXIST);
74df2951 138 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
af189935
PS
139
140 require_login($course, false, $cm);
9a5e297b
AA
141 $coursecontext = context_course::instance($course->id);
142 $modcontext = context_module::instance($cm->id);
af189935 143 require_capability('moodle/course:manageactivities', $modcontext);
f9903ed0 144
d9cb06dc 145 if (!empty($movetosection)) {
146 if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
147 print_error('sectionnotexist');
148 }
149 $beforecm = NULL;
a10464d6 150
d9cb06dc 151 } else { // normal moveto
152 if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
3ba70534 153 print_error('invalidcoursemodule');
154 }
d9cb06dc 155 if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
156 print_error('sectionnotexist');
7977cffd 157 }
d9cb06dc 158 }
7977cffd 159
d9cb06dc 160 if (!ismoving($section->course)) {
161 print_error('needcopy', '', "view.php?id=$section->course");
162 }
7977cffd 163
d9cb06dc 164 moveto_module($cm, $section, $beforecm);
7977cffd 165
76055f5d 166 $sectionreturn = $USER->activitycopysectionreturn;
d9cb06dc 167 unset($USER->activitycopy);
168 unset($USER->activitycopycourse);
169 unset($USER->activitycopyname);
76055f5d 170 unset($USER->activitycopysectionreturn);
7977cffd 171
923451c5 172 redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
7977cffd 173
d9cb06dc 174} else if (!empty($indent) and confirm_sesskey()) {
175 $id = required_param('id', PARAM_INT);
aac94fd0 176
af189935 177 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
74df2951 178 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
af189935
PS
179
180 require_login($course, false, $cm);
9a5e297b
AA
181 $coursecontext = context_course::instance($course->id);
182 $modcontext = context_module::instance($cm->id);
af189935 183 require_capability('moodle/course:manageactivities', $modcontext);
bdc04ca9 184
d9cb06dc 185 $cm->indent += $indent;
aac94fd0 186
d9cb06dc 187 if ($cm->indent < 0) {
188 $cm->indent = 0;
189 }
aac94fd0 190
d9cb06dc 191 $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
aac94fd0 192
d9cb06dc 193 rebuild_course_cache($cm->course);
82bd6a5e 194
923451c5 195 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
aac94fd0 196
d9cb06dc 197} else if (!empty($hide) and confirm_sesskey()) {
af189935 198 $cm = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST);
74df2951 199 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
522f608d 200
af189935 201 require_login($course, false, $cm);
9a5e297b
AA
202 $coursecontext = context_course::instance($course->id);
203 $modcontext = context_module::instance($cm->id);
af189935 204 require_capability('moodle/course:activityvisibility', $modcontext);
a10464d6 205
8341055e
MG
206 if (set_coursemodule_visible($cm->id, 0)) {
207 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
208 }
923451c5 209 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
1acfbce5 210
8341055e
MG
211} else if (!empty($stealth) and confirm_sesskey()) {
212 list($course, $cm) = get_course_and_cm_from_cmid($stealth);
af189935 213 require_login($course, false, $cm);
8341055e 214 require_capability('moodle/course:activityvisibility', $cm->context);
522f608d 215
8341055e
MG
216 if (set_coursemodule_visible($cm->id, 1, 0)) {
217 \core\event\course_module_updated::create_from_cm($cm)->trigger();
218 }
219 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
5867bfb5 220
8341055e
MG
221} else if (!empty($show) and confirm_sesskey()) {
222 list($course, $cm) = get_course_and_cm_from_cmid($show);
223 require_login($course, false, $cm);
224 require_capability('moodle/course:activityvisibility', $cm->context);
225 $section = $cm->get_section_info();
5867bfb5 226
8341055e
MG
227 if (set_coursemodule_visible($cm->id, 1)) {
228 \core\event\course_module_updated::create_from_cm($cm)->trigger();
d9cb06dc 229 }
923451c5 230 redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
3d575e6f 231
d9cb06dc 232} else if ($groupmode > -1 and confirm_sesskey()) {
233 $id = required_param('id', PARAM_INT);
3d575e6f 234
af189935 235 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
74df2951 236 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
af189935
PS
237
238 require_login($course, false, $cm);
9a5e297b
AA
239 $coursecontext = context_course::instance($course->id);
240 $modcontext = context_module::instance($cm->id);
af189935 241 require_capability('moodle/course:manageactivities', $modcontext);
a10464d6 242
d9cb06dc 243 set_coursemodule_groupmode($cm->id, $groupmode);
9e533215 244 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
923451c5 245 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
f9903ed0 246
d9cb06dc 247} else if (!empty($copy) and confirm_sesskey()) { // value = course module
af189935 248 $cm = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST);
74df2951 249 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
7977cffd 250
af189935 251 require_login($course, false, $cm);
9a5e297b
AA
252 $coursecontext = context_course::instance($course->id);
253 $modcontext = context_module::instance($cm->id);
af189935 254 require_capability('moodle/course:manageactivities', $modcontext);
7977cffd 255
af189935 256 $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
7977cffd 257
76055f5d
FM
258 $USER->activitycopy = $copy;
259 $USER->activitycopycourse = $cm->course;
260 $USER->activitycopyname = $cm->name;
261 $USER->activitycopysectionreturn = $sectionreturn;
7977cffd 262
923451c5 263 redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
7977cffd 264
d9cb06dc 265} else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
7977cffd 266
d9cb06dc 267 $courseid = $USER->activitycopycourse;
74df2951 268 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
7977cffd 269
923451c5
MG
270 $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, IGNORE_MISSING);
271 $sectionreturn = $USER->activitycopysectionreturn;
d9cb06dc 272 unset($USER->activitycopy);
273 unset($USER->activitycopycourse);
274 unset($USER->activitycopyname);
923451c5
MG
275 unset($USER->activitycopysectionreturn);
276 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
d9cb06dc 277} else {
278 print_error('unknowaction');
279}