From 474e5c539d96158d57f1171f541d76ee588117ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Tue, 21 May 2019 16:56:25 +0200 Subject: [PATCH] MDL-65634 analytics: Discard late and early student enrolments --- .../analytics/target/course_dropout.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/classes/analytics/target/course_dropout.php b/lib/classes/analytics/target/course_dropout.php index b44ec4d34ae..4cd82752828 100644 --- a/lib/classes/analytics/target/course_dropout.php +++ b/lib/classes/analytics/target/course_dropout.php @@ -203,6 +203,8 @@ class course_dropout extends \core_analytics\local\target\binary { */ public function is_valid_sample($sampleid, \core_analytics\analysable $course, $fortraining = true) { + $now = time(); + $userenrol = $this->retrieve('user_enrolments', $sampleid); if ($userenrol->timeend && $course->get_start() > $userenrol->timeend) { // Discard enrolments which time end is prior to the course start. This should get rid of @@ -227,6 +229,16 @@ class course_dropout extends \core_analytics\local\target\binary { return false; } + if ($now < $userenrol->timestart && $userenrol->timestart) { + // Discard enrolments whose start date is after now (no need to check timecreated > $now :P). + return false; + } + + if (!$fortraining && $userenrol->timeend && $userenrol->timeend < $now) { + // We don't want to generate predictions for finished enrolments. + return false; + } + return true; } @@ -245,6 +257,11 @@ class course_dropout extends \core_analytics\local\target\binary { */ protected function calculate_sample($sampleid, \core_analytics\analysable $course, $starttime = false, $endtime = false) { + if ($this->enrolment_starts_after_calculation_start($sampleid, $starttime)) { + // Discard user enrolments whose start date is after $starttime. + return null; + } + $userenrol = $this->retrieve('user_enrolments', $sampleid); // We use completion as a success metric only when it is enabled. @@ -273,4 +290,25 @@ class course_dropout extends \core_analytics\local\target\binary { } return 0; } + + /** + * Does the user enrolment created after this time range start time or starts after it? + * + * We need to identify these enrolments because the indicators can not be calculated properly + * if the student enrolment started half way through this time range. + * + * User enrolments whose end date is before time() have already been discarded in + * course_enrolments::is_valid_sample. + * + * @param int $sampleid + * @param int $starttime + * @return bool + */ + protected function enrolment_starts_after_calculation_start(int $sampleid, int $starttime) { + $userenrol = $this->retrieve('user_enrolments', $sampleid); + if ($userenrol->timestart && $userenrol->timestart > $starttime) { + return true; + } + return false; + } }