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