MDL-32873 Separate out course ajax enabled detection into new function
[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
ff92b711
RK
51$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
52// Check user is logged in and set contexts if we are dealing with resource
53if (in_array($class, array('resource'))) {
54 $cm = get_coursemodule_from_id(null, $id, $course->id, false, MUST_EXIST);
55 require_login($course, false, $cm);
56 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
57} else {
58 require_login($course);
59 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
60}
92942376
PS
61require_sesskey();
62
ff92b711
RK
63echo $OUTPUT->header(); // send headers
64
29ca8b88 65// OK, now let's process the parameters and do stuff
49c4d8ca 66// MDL-10221 the DELETE method is not allowed on some web servers, so we simulate it with the action URL param
67$requestmethod = $_SERVER['REQUEST_METHOD'];
68if ($pageaction == 'DELETE') {
69 $requestmethod = 'DELETE';
70}
71
72switch($requestmethod) {
b2054038 73 case 'POST':
29ca8b88 74
b2054038 75 switch ($class) {
b2054038 76 case 'section':
af189935 77 require_capability('moodle/course:update', $coursecontext);
3440ec12 78
6bb08163 79 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
ff92b711 80 throw new moodle_exception('AJAX commands.php: Bad Section ID '.$id);
b2054038 81 }
3440ec12 82
b2054038 83 switch ($field) {
84 case 'visible':
ebaa29d1
ARN
85 $resourcestotoggle = set_section_visible($course->id, $id, $value);
86 echo json_encode(array('resourcestotoggle' => $resourcestotoggle));
b2054038 87 break;
88
89 case 'move':
3440ec12 90 move_section_to($course, $id, $value);
b2054038 91 break;
92 }
edc06a53 93 rebuild_course_cache($course->id);
b2054038 94 break;
95
96 case 'resource':
b2054038 97 switch ($field) {
98 case 'visible':
af189935 99 require_capability('moodle/course:activityvisibility', $modcontext);
cd9224ab 100 set_coursemodule_visible($cm->id, $value);
b2054038 101 break;
102
103 case 'groupmode':
af189935 104 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 105 set_coursemodule_groupmode($cm->id, $value);
b2054038 106 break;
107
ebaa29d1 108 case 'indent':
af189935 109 require_capability('moodle/course:manageactivities', $modcontext);
ebaa29d1
ARN
110 $cm->indent = $value;
111 if ($cm->indent >= 0) {
cd9224ab 112 $DB->update_record('course_modules', $cm);
607c1bc1 113 }
114 break;
607c1bc1 115
b2054038 116 case 'move':
af189935 117 require_capability('moodle/course:manageactivities', $modcontext);
6bb08163 118 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
ff92b711 119 throw new moodle_exception('AJAX commands.php: Bad section ID '.$sectionid);
b2054038 120 }
3440ec12 121
b2054038 122 if ($beforeid > 0){
cd9224ab 123 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
6bb08163 124 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
b2054038 125 } else {
126 $beforemod = NULL;
127 }
128
cd9224ab 129 moveto_module($cm, $section, $beforemod);
b2054038 130 break;
131 }
edc06a53 132 rebuild_course_cache($course->id);
b2054038 133 break;
3440ec12 134
135 case 'course':
b2054038 136 switch($field) {
137 case 'marker':
af189935 138 require_capability('moodle/course:update', $coursecontext);
93d46f48 139 course_set_marker($course->id, $value);
b2054038 140 break;
141 }
142 break;
143 }
144 break;
145
146 case 'DELETE':
147 switch ($class) {
b2054038 148 case 'resource':
af189935 149 require_capability('moodle/course:manageactivities', $modcontext);
cd9224ab 150 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
37fb48e0 151
152 if (file_exists($modlib)) {
153 include_once($modlib);
154 } else {
ff92b711 155 throw new moodle_exception("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
b2054038 156 }
cd9224ab 157 $deleteinstancefunction = $cm->modname."_delete_instance";
37fb48e0 158
159 // Run the module's cleanup funtion.
160 if (!$deleteinstancefunction($cm->instance)) {
ff92b711 161 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
37fb48e0 162 die;
163 }
cd9224ab 164
cd9224ab 165 // remove all module files in case modules forget to do that
166 $fs = get_file_storage();
167 $fs->delete_area_files($modcontext->id);
168
37fb48e0 169 if (!delete_course_module($cm->id)) {
ff92b711 170 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
cd9224ab 171 }
172 // Remove the course_modules entry.
173 if (!delete_mod_from_section($cm->id, $cm->section)) {
ff92b711 174 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
37fb48e0 175 }
176
c5234aa1
SZ
177 // Trigger a mod_deleted event with information about this module.
178 $eventdata = new stdClass();
179 $eventdata->modulename = $cm->modname;
180 $eventdata->cmid = $cm->id;
181 $eventdata->courseid = $course->id;
182 $eventdata->userid = $USER->id;
183 events_trigger('mod_deleted', $eventdata);
184
c4ce5def 185 rebuild_course_cache($course->id);
186
37fb48e0 187 add_to_log($courseid, "course", "delete mod",
188 "view.php?id=$courseid",
cd9224ab 189 "$cm->modname $cm->instance", $cm->id);
b2054038 190 break;
191 }
192 break;
193}