diff --git a/classes/local/calendar.php b/classes/local/calendar.php index e3ef676..982d9f9 100644 --- a/classes/local/calendar.php +++ b/classes/local/calendar.php @@ -181,6 +181,7 @@ public static function fix_program_events(?stdClass $program): void { LEFT JOIN {enrol_programs_allocations} pa ON pa.id = e.instance AND e.component = 'enrol_programs' LEFT JOIN {enrol_programs_programs} p ON p.id = pa.programid WHERE (pa.id IS NULL OR pa.archived = 1 OR p.archived = 1 OR pa.timecompleted IS NOT NULL) + AND e.component = 'enrol_programs' $programselect ORDER BY e.id ASC"; $rs = $DB->get_recordset_sql($sql, $params); diff --git a/tests/local/calendar_test.php b/tests/local/calendar_test.php index c568ac2..031ffe0 100644 --- a/tests/local/calendar_test.php +++ b/tests/local/calendar_test.php @@ -1014,4 +1014,40 @@ public function test_archiving() { ['component' => 'enrol_programs', 'instance' => $allocation1x1->id, 'eventtype' => calendar::EVENTTYPE_START]); $this->assertFalse($event); } -} \ No newline at end of file + + /** + * Tests that manual events are not deleted by fixing program events + * + * @see https://github.com/open-lms-open-source/moodle-enrol_programs/issues/11 Issue + */ + public function test_manual_calendar_events_not_deleted_by_fix_program_events() { + global $DB, $USER; + $this->setAdminUser(); + + $event = new \stdClass(); + $event->userid = $USER->id; + $event->timestart = 0; + $event->name = 'test'; + + // Manually created events have modulename 0 and component as null. + $event->modulename = 0; + $event->component = null; + + \calendar_event::create($event); + + $events = $DB->get_records('event'); + $this->assertCount(1, $events); + $event = current($events); + $this->assertNull($event->component); + $this->assertEquals(0, $event->modulename); + + // Run fix events, as if cron would run it. + calendar::fix_program_events(null); + + // Ensure calendar event that was created is left untouched. + $events = $DB->get_records('event'); + $eventids = array_column($events, 'id'); + $this->assertTrue(in_array($event->id, $eventids)); + } +} +