MDL-12531, make the new member value available to all the affected plugins, thanks...
[moodle-linuxchix.git] / admin / oacleanup.php
blob04d0cd3fd6bbbe067207c31dbf9bc89a708a47f6
1 <?php // $Id$
3 if (!isset($CFG)) {
5 require('../config.php');
6 require_once($CFG->libdir.'/adminlib.php');
8 admin_externalpage_setup('oacleanup');
10 admin_externalpage_print_header();
11 online_assignment_cleanup(true);
12 admin_externalpage_print_footer();
18 function online_assignment_cleanup($output=false) {
19 global $CFG;
21 if ($output) {
22 print_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 = get_field('modules', 'version', '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 = get_record('modules', 'name', 'assignment');
41 $aid = $arecord->id;
44 /// get a list of all courses on this site
45 $courses = 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) print_heading($fullname);
53 /// retrieve a list of sections beyond what is currently being shown
54 $sql = 'SELECT * FROM '.$CFG->prefix.'course_sections WHERE course='.$course->id.' AND section>'.$course->numsections.' ORDER BY section ASC';
55 if (!($xsections = get_records_sql($sql))) {
56 if ($output) echo 'No extra sections<br />';
57 continue;
60 /// cycle through each of the xtra sections
61 foreach ($xsections as $xsection) {
63 if ($output) echo 'Checking Section: '.$xsection->section.'<br />';
65 /// grab any module instances from the sequence field
66 if (!empty($xsection->sequence)) {
67 $instances = explode(',', $xsection->sequence);
69 /// cycle through the instances
70 foreach ($instances as $instance) {
71 /// is this an instance of an online assignment
72 $sql = "SELECT a.id
73 FROM {$CFG->prefix}course_modules cm,
74 {$CFG->prefix}assignment a
75 WHERE cm.id = '$instance' AND
76 cm.module = '$aid' AND
77 cm.instance = a.id AND
78 a.assignmenttype = 'online'";
81 /// if record exists then we need to move instance to it's correct section
82 if (record_exists_sql($sql)) {
84 /// check the new section id
85 /// the journal update erroneously stored it in course_sections->section
86 $newsection = $xsection->section;
87 /// double check the new section
88 if ($newsection > $course->numsections) {
89 /// get the record for section 0 for this course
90 if (!($zerosection = get_record('course_sections', 'course', $course->id, 'section', '0'))) {
91 continue;
93 $newsection = $zerosection->id;
96 /// grab the section record
97 if (!($section = get_record('course_sections', 'id', $newsection))) {
98 if ($output) echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '. format_string($course->fullname) .'<br />';
99 continue;
102 /// explode the sequence
103 if (($sequence = explode(',', $section->sequence)) === false) {
104 $sequence = array();
107 /// add instance to correct section
108 array_push($sequence, $instance);
110 /// implode the sequence
111 $section->sequence = implode(',', $sequence);
113 set_field('course_sections', 'sequence', $section->sequence, 'id', $section->id);
115 /// now we need to remove the instance from the old sequence
117 /// grab the old section record
118 if (!($section = get_record('course_sections', 'id', $xsection->id))) {
119 if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id.' for course: '.$course->fullname.'<br />';
120 continue;
123 /// explode the sequence
124 if (($sequence = explode(',', $section->sequence)) === false) {
125 $sequence = array();
128 /// remove the old value from the array
129 $key = array_search($instance, $sequence);
130 unset($sequence[$key]);
132 /// implode the sequence
133 $section->sequence = implode(',', $sequence);
135 set_field('course_sections', 'sequence', $section->sequence, 'id', $section->id);
138 if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
144 /// if the summary and sequence are empty then remove this section
145 if (empty($xsection->summary) and empty($xsection->sequence)) {
146 delete_records('course_sections', 'id', $xsection->id);
147 if ($output) echo 'Deleting empty section '.$xsection->section.'<br />';
152 echo '</center>';