From f15afac206fe0b3a37dc64f29a3caa2aa2b60eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yerai=20Rodr=C3=ADguez?= Date: Mon, 25 Aug 2025 12:47:19 +0200 Subject: [PATCH] MDL-86330 enrol: remove time rounding to avoid missing active enrolments Rounding time could lead to newest enrolments being excluded, resulting in these courses not appearing under the "My courses" tab in the Dashboard until the rounded time aligned with the actual time. enrol_get_all_users_courses() now uses the PSR-20 clock to provide stable and testable time values. --- enrol/tests/enrollib_test.php | 12 ++++++++++-- lib/enrollib.php | 3 +-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/enrol/tests/enrollib_test.php b/enrol/tests/enrollib_test.php index c7c39af7a41..ce998fc46ab 100644 --- a/enrol/tests/enrollib_test.php +++ b/enrol/tests/enrollib_test.php @@ -43,6 +43,9 @@ final class enrollib_test extends advanced_testcase { $this->resetAfterTest(); + // Use a mock incrementing clock to ensure deterministic and testable time values. + $clock = $this->mock_clock_with_incrementing(1750000000); + $studentrole = $DB->get_record('role', array('shortname'=>'student')); $this->assertNotEmpty($studentrole); $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); @@ -96,13 +99,14 @@ final class enrollib_test extends advanced_testcase { $manual->enrol_user($maninstance2, $user1->id); $manual->enrol_user($maninstance2, $user2->id); - $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()+(60*60)); + $manual->enrol_user($maninstance2, $user3->id, 0, 1, $clock->time() + HOURSECS); $manual->enrol_user($maninstance3, $user1->id); $manual->enrol_user($maninstance3, $user2->id); - $manual->enrol_user($maninstance3, $user3->id, 0, 1, time()-(60*60)); + $manual->enrol_user($maninstance3, $user3->id, 0, 1, $clock->time() - HOURSECS); $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED); + $manual->enrol_user($maninstance4, $user5->id, 0, $clock->time()); $courses = enrol_get_all_users_courses($CFG->siteguest); $this->assertSame(array(), $courses); @@ -152,6 +156,10 @@ final class enrollib_test extends advanced_testcase { $this->assertCount(0, $courses); $this->assertEquals(array(), array_keys($courses)); + $courses = enrol_get_all_users_courses($user5->id, true); + $this->assertCount(1, $courses); + $this->assertEquals([$course4->id], array_keys($courses)); + // Make sure sorting and columns work. $basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber', diff --git a/lib/enrollib.php b/lib/enrollib.php index 1a4d05aa9c1..c7014a585fe 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -1111,8 +1111,7 @@ function enrol_get_all_users_courses($userid, $onlyactive = false, $fields = nul if ($onlyactive) { $subwhere = "WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)"; - $params['now1'] = round(time(), -2); // improves db caching - $params['now2'] = $params['now1']; + $params['now1'] = $params['now2'] = \core\di::get(\core\clock::class)->time(); $params['active'] = ENROL_USER_ACTIVE; $params['enabled'] = ENROL_INSTANCE_ENABLED; } else {