diff --git a/backup/moodle2/restore_attendance_activity_task.class.php b/backup/moodle2/restore_attendance_activity_task.class.php index d30493d..ec9e199 100644 --- a/backup/moodle2/restore_attendance_activity_task.class.php +++ b/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); + } + } } diff --git a/backup/moodle2/restore_attendance_stepslib.php b/backup/moodle2/restore_attendance_stepslib.php index ae7cad8..efdfec1 100644 --- a/backup/moodle2/restore_attendance_stepslib.php +++ b/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() { diff --git a/db/upgrade.php b/db/upgrade.php index f86843c..2ea545d 100755 --- a/db/upgrade.php +++ b/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; } diff --git a/resetcalendar.php b/resetcalendar.php index 129b5dd..6b0ac3b 100644 --- a/resetcalendar.php +++ b/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. diff --git a/version.php b/version.php index ed84199..02e6f12 100755 --- a/version.php +++ b/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;