Merge branch 'MDL-53249-30' of git://github.com/merrill-oakland/moodle into MOODLE_30...
[moodle.git] / course / mod.php
blob9c23493ae2a53207e92d67fd7d3b1b6249898c35
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', 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) {
49 if ($value !== 0) {
50 $url->param($key, $value);
53 $url->param('sr', $sectionreturn);
54 if ($add !== '') {
55 $url->param('add', $add);
57 if ($type !== '') {
58 $url->param('type', $type);
60 if ($groupmode !== '') {
61 $url->param('groupmode', $groupmode);
63 $PAGE->set_url($url);
65 require_login();
67 //check if we are adding / editing a module that has new forms using formslib
68 if (!empty($add)) {
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&section=$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) and confirm_sesskey()) {
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 $modcontext = context_module::instance($cm->id);
87 require_capability('moodle/course:manageactivities', $modcontext);
89 // Duplicate the module.
90 $newcm = duplicate_module($course, $cm);
91 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
93 } else if (!empty($delete)) {
94 $cm = get_coursemodule_from_id('', $delete, 0, true, MUST_EXIST);
95 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
97 require_login($course, false, $cm);
98 $modcontext = context_module::instance($cm->id);
99 require_capability('moodle/course:manageactivities', $modcontext);
101 $return = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
103 if (!$confirm or !confirm_sesskey()) {
104 $fullmodulename = get_string('modulename', $cm->modname);
106 $optionsyes = array('confirm'=>1, 'delete'=>$cm->id, 'sesskey'=>sesskey(), 'sr' => $sectionreturn);
108 $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
109 $strparams = (object)array('type' => $fullmodulename, 'name' => $cm->name);
110 $strdeletechecktypename = get_string('deletechecktypename', '', $strparams);
112 $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
113 $PAGE->set_title($strdeletecheck);
114 $PAGE->set_heading($course->fullname);
115 $PAGE->navbar->add($strdeletecheck);
117 echo $OUTPUT->header();
118 echo $OUTPUT->box_start('noticebox');
119 $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
120 $formcancel = new single_button($return, get_string('no'), 'get');
121 echo $OUTPUT->confirm($strdeletechecktypename, $formcontinue, $formcancel);
122 echo $OUTPUT->box_end();
123 echo $OUTPUT->footer();
125 exit;
128 // Delete the module.
129 course_delete_module($cm->id);
131 redirect($return);
135 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
136 $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, MUST_EXIST);
137 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
139 require_login($course, false, $cm);
140 $coursecontext = context_course::instance($course->id);
141 $modcontext = context_module::instance($cm->id);
142 require_capability('moodle/course:manageactivities', $modcontext);
144 if (!empty($movetosection)) {
145 if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
146 print_error('sectionnotexist');
148 $beforecm = NULL;
150 } else { // normal moveto
151 if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
152 print_error('invalidcoursemodule');
154 if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
155 print_error('sectionnotexist');
159 if (!ismoving($section->course)) {
160 print_error('needcopy', '', "view.php?id=$section->course");
163 moveto_module($cm, $section, $beforecm);
165 $sectionreturn = $USER->activitycopysectionreturn;
166 unset($USER->activitycopy);
167 unset($USER->activitycopycourse);
168 unset($USER->activitycopyname);
169 unset($USER->activitycopysectionreturn);
171 redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
173 } else if (!empty($indent) and confirm_sesskey()) {
174 $id = required_param('id', PARAM_INT);
176 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
177 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
179 require_login($course, false, $cm);
180 $coursecontext = context_course::instance($course->id);
181 $modcontext = context_module::instance($cm->id);
182 require_capability('moodle/course:manageactivities', $modcontext);
184 $cm->indent += $indent;
186 if ($cm->indent < 0) {
187 $cm->indent = 0;
190 $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
192 rebuild_course_cache($cm->course);
194 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
196 } else if (!empty($hide) and confirm_sesskey()) {
197 $cm = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST);
198 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
200 require_login($course, false, $cm);
201 $coursecontext = context_course::instance($course->id);
202 $modcontext = context_module::instance($cm->id);
203 require_capability('moodle/course:activityvisibility', $modcontext);
205 set_coursemodule_visible($cm->id, 0);
206 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
207 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
209 } else if (!empty($show) and confirm_sesskey()) {
210 $cm = get_coursemodule_from_id('', $show, 0, true, MUST_EXIST);
211 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
213 require_login($course, false, $cm);
214 $coursecontext = context_course::instance($course->id);
215 $modcontext = context_module::instance($cm->id);
216 require_capability('moodle/course:activityvisibility', $modcontext);
218 $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
220 $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
222 if ($module->visible and ($section->visible or (SITEID == $cm->course))) {
223 set_coursemodule_visible($cm->id, 1);
224 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
227 redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
229 } else if ($groupmode > -1 and confirm_sesskey()) {
230 $id = required_param('id', PARAM_INT);
232 $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
233 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
235 require_login($course, false, $cm);
236 $coursecontext = context_course::instance($course->id);
237 $modcontext = context_module::instance($cm->id);
238 require_capability('moodle/course:manageactivities', $modcontext);
240 set_coursemodule_groupmode($cm->id, $groupmode);
241 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
242 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
244 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
245 $cm = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST);
246 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
248 require_login($course, false, $cm);
249 $coursecontext = context_course::instance($course->id);
250 $modcontext = context_module::instance($cm->id);
251 require_capability('moodle/course:manageactivities', $modcontext);
253 $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
255 $USER->activitycopy = $copy;
256 $USER->activitycopycourse = $cm->course;
257 $USER->activitycopyname = $cm->name;
258 $USER->activitycopysectionreturn = $sectionreturn;
260 redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
262 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
264 $courseid = $USER->activitycopycourse;
265 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
267 $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, IGNORE_MISSING);
268 $sectionreturn = $USER->activitycopysectionreturn;
269 unset($USER->activitycopy);
270 unset($USER->activitycopycourse);
271 unset($USER->activitycopyname);
272 unset($USER->activitycopysectionreturn);
273 redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
274 } else {
275 print_error('unknowaction');