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');
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);
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');
56 // OK, now let's process the parameters and do stuff
58 $req_method = ($pageaction == 'DELETE') ?
'DELETE' : $_SERVER['REQUEST_METHOD'];
69 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
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
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;
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
;
109 $destweight = ($targetweight->weight
== 0 ||
empty($targetweight->weight
)) ?
0 : $targetweight->weight
-1 ;
111 } else if ($positiontoinsert == "after") {
112 $destweight = $targetweight->weight +
1;
115 $destweight = ($targetweight->weight
== 0 ||
empty($targetweight->weight
)) ?
0 : $targetweight->weight
- 1 ;
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 ;
132 if (!empty($countrecords)) {
133 $destweight = ($recordexists) ?
$instweight->weight
: $instweight->weight +
1 ;
140 blocks_move_block($PAGE, $blockinstance, $column, $destweight);
142 $current_blocks = blocks_get_by_page_pinned($PAGE);
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'), '');
155 if (!record_exists('course_sections','course',$course->id
,'section',$id)) {
156 error_log('AJAX commands.php: Bad Section ID '.$id);
162 set_section_visible($course->id
, $id, $value);
166 move_section_to($course, $id, $value);
169 rebuild_course_cache($course->id
);
173 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id
)) {
174 error_log('AJAX commands.php: Bad course module ID '.$id);
179 set_coursemodule_visible($mod->id
, $value);
183 set_coursemodule_groupmode($mod->id
, $value);
187 if ($mod->indent
> 0) {
189 update_record('course_modules', $mod);
195 update_record('course_modules', $mod);
199 if (!$section = get_record('course_sections','course',$course->id
,'section',$sectionid)) {
200 error_log('AJAX commands.php: Bad section ID '.$sectionid);
205 $beforemod = get_record('course_modules', 'id', $beforeid);
210 if (debugging('',DEBUG_DEVELOPER
)) {
211 error_log(serialize($beforemod));
214 moveto_module($mod, $section, $beforemod);
217 rebuild_course_cache($course->id
);
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
);
239 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance, false, false);
243 if (!$cm = get_record('course_modules', 'id', $id, 'course', $course->id
)) {
244 error_log('AJAX rest.php: Bad course module ID '.$id);
247 if (!$mod = get_record('modules', 'id', $cm->module
)) {
248 error_log('AJAX rest.php: Bad module ID '.$cm->module
);
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);
257 error_log("Ajax rest.php: This module is missing important code ($modlib)");
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)");
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)");
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
);