Fixes bug MDL-9175 "Groups simpletest, fatal errors", also DONOTCOMMIT.
[moodle-pu.git] / admin / oacleanup.php
blob78ac48f39a3bdb5225e5c7178de922a0c4244e54
1 <?php // $Id$
3 if (!isset($CFG)) {
5 require('../config.php');
6 require_once($CFG->libdir.'/adminlib.php');
7 $adminroot = admin_get_root();
8 admin_externalpage_setup('oacleanup', $adminroot);
10 require_login();
12 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID));
14 admin_externalpage_print_header($adminroot);
15 online_assignment_cleanup(true);
16 admin_externalpage_print_footer($adminroot);
22 function online_assignment_cleanup($output=false) {
23 global $CFG;
25 if ($output) {
26 print_heading('Online Assignment Cleanup');
27 echo '<center>';
31 /// We don't want to run this code if we are doing an upgrade from an assignment
32 /// version earlier than 2005041400
33 /// because the assignment type field will not exist
34 $amv = get_field('modules', 'version', 'name', 'assignment');
35 if ((int)$amv < 2005041400) {
36 if ($output) {
37 echo '</center>';
39 return;
43 /// get the module id for assignments from db
44 $arecord = get_record('modules', 'name', 'assignment');
45 $aid = $arecord->id;
48 /// get a list of all courses on this site
49 $courses = get_records('course');
51 /// cycle through each course
52 foreach ($courses as $course) {
54 $fullname = empty($course->fullname) ? 'Course: '.$course->id : $course->fullname;
55 if ($output) print_heading($fullname);
57 /// retrieve a list of sections beyond what is currently being shown
58 $sql = 'SELECT * FROM '.$CFG->prefix.'course_sections WHERE course='.$course->id.' AND section>'.$course->numsections.' ORDER BY section ASC';
59 if (!($xsections = get_records_sql($sql))) {
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 {$CFG->prefix}course_modules cm,
78 {$CFG->prefix}assignment a
79 WHERE cm.id = '$instance' AND
80 cm.module = '$aid' AND
81 cm.instance = a.id AND
82 a.assignmenttype = 'online'";
85 /// if record exists then we need to move instance to it's correct section
86 if (record_exists_sql($sql)) {
88 /// check the new section id
89 /// the journal update erroneously stored it in course_sections->section
90 $newsection = $xsection->section;
91 /// double check the new section
92 if ($newsection > $course->numsections) {
93 /// get the record for section 0 for this course
94 if (!($zerosection = get_record('course_sections', 'course', $course->id, 'section', '0'))) {
95 continue;
97 $newsection = $zerosection->id;
100 /// grab the section record
101 if (!($section = get_record('course_sections', 'id', $newsection))) {
102 if ($output) echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '. format_string($course->fullname) .'<br />';
103 continue;
106 /// explode the sequence
107 if (($sequence = explode(',', $section->sequence)) === false) {
108 $sequence = array();
111 /// add instance to correct section
112 array_push($sequence, $instance);
114 /// implode the sequence
115 $section->sequence = implode(',', $sequence);
117 set_field('course_sections', 'sequence', $section->sequence, 'id', $section->id);
119 /// now we need to remove the instance from the old sequence
121 /// grab the old section record
122 if (!($section = get_record('course_sections', 'id', $xsection->id))) {
123 if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id.' for course: '.$course->fullname.'<br />';
124 continue;
127 /// explode the sequence
128 if (($sequence = explode(',', $section->sequence)) === false) {
129 $sequence = array();
132 /// remove the old value from the array
133 $key = array_search($instance, $sequence);
134 unset($sequence[$key]);
136 /// implode the sequence
137 $section->sequence = implode(',', $sequence);
139 set_field('course_sections', 'sequence', $section->sequence, 'id', $section->id);
142 if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
148 /// if the summary and sequence are empty then remove this section
149 if (empty($xsection->summary) and empty($xsection->sequence)) {
150 delete_records('course_sections', 'id', $xsection->id);
151 if ($output) echo 'Deleting empty section '.$xsection->section.'<br />';
156 echo '</center>';