Updated the 19 build version to 20100915
[moodle.git] / course / rest.php
blob69d106d32fb74ef23b41d0f3118957f5ff53f9a9
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
24 $positiontoinsert = optional_param('positiontoinsert', '', PARAM_ALPHA);
25 $positiontoinsertid = optional_param('positiontoinsertid', '', PARAM_RAW);
27 // Authorise the user and verify some incoming data
28 if (!$course = get_record('course', 'id', $courseid)) {
29 error_log('AJAX commands.php: Course does not exist');
30 die;
33 $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
34 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
36 if (!empty($instanceid)) {
37 $blockinstance = blocks_find_instance($instanceid, $pageblocks);
38 if (!$blockinstance || $blockinstance->pageid != $course->id
39 || $blockinstance->pagetype != 'course-view') {
40 error_log('AJAX commands.php: Bad block ID '.$instanceid);
41 die;
45 $context = get_context_instance(CONTEXT_COURSE, $course->id);
46 require_login($course->id);
47 require_capability('moodle/course:update', $context);
49 if (!empty($CFG->disablecourseajax)) {
50 error_log('Course AJAX not allowed');
51 die;
54 require_sesskey();
56 // OK, now let's process the parameters and do stuff
58 $req_method = ($pageaction == 'DELETE') ? 'DELETE' : $_SERVER['REQUEST_METHOD'];
60 switch($req_method) {
62 case 'POST':
64 switch ($class) {
65 case 'block':
67 switch ($field) {
68 case 'visible':
69 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
70 break;
72 case 'position': // Misleading case. Should probably call it 'move'.
73 // We want to move the block around. This means changing
74 // the column (position field) and/or block sort order
75 // (weight field).
76 $undifinedinsertedid = FALSE;
77 $isaddednewblock = FALSE;
79 if (!empty($positiontoinsertid) && $positiontoinsertid != 'undefined' && strpos($positiontoinsertid, 'column') === FALSE ) {
80 $isaddednewblock = (substr($positiontoinsertid,0,1) != 'i') ? TRUE : FALSE;
81 if ($positiontoinsertid == 'linst0' || $positiontoinsertid == 'rinst0') {
82 $isaddednewblock = FALSE;
85 $positiontoinsertid = clean_param($positiontoinsertid, PARAM_SEQUENCE);
86 } else if($positiontoinsertid == 'undefined') {
87 $undifinedinsertedid = TRUE;
88 $positiontoinsertid = 0;
89 } else {
90 $positiontoinsertid = 0;
93 if ($positiontoinsertid > 0) {
95 $instsql = 'SELECT * FROM '. $CFG->prefix .'block_instance WHERE '
96 .' id = \''. $instanceid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
97 $instweight = get_record_sql($instsql);
99 $sql = 'SELECT * FROM '. $CFG->prefix .'block_instance WHERE '
100 .' id = \''. $positiontoinsertid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
101 $targetweight = get_record_sql($sql);
103 //$instweight = get_record("block_instance", 'id', $positiontoinsertid, "position",$column, 'pageId', $courseid);
104 if (!empty($targetweight->weight) && !empty($instweight->weight)) {
105 if ($positiontoinsert == "before") {
106 if ($targetweight->weight < $instweight->weight || ($instanceid == $positiontoinsertid)) {
107 $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight ;
108 } else {
109 $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight -1 ;
111 } else if ($positiontoinsert == "after") {
112 $destweight = $targetweight->weight + 1;
114 } else {
115 $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight - 1 ;
117 } else {
118 $sql = 'SELECT max(weight) as weight FROM '. $CFG->prefix .'block_instance WHERE '
119 .'position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
120 $instweight = get_record_sql($sql);
122 $countrecords = count_records('block_instance', 'position', $column, 'pageId', $courseid);
123 $recordexists = record_exists('block_instance', 'position', $column, 'pageId', $courseid, 'id', $instanceid);
125 if ($isaddednewblock || $undifinedinsertedid) {
126 if (!empty($countrecords)) {
127 $destweight = ($recordexists) ? $instweight->weight : $instweight->weight + 1 ;
128 } else {
129 $destweight = 0;
131 } else {
132 if (!empty($countrecords)) {
133 $destweight = ($recordexists) ? $instweight->weight : $instweight->weight + 1 ;
134 } else {
135 $destweight = 0;
140 blocks_move_block($PAGE, $blockinstance, $column, $destweight);
142 $current_blocks = blocks_get_by_page_pinned($PAGE);
143 global $COURSE;
144 foreach ($current_blocks as $pos =>$blocks) {
145 if (count($current_blocks[$pos]) == 0) {
146 print_side_block('', '', NULL, NULL, '', array('id'=> $pos.'inst0', 'class'=>'tempblockhandle'), '');
149 break;
151 break;
153 case 'section':
155 if (!record_exists('course_sections','course',$course->id,'section',$id)) {
156 error_log('AJAX commands.php: Bad Section ID '.$id);
157 die;
160 switch ($field) {
161 case 'visible':
162 set_section_visible($course->id, $id, $value);
163 break;
165 case 'move':
166 move_section_to($course, $id, $value);
167 break;
169 rebuild_course_cache($course->id);
170 break;
172 case 'resource':
173 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id)) {
174 error_log('AJAX commands.php: Bad course module ID '.$id);
175 die;
177 switch ($field) {
178 case 'visible':
179 set_coursemodule_visible($mod->id, $value);
180 break;
182 case 'groupmode':
183 set_coursemodule_groupmode($mod->id, $value);
184 break;
186 case 'indentleft':
187 if ($mod->indent > 0) {
188 $mod->indent--;
189 update_record('course_modules', $mod);
191 break;
193 case 'indentright':
194 $mod->indent++;
195 update_record('course_modules', $mod);
196 break;
198 case 'move':
199 if (!$section = get_record('course_sections','course',$course->id,'section',$sectionid)) {
200 error_log('AJAX commands.php: Bad section ID '.$sectionid);
201 die;
204 if ($beforeid > 0){
205 $beforemod = get_record('course_modules', 'id', $beforeid);
206 } else {
207 $beforemod = NULL;
210 if (debugging('',DEBUG_DEVELOPER)) {
211 error_log(serialize($beforemod));
214 moveto_module($mod, $section, $beforemod);
215 break;
217 rebuild_course_cache($course->id);
218 break;
220 case 'course':
221 switch($field) {
222 case 'marker':
223 $newcourse = new object;
224 $newcourse->id = $course->id;
225 $newcourse->marker = $value;
226 if (!update_record('course',$newcourse)) {
227 error_log('AJAX commands.php: Failed to update course marker for course '.$newcourse->id);
228 die;
230 break;
232 break;
234 break;
236 case 'DELETE':
237 switch ($class) {
238 case 'block':
239 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance, false, false);
240 break;
242 case 'resource':
243 if (!$cm = get_record('course_modules', 'id', $id, 'course', $course->id)) {
244 error_log('AJAX rest.php: Bad course module ID '.$id);
245 die;
247 if (!$mod = get_record('modules', 'id', $cm->module)) {
248 error_log('AJAX rest.php: Bad module ID '.$cm->module);
249 die;
251 $mod->name = clean_param($mod->name, PARAM_SAFEDIR); // For safety
252 $modlib = "$CFG->dirroot/mod/$mod->name/lib.php";
254 if (file_exists($modlib)) {
255 include_once($modlib);
256 } else {
257 error_log("Ajax rest.php: This module is missing important code ($modlib)");
258 die;
260 $deleteinstancefunction = $mod->name."_delete_instance";
262 // Run the module's cleanup funtion.
263 if (!$deleteinstancefunction($cm->instance)) {
264 error_log("Ajax rest.php: Could not delete the $mod->name (instance)");
265 die;
267 // Remove the course_modules entry.
268 if (!delete_course_module($cm->id)) {
269 error_log("Ajax rest.php: Could not delete the $mod->modulename (coursemodule)");
270 die;
273 rebuild_course_cache($course->id);
275 add_to_log($courseid, "course", "delete mod",
276 "view.php?id=$courseid",
277 "$mod->name $cm->instance", $cm->id);
278 break;
280 break;