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/task/cron_test.php b/tests/task/cron_test.php index 28d819a..d6d293e 100644 --- a/tests/task/cron_test.php +++ b/tests/task/cron_test.php @@ -85,4 +85,40 @@ public function test_execute() { $task = new \enrol_programs\task\cron(); $task->execute(); } + + /** + * Tests that manual events are not deleted by the programs cron task. + * + * @see https://github.com/open-lms-open-source/moodle-enrol_programs/issues/11 Issue + */ + public function test_manual_calendar_events_not_deleted() { + 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 cron. + $task = new \enrol_programs\task\cron(); + $task->execute(); + + // 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)); + } }