[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/admin/ -> oacleanup.php (source)

   1  <?php // $Id: oacleanup.php,v 1.9 2007/04/30 17:08:43 skodak Exp $
   2  
   3  if (!isset($CFG)) {
   4  
   5      require ('../config.php');
   6      require_once($CFG->libdir.'/adminlib.php');
   7  
   8      admin_externalpage_setup('oacleanup');
   9  
  10      admin_externalpage_print_header();
  11      online_assignment_cleanup(true);
  12      admin_externalpage_print_footer();
  13  
  14  }
  15  
  16  
  17  
  18  function online_assignment_cleanup($output=false) {
  19      global $CFG;
  20  
  21      if ($output) {
  22          print_heading('Online Assignment Cleanup');
  23          echo '<center>';
  24      }
  25  
  26  
  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>';
  34          }
  35          return;
  36      }
  37  
  38  
  39      /// get the module id for assignments from db
  40      $arecord = get_record('modules', 'name', 'assignment');
  41      $aid = $arecord->id;
  42  
  43  
  44      /// get a list of all courses on this site
  45      $courses = get_records('course');
  46  
  47      /// cycle through each course
  48      foreach ($courses as $course) {
  49  
  50          $fullname = empty($course->fullname) ? 'Course: '.$course->id : $course->fullname;
  51          if ($output) print_heading($fullname);
  52  
  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;
  58          }
  59  
  60          /// cycle through each of the xtra sections
  61          foreach ($xsections as $xsection) {
  62  
  63              if ($output) echo 'Checking Section: '.$xsection->section.'<br />';
  64  
  65              /// grab any module instances from the sequence field
  66              if (!empty($xsection->sequence)) {
  67                  $instances = explode(',', $xsection->sequence);
  68  
  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'";
  79  
  80  
  81                      /// if record exists then we need to move instance to it's correct section
  82                      if (record_exists_sql($sql)) {
  83  
  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;
  92                              }
  93                              $newsection = $zerosection->id;
  94                          }
  95  
  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;
 100                          }
 101  
 102                          /// explode the sequence
 103                          if  (($sequence = explode(',', $section->sequence)) === false) {
 104                              $sequence = array();
 105                          }
 106  
 107                          /// add instance to correct section
 108                          array_push($sequence, $instance);
 109  
 110                          /// implode the sequence
 111                          $section->sequence = implode(',', $sequence);
 112  
 113                          set_field('course_sections', 'sequence', $section->sequence, 'id', $section->id);
 114  
 115                          /// now we need to remove the instance from the old sequence
 116  
 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;
 121                          }
 122  
 123                          /// explode the sequence
 124                          if  (($sequence = explode(',', $section->sequence)) === false) {
 125                              $sequence = array();
 126                          }
 127  
 128                          /// remove the old value from the array
 129                          $key = array_search($instance, $sequence);
 130                          unset($sequence[$key]);
 131  
 132                          /// implode the sequence
 133                          $section->sequence = implode(',', $sequence);
 134  
 135                          set_field('course_sections', 'sequence', $section->sequence, 'id', $section->id);
 136  
 137  
 138                          if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
 139  
 140                      }
 141                  }
 142              }
 143  
 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 />';
 148              }
 149          }
 150      }
 151  
 152      echo '</center>';
 153  }
 154  
 155  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7