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
25 // Authorise the user and verify some incoming data
26 if (!$course = get_record('course', 'id', $courseid)) {
27 error_log('AJAX commands.php: Course does not exist');
31 $PAGE = page_create_object(PAGE_COURSE_VIEW
, $course->id
);
32 $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH
);
34 if (!empty($instanceid)) {
35 $blockinstance = blocks_find_instance($instanceid, $pageblocks);
36 if (!$blockinstance ||
$blockinstance->pageid
!= $course->id
37 ||
$blockinstance->pagetype
!= 'course-view') {
38 error_log('AJAX commands.php: Bad block ID '.$instanceid);
43 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
44 require_login($course->id
);
45 require_capability('moodle/course:update', $context);
47 if (!empty($CFG->disablecourseajax
)) {
48 error_log('Course AJAX not allowed');
54 // OK, now let's process the parameters and do stuff
56 $req_method = ($pageaction == 'DELETE') ?
'DELETE' : $_SERVER['REQUEST_METHOD'];
67 blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
70 case 'position': // Misleading case. Should probably call it 'move'.
71 // We want to move the block around. This means changing
72 // the column (position field) and/or block sort order
74 $undifinedinsertedid = FALSE;
75 $isaddednewblock = FALSE;
77 if (!empty($positiontoinsertid) && $positiontoinsertid != 'undefined' && strpos($positiontoinsertid, 'column') === FALSE ) {
78 $isaddednewblock = (substr($positiontoinsertid,0,1) != 'i') ?
TRUE : FALSE;
79 if ($positiontoinsertid == 'linst0' ||
$positiontoinsertid == 'rinst0') {
80 $isaddednewblock = FALSE;
83 $positiontoinsertid = clean_param($positiontoinsertid, PARAM_SEQUENCE
);
84 } else if($positiontoinsertid == 'undefined') {
85 $undifinedinsertedid = TRUE;
86 $positiontoinsertid = 0;
88 $positiontoinsertid = 0;
91 if ($positiontoinsertid > 0) {
93 $instsql = 'SELECT * FROM '. $CFG->prefix
.'block_instance WHERE '
94 .' id = \''. $instanceid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
95 $instweight = get_record_sql($instsql);
97 $sql = 'SELECT * FROM '. $CFG->prefix
.'block_instance WHERE '
98 .' id = \''. $positiontoinsertid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
99 $targetweight = get_record_sql($sql);
101 //$instweight = get_record("block_instance", 'id', $positiontoinsertid, "position",$column, 'pageId', $courseid);
102 if (!empty($targetweight->weight
) && !empty($instweight->weight
)) {
103 if ($positiontoinsert == "before") {
104 if ($targetweight->weight
< $instweight->weight ||
($instanceid == $positiontoinsertid)) {
105 $destweight = ($targetweight->weight
== 0 ||
empty($targetweight->weight
)) ?
0 : $targetweight->weight
;
107 $destweight = ($targetweight->weight
== 0 ||
empty($targetweight->weight
)) ?
0 : $targetweight->weight
-1 ;
109 } else if ($positiontoinsert == "after") {
110 $destweight = $targetweight->weight +
1;
113 $destweight = ($targetweight->weight
== 0 ||
empty($targetweight->weight
)) ?
0 : $targetweight->weight
- 1 ;
116 $sql = 'SELECT max(weight) as weight FROM '. $CFG->prefix
.'block_instance WHERE '
117 .'position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
118 $instweight = get_record_sql($sql);
120 $countrecords = count_records('block_instance', 'position', $column, 'pageId', $courseid);
121 $recordexists = record_exists('block_instance', 'position', $column, 'pageId', $courseid, 'id', $instanceid);
123 if ($isaddednewblock ||
$undifinedinsertedid) {
124 if (!empty($countrecords)) {
125 $destweight = ($recordexists) ?
$instweight->weight
: $instweight->weight +
1 ;
130 if (!empty($countrecords)) {
131 $destweight = ($recordexists) ?
$instweight->weight
: $instweight->weight +
1 ;
138 blocks_move_block($PAGE, $blockinstance, $column, $destweight);
140 $current_blocks = blocks_get_by_page_pinned($PAGE);
142 foreach ($current_blocks as $pos =>$blocks) {
143 if (count($current_blocks[$pos]) == 0) {
144 print_side_block('', '', NULL, NULL, '', array('id'=> $pos.'inst0', 'class'=>'tempblockhandle'), '');
153 if (!record_exists('course_sections','course',$course->id
,'section',$id)) {
154 error_log('AJAX commands.php: Bad Section ID '.$id);
160 set_section_visible($course->id
, $id, $value);
164 move_section_to($course, $id, $value);
167 rebuild_course_cache($course->id
);
171 if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id
)) {
172 error_log('AJAX commands.php: Bad course module ID '.$id);
177 set_coursemodule_visible($mod->id
, $value);
181 set_coursemodule_groupmode($mod->id
, $value);
185 if ($mod->indent
> 0) {
187 update_record('course_modules', $mod);
193 update_record('course_modules', $mod);
197 if (!$section = get_record('course_sections','course',$course->id
,'section',$sectionid)) {
198 error_log('AJAX commands.php: Bad section ID '.$sectionid);
203 $beforemod = get_record('course_modules', 'id', $beforeid);
208 if (debugging('',DEBUG_DEVELOPER
)) {
209 error_log(serialize($beforemod));
212 moveto_module($mod, $section, $beforemod);
215 rebuild_course_cache($course->id
);
221 $newcourse = new object;
222 $newcourse->id
= $course->id
;
223 $newcourse->marker
= $value;
224 if (!update_record('course',$newcourse)) {
225 error_log('AJAX commands.php: Failed to update course marker for course '.$newcourse->id
);
237 blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
241 if (!$cm = get_record('course_modules', 'id', $id, 'course', $course->id
)) {
242 error_log('AJAX rest.php: Bad course module ID '.$id);
245 if (!$mod = get_record('modules', 'id', $cm->module
)) {
246 error_log('AJAX rest.php: Bad module ID '.$cm->module
);
249 $mod->name
= clean_param($mod->name
, PARAM_SAFEDIR
); // For safety
250 $modlib = "$CFG->dirroot/mod/$mod->name/lib.php";
252 if (file_exists($modlib)) {
253 include_once($modlib);
255 error_log("Ajax rest.php: This module is missing important code ($modlib)");
258 $deleteinstancefunction = $mod->name
."_delete_instance";
260 // Run the module's cleanup funtion.
261 if (!$deleteinstancefunction($cm->instance
)) {
262 error_log("Ajax rest.php: Could not delete the $mod->name (instance)");
265 // Remove the course_modules entry.
266 if (!delete_course_module($cm->id
)) {
267 error_log("Ajax rest.php: Could not delete the $mod->modulename (coursemodule)");
271 rebuild_course_cache($course->id
);
273 add_to_log($courseid, "course", "delete mod",
274 "view.php?id=$courseid",
275 "$mod->name $cm->instance", $cm->id
);