weekly release 2.2.4+
[moodle.git] / admin / oacleanup.php
blob891a20af9473062dd9f766a69a96eb6f2fd9107d
1 <?php
3 if (!isset($CFG)) {
5 require('../config.php');
6 require_once($CFG->libdir.'/adminlib.php');
8 admin_externalpage_setup('oacleanup');
10 echo $OUTPUT->header();
11 online_assignment_cleanup(true);
12 echo $OUTPUT->footer();
18 function online_assignment_cleanup($output=false) {
19 global $CFG, $DB, $OUTPUT;
21 if ($output) {
22 echo $OUTPUT->heading('Online Assignment Cleanup');
23 echo '<center>';
27 /// We don't want to run this code if we are doing an upgrade from an assignment
28 /// version earlier than 2005041400
29 /// because the assignment type field will not exist
30 $amv = $DB->get_field('modules', 'version', array('name'=>'assignment'));
31 if ((int)$amv < 2005041400) {
32 if ($output) {
33 echo '</center>';
35 return;
39 /// get the module id for assignments from db
40 $arecord = $DB->get_record('modules', array('name', 'assignment'));
41 $aid = $arecord->id;
44 /// get a list of all courses on this site
45 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
46 $sql = "SELECT c.* $ctxselect FROM {course} c $ctxjoin";
47 $courses = $DB->get_records_sql($sql);
49 /// cycle through each course
50 foreach ($courses as $course) {
51 context_instance_preload($course);
52 $context = get_context_instance(CONTEXT_COURSE, $course->id);
54 if (empty($course->fullname)) {
55 $fullname = get_string('course').': '.$course->id;
56 } else {
57 $fullname = format_string($course->fullname, true, array('context' => $context));
59 if ($output) echo $OUTPUT->heading($fullname);
61 /// retrieve a list of sections beyond what is currently being shown
62 $sql = "SELECT *
63 FROM {course_sections}
64 WHERE course=? AND section>?
65 ORDER BY section ASC";
66 $params = array($course->id, $course->numsections);
67 if (!($xsections = $DB->get_records_sql($sql, $params))) {
68 if ($output) echo 'No extra sections<br />';
69 continue;
72 /// cycle through each of the xtra sections
73 foreach ($xsections as $xsection) {
75 if ($output) echo 'Checking Section: '.$xsection->section.'<br />';
77 /// grab any module instances from the sequence field
78 if (!empty($xsection->sequence)) {
79 $instances = explode(',', $xsection->sequence);
81 /// cycle through the instances
82 foreach ($instances as $instance) {
83 /// is this an instance of an online assignment
84 $sql = "SELECT a.id
85 FROM {course_modules} cm, {assignment} a
86 WHERE cm.id = ? AND cm.module = ? AND
87 cm.instance = a.id AND a.assignmenttype = 'online'";
88 $params = array($instance, $aid);
90 /// if record exists then we need to move instance to it's correct section
91 if ($DB->record_exists_sql($sql, $params)) {
93 /// check the new section id
94 /// the journal update erroneously stored it in course_sections->section
95 $newsection = $xsection->section;
96 /// double check the new section
97 if ($newsection > $course->numsections) {
98 /// get the record for section 0 for this course
99 if (!($zerosection = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>'0')))) {
100 continue;
102 $newsection = $zerosection->id;
105 /// grab the section record
106 if (!($section = $DB->get_record('course_sections', array('id'=>$newsection)))) {
107 if ($output) {
108 echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '. $fullname .'<br />';
110 continue;
113 /// explode the sequence
114 if (($sequence = explode(',', $section->sequence)) === false) {
115 $sequence = array();
118 /// add instance to correct section
119 array_push($sequence, $instance);
121 /// implode the sequence
122 $section->sequence = implode(',', $sequence);
124 $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
126 /// now we need to remove the instance from the old sequence
128 /// grab the old section record
129 if (!($section = $DB->get_record('course_sections', array('id'=>$xsection->id)))) {
130 if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id.' for course: '.$fullname.'<br />';
131 continue;
134 /// explode the sequence
135 if (($sequence = explode(',', $section->sequence)) === false) {
136 $sequence = array();
139 /// remove the old value from the array
140 $key = array_search($instance, $sequence);
141 unset($sequence[$key]);
143 /// implode the sequence
144 $section->sequence = implode(',', $sequence);
146 $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
149 if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
155 /// if the summary and sequence are empty then remove this section
156 if (empty($xsection->summary) and empty($xsection->sequence)) {
157 $DB->delete_records('course_sections', array('id'=>$xsection->id));
158 if ($output) echo 'Deleting empty section '.$xsection->section.'<br />';
163 echo '</center>';