Moved the REST interface for course modification from
[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);
34
35if (!empty($instanceid)) {
36 $blockinstance = blocks_find_instance($instanceid, $pageblocks);
37 if (!$blockinstance || $blockinstance->pageid != $course->id || $blockinstance->pagetype != 'course-view') {
38 error_log('AJAX commands.php: Bad block ID '.$instanceid);
39 die;
40 }
41}
42
43$context = get_context_instance(CONTEXT_COURSE, $course->id);
44
45require_login($course->id);
46require_capability('moodle/course:update', $context);
47
48// OK, now let's process the parameters and do stuff
49
50switch($_SERVER['REQUEST_METHOD']) {
51 case 'POST':
52 switch ($class) {
53 case 'block':
54 switch ($field) {
55 case 'visible':
56 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
57 break;
58
59 case 'position':
60 $newblockinstance = new object;
61 $newblockinstance->id = $blockinstance->id;
62 $newblockinstance->position = $column;
63 $newblockinstance->weight = $value;
64 if (!update_record('block_instance',$newblockinstance)) {
65 error_log('AJAX commands.php: Failed to update block with ID '.$blockinstance->id);
66 die;
67 }
68 break;
69 }
70 break;
71
72
73 case 'section':
74
75 if (!record_exists('course_sections','course',$course->id,'section',$id)) {
76 error_log('AJAX commands.php: Bad Section ID '.$id);
77 die;
78 }
79
80 switch ($field) {
81 case 'visible':
82 set_section_visible($course->id, $id, $value);
83 break;
84
85 case 'move':
86 move_section($course, $id, $value);
87 break;
88 }
89 break;
90
91 case 'resource':
92 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id)) {
93 error_log('AJAX commands.php: Bad course module ID '.$id);
94 die;
95 }
96 switch ($field) {
97 case 'visible':
98 set_coursemodule_visible($mod->id, $value);
99 break;
100
101 case 'groupmode':
102 set_coursemodule_groupmode($mod->id, $value);
103 break;
104
105 case 'move':
106 if (!$section = get_record('course_sections','course',$course->id,'section',$sectionid)) {
107 error_log('AJAX commands.php: Bad section ID '.$sectionid);
108 die;
109 }
110
111 if ($beforeid > 0){
112 $beforemod = get_record('course_modules', 'id', $beforeid);
113 } else {
114 $beforemod = NULL;
115 }
116
117 moveto_module($mod, $section, $beforemod);
118 break;
119 }
120 break;
121
122 case 'course':
123 switch($field) {
124 case 'marker':
125 $newcourse = new object;
126 $newcourse->id = $course->id;
127 $newcourse->marker = $value;
128 if (!update_record('course',$newcourse)) {
129 error_log('AJAX commands.php: Failed to update course marker for course '.$newcourse->id);
130 die;
131 }
132 break;
133 }
134 break;
135 }
136 break;
137
138 case 'DELETE':
139 switch ($class) {
140 case 'block':
141 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
142 break;
143
144 case 'resource':
145 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id)) {
146 error_log('AJAX commands.php: Bad course module ID '.$id);
147 die;
148 }
149 delete_course_module($id);
150 break;
151 }
152 break;
153}
154
155?>