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