weekly release 2.4.4+
[moodle.git] / admin / oacleanup.php
bloba27aff9341f28b5bb540b8e6d15fbcf751f70b74
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 = context_course::instance($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 $courseformatoptions = course_get_format($course)->get_format_options();
63 if (!isset($courseformatoptions['numsections'])) {
64 // Course format does not use numsections
65 if ($output) {
66 echo 'No extra sections<br />';
68 continue;
70 $sql = "SELECT *
71 FROM {course_sections}
72 WHERE course=? AND section>?
73 ORDER BY section ASC";
74 $params = array($course->id, $courseformatoptions['numsections']);
75 if (!($xsections = $DB->get_records_sql($sql, $params))) {
76 if ($output) echo 'No extra sections<br />';
77 continue;
80 /// cycle through each of the xtra sections
81 foreach ($xsections as $xsection) {
83 if ($output) echo 'Checking Section: '.$xsection->section.'<br />';
85 /// grab any module instances from the sequence field
86 if (!empty($xsection->sequence)) {
87 $instances = explode(',', $xsection->sequence);
89 /// cycle through the instances
90 foreach ($instances as $instance) {
91 /// is this an instance of an online assignment
92 $sql = "SELECT a.id
93 FROM {course_modules} cm, {assignment} a
94 WHERE cm.id = ? AND cm.module = ? AND
95 cm.instance = a.id AND a.assignmenttype = 'online'";
96 $params = array($instance, $aid);
98 /// if record exists then we need to move instance to it's correct section
99 if ($DB->record_exists_sql($sql, $params)) {
101 /// check the new section id
102 /// the journal update erroneously stored it in course_sections->section
103 $newsection = $xsection->section;
104 /// double check the new section
105 if ($newsection > $courseformatoptions['numsections']) {
106 /// get the record for section 0 for this course
107 if (!($zerosection = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>'0')))) {
108 continue;
110 $newsection = $zerosection->id;
113 /// grab the section record
114 if (!($section = $DB->get_record('course_sections', array('id'=>$newsection)))) {
115 if ($output) {
116 echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '. $fullname .'<br />';
118 continue;
121 /// explode the sequence
122 if (($sequence = explode(',', $section->sequence)) === false) {
123 $sequence = array();
126 /// add instance to correct section
127 array_push($sequence, $instance);
129 /// implode the sequence
130 $section->sequence = implode(',', $sequence);
132 $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
134 /// now we need to remove the instance from the old sequence
136 /// grab the old section record
137 if (!($section = $DB->get_record('course_sections', array('id'=>$xsection->id)))) {
138 if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id.' for course: '.$fullname.'<br />';
139 continue;
142 /// explode the sequence
143 if (($sequence = explode(',', $section->sequence)) === false) {
144 $sequence = array();
147 /// remove the old value from the array
148 $key = array_search($instance, $sequence);
149 unset($sequence[$key]);
151 /// implode the sequence
152 $section->sequence = implode(',', $sequence);
154 $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
157 if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
163 /// if the summary and sequence are empty then remove this section
164 if (empty($xsection->summary) and empty($xsection->sequence)) {
165 $DB->delete_records('course_sections', array('id'=>$xsection->id));
166 if ($output) echo 'Deleting empty section '.$xsection->section.'<br />';
171 echo '</center>';