Skip to content

Commit

Permalink
MDL-82620 completion: Make enrol duration comp happen at the right time
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmerrill committed Jul 26, 2024
1 parent 5fe8984 commit 8b76c9a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions completion/criteria/completion_criteria_duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public function cron() {
AND cc.id IS NULL
AND
(
ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?
OR ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?
(ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?)
OR (ue.timestart = 0 AND ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?)
)
';

Expand Down
36 changes: 36 additions & 0 deletions completion/tests/completion_criteria_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class completion_criteria_test extends \advanced_testcase {
*/
public function setUp(): void {
global $CFG;
require_once($CFG->dirroot.'/completion/criteria/completion_criteria.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
Expand Down Expand Up @@ -112,6 +113,41 @@ public function test_completion_criteria_duration_timestart(): void {
$ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
$this->assertEquals($timestarted + $durationperiod, $ccompletion->timecompleted);
$this->assertTrue($ccompletion->is_complete());

// Now we want to check the scenario where "now" sits in the middle of the timestart + duration
// and timecreated + duration window.
$nowtime = time();
$timestarted = $nowtime - $durationperiod + (2 * DAYSECS);
$timecreated = $nowtime - $durationperiod - (2 * DAYSECS);

// Using a new user for this.
$user = $this->getDataGenerator()->create_and_enrol($course, 'student', null, 'manual', $timestarted);

// We need to manually update the enrollment's time created.
$DB->set_field('user_enrolments', 'timecreated', $timecreated, ['userid' => $user->id]);

// Run the completion cron. See MDL-33320.
$task->execute();
sleep(1);
$task->execute();

// We do NOT expect the user to be complete currently.
$ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
$this->assertFalse($ccompletion->is_complete());

// Now, finally, we will move the timestart to be in the past, but still after the timecreated.
$timestarted = $timecreated + DAYSECS;
$DB->set_field('user_enrolments', 'timestart', $timestarted, ['userid' => $user->id]);

// Run the completion cron. See MDL-33320.
$task->execute();
sleep(1);
$task->execute();

// Now they should be complete.
$ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
$this->assertEquals($timestarted + $durationperiod, $ccompletion->timecompleted);
$this->assertTrue($ccompletion->is_complete());
}

/**
Expand Down

0 comments on commit 8b76c9a

Please sign in to comment.