Merged from 1.7
[moodle.git] / course / rest.php
CommitLineData
b2054038 1<?php // $Id$
2 // Provide interface for topics AJAX course formats
3
4require_once('../config.php');
5require_once($CFG->dirroot.'/course/lib.php');
6require_once($CFG->libdir .'/pagelib.php');
7require_once($CFG->libdir .'/blocklib.php');
8
9// Initialise ALL the incoming parameters here, up front.
10
11$courseid = required_param('courseId', PARAM_INT);
12$class = required_param('class', PARAM_ALPHA);
13
14$field = optional_param('field', '', PARAM_ALPHA);
15$instanceid = optional_param('instanceId', 0, PARAM_INT);
16$sectionid = optional_param('sectionId', 0, PARAM_INT);
17$beforeid = optional_param('beforeId', 0, PARAM_INT);
18$value = optional_param('value', 0, PARAM_INT);
19$column = optional_param('column', 0, PARAM_ALPHA);
20$id = optional_param('id', 0, PARAM_INT);
21$summary = optional_param('summary', '', PARAM_RAW);
22$sequence = optional_param('sequence', '', PARAM_SEQUENCE);
23$visible = optional_param('visible', 0, PARAM_INT);
24
25// Authorise the user and verify some incoming data
26
27if (!$course = get_record('course', 'id', $courseid)) {
28 error_log('AJAX commands.php: Course does not exist');
29 die;
30}
31
32$PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
33$pageblocks = blocks_setup($PAGE,BLOCKS_PINNED_BOTH);
b2054038 34if (!empty($instanceid)) {
35 $blockinstance = blocks_find_instance($instanceid, $pageblocks);
36 if (!$blockinstance || $blockinstance->pageid != $course->id || $blockinstance->pagetype != 'course-view') {
37 error_log('AJAX commands.php: Bad block ID '.$instanceid);
38 die;
39 }
40}
41
42$context = get_context_instance(CONTEXT_COURSE, $course->id);
43
44require_login($course->id);
45require_capability('moodle/course:update', $context);
46
47// OK, now let's process the parameters and do stuff
48
49switch($_SERVER['REQUEST_METHOD']) {
50 case 'POST':
51 switch ($class) {
52 case 'block':
53 switch ($field) {
54 case 'visible':
55 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
56 break;
57
58 case 'position':
bb781a70 59 //while not entirely restful this will handle all the details of moving a block
60 blocks_execute_repositioning_atomic($blockinstance, $column, $value);
b2054038 61 break;
62 }
63 break;
64
65
66 case 'section':
67
68 if (!record_exists('course_sections','course',$course->id,'section',$id)) {
69 error_log('AJAX commands.php: Bad Section ID '.$id);
70 die;
71 }
72
73 switch ($field) {
74 case 'visible':
75 set_section_visible($course->id, $id, $value);
76 break;
77
78 case 'move':
79 move_section($course, $id, $value);
80 break;
81 }
82 break;
83
84 case 'resource':
85 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id)) {
86 error_log('AJAX commands.php: Bad course module ID '.$id);
87 die;
88 }
89 switch ($field) {
90 case 'visible':
91 set_coursemodule_visible($mod->id, $value);
92 break;
93
94 case 'groupmode':
95 set_coursemodule_groupmode($mod->id, $value);
96 break;
97
98 case 'move':
99 if (!$section = get_record('course_sections','course',$course->id,'section',$sectionid)) {
100 error_log('AJAX commands.php: Bad section ID '.$sectionid);
101 die;
102 }
103
104 if ($beforeid > 0){
105 $beforemod = get_record('course_modules', 'id', $beforeid);
106 } else {
107 $beforemod = NULL;
108 }
109
1bbcb7c0 110 if (debugging('',DEBUG_DEVELOPER)) {
111 error_log(serialize($beforemod));
112 }
113
b2054038 114 moveto_module($mod, $section, $beforemod);
115 break;
116 }
117 break;
118
119 case 'course':
120 switch($field) {
121 case 'marker':
122 $newcourse = new object;
123 $newcourse->id = $course->id;
124 $newcourse->marker = $value;
125 if (!update_record('course',$newcourse)) {
126 error_log('AJAX commands.php: Failed to update course marker for course '.$newcourse->id);
127 die;
128 }
129 break;
130 }
131 break;
132 }
133 break;
134
135 case 'DELETE':
136 switch ($class) {
137 case 'block':
138 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
139 break;
140
141 case 'resource':
37fb48e0 142 if (!$cm = get_record('course_modules', 'id', $id, 'course', $course->id)) {
143 error_log('AJAX rest.php: Bad course module ID '.$id);
144 die;
145 }
146 if (!$mod = get_record('modules', 'id', $cm->module)) {
147 error_log('AJAX rest.php: Bad module ID '.$cm->module);
148 die;
149 }
150 $mod->name = clean_param($mod->name, PARAM_SAFEDIR); // For safety
151 $modlib = "$CFG->dirroot/mod/$mod->name/lib.php";
152
153 if (file_exists($modlib)) {
154 include_once($modlib);
155 } else {
156 error_log("Ajax rest.php: This module is missing important code ($modlib)");
b2054038 157 die;
158 }
37fb48e0 159 $deleteinstancefunction = $mod->name."_delete_instance";
160
161 // Run the module's cleanup funtion.
162 if (!$deleteinstancefunction($cm->instance)) {
163 error_log("Ajax rest.php: Could not delete the $mod->name (instance)");
164 die;
165 }
166 // Remove the course_modules entry.
167 if (!delete_course_module($cm->id)) {
168 error_log("Ajax rest.php: Could not delete the $mod->modulename (coursemodule)");
169 die;
170 }
171
c4ce5def 172 rebuild_course_cache($course->id);
173
37fb48e0 174 add_to_log($courseid, "course", "delete mod",
175 "view.php?id=$courseid",
176 "$mod->name $cm->instance", $cm->id);
b2054038 177 break;
178 }
179 break;
180}
181
f45cc76f 182?>