MDL-26880 - Fixed missing of activity id
[moodle.git] / admin / oacleanup.php
blob12a1e4413d6beba47edc91a25f665c9a7e092acc
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 $courses = $DB->get_records('course');
47 /// cycle through each course
48 foreach ($courses as $course) {
50 $fullname = empty($course->fullname) ? 'Course: '.$course->id : $course->fullname;
51 if ($output) echo $OUTPUT->heading($fullname);
53 /// retrieve a list of sections beyond what is currently being shown
54 $sql = "SELECT *
55 FROM {course_sections}
56 WHERE course=? AND section>?
57 ORDER BY section ASC";
58 $params = array($course->id, $course->numsections);
59 if (!($xsections = $DB->get_records_sql($sql, $params))) {
60 if ($output) echo 'No extra sections<br />';
61 continue;
64 /// cycle through each of the xtra sections
65 foreach ($xsections as $xsection) {
67 if ($output) echo 'Checking Section: '.$xsection->section.'<br />';
69 /// grab any module instances from the sequence field
70 if (!empty($xsection->sequence)) {
71 $instances = explode(',', $xsection->sequence);
73 /// cycle through the instances
74 foreach ($instances as $instance) {
75 /// is this an instance of an online assignment
76 $sql = "SELECT a.id
77 FROM {course_modules} cm, {assignment} a
78 WHERE cm.id = ? AND cm.module = ? AND
79 cm.instance = a.id AND a.assignmenttype = 'online'";
80 $params = array($instance, $aid);
82 /// if record exists then we need to move instance to it's correct section
83 if ($DB->record_exists_sql($sql, $params)) {
85 /// check the new section id
86 /// the journal update erroneously stored it in course_sections->section
87 $newsection = $xsection->section;
88 /// double check the new section
89 if ($newsection > $course->numsections) {
90 /// get the record for section 0 for this course
91 if (!($zerosection = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>'0')))) {
92 continue;
94 $newsection = $zerosection->id;
97 /// grab the section record
98 if (!($section = $DB->get_record('course_sections', array('id'=>$newsection)))) {
99 if ($output) echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '. format_string($course->fullname) .'<br />';
100 continue;
103 /// explode the sequence
104 if (($sequence = explode(',', $section->sequence)) === false) {
105 $sequence = array();
108 /// add instance to correct section
109 array_push($sequence, $instance);
111 /// implode the sequence
112 $section->sequence = implode(',', $sequence);
114 $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
116 /// now we need to remove the instance from the old sequence
118 /// grab the old section record
119 if (!($section = $DB->get_record('course_sections', array('id'=>$xsection->id)))) {
120 if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id.' for course: '.$course->fullname.'<br />';
121 continue;
124 /// explode the sequence
125 if (($sequence = explode(',', $section->sequence)) === false) {
126 $sequence = array();
129 /// remove the old value from the array
130 $key = array_search($instance, $sequence);
131 unset($sequence[$key]);
133 /// implode the sequence
134 $section->sequence = implode(',', $sequence);
136 $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
139 if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
145 /// if the summary and sequence are empty then remove this section
146 if (empty($xsection->summary) and empty($xsection->sequence)) {
147 $DB->delete_records('course_sections', array('id'=>$xsection->id));
148 if ($output) echo 'Deleting empty section '.$xsection->section.'<br />';
153 echo '</center>';