Noted that xml format can now handle images.
[moodle-linuxchix.git] / admin / oacleanup.php
blob6ba9634188c79f4940fc4aab4b3b9fb1a1daaf61
1 <?php // $Id$
3 if (!isset($CFG)) {
5 require('../config.php');
7 require_login();
9 if (!isadmin()) {
10 error('You must be an admin to use this script');
11 exit;
14 print_header_simple('Online Assignment Cleanup','Online Assignment Cleanup', 'Admin');
16 online_assignment_cleanup(true);
18 print_footer();
24 function online_assignment_cleanup($output=false) {
25 global $CFG;
27 if ($output) {
28 print_heading('Online Assignment Cleanup');
29 echo '<center>';
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) {
38 if ($output) {
39 echo '</center>';
41 return;
45 /// get the module id for assignments from db
46 $arecord = get_record('modules', 'name', 'assignment');
47 $aid = $arecord->id;
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 />';
63 continue;
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
78 $sql = "SELECT a.id
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'))) {
97 continue;
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 />';
105 continue;
108 /// explode the sequence
109 if (($sequence = explode(',', $section->sequence)) === false) {
110 $sequence = array();
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 />';
126 continue;
129 /// explode the sequence
130 if (($sequence = explode(',', $section->sequence)) === false) {
131 $sequence = array();
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 />';
158 echo '</center>';