5 require('../config.php');
10 error('You must be an admin to use this script');
14 print_header_simple('Online Assignment Cleanup','Online Assignment Cleanup', 'Admin');
16 online_assignment_cleanup(true);
24 function online_assignment_cleanup($output=false) {
28 print_heading('Online Assignment Cleanup');
33 /// We don't want to run this code if we are doing an upgrade from an assignment
34 /// version earlier than 2005041400
35 /// because the assignment type field will not exist
36 $amv = get_field('modules', 'version', 'name', 'assignment');
37 if ((int)$amv < 2005041400) {
45 /// get the module id for assignments from db
46 $arecord = get_record('modules', 'name', 'assignment');
50 /// get a list of all courses on this site
51 $courses = get_courses();
53 /// cycle through each course
54 foreach ($courses as $course) {
56 $fullname = empty($course->fullname
) ?
'Course: '.$course->id
: $course->fullname
;
57 if ($output) print_heading($fullname);
59 /// retrieve a list of sections beyond what is currently being shown
60 $sql = 'SELECT * FROM '.$CFG->prefix
.'course_sections WHERE course='.$course->id
.' AND section>'.$course->numsections
.' ORDER BY section ASC';
61 if (!($xsections = get_records_sql($sql))) {
62 if ($output) echo 'No extra sections<br />';
66 /// cycle through each of the xtra sections
67 foreach ($xsections as $xsection) {
69 if ($output) echo 'Checking Section: '.$xsection->section
.'<br />';
71 /// grab any module instances from the sequence field
72 if (!empty($xsection->sequence
)) {
73 $instances = explode(',', $xsection->sequence
);
75 /// cycle through the instances
76 foreach ($instances as $instance) {
77 /// is this an instance of an online assignment
79 FROM {$CFG->prefix}course_modules cm,
80 {$CFG->prefix}assignment a
81 WHERE cm.id = '$instance' AND
82 cm.module = '$aid' AND
83 cm.instance = a.id AND
84 a.assignmenttype = 'online'";
87 /// if record exists then we need to move instance to it's correct section
88 if (record_exists_sql($sql)) {
90 /// check the new section id
91 /// the journal update erroneously stored it in course_sections->section
92 $newsection = $xsection->section
;
93 /// double check the new section
94 if ($newsection > $course->numsections
) {
95 /// get the record for section 0 for this course
96 if (!($zerosection = get_record('course_sections', 'course', $course->id
, 'section', '0'))) {
99 $newsection = $zerosection->id
;
102 /// grab the section record
103 if (!($section = get_record('course_sections', 'id', $newsection))) {
104 if ($output) echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '.$course->fullname
.'<br />';
108 /// explode the sequence
109 if (($sequence = explode(',', $section->sequence
)) === false) {
113 /// add instance to correct section
114 array_push($sequence, $instance);
116 /// implode the sequence
117 $section->sequence
= implode(',', $sequence);
119 set_field('course_sections', 'sequence', $section->sequence
, 'id', $section->id
);
121 /// now we need to remove the instance from the old sequence
123 /// grab the old section record
124 if (!($section = get_record('course_sections', 'id', $xsection->id
))) {
125 if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id
.' for course: '.$course->fullname
.'<br />';
129 /// explode the sequence
130 if (($sequence = explode(',', $section->sequence
)) === false) {
134 /// remove the old value from the array
135 $key = array_search($instance, $sequence);
136 unset($sequence[$key]);
138 /// implode the sequence
139 $section->sequence
= implode(',', $sequence);
141 set_field('course_sections', 'sequence', $section->sequence
, 'id', $section->id
);
144 if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id
.': to section '.$newsection.'<br />';
150 /// if the summary and sequence are empty then remove this section
151 if (empty($xsection->summary
) and empty($xsection->sequence
)) {
152 delete_records('course_sections', 'id', $xsection->id
);
153 if ($output) echo 'Deleting empty section '.$xsection->section
.'<br />';