2009-11-01 10:57:00 +00:00
|
|
|
<?php
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
if (!isset($CFG)) {
|
|
|
|
|
|
|
|
require('../config.php');
|
2006-09-25 20:22:55 +00:00
|
|
|
require_once($CFG->libdir.'/adminlib.php');
|
2005-07-21 10:28:30 +00:00
|
|
|
|
2007-04-30 17:08:34 +00:00
|
|
|
admin_externalpage_setup('oacleanup');
|
2005-07-21 10:28:30 +00:00
|
|
|
|
2010-03-31 08:05:53 +00:00
|
|
|
echo $OUTPUT->header();
|
2005-07-21 10:28:30 +00:00
|
|
|
online_assignment_cleanup(true);
|
2009-08-06 14:12:46 +00:00
|
|
|
echo $OUTPUT->footer();
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function online_assignment_cleanup($output=false) {
|
2009-08-06 08:17:12 +00:00
|
|
|
global $CFG, $DB, $OUTPUT;
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
if ($output) {
|
2009-08-06 08:17:12 +00:00
|
|
|
echo $OUTPUT->heading('Online Assignment Cleanup');
|
2005-07-21 10:28:30 +00:00
|
|
|
echo '<center>';
|
|
|
|
}
|
|
|
|
|
2005-07-23 10:53:25 +00:00
|
|
|
|
|
|
|
/// We don't want to run this code if we are doing an upgrade from an assignment
|
|
|
|
/// version earlier than 2005041400
|
|
|
|
/// because the assignment type field will not exist
|
2008-05-30 21:36:57 +00:00
|
|
|
$amv = $DB->get_field('modules', 'version', array('name'=>'assignment'));
|
2005-07-23 10:53:25 +00:00
|
|
|
if ((int)$amv < 2005041400) {
|
|
|
|
if ($output) {
|
|
|
|
echo '</center>';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// get the module id for assignments from db
|
2008-05-30 21:36:57 +00:00
|
|
|
$arecord = $DB->get_record('modules', array('name', 'assignment'));
|
2005-07-21 10:28:30 +00:00
|
|
|
$aid = $arecord->id;
|
|
|
|
|
|
|
|
|
|
|
|
/// get a list of all courses on this site
|
2011-09-06 13:40:44 +12:00
|
|
|
list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
|
|
|
|
$sql = "SELECT c.* $ctxselect FROM {course} c $ctxjoin";
|
|
|
|
$courses = $DB->get_records_sql($sql);
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
/// cycle through each course
|
|
|
|
foreach ($courses as $course) {
|
2011-09-06 13:40:44 +12:00
|
|
|
context_instance_preload($course);
|
2012-07-23 14:33:02 +08:00
|
|
|
$context = context_course::instance($course->id);
|
2005-07-21 10:28:30 +00:00
|
|
|
|
2011-09-06 13:40:44 +12:00
|
|
|
if (empty($course->fullname)) {
|
|
|
|
$fullname = get_string('course').': '.$course->id;
|
|
|
|
} else {
|
|
|
|
$fullname = format_string($course->fullname, true, array('context' => $context));
|
|
|
|
}
|
2009-08-06 08:17:12 +00:00
|
|
|
if ($output) echo $OUTPUT->heading($fullname);
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
/// retrieve a list of sections beyond what is currently being shown
|
2008-05-30 21:36:57 +00:00
|
|
|
$sql = "SELECT *
|
|
|
|
FROM {course_sections}
|
|
|
|
WHERE course=? AND section>?
|
|
|
|
ORDER BY section ASC";
|
|
|
|
$params = array($course->id, $course->numsections);
|
|
|
|
if (!($xsections = $DB->get_records_sql($sql, $params))) {
|
2005-07-21 10:28:30 +00:00
|
|
|
if ($output) echo 'No extra sections<br />';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// cycle through each of the xtra sections
|
|
|
|
foreach ($xsections as $xsection) {
|
|
|
|
|
|
|
|
if ($output) echo 'Checking Section: '.$xsection->section.'<br />';
|
|
|
|
|
|
|
|
/// grab any module instances from the sequence field
|
|
|
|
if (!empty($xsection->sequence)) {
|
|
|
|
$instances = explode(',', $xsection->sequence);
|
|
|
|
|
|
|
|
/// cycle through the instances
|
|
|
|
foreach ($instances as $instance) {
|
|
|
|
/// is this an instance of an online assignment
|
2006-09-25 20:22:55 +00:00
|
|
|
$sql = "SELECT a.id
|
2008-05-30 21:36:57 +00:00
|
|
|
FROM {course_modules} cm, {assignment} a
|
|
|
|
WHERE cm.id = ? AND cm.module = ? AND
|
|
|
|
cm.instance = a.id AND a.assignmenttype = 'online'";
|
2009-11-01 10:57:00 +00:00
|
|
|
$params = array($instance, $aid);
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
/// if record exists then we need to move instance to it's correct section
|
2008-05-30 21:36:57 +00:00
|
|
|
if ($DB->record_exists_sql($sql, $params)) {
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
/// check the new section id
|
|
|
|
/// the journal update erroneously stored it in course_sections->section
|
|
|
|
$newsection = $xsection->section;
|
|
|
|
/// double check the new section
|
|
|
|
if ($newsection > $course->numsections) {
|
|
|
|
/// get the record for section 0 for this course
|
2008-05-30 21:36:57 +00:00
|
|
|
if (!($zerosection = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>'0')))) {
|
2005-07-21 10:28:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$newsection = $zerosection->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// grab the section record
|
2008-05-30 21:36:57 +00:00
|
|
|
if (!($section = $DB->get_record('course_sections', array('id'=>$newsection)))) {
|
2011-09-06 13:40:44 +12:00
|
|
|
if ($output) {
|
|
|
|
echo 'Serious error: Cannot retrieve section: '.$newsection.' for course: '. $fullname .'<br />';
|
|
|
|
}
|
2005-07-21 10:28:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// explode the sequence
|
|
|
|
if (($sequence = explode(',', $section->sequence)) === false) {
|
|
|
|
$sequence = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// add instance to correct section
|
|
|
|
array_push($sequence, $instance);
|
|
|
|
|
|
|
|
/// implode the sequence
|
|
|
|
$section->sequence = implode(',', $sequence);
|
|
|
|
|
2008-05-30 21:36:57 +00:00
|
|
|
$DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
|
2005-07-21 10:28:30 +00:00
|
|
|
|
|
|
|
/// now we need to remove the instance from the old sequence
|
|
|
|
|
|
|
|
/// grab the old section record
|
2008-05-30 21:36:57 +00:00
|
|
|
if (!($section = $DB->get_record('course_sections', array('id'=>$xsection->id)))) {
|
2011-09-06 13:40:44 +12:00
|
|
|
if ($output) echo 'Serious error: Cannot retrieve old section: '.$xsection->id.' for course: '.$fullname.'<br />';
|
2005-07-21 10:28:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// explode the sequence
|
|
|
|
if (($sequence = explode(',', $section->sequence)) === false) {
|
|
|
|
$sequence = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// remove the old value from the array
|
|
|
|
$key = array_search($instance, $sequence);
|
|
|
|
unset($sequence[$key]);
|
|
|
|
|
|
|
|
/// implode the sequence
|
|
|
|
$section->sequence = implode(',', $sequence);
|
|
|
|
|
2008-05-30 21:36:57 +00:00
|
|
|
$DB->set_field('course_sections', 'sequence', $section->sequence, array('id'=>$section->id));
|
2005-07-21 10:28:30 +00:00
|
|
|
|
2006-09-25 20:22:55 +00:00
|
|
|
|
2005-07-21 10:28:30 +00:00
|
|
|
if ($output) echo 'Online Assignment (instance '.$instance.') moved from section '.$section->id.': to section '.$newsection.'<br />';
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// if the summary and sequence are empty then remove this section
|
|
|
|
if (empty($xsection->summary) and empty($xsection->sequence)) {
|
2008-05-30 21:36:57 +00:00
|
|
|
$DB->delete_records('course_sections', array('id'=>$xsection->id));
|
2005-07-21 10:28:30 +00:00
|
|
|
if ($output) echo 'Deleting empty section '.$xsection->section.'<br />';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '</center>';
|
|
|
|
}
|
|
|
|
|
2009-11-01 10:57:00 +00:00
|
|
|
|