MDL-33768 enable multimedia filter by default in new installs
[moodle.git] / course / rest.php
blob6efd228db6519c634474afbc3a501aa9a9f597c8
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 if (!defined('AJAX_SCRIPT')) {
27 define('AJAX_SCRIPT', true);
29 require_once(dirname(__FILE__) . '/../config.php');
30 require_once($CFG->dirroot.'/course/lib.php');
32 // Initialise ALL the incoming parameters here, up front.
33 $courseid = required_param('courseId', PARAM_INT);
34 $class = required_param('class', PARAM_ALPHA);
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);
45 $pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command
46 $title = optional_param('title', '', PARAM_TEXT);
48 $PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
50 //NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
52 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
53 // Check user is logged in and set contexts if we are dealing with resource
54 if (in_array($class, array('resource'))) {
55 $cm = get_coursemodule_from_id(null, $id, $course->id, false, MUST_EXIST);
56 require_login($course, false, $cm);
57 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
58 } else {
59 require_login($course);
61 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
62 require_sesskey();
64 echo $OUTPUT->header(); // send headers
66 // OK, now let's process the parameters and do stuff
67 // MDL-10221 the DELETE method is not allowed on some web servers, so we simulate it with the action URL param
68 $requestmethod = $_SERVER['REQUEST_METHOD'];
69 if ($pageaction == 'DELETE') {
70 $requestmethod = 'DELETE';
73 switch($requestmethod) {
74 case 'POST':
76 switch ($class) {
77 case 'section':
78 require_capability('moodle/course:update', $coursecontext);
80 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
81 throw new moodle_exception('AJAX commands.php: Bad Section ID '.$id);
84 switch ($field) {
85 case 'visible':
86 $resourcestotoggle = set_section_visible($course->id, $id, $value);
87 echo json_encode(array('resourcestotoggle' => $resourcestotoggle));
88 break;
90 case 'move':
91 move_section_to($course, $id, $value);
92 // See if format wants to do something about it
93 $libfile = $CFG->dirroot.'/course/format/'.$course->format.'/lib.php';
94 $functionname = 'callback_'.$course->format.'_ajax_section_move';
95 if (!function_exists($functionname) && file_exists($libfile)) {
96 require_once $libfile;
98 if (function_exists($functionname)) {
99 echo json_encode($functionname($course));
101 break;
103 rebuild_course_cache($course->id);
104 break;
106 case 'resource':
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 'indent':
119 require_capability('moodle/course:manageactivities', $modcontext);
120 $cm->indent = $value;
121 if ($cm->indent >= 0) {
122 $DB->update_record('course_modules', $cm);
124 break;
126 case 'move':
127 require_capability('moodle/course:manageactivities', $modcontext);
128 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
129 throw new moodle_exception('AJAX commands.php: Bad section ID '.$sectionid);
132 if ($beforeid > 0){
133 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
134 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
135 } else {
136 $beforemod = NULL;
139 moveto_module($cm, $section, $beforemod);
140 break;
141 case 'gettitle':
142 require_capability('moodle/course:manageactivities', $modcontext);
143 $cm = get_coursemodule_from_id('', $id, 0, false, MUST_EXIST);
144 $module = new stdClass();
145 $module->id = $cm->instance;
147 // Don't pass edit strings through multilang filters - we need the entire string
148 echo json_encode(array('instancename' => $cm->name));
149 break;
150 case 'updatetitle':
151 require_capability('moodle/course:manageactivities', $modcontext);
152 $cm = get_coursemodule_from_id('', $id, 0, false, MUST_EXIST);
153 $module = new stdClass();
154 $module->id = $cm->instance;
156 // Escape strings as they would be by mform
157 if (!empty($CFG->formatstringstriptags)) {
158 $module->name = clean_param($title, PARAM_TEXT);
159 } else {
160 $module->name = clean_param($title, PARAM_CLEANHTML);
163 if (!empty($module->name)) {
164 $DB->update_record($cm->modname, $module);
165 } else {
166 $module->name = $cm->name;
169 // We need to return strings after they've been through filters for multilang
170 $stringoptions = new stdClass;
171 $stringoptions->context = $coursecontext;
172 echo json_encode(array('instancename' => format_string($module->name, true, $stringoptions)));
173 break;
175 rebuild_course_cache($course->id);
176 break;
178 case 'course':
179 switch($field) {
180 case 'marker':
181 require_capability('moodle/course:update', $coursecontext);
182 course_set_marker($course->id, $value);
183 break;
185 break;
187 break;
189 case 'DELETE':
190 switch ($class) {
191 case 'resource':
192 require_capability('moodle/course:manageactivities', $modcontext);
193 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
195 if (file_exists($modlib)) {
196 include_once($modlib);
197 } else {
198 throw new moodle_exception("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
200 $deleteinstancefunction = $cm->modname."_delete_instance";
202 // Run the module's cleanup funtion.
203 if (!$deleteinstancefunction($cm->instance)) {
204 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
205 die;
208 // remove all module files in case modules forget to do that
209 $fs = get_file_storage();
210 $fs->delete_area_files($modcontext->id);
212 if (!delete_course_module($cm->id)) {
213 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
215 // Remove the course_modules entry.
216 if (!delete_mod_from_section($cm->id, $cm->section)) {
217 throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
220 // Trigger a mod_deleted event with information about this module.
221 $eventdata = new stdClass();
222 $eventdata->modulename = $cm->modname;
223 $eventdata->cmid = $cm->id;
224 $eventdata->courseid = $course->id;
225 $eventdata->userid = $USER->id;
226 events_trigger('mod_deleted', $eventdata);
228 rebuild_course_cache($course->id);
230 add_to_log($courseid, "course", "delete mod",
231 "view.php?id=$courseid",
232 "$cm->modname $cm->instance", $cm->id);
233 break;
235 break;