Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/oacleanup.php
Go to the documentation of this file.
00001 <?php
00002 
00003 if (!isset($CFG)) {
00004 
00005     require('../config.php');
00006     require_once($CFG->libdir.'/adminlib.php');
00007 
00008     admin_externalpage_setup('oacleanup');
00009 
00010     echo $OUTPUT->header();
00011     online_assignment_cleanup(true);
00012     echo $OUTPUT->footer();
00013 
00014 }
00015 
00016 
00017 
00018 function online_assignment_cleanup($output=false) {
00019     global $CFG, $DB, $OUTPUT;
00020 
00021     if ($output) {
00022         echo $OUTPUT->heading('Online Assignment Cleanup');
00023         echo '<center>';
00024     }
00025 
00026 
00030     $amv = $DB->get_field('modules', 'version', array('name'=>'assignment'));
00031     if ((int)$amv < 2005041400) {
00032         if ($output) {
00033             echo '</center>';
00034         }
00035         return;
00036     }
00037 
00038 
00040     $arecord = $DB->get_record('modules', array('name', 'assignment'));
00041     $aid = $arecord->id;
00042 
00043 
00045     list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
00046     $sql = "SELECT c.* $ctxselect FROM {course} c $ctxjoin";
00047     $courses = $DB->get_records_sql($sql);
00048 
00050     foreach ($courses as $course) {
00051         context_instance_preload($course);
00052         $context = get_context_instance(CONTEXT_COURSE, $course->id);
00053 
00054         if (empty($course->fullname)) {
00055             $fullname = get_string('course').': '.$course->id;
00056         } else {
00057             $fullname = format_string($course->fullname, true, array('context' => $context));
00058         }
00059         if ($output) echo $OUTPUT->heading($fullname);
00060 
00062         $sql = "SELECT *
00063                   FROM {course_sections}
00064                  WHERE course=? AND section>?
00065               ORDER BY section ASC";
00066         $params = array($course->id, $course->numsections);
00067         if (!($xsections = $DB->get_records_sql($sql, $params))) {
00068             if ($output) echo 'No extra sections<br />';
00069             continue;
00070         }
00071 
00073         foreach ($xsections as $xsection) {
00074 
00075             if ($output) echo 'Checking Section: '.$xsection->section.'<br />';
00076 
00078             if (!empty($xsection->sequence)) {
00079                 $instances = explode(',', $xsection->sequence);
00080 
00082                 foreach ($instances as $instance) {
00084                     $sql = "SELECT a.id
00085                               FROM  {course_modules} cm, {assignment} a
00086                              WHERE cm.id = ? AND cm.module = ? AND
00087                                    cm.instance = a.id AND a.assignmenttype = 'online'";
00088                     $params = array($instance, $aid);
00089 
00091                     if ($DB->record_exists_sql($sql, $params)) {
00092 
00095                         $newsection = $xsection->section;
00097                         if ($newsection > $course->numsections) {
00099                             if (!($zerosection = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>'0')))) {
00100                                 continue;
00101                             }
00102                             $newsection = $zerosection->id;
00103                         }
00104 
00106                         if (!($section = $DB->get_record('course_sections', array('id'=>$newsection)))) {
00107                             if ($output) {
00108                                 echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '. $fullname .'<br />';
00109                             }
00110                             continue;
00111                         }
00112 
00114                         if  (($sequence = explode(',', $section->sequence)) === false) {
00115                             $sequence = array();
00116                         }
00117 
00119                         array_push($sequence, $instance);
00120 
00122                         $section->sequence = implode(',', $sequence);
00123 
00124                         $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
00125 
00127 
00129                         if (!($section = $DB->get_record('course_sections', array('id'=>$xsection->id)))) {
00130                             if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id.' for course: '.$fullname.'<br />';
00131                             continue;
00132                         }
00133 
00135                         if  (($sequence = explode(',', $section->sequence)) === false) {
00136                             $sequence = array();
00137                         }
00138 
00140                         $key = array_search($instance, $sequence);
00141                         unset($sequence[$key]);
00142 
00144                         $section->sequence = implode(',', $sequence);
00145 
00146                         $DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
00147 
00148 
00149                         if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
00150 
00151                     }
00152                 }
00153             }
00154 
00156             if (empty($xsection->summary) and empty($xsection->sequence)) {
00157                 $DB->delete_records('course_sections', array('id'=>$xsection->id));
00158                 if ($output) echo 'Deleting empty section '.$xsection->section.'<br />';
00159             }
00160         }
00161     }
00162 
00163     echo '</center>';
00164 }
00165 
00166 
 All Data Structures Namespaces Files Functions Variables Enumerations