MDL-30988 ajax: Fixed up phpdocs package in ajax scripts
[moodle.git] / course / rest.php
blob933e4f1a9c15be7ac5ef4a07e6c64d57331012a3
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 * 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
23 * @package course
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 //NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
48 require_login();
50 // Authorise the user and verify some incoming data
51 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
52 error_log('AJAX commands.php: Course does not exist');
53 die;
56 if (empty($CFG->enablecourseajax)) {
57 error_log('Course AJAX not allowed');
58 die;
61 require_sesskey();
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) {
71 case 'POST':
73 switch ($class) {
74 case 'block':
75 // not used any more
76 break;
78 case 'section':
79 require_login($course);
80 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
81 require_capability('moodle/course:update', $coursecontext);
83 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
84 error_log('AJAX commands.php: Bad Section ID '.$id);
85 die;
88 switch ($field) {
89 case 'visible':
90 set_section_visible($course->id, $id, $value);
91 break;
93 case 'move':
94 move_section_to($course, $id, $value);
95 break;
97 rebuild_course_cache($course->id);
98 break;
100 case 'resource':
101 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
102 error_log('AJAX commands.php: Bad course module ID '.$id);
103 die;
105 require_login($course, false, $cm);
106 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
107 switch ($field) {
108 case 'visible':
109 require_capability('moodle/course:activityvisibility', $modcontext);
110 set_coursemodule_visible($cm->id, $value);
111 break;
113 case 'groupmode':
114 require_capability('moodle/course:manageactivities', $modcontext);
115 set_coursemodule_groupmode($cm->id, $value);
116 break;
118 case 'indentleft':
119 require_capability('moodle/course:manageactivities', $modcontext);
120 if ($cm->indent > 0) {
121 $cm->indent--;
122 $DB->update_record('course_modules', $cm);
124 break;
126 case 'indentright':
127 require_capability('moodle/course:manageactivities', $modcontext);
128 $cm->indent++;
129 $DB->update_record('course_modules', $cm);
130 break;
132 case 'move':
133 require_capability('moodle/course:manageactivities', $modcontext);
134 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
135 error_log('AJAX commands.php: Bad section ID '.$sectionid);
136 die;
139 if ($beforeid > 0){
140 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
141 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
142 } else {
143 $beforemod = NULL;
146 if (debugging('',DEBUG_DEVELOPER)) {
147 error_log(serialize($beforemod));
150 moveto_module($cm, $section, $beforemod);
151 break;
153 rebuild_course_cache($course->id);
154 break;
156 case 'course':
157 switch($field) {
158 case 'marker':
159 require_login($course);
160 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
161 require_capability('moodle/course:update', $coursecontext);
162 $newcourse = new stdClass();
163 $newcourse->id = $course->id;
164 $newcourse->marker = $value;
165 $DB->update_record('course', $newcourse);
166 break;
168 break;
170 break;
172 case 'DELETE':
173 switch ($class) {
174 case 'block':
175 // not used any more
176 break;
178 case 'resource':
179 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
180 error_log('AJAX rest.php: Bad course module ID '.$id);
181 die;
183 require_login($course, false, $cm);
184 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
185 require_capability('moodle/course:manageactivities', $modcontext);
186 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
188 if (file_exists($modlib)) {
189 include_once($modlib);
190 } else {
191 error_log("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
192 die;
194 $deleteinstancefunction = $cm->modname."_delete_instance";
196 // Run the module's cleanup funtion.
197 if (!$deleteinstancefunction($cm->instance)) {
198 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
199 die;
202 // remove all module files in case modules forget to do that
203 $fs = get_file_storage();
204 $fs->delete_area_files($modcontext->id);
206 if (!delete_course_module($cm->id)) {
207 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
209 // Remove the course_modules entry.
210 if (!delete_mod_from_section($cm->id, $cm->section)) {
211 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
214 // Trigger a mod_deleted event with information about this module.
215 $eventdata = new stdClass();
216 $eventdata->modulename = $cm->modname;
217 $eventdata->cmid = $cm->id;
218 $eventdata->courseid = $course->id;
219 $eventdata->userid = $USER->id;
220 events_trigger('mod_deleted', $eventdata);
222 rebuild_course_cache($course->id);
224 add_to_log($courseid, "course", "delete mod",
225 "view.php?id=$courseid",
226 "$cm->modname $cm->instance", $cm->id);
227 break;
229 break;