MDL-32676 improve YUI init performance via combo loader caching
[moodle.git] / course / rest.php
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.
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/>.
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  */
26 if (!defined('AJAX_SCRIPT')) {
27     define('AJAX_SCRIPT', true);
28 }
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
47 $PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
49 //NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
51 if (empty($CFG->enablecourseajax)) {
52     throw new moodle_exception('Course AJAX not allowed');
53 }
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
57 if (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 }
65 require_sesskey();
67 echo $OUTPUT->header(); // send headers
69 // OK, now let's process the parameters and do stuff
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'];
72 if ($pageaction == 'DELETE') {
73     $requestmethod = 'DELETE';
74 }
76 switch($requestmethod) {
77     case 'POST':
79         switch ($class) {
80             case 'section':
81                 require_capability('moodle/course:update', $coursecontext);
83                 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
84                     throw new moodle_exception('AJAX commands.php: Bad Section ID '.$id);
85                 }
87                 switch ($field) {
88                     case 'visible':
89                         $resourcestotoggle = set_section_visible($course->id, $id, $value);
90                         echo json_encode(array('resourcestotoggle' => $resourcestotoggle));
91                         break;
93                     case 'move':
94                         move_section_to($course, $id, $value);
95                         break;
96                 }
97                 rebuild_course_cache($course->id);
98                 break;
100             case 'resource':
101                 switch ($field) {
102                     case 'visible':
103                         require_capability('moodle/course:activityvisibility', $modcontext);
104                         set_coursemodule_visible($cm->id, $value);
105                         break;
107                     case 'groupmode':
108                         require_capability('moodle/course:manageactivities', $modcontext);
109                         set_coursemodule_groupmode($cm->id, $value);
110                         break;
112                     case 'indent':
113                         require_capability('moodle/course:manageactivities', $modcontext);
114                         $cm->indent = $value;
115                         if ($cm->indent >= 0) {
116                             $DB->update_record('course_modules', $cm);
117                         }
118                         break;
120                     case 'move':
121                         require_capability('moodle/course:manageactivities', $modcontext);
122                         if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
123                             throw new moodle_exception('AJAX commands.php: Bad section ID '.$sectionid);
124                         }
126                         if ($beforeid > 0){
127                             $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
128                             $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
129                         } else {
130                             $beforemod = NULL;
131                         }
133                         if (debugging('',DEBUG_DEVELOPER)) {
134                             error_log(serialize($beforemod));
135                         }
137                         moveto_module($cm, $section, $beforemod);
138                         break;
139                 }
140                 rebuild_course_cache($course->id);
141                 break;
143             case 'course':
144                 switch($field) {
145                     case 'marker':
146                         require_capability('moodle/course:update', $coursecontext);
147                         course_set_marker($course->id, $value);
148                         break;
149                 }
150                 break;
151         }
152         break;
154     case 'DELETE':
155         switch ($class) {
156             case 'resource':
157                 require_capability('moodle/course:manageactivities', $modcontext);
158                 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
160                 if (file_exists($modlib)) {
161                     include_once($modlib);
162                 } else {
163                     throw new moodle_exception("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
164                 }
165                 $deleteinstancefunction = $cm->modname."_delete_instance";
167                 // Run the module's cleanup funtion.
168                 if (!$deleteinstancefunction($cm->instance)) {
169                     throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
170                     die;
171                 }
173                 // remove all module files in case modules forget to do that
174                 $fs = get_file_storage();
175                 $fs->delete_area_files($modcontext->id);
177                 if (!delete_course_module($cm->id)) {
178                     throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
179                 }
180                 // Remove the course_modules entry.
181                 if (!delete_mod_from_section($cm->id, $cm->section)) {
182                     throw new moodle_exception("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
183                 }
185                 // Trigger a mod_deleted event with information about this module.
186                 $eventdata = new stdClass();
187                 $eventdata->modulename = $cm->modname;
188                 $eventdata->cmid       = $cm->id;
189                 $eventdata->courseid   = $course->id;
190                 $eventdata->userid     = $USER->id;
191                 events_trigger('mod_deleted', $eventdata);
193                 rebuild_course_cache($course->id);
195                 add_to_log($courseid, "course", "delete mod",
196                            "view.php?id=$courseid",
197                            "$cm->modname $cm->instance", $cm->id);
198                 break;
199         }
200         break;