weblib MDL-6820 Reverted modification due to unforseen regressions
[moodle.git] / course / rest.php
blobc3496f4d60bb9d90a0a0b3fc9617a284f3b0f746
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);
23 $pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command
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);
47 if (!empty($CFG->disablecourseajax)) {
48 error_log('Course AJAX not allowed');
49 die;
52 require_sesskey();
54 // OK, now let's process the parameters and do stuff
56 $req_method = ($pageaction == 'DELETE') ? 'DELETE' : $_SERVER['REQUEST_METHOD'];
58 switch($req_method) {
60 case 'POST':
62 switch ($class) {
63 case 'block':
65 switch ($field) {
66 case 'visible':
67 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
68 break;
70 case 'position': // Misleading case. Should probably call it 'move'.
71 // We want to move the block around. This means changing
72 // the column (position field) and/or block sort order
73 // (weight field).
74 $undifinedinsertedid = FALSE;
75 $isaddednewblock = FALSE;
77 if (!empty($positiontoinsertid) && $positiontoinsertid != 'undefined' && strpos($positiontoinsertid, 'column') === FALSE ) {
78 $isaddednewblock = (substr($positiontoinsertid,0,1) != 'i') ? TRUE : FALSE;
79 if ($positiontoinsertid == 'linst0' || $positiontoinsertid == 'rinst0') {
80 $isaddednewblock = FALSE;
83 $positiontoinsertid = clean_param($positiontoinsertid, PARAM_SEQUENCE);
84 } else if($positiontoinsertid == 'undefined') {
85 $undifinedinsertedid = TRUE;
86 $positiontoinsertid = 0;
87 } else {
88 $positiontoinsertid = 0;
91 if ($positiontoinsertid > 0) {
93 $instsql = 'SELECT * FROM '. $CFG->prefix .'block_instance WHERE '
94 .' id = \''. $instanceid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
95 $instweight = get_record_sql($instsql);
97 $sql = 'SELECT * FROM '. $CFG->prefix .'block_instance WHERE '
98 .' id = \''. $positiontoinsertid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
99 $targetweight = get_record_sql($sql);
101 //$instweight = get_record("block_instance", 'id', $positiontoinsertid, "position",$column, 'pageId', $courseid);
102 if (!empty($targetweight->weight) && !empty($instweight->weight)) {
103 if ($positiontoinsert == "before") {
104 if ($targetweight->weight < $instweight->weight || ($instanceid == $positiontoinsertid)) {
105 $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight ;
106 } else {
107 $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight -1 ;
109 } else if ($positiontoinsert == "after") {
110 $destweight = $targetweight->weight + 1;
112 } else {
113 $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight - 1 ;
115 } else {
116 $sql = 'SELECT max(weight) as weight FROM '. $CFG->prefix .'block_instance WHERE '
117 .'position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
118 $instweight = get_record_sql($sql);
120 $countrecords = count_records('block_instance', 'position', $column, 'pageId', $courseid);
121 $recordexists = record_exists('block_instance', 'position', $column, 'pageId', $courseid, 'id', $instanceid);
123 if ($isaddednewblock || $undifinedinsertedid) {
124 if (!empty($countrecords)) {
125 $destweight = ($recordexists) ? $instweight->weight : $instweight->weight + 1 ;
126 } else {
127 $destweight = 0;
129 } else {
130 if (!empty($countrecords)) {
131 $destweight = ($recordexists) ? $instweight->weight : $instweight->weight + 1 ;
132 } else {
133 $destweight = 0;
138 blocks_move_block($PAGE, $blockinstance, $column, $destweight);
140 $current_blocks = blocks_get_by_page_pinned($PAGE);
141 global $COURSE;
142 foreach ($current_blocks as $pos =>$blocks) {
143 if (count($current_blocks[$pos]) == 0) {
144 print_side_block('', '', NULL, NULL, '', array('id'=> $pos.'inst0', 'class'=>'tempblockhandle'), '');
147 break;
149 break;
151 case 'section':
153 if (!record_exists('course_sections','course',$course->id,'section',$id)) {
154 error_log('AJAX commands.php: Bad Section ID '.$id);
155 die;
158 switch ($field) {
159 case 'visible':
160 set_section_visible($course->id, $id, $value);
161 break;
163 case 'move':
164 move_section_to($course, $id, $value);
165 break;
167 rebuild_course_cache($course->id);
168 break;
170 case 'resource':
171 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id)) {
172 error_log('AJAX commands.php: Bad course module ID '.$id);
173 die;
175 switch ($field) {
176 case 'visible':
177 set_coursemodule_visible($mod->id, $value);
178 break;
180 case 'groupmode':
181 set_coursemodule_groupmode($mod->id, $value);
182 break;
184 case 'indentleft':
185 if ($mod->indent > 0) {
186 $mod->indent--;
187 update_record('course_modules', $mod);
189 break;
191 case 'indentright':
192 $mod->indent++;
193 update_record('course_modules', $mod);
194 break;
196 case 'move':
197 if (!$section = get_record('course_sections','course',$course->id,'section',$sectionid)) {
198 error_log('AJAX commands.php: Bad section ID '.$sectionid);
199 die;
202 if ($beforeid > 0){
203 $beforemod = get_record('course_modules', 'id', $beforeid);
204 } else {
205 $beforemod = NULL;
208 if (debugging('',DEBUG_DEVELOPER)) {
209 error_log(serialize($beforemod));
212 moveto_module($mod, $section, $beforemod);
213 break;
215 rebuild_course_cache($course->id);
216 break;
218 case 'course':
219 switch($field) {
220 case 'marker':
221 $newcourse = new object;
222 $newcourse->id = $course->id;
223 $newcourse->marker = $value;
224 if (!update_record('course',$newcourse)) {
225 error_log('AJAX commands.php: Failed to update course marker for course '.$newcourse->id);
226 die;
228 break;
230 break;
232 break;
234 case 'DELETE':
235 switch ($class) {
236 case 'block':
237 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
238 break;
240 case 'resource':
241 if (!$cm = get_record('course_modules', 'id', $id, 'course', $course->id)) {
242 error_log('AJAX rest.php: Bad course module ID '.$id);
243 die;
245 if (!$mod = get_record('modules', 'id', $cm->module)) {
246 error_log('AJAX rest.php: Bad module ID '.$cm->module);
247 die;
249 $mod->name = clean_param($mod->name, PARAM_SAFEDIR); // For safety
250 $modlib = "$CFG->dirroot/mod/$mod->name/lib.php";
252 if (file_exists($modlib)) {
253 include_once($modlib);
254 } else {
255 error_log("Ajax rest.php: This module is missing important code ($modlib)");
256 die;
258 $deleteinstancefunction = $mod->name."_delete_instance";
260 // Run the module's cleanup funtion.
261 if (!$deleteinstancefunction($cm->instance)) {
262 error_log("Ajax rest.php: Could not delete the $mod->name (instance)");
263 die;
265 // Remove the course_modules entry.
266 if (!delete_course_module($cm->id)) {
267 error_log("Ajax rest.php: Could not delete the $mod->modulename (coursemodule)");
268 die;
271 rebuild_course_cache($course->id);
273 add_to_log($courseid, "course", "delete mod",
274 "view.php?id=$courseid",
275 "$mod->name $cm->instance", $cm->id);
276 break;
278 break;