question MDL-19820 Replaced deprecated functions
[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');
b2054038 6
29ca8b88 7// Initialise ALL the incoming parameters here, up front.
8$courseid = required_param('courseId', PARAM_INT);
9$class = required_param('class', PARAM_ALPHA);
b2054038 10$field = optional_param('field', '', PARAM_ALPHA);
11$instanceid = optional_param('instanceId', 0, PARAM_INT);
12$sectionid = optional_param('sectionId', 0, PARAM_INT);
13$beforeid = optional_param('beforeId', 0, PARAM_INT);
14$value = optional_param('value', 0, PARAM_INT);
15$column = optional_param('column', 0, PARAM_ALPHA);
16$id = optional_param('id', 0, PARAM_INT);
17$summary = optional_param('summary', '', PARAM_RAW);
18$sequence = optional_param('sequence', '', PARAM_SEQUENCE);
19$visible = optional_param('visible', 0, PARAM_INT);
20
b2054038 21
29ca8b88 22// Authorise the user and verify some incoming data
6bb08163 23if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b2054038 24 error_log('AJAX commands.php: Course does not exist');
25 die;
26}
27
b2054038 28$context = get_context_instance(CONTEXT_COURSE, $course->id);
cd9224ab 29require_login($course);
b2054038 30require_capability('moodle/course:update', $context);
31
29ca8b88 32// OK, now let's process the parameters and do stuff
b2054038 33switch($_SERVER['REQUEST_METHOD']) {
34 case 'POST':
29ca8b88 35
b2054038 36 switch ($class) {
29ca8b88 37 case 'block':
38
39 switch ($field) {
3440ec12 40 case 'visible':
29ca8b88 41 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
42 break;
43
44 case 'position': // Misleading case. Should probably call it 'move'.
45 // We want to move the block around. This means changing
46 // the column (position field) and/or block sort order
47 // (weight field).
48 blocks_move_block($PAGE, $blockinstance, $column, $value);
49 break;
50 }
51 break;
b2054038 52
53 case 'section':
3440ec12 54
6bb08163 55 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
b2054038 56 error_log('AJAX commands.php: Bad Section ID '.$id);
57 die;
58 }
3440ec12 59
b2054038 60 switch ($field) {
61 case 'visible':
62 set_section_visible($course->id, $id, $value);
63 break;
64
65 case 'move':
3440ec12 66 move_section_to($course, $id, $value);
b2054038 67 break;
68 }
edc06a53 69 rebuild_course_cache($course->id);
b2054038 70 break;
71
72 case 'resource':
cd9224ab 73 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
b2054038 74 error_log('AJAX commands.php: Bad course module ID '.$id);
75 die;
76 }
77 switch ($field) {
78 case 'visible':
cd9224ab 79 set_coursemodule_visible($cm->id, $value);
b2054038 80 break;
81
82 case 'groupmode':
cd9224ab 83 set_coursemodule_groupmode($cm->id, $value);
b2054038 84 break;
85
607c1bc1 86 case 'indentleft':
cd9224ab 87 if ($cm->indent > 0) {
88 $cm->indent--;
89 $DB->update_record('course_modules', $cm);
607c1bc1 90 }
91 break;
92
93 case 'indentright':
cd9224ab 94 $cm->indent++;
95 $DB->update_record('course_modules', $cm);
607c1bc1 96 break;
97
b2054038 98 case 'move':
6bb08163 99 if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
b2054038 100 error_log('AJAX commands.php: Bad section ID '.$sectionid);
101 die;
102 }
3440ec12 103
b2054038 104 if ($beforeid > 0){
cd9224ab 105 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
6bb08163 106 $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
b2054038 107 } else {
108 $beforemod = NULL;
109 }
110
1bbcb7c0 111 if (debugging('',DEBUG_DEVELOPER)) {
112 error_log(serialize($beforemod));
113 }
114
cd9224ab 115 moveto_module($cm, $section, $beforemod);
b2054038 116 break;
117 }
edc06a53 118 rebuild_course_cache($course->id);
b2054038 119 break;
3440ec12 120
121 case 'course':
b2054038 122 switch($field) {
123 case 'marker':
124 $newcourse = new object;
125 $newcourse->id = $course->id;
126 $newcourse->marker = $value;
bb4b6010 127 $DB->update_record('course', $newcourse);
b2054038 128 break;
129 }
130 break;
131 }
132 break;
133
134 case 'DELETE':
135 switch ($class) {
136 case 'block':
137 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
3440ec12 138 break;
139
b2054038 140 case 'resource':
cd9224ab 141 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
37fb48e0 142 error_log('AJAX rest.php: Bad course module ID '.$id);
143 die;
144 }
cd9224ab 145 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
37fb48e0 146
147 if (file_exists($modlib)) {
148 include_once($modlib);
149 } else {
cd9224ab 150 error_log("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
b2054038 151 die;
152 }
cd9224ab 153 $deleteinstancefunction = $cm->modname."_delete_instance";
37fb48e0 154
155 // Run the module's cleanup funtion.
156 if (!$deleteinstancefunction($cm->instance)) {
cd9224ab 157 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
37fb48e0 158 die;
159 }
cd9224ab 160
161 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
162
163 // remove all module files in case modules forget to do that
164 $fs = get_file_storage();
165 $fs->delete_area_files($modcontext->id);
166
37fb48e0 167 if (!delete_course_module($cm->id)) {
cd9224ab 168 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
169 }
170 // Remove the course_modules entry.
171 if (!delete_mod_from_section($cm->id, $cm->section)) {
172 error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
37fb48e0 173 }
174
c4ce5def 175 rebuild_course_cache($course->id);
176
37fb48e0 177 add_to_log($courseid, "course", "delete mod",
178 "view.php?id=$courseid",
cd9224ab 179 "$cm->modname $cm->instance", $cm->id);
b2054038 180 break;
181 }
182 break;
183}
184
3440ec12 185?>