MDL-9265:
[moodle-linuxchix.git] / course / rest.php
blobe47e04f0f12eb9c4c8833ecfd64d9bda77969541
1 <?php // $Id$
2 // Provide interface for topics AJAX course formats
4 require_once('../config.php');
5 require_once($CFG->dirroot.'/course/lib.php');
6 require_once($CFG->libdir .'/pagelib.php');
7 require_once($CFG->libdir .'/blocklib.php');
10 // Initialise ALL the incoming parameters here, up front.
11 $courseid = required_param('courseId', PARAM_INT);
12 $class = required_param('class', PARAM_ALPHA);
13 $field = optional_param('field', '', PARAM_ALPHA);
14 $instanceid = optional_param('instanceId', 0, PARAM_INT);
15 $sectionid = optional_param('sectionId', 0, PARAM_INT);
16 $beforeid = optional_param('beforeId', 0, PARAM_INT);
17 $value = optional_param('value', 0, PARAM_INT);
18 $column = optional_param('column', 0, PARAM_ALPHA);
19 $id = optional_param('id', 0, PARAM_INT);
20 $summary = optional_param('summary', '', PARAM_RAW);
21 $sequence = optional_param('sequence', '', PARAM_SEQUENCE);
22 $visible = optional_param('visible', 0, PARAM_INT);
25 // Authorise the user and verify some incoming data
26 if (!$course = get_record('course', 'id', $courseid)) {
27 error_log('AJAX commands.php: Course does not exist');
28 die;
31 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
32 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
34 if (!empty($instanceid)) {
35 $blockinstance = blocks_find_instance($instanceid, $pageblocks);
36 if (!$blockinstance || $blockinstance->pageid != $course->id
37 || $blockinstance->pagetype != 'course-view') {
38 error_log('AJAX commands.php: Bad block ID '.$instanceid);
39 die;
43 $context = get_context_instance(CONTEXT_COURSE, $course->id);
44 require_login($course->id);
45 require_capability('moodle/course:update', $context);
49 // OK, now let's process the parameters and do stuff
50 switch($_SERVER['REQUEST_METHOD']) {
51 case 'POST':
53 switch ($class) {
54 case 'block':
56 switch ($field) {
57 case 'visible':
58 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
59 break;
61 case 'position': // Misleading case. Should probably call it 'move'.
62 // We want to move the block around. This means changing
63 // the column (position field) and/or block sort order
64 // (weight field).
65 blocks_move_block($PAGE, $blockinstance, $column, $value);
66 break;
68 break;
70 case 'section':
72 if (!record_exists('course_sections','course',$course->id,'section',$id)) {
73 error_log('AJAX commands.php: Bad Section ID '.$id);
74 die;
77 switch ($field) {
78 case 'visible':
79 set_section_visible($course->id, $id, $value);
80 break;
82 case 'move':
83 move_section($course, $id, $value);
84 break;
86 break;
88 case 'resource':
89 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id)) {
90 error_log('AJAX commands.php: Bad course module ID '.$id);
91 die;
93 switch ($field) {
94 case 'visible':
95 set_coursemodule_visible($mod->id, $value);
96 break;
98 case 'groupmode':
99 set_coursemodule_groupmode($mod->id, $value);
100 break;
102 case 'indentleft':
103 if ($mod->indent > 0) {
104 $mod->indent--;
105 update_record('course_modules', $mod);
107 break;
109 case 'indentright':
110 $mod->indent++;
111 update_record('course_modules', $mod);
112 break;
114 case 'move':
115 if (!$section = get_record('course_sections','course',$course->id,'section',$sectionid)) {
116 error_log('AJAX commands.php: Bad section ID '.$sectionid);
117 die;
120 if ($beforeid > 0){
121 $beforemod = get_record('course_modules', 'id', $beforeid);
122 } else {
123 $beforemod = NULL;
126 if (debugging('',DEBUG_DEVELOPER)) {
127 error_log(serialize($beforemod));
130 moveto_module($mod, $section, $beforemod);
131 break;
133 break;
135 case 'course':
136 switch($field) {
137 case 'marker':
138 $newcourse = new object;
139 $newcourse->id = $course->id;
140 $newcourse->marker = $value;
141 if (!update_record('course',$newcourse)) {
142 error_log('AJAX commands.php: Failed to update course marker for course '.$newcourse->id);
143 die;
145 break;
147 break;
149 break;
151 case 'DELETE':
152 switch ($class) {
153 case 'block':
154 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
155 break;
157 case 'resource':
158 if (!$cm = get_record('course_modules', 'id', $id, 'course', $course->id)) {
159 error_log('AJAX rest.php: Bad course module ID '.$id);
160 die;
162 if (!$mod = get_record('modules', 'id', $cm->module)) {
163 error_log('AJAX rest.php: Bad module ID '.$cm->module);
164 die;
166 $mod->name = clean_param($mod->name, PARAM_SAFEDIR); // For safety
167 $modlib = "$CFG->dirroot/mod/$mod->name/lib.php";
169 if (file_exists($modlib)) {
170 include_once($modlib);
171 } else {
172 error_log("Ajax rest.php: This module is missing important code ($modlib)");
173 die;
175 $deleteinstancefunction = $mod->name."_delete_instance";
177 // Run the module's cleanup funtion.
178 if (!$deleteinstancefunction($cm->instance)) {
179 error_log("Ajax rest.php: Could not delete the $mod->name (instance)");
180 die;
182 // Remove the course_modules entry.
183 if (!delete_course_module($cm->id)) {
184 error_log("Ajax rest.php: Could not delete the $mod->modulename (coursemodule)");
185 die;
188 rebuild_course_cache($course->id);
190 add_to_log($courseid, "course", "delete mod",
191 "view.php?id=$courseid",
192 "$mod->name $cm->instance", $cm->id);
193 break;
195 break;