3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Provide interface for topics AJAX course formats
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once($CFG->dirroot
.'/course/lib.php');
29 // Initialise ALL the incoming parameters here, up front.
30 $courseid = required_param('courseId', PARAM_INT
);
31 $class = required_param('class', PARAM_ALPHA
);
32 $field = optional_param('field', '', PARAM_ALPHA
);
33 $instanceid = optional_param('instanceId', 0, PARAM_INT
);
34 $sectionid = optional_param('sectionId', 0, PARAM_INT
);
35 $beforeid = optional_param('beforeId', 0, PARAM_INT
);
36 $value = optional_param('value', 0, PARAM_INT
);
37 $column = optional_param('column', 0, PARAM_ALPHA
);
38 $id = optional_param('id', 0, PARAM_INT
);
39 $summary = optional_param('summary', '', PARAM_RAW
);
40 $sequence = optional_param('sequence', '', PARAM_SEQUENCE
);
41 $visible = optional_param('visible', 0, PARAM_INT
);
42 $pageaction = optional_param('action', '', PARAM_ALPHA
); // Used to simulate a DELETE command
44 $PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
46 // Authorise the user and verify some incoming data
47 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
48 error_log('AJAX commands.php: Course does not exist');
52 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
53 require_login($course);
54 require_capability('moodle/course:update', $context);
56 if (empty($CFG->enablecourseajax
)) {
57 error_log('Course AJAX not allowed');
63 // OK, now let's process the parameters and do stuff
64 // MDL-10221 the DELETE method is not allowed on some web servers, so we simulate it with the action URL param
65 $requestmethod = $_SERVER['REQUEST_METHOD'];
66 if ($pageaction == 'DELETE') {
67 $requestmethod = 'DELETE';
70 switch($requestmethod) {
78 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
81 case 'position': // Misleading case. Should probably call it 'move'.
82 // We want to move the block around. This means changing
83 // the column (position field) and/or block sort order
85 blocks_move_block($PAGE, $blockinstance, $column, $value);
92 if (!$DB->record_exists('course_sections', array('course'=>$course->id
, 'section'=>$id))) {
93 error_log('AJAX commands.php: Bad Section ID '.$id);
99 set_section_visible($course->id
, $id, $value);
103 move_section_to($course, $id, $value);
106 rebuild_course_cache($course->id
);
110 if (!$cm = get_coursemodule_from_id('', $id, $course->id
)) {
111 error_log('AJAX commands.php: Bad course module ID '.$id);
116 set_coursemodule_visible($cm->id
, $value);
120 set_coursemodule_groupmode($cm->id
, $value);
124 if ($cm->indent
> 0) {
126 $DB->update_record('course_modules', $cm);
132 $DB->update_record('course_modules', $cm);
136 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id
, 'section'=>$sectionid))) {
137 error_log('AJAX commands.php: Bad section ID '.$sectionid);
142 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id
);
143 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
148 if (debugging('',DEBUG_DEVELOPER
)) {
149 error_log(serialize($beforemod));
152 moveto_module($cm, $section, $beforemod);
155 rebuild_course_cache($course->id
);
161 course_set_marker($course->id
, $value);
171 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
175 if (!$cm = get_coursemodule_from_id('', $id, $course->id
)) {
176 error_log('AJAX rest.php: Bad course module ID '.$id);
179 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
181 if (file_exists($modlib)) {
182 include_once($modlib);
184 error_log("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
187 $deleteinstancefunction = $cm->modname
."_delete_instance";
189 // Run the module's cleanup funtion.
190 if (!$deleteinstancefunction($cm->instance
)) {
191 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
195 $modcontext = get_context_instance(CONTEXT_MODULE
, $cm->id
);
197 // remove all module files in case modules forget to do that
198 $fs = get_file_storage();
199 $fs->delete_area_files($modcontext->id
);
201 if (!delete_course_module($cm->id
)) {
202 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
204 // Remove the course_modules entry.
205 if (!delete_mod_from_section($cm->id
, $cm->section
)) {
206 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
209 // Trigger a mod_deleted event with information about this module.
210 $eventdata = new stdClass();
211 $eventdata->modulename
= $cm->modname
;
212 $eventdata->cmid
= $cm->id
;
213 $eventdata->courseid
= $course->id
;
214 $eventdata->userid
= $USER->id
;
215 events_trigger('mod_deleted', $eventdata);
217 rebuild_course_cache($course->id
);
219 add_to_log($courseid, "course", "delete mod",
220 "view.php?id=$courseid",
221 "$cm->modname $cm->instance", $cm->id
);