Browse Source

Fix #517 - restore creates orphan calendar events.

clean up any existing orphaned records via upgrade script,
MOODLE_38_STABLE
Dan Marsden 4 years ago
parent
commit
9445296b05
  1. 24
      backup/moodle2/restore_attendance_activity_task.class.php
  2. 2
      backup/moodle2/restore_attendance_stepslib.php
  3. 11
      db/upgrade.php
  4. 6
      resetcalendar.php
  5. 2
      version.php

24
backup/moodle2/restore_attendance_activity_task.class.php

@ -110,4 +110,28 @@ class restore_attendance_activity_task extends restore_activity_task {
return $rules;
}
/**
* After restore - clean up any incorrect calendar items that have been restored.
* @throws dml_exception
*/
public function after_restore() {
global $DB;
$attendanceid = $this->get_activityid();
$courseid = $this->get_courseid();
if (empty($courseid) || empty($attendanceid)) {
return;
}
if (empty(get_config('attendance', 'enablecalendar'))) {
// Attendance isn't using Calendar - delete anything that was created.
$DB->delete_records('event', ['modulename' => 'attendance', 'instance' => $attendanceid, 'courseid' => $courseid]);
} else {
// Clean up any orphaned events.
$sql = "modulename = 'attendance' AND courseid = :courseid AND id NOT IN (SELECT caleventid
FROM {attendance_sessions}
WHERE attendanceid = :attendanceid)";
$params = ['courseid' => $courseid, 'attendanceid' => $attendanceid];
$DB->delete_records_select('event', $sql, $params);
}
}
}

2
backup/moodle2/restore_attendance_stepslib.php

@ -178,7 +178,7 @@ class restore_attendance_activity_structure_step extends restore_activity_struct
}
/**
* Once the database tables have been fully restored, restore the files
* Once the database tables have been fully restored, restore the files and clean up any calendar stuff.
* @return void
*/
protected function after_execute() {

11
db/upgrade.php

@ -640,5 +640,16 @@ function xmldb_attendance_upgrade($oldversion=0) {
upgrade_mod_savepoint(true, 2020072900, 'attendance');
}
if ($oldversion < 2021050700) {
// Restore process sometimes creates orphan attendance calendar events - clean them up.
$sql = "modulename = 'attendance' AND id NOT IN (SELECT caleventid
FROM {attendance_sessions})";
$DB->delete_records_select('event', $sql);
// Attendance savepoint reached.
upgrade_mod_savepoint(true, 2021050700, 'attendance');
}
return $result;
}

6
resetcalendar.php

@ -70,10 +70,8 @@ if (get_config('attendance', 'enablecalendar')) {
}
} else {
if ($action == 'delete' && confirm_sesskey()) {
$caleventids = $DB->get_records_select_menu('attendance_sessions', 'caleventid > 0', array(),
'', 'caleventid, caleventid as id2');
$DB->delete_records_list('event', 'id', $caleventids);
$DB->execute("UPDATE {attendance_sessions} set caleventid = 0");
// Attendance isn't using Calendar - delete anything that was created.
$DB->delete_records('event', ['modulename' => 'attendance', 'eventtype' => 'attendance']);
echo $OUTPUT->notification(get_string('eventsdeleted', 'mod_attendance'), 'notifysuccess');
} else {
// Check to see if there are any events that need to be deleted.

2
version.php

@ -23,7 +23,7 @@
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2021011500;
$plugin->version = 2021050700;
$plugin->requires = 2019072500; // Requires 3.8.
$plugin->release = '3.9.1';
$plugin->maturity = MATURITY_STABLE;

Loading…
Cancel
Save