MDL-31096 Create generic coursemodule javascript library
[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
af189935
PS
46//NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
47
48require_login();
49
29ca8b88 50// Authorise the user and verify some incoming data
6bb08163 51if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b2054038 52 error_log('AJAX commands.php: Course does not exist');
53 die;
54}
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':
af189935 75 // not used any more
29ca8b88 76 break;
b2054038 77
78 case 'section':
af189935
PS
79 require_login($course);
80 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
81 require_capability('moodle/course:update', $coursecontext);
3440ec12 82
6bb08163 83 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
b2054038 84 error_log('AJAX commands.php: Bad Section ID '.$id);
85 die;
86 }
3440ec12 87
b2054038 88 switch ($field) {
89 case 'visible':
90 set_section_visible($course->id, $id, $value);
91 break;
92
93 case 'move':
3440ec12 94 move_section_to($course, $id, $value);
b2054038 95 break;
96 }
edc06a53 97 rebuild_course_cache($course->id);
b2054038 98 break;
99
100 case 'resource':
cd9224ab 101 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
b2054038 102 error_log('AJAX commands.php: Bad course module ID '.$id);
103 die;
104 }
af189935
PS
105 require_login($course, false, $cm);
106 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
b2054038 107 switch ($field) {
108 case 'visible':
af189935 109 require_capability('moodle/course:activityvisibility', $modcontext);
cd9224ab 110 set_coursemodule_visible($cm->id, $value);
b2054038 111 break;
112
113 case 'groupmode':
af189935 114 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 115 set_coursemodule_groupmode($cm->id, $value);
b2054038 116 break;
117
607c1bc1 118 case 'indentleft':
af189935 119 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 120 if ($cm->indent > 0) {
121 $cm->indent--;
122 $DB->update_record('course_modules', $cm);
607c1bc1 123 }
124 break;
125
126 case 'indentright':
af189935 127 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 128 $cm->indent++;
129 $DB->update_record('course_modules', $cm);
607c1bc1 130 break;
131
b2054038 132 case 'move':
af189935 133 require_capability('moodle/course:manageactivities', $modcontext);
6bb08163 134 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
b2054038 135 error_log('AJAX commands.php: Bad section ID '.$sectionid);
136 die;
137 }
3440ec12 138
b2054038 139 if ($beforeid > 0){
cd9224ab 140 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
6bb08163 141 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
b2054038 142 } else {
143 $beforemod = NULL;
144 }
145
1bbcb7c0 146 if (debugging('',DEBUG_DEVELOPER)) {
147 error_log(serialize($beforemod));
148 }
149
cd9224ab 150 moveto_module($cm, $section, $beforemod);
b2054038 151 break;
152 }
edc06a53 153 rebuild_course_cache($course->id);
b2054038 154 break;
3440ec12 155
156 case 'course':
b2054038 157 switch($field) {
158 case 'marker':
af189935
PS
159 require_login($course);
160 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
161 require_capability('moodle/course:update', $coursecontext);
93d46f48 162 course_set_marker($course->id, $value);
b2054038 163 break;
164 }
165 break;
166 }
167 break;
168
169 case 'DELETE':
170 switch ($class) {
171 case 'block':
af189935 172 // not used any more
3440ec12 173 break;
174
b2054038 175 case 'resource':
cd9224ab 176 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
37fb48e0 177 error_log('AJAX rest.php: Bad course module ID '.$id);
178 die;
179 }
af189935
PS
180 require_login($course, false, $cm);
181 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
182 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 183 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
37fb48e0 184
185 if (file_exists($modlib)) {
186 include_once($modlib);
187 } else {
cd9224ab 188 error_log("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
b2054038 189 die;
190 }
cd9224ab 191 $deleteinstancefunction = $cm->modname."_delete_instance";
37fb48e0 192
193 // Run the module's cleanup funtion.
194 if (!$deleteinstancefunction($cm->instance)) {
cd9224ab 195 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
37fb48e0 196 die;
197 }
cd9224ab 198
cd9224ab 199 // remove all module files in case modules forget to do that
200 $fs = get_file_storage();
201 $fs->delete_area_files($modcontext->id);
202
37fb48e0 203 if (!delete_course_module($cm->id)) {
cd9224ab 204 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
205 }
206 // Remove the course_modules entry.
207 if (!delete_mod_from_section($cm->id, $cm->section)) {
208 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
37fb48e0 209 }
210
c5234aa1
SZ
211 // Trigger a mod_deleted event with information about this module.
212 $eventdata = new stdClass();
213 $eventdata->modulename = $cm->modname;
214 $eventdata->cmid = $cm->id;
215 $eventdata->courseid = $course->id;
216 $eventdata->userid = $USER->id;
217 events_trigger('mod_deleted', $eventdata);
218
c4ce5def 219 rebuild_course_cache($course->id);
220
37fb48e0 221 add_to_log($courseid, "course", "delete mod",
222 "view.php?id=$courseid",
cd9224ab 223 "$cm->modname $cm->instance", $cm->id);
b2054038 224 break;
225 }
226 break;
227}
228
aa6c1ced 229