Merge branch 'MDL-28724' of git://github.com/nebgor/moodle
[moodle.git] / course / rest.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 * Provide interface for topics AJAX course formats
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 */
b2054038 25
26require_once('../config.php');
27require_once($CFG->dirroot.'/course/lib.php');
b2054038 28
29ca8b88 29// Initialise ALL the incoming parameters here, up front.
30$courseid = required_param('courseId', PARAM_INT);
31$class = required_param('class', PARAM_ALPHA);
b2054038 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);
49c4d8ca 42$pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command
b2054038 43
51fc46a3 44$PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
b2054038 45
29ca8b88 46// Authorise the user and verify some incoming data
6bb08163 47if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b2054038 48 error_log('AJAX commands.php: Course does not exist');
49 die;
50}
51
b2054038 52$context = get_context_instance(CONTEXT_COURSE, $course->id);
cd9224ab 53require_login($course);
b2054038 54require_capability('moodle/course:update', $context);
55
8b2918a4 56if (empty($CFG->enablecourseajax)) {
c74d0527 57 error_log('Course AJAX not allowed');
92942376
PS
58 die;
59}
60
61require_sesskey();
62
29ca8b88 63// OK, now let's process the parameters and do stuff
49c4d8ca 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'];
66if ($pageaction == 'DELETE') {
67 $requestmethod = 'DELETE';
68}
69
70switch($requestmethod) {
b2054038 71 case 'POST':
29ca8b88 72
b2054038 73 switch ($class) {
29ca8b88 74 case 'block':
75
76 switch ($field) {
3440ec12 77 case 'visible':
29ca8b88 78 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
79 break;
80
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
84 // (weight field).
85 blocks_move_block($PAGE, $blockinstance, $column, $value);
86 break;
87 }
88 break;
b2054038 89
90 case 'section':
3440ec12 91
6bb08163 92 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
b2054038 93 error_log('AJAX commands.php: Bad Section ID '.$id);
94 die;
95 }
3440ec12 96
b2054038 97 switch ($field) {
98 case 'visible':
99 set_section_visible($course->id, $id, $value);
100 break;
101
102 case 'move':
3440ec12 103 move_section_to($course, $id, $value);
b2054038 104 break;
105 }
edc06a53 106 rebuild_course_cache($course->id);
b2054038 107 break;
108
109 case 'resource':
cd9224ab 110 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
b2054038 111 error_log('AJAX commands.php: Bad course module ID '.$id);
112 die;
113 }
114 switch ($field) {
115 case 'visible':
cd9224ab 116 set_coursemodule_visible($cm->id, $value);
b2054038 117 break;
118
119 case 'groupmode':
cd9224ab 120 set_coursemodule_groupmode($cm->id, $value);
b2054038 121 break;
122
607c1bc1 123 case 'indentleft':
cd9224ab 124 if ($cm->indent > 0) {
125 $cm->indent--;
126 $DB->update_record('course_modules', $cm);
607c1bc1 127 }
128 break;
129
130 case 'indentright':
cd9224ab 131 $cm->indent++;
132 $DB->update_record('course_modules', $cm);
607c1bc1 133 break;
134
b2054038 135 case 'move':
6bb08163 136 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
b2054038 137 error_log('AJAX commands.php: Bad section ID '.$sectionid);
138 die;
139 }
3440ec12 140
b2054038 141 if ($beforeid > 0){
cd9224ab 142 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
6bb08163 143 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
b2054038 144 } else {
145 $beforemod = NULL;
146 }
147
1bbcb7c0 148 if (debugging('',DEBUG_DEVELOPER)) {
149 error_log(serialize($beforemod));
150 }
151
cd9224ab 152 moveto_module($cm, $section, $beforemod);
b2054038 153 break;
154 }
edc06a53 155 rebuild_course_cache($course->id);
b2054038 156 break;
3440ec12 157
158 case 'course':
b2054038 159 switch($field) {
160 case 'marker':
fbaea88f 161 $newcourse = new stdClass();
b2054038 162 $newcourse->id = $course->id;
163 $newcourse->marker = $value;
bb4b6010 164 $DB->update_record('course', $newcourse);
b2054038 165 break;
166 }
167 break;
168 }
169 break;
170
171 case 'DELETE':
172 switch ($class) {
173 case 'block':
174 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
3440ec12 175 break;
176
b2054038 177 case 'resource':
cd9224ab 178 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
37fb48e0 179 error_log('AJAX rest.php: Bad course module ID '.$id);
180 die;
181 }
cd9224ab 182 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
37fb48e0 183
184 if (file_exists($modlib)) {
185 include_once($modlib);
186 } else {
cd9224ab 187 error_log("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
b2054038 188 die;
189 }
cd9224ab 190 $deleteinstancefunction = $cm->modname."_delete_instance";
37fb48e0 191
192 // Run the module's cleanup funtion.
193 if (!$deleteinstancefunction($cm->instance)) {
cd9224ab 194 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
37fb48e0 195 die;
196 }
cd9224ab 197
198 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
199
200 // remove all module files in case modules forget to do that
201 $fs = get_file_storage();
202 $fs->delete_area_files($modcontext->id);
203
37fb48e0 204 if (!delete_course_module($cm->id)) {
cd9224ab 205 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
206 }
207 // Remove the course_modules entry.
208 if (!delete_mod_from_section($cm->id, $cm->section)) {
209 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
37fb48e0 210 }
211
c4ce5def 212 rebuild_course_cache($course->id);
213
37fb48e0 214 add_to_log($courseid, "course", "delete mod",
215 "view.php?id=$courseid",
cd9224ab 216 "$cm->modname $cm->instance", $cm->id);
b2054038 217 break;
218 }
219 break;
220}
221
aa6c1ced 222