public function is_valid_sample($sampleid, \core_analytics\analysable $course, $fortraining = true) {

        $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
            // old user enrolments that remain on the course.
            return false;
        }

        $limit = $course->get_start() - (YEARSECS + (WEEKSECS * 4));
        if (($userenrol->timestart && $userenrol->timestart < $limit) ||
                (!$userenrol->timestart && $userenrol->timecreated < $limit)) {
            // Following what we do in is_valid_sample, we will discard enrolments that last more than 1 academic year
            // because they have incorrect start and end dates or because they are reused along multiple years
            // without removing previous academic years students. This may not be very accurate because some courses
            // can last just some months, but it is better than nothing.
            return false;
        }

        if (($userenrol->timestart && $userenrol->timestart > $course->get_end()) ||
                (!$userenrol->timestart && $userenrol->timecreated > $course->get_end())) {
            // Discard user enrolments that starts after the analysable official end.
            return false;
        }

        return true;
    }