Merge branch 'w18_MDL-32676_m23_yuibootstrap' of git://github.com/skodak/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
ff92b711
RK
26if (!defined('AJAX_SCRIPT')) {
27 define('AJAX_SCRIPT', true);
28}
29require_once(dirname(__FILE__) . '/../config.php');
b2054038 30require_once($CFG->dirroot.'/course/lib.php');
b2054038 31
29ca8b88 32// Initialise ALL the incoming parameters here, up front.
33$courseid = required_param('courseId', PARAM_INT);
34$class = required_param('class', PARAM_ALPHA);
b2054038 35$field = optional_param('field', '', PARAM_ALPHA);
36$instanceid = optional_param('instanceId', 0, PARAM_INT);
37$sectionid = optional_param('sectionId', 0, PARAM_INT);
38$beforeid = optional_param('beforeId', 0, PARAM_INT);
39$value = optional_param('value', 0, PARAM_INT);
40$column = optional_param('column', 0, PARAM_ALPHA);
41$id = optional_param('id', 0, PARAM_INT);
42$summary = optional_param('summary', '', PARAM_RAW);
43$sequence = optional_param('sequence', '', PARAM_SEQUENCE);
44$visible = optional_param('visible', 0, PARAM_INT);
49c4d8ca 45$pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command
b2054038 46
51fc46a3 47$PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
b2054038 48
af189935
PS
49//NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
50
8b2918a4 51if (empty($CFG->enablecourseajax)) {
ff92b711 52 throw new moodle_exception('Course AJAX not allowed');
92942376
PS
53}
54
ff92b711
RK
55$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
56// Check user is logged in and set contexts if we are dealing with resource
57if (in_array($class, array('resource'))) {
58 $cm = get_coursemodule_from_id(null, $id, $course->id, false, MUST_EXIST);
59 require_login($course, false, $cm);
60 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
61} else {
62 require_login($course);
63 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
64}
92942376
PS
65require_sesskey();
66
ff92b711
RK
67echo $OUTPUT->header(); // send headers
68
29ca8b88 69// OK, now let's process the parameters and do stuff
49c4d8ca 70// MDL-10221 the DELETE method is not allowed on some web servers, so we simulate it with the action URL param
71$requestmethod = $_SERVER['REQUEST_METHOD'];
72if ($pageaction == 'DELETE') {
73 $requestmethod = 'DELETE';
74}
75
76switch($requestmethod) {
b2054038 77 case 'POST':
29ca8b88 78
b2054038 79 switch ($class) {
b2054038 80 case 'section':
af189935 81 require_capability('moodle/course:update', $coursecontext);
3440ec12 82
6bb08163 83 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
ff92b711 84 throw new moodle_exception('AJAX commands.php: Bad Section ID '.$id);
b2054038 85 }
3440ec12 86
b2054038 87 switch ($field) {
88 case 'visible':
ebaa29d1
ARN
89 $resourcestotoggle = set_section_visible($course->id, $id, $value);
90 echo json_encode(array('resourcestotoggle' => $resourcestotoggle));
b2054038 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':
b2054038 101 switch ($field) {
102 case 'visible':
af189935 103 require_capability('moodle/course:activityvisibility', $modcontext);
cd9224ab 104 set_coursemodule_visible($cm->id, $value);
b2054038 105 break;
106
107 case 'groupmode':
af189935 108 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 109 set_coursemodule_groupmode($cm->id, $value);
b2054038 110 break;
111
ebaa29d1 112 case 'indent':
af189935 113 require_capability('moodle/course:manageactivities', $modcontext);
ebaa29d1
ARN
114 $cm->indent = $value;
115 if ($cm->indent >= 0) {
cd9224ab 116 $DB->update_record('course_modules', $cm);
607c1bc1 117 }
118 break;
607c1bc1 119
b2054038 120 case 'move':
af189935 121 require_capability('moodle/course:manageactivities', $modcontext);
6bb08163 122 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
ff92b711 123 throw new moodle_exception('AJAX commands.php: Bad section ID '.$sectionid);
b2054038 124 }
3440ec12 125
b2054038 126 if ($beforeid > 0){
cd9224ab 127 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
6bb08163 128 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
b2054038 129 } else {
130 $beforemod = NULL;
131 }
132
cd9224ab 133 moveto_module($cm, $section, $beforemod);
b2054038 134 break;
135 }
edc06a53 136 rebuild_course_cache($course->id);
b2054038 137 break;
3440ec12 138
139 case 'course':
b2054038 140 switch($field) {
141 case 'marker':
af189935 142 require_capability('moodle/course:update', $coursecontext);
93d46f48 143 course_set_marker($course->id, $value);
b2054038 144 break;
145 }
146 break;
147 }
148 break;
149
150 case 'DELETE':
151 switch ($class) {
b2054038 152 case 'resource':
af189935 153 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 154 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
37fb48e0 155
156 if (file_exists($modlib)) {
157 include_once($modlib);
158 } else {
ff92b711 159 throw new moodle_exception("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
b2054038 160 }
cd9224ab 161 $deleteinstancefunction = $cm->modname."_delete_instance";
37fb48e0 162
163 // Run the module's cleanup funtion.
164 if (!$deleteinstancefunction($cm->instance)) {
ff92b711 165 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
37fb48e0 166 die;
167 }
cd9224ab 168
cd9224ab 169 // remove all module files in case modules forget to do that
170 $fs = get_file_storage();
171 $fs->delete_area_files($modcontext->id);
172
37fb48e0 173 if (!delete_course_module($cm->id)) {
ff92b711 174 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
cd9224ab 175 }
176 // Remove the course_modules entry.
177 if (!delete_mod_from_section($cm->id, $cm->section)) {
ff92b711 178 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
37fb48e0 179 }
180
c5234aa1
SZ
181 // Trigger a mod_deleted event with information about this module.
182 $eventdata = new stdClass();
183 $eventdata->modulename = $cm->modname;
184 $eventdata->cmid = $cm->id;
185 $eventdata->courseid = $course->id;
186 $eventdata->userid = $USER->id;
187 events_trigger('mod_deleted', $eventdata);
188
c4ce5def 189 rebuild_course_cache($course->id);
190
37fb48e0 191 add_to_log($courseid, "course", "delete mod",
192 "view.php?id=$courseid",
cd9224ab 193 "$cm->modname $cm->instance", $cm->id);
b2054038 194 break;
195 }
196 break;
197}