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.
This commit is contained in:
Yerai Rodríguez
2025-09-04 16:47:23 +02:00
parent 8e37adaddf
commit 13a32ba37d
2 changed files with 11 additions and 4 deletions
+10 -2
View File
@@ -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',
+1 -2
View File
@@ -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 {