Merge branch 'MDL-66254_master' of git://github.com/dmonllao/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2019-10-02 17:00:39 +02:00
commit 35da660ab0
7 changed files with 293 additions and 11 deletions

View File

@ -114,10 +114,15 @@ class course_competencies extends course_enrolments {
* @param \core_analytics\analysable $course
* @param int $starttime
* @param int $endtime
* @return float 0 -> competencies achieved, 1 -> competencies not achieved
* @return float|null 0 -> competencies achieved, 1 -> competencies not achieved
*/
protected function calculate_sample($sampleid, \core_analytics\analysable $course, $starttime = false, $endtime = false) {
if (!$this->enrolment_active_during_analysis_time($sampleid, $starttime, $endtime)) {
// We should not use this sample as the analysis results could be misleading.
return null;
}
$userenrol = $this->retrieve('user_enrolments', $sampleid);
$key = $course->get_id();

View File

@ -92,10 +92,15 @@ class course_completion extends course_enrolments {
* @param \core_analytics\analysable $course
* @param int $starttime
* @param int $endtime
* @return float 0 -> course not completed, 1 -> course completed
* @return float|null 0 -> course not completed, 1 -> course completed
*/
protected function calculate_sample($sampleid, \core_analytics\analysable $course, $starttime = false, $endtime = false) {
if (!$this->enrolment_active_during_analysis_time($sampleid, $starttime, $endtime)) {
// We should not use this sample as the analysis results could be misleading.
return null;
}
$userenrol = $this->retrieve('user_enrolments', $sampleid);
// We use completion as a success metric.

View File

@ -114,10 +114,15 @@ class course_dropout extends course_enrolments {
* @param \core_analytics\analysable $course
* @param int $starttime
* @param int $endtime
* @return float 0 -> not at risk, 1 -> at risk
* @return float|null 0 -> not at risk, 1 -> at risk
*/
protected function calculate_sample($sampleid, \core_analytics\analysable $course, $starttime = false, $endtime = false) {
if (!$this->enrolment_active_during_analysis_time($sampleid, $starttime, $endtime)) {
// We should not use this sample as the analysis results could be misleading.
return null;
}
$userenrol = $this->retrieve('user_enrolments', $sampleid);
// We use completion as a success metric only when it is enabled.

View File

@ -40,6 +40,11 @@ abstract class course_enrolments extends \core_analytics\local\target\binary {
*/
const MESSAGE_ACTION_NAME = 'studentmessage';
/**
* @var float
*/
const ENROL_ACTIVE_PERCENT_REQUIRED = 0.7;
/**
* Students in the course.
* @var int[]
@ -151,6 +156,11 @@ abstract class course_enrolments extends \core_analytics\local\target\binary {
/**
* Discard student enrolments that are invalid.
*
* Note that this method assumes that the target is only interested in enrolments that are/were active
* between the current course start and end times. Targets interested in predicting students at risk before
* their enrolment start and targets interested in getting predictions for students whose enrolment already
* finished should overwrite this method as these students are discarded by this method.
*
* @param int $sampleid
* @param \core_analytics\analysable $course
* @param bool $fortraining
@ -170,7 +180,7 @@ abstract class course_enrolments extends \core_analytics\local\target\binary {
$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
// Following what we do in is_valid_analysable, 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.
@ -180,7 +190,7 @@ abstract class course_enrolments extends \core_analytics\local\target\binary {
if ($course->get_end()) {
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.
// Discard user enrolments that start after the analysable official end.
return false;
}
@ -267,4 +277,101 @@ abstract class course_enrolments extends \core_analytics\local\target\binary {
['.insights-bulk-actions', self::MESSAGE_ACTION_NAME]);
parent::add_bulk_actions_js();
}
/**
* Is/was this user enrolment active during most of the analysis interval?
*
* This method discards enrolments that were not active during most of the analysis interval. It is
* important to discard these enrolments because the indicator calculations can lead to misleading
* results.
*
* Note that this method assumes that the target is interested in enrolments that are/were active
* during the analysis interval. Targets interested in predicting students at risk before
* their enrolment start should not call this method. Similarly, targets interested in getting
* predictions for students whose enrolment already finished should not call this method either.
*
* @param int $sampleid The id of the sample that is being calculated
* @param int $starttime The analysis interval start time
* @param int $endtime The analysis interval end time
* @return bool
*/
protected function enrolment_active_during_analysis_time(int $sampleid, int $starttime, int $endtime) {
$userenrol = $this->retrieve('user_enrolments', $sampleid);
$enrolstart = $userenrol->timestart ?? $userenrol->timecreated;
$enrolend = $userenrol->timeend ?? PHP_INT_MAX;
if ($endtime && $endtime < $enrolstart) {
/* The enrolment starts/ed after the analysis end time.
* |=========| |----------|
* A start A end E start E end
*/
return false;
}
if ($starttime && $enrolend < $starttime) {
/* The enrolment finishes/ed before the analysis start time.
* |---------| |==========|
* E start E end A start A end
*/
return false;
}
// Now we want to discard enrolments that were not active for most of the analysis interval. We
// need both a $starttime and an $endtime to calculate this.
if (!$starttime) {
// Early return. Nothing to discard if there is no start.
return true;
}
if (!$endtime) {
// We can not calculate in relative terms (percent) how far from the analysis start time
// this enrolment start is/was.
return true;
}
if ($enrolstart < $starttime && $endtime < $enrolend) {
/* The enrolment is active during all the analysis time.
* |-----------------------------|
* |========|
* E start A start A end E end
*/
return true;
}
// If we reach this point is because the enrolment is only active for a portion of the analysis interval.
// Therefore, we check that it was active for most of the analysis interval, a self::ENROL_ACTIVE_PERCENT_REQUIRED.
if ($starttime <= $enrolstart && $enrolend <= $endtime) {
/* |=============================|
* |--------|
* A start E start E end A end
*/
$activeenrolduration = $enrolend - $enrolstart;
} else if ($enrolstart <= $starttime && $enrolend <= $endtime) {
/* |===================|
* |------------------|
* E start A start E end A end
*/
$activeenrolduration = $enrolend - $starttime;
} else if ($starttime <= $enrolstart && $endtime <= $enrolend) {
/* |===================|
* |------------------|
* A start E start A end E end
*/
$activeenrolduration = $endtime - $enrolstart;
}
$analysisduration = $endtime - $starttime;
if (floatval($activeenrolduration) / floatval($analysisduration) < self::ENROL_ACTIVE_PERCENT_REQUIRED) {
// The student was not enroled in the course for most of the analysis interval.
return false;
}
// We happily return true if the enrolment was active for more than self::ENROL_ACTIVE_PERCENT_REQUIRED of
// the analysis interval.
return true;
}
}

View File

@ -160,10 +160,15 @@ class course_gradetopass extends course_enrolments {
* @param \core_analytics\analysable $course
* @param int $starttime
* @param int $endtime
* @return float 0 -> course grade to pass achieved, 1 -> course grade to pass not achieved
* @return float|null 0 -> course grade to pass achieved, 1 -> course grade to pass not achieved
*/
protected function calculate_sample($sampleid, \core_analytics\analysable $course, $starttime = false, $endtime = false) {
if (!$this->enrolment_active_during_analysis_time($sampleid, $starttime, $endtime)) {
// We should not use this sample as the analysis results could be misleading.
return null;
}
$userenrol = $this->retrieve('user_enrolments', $sampleid);
// Get course grade to pass.

View File

@ -122,10 +122,15 @@ class no_recent_accesses extends course_enrolments {
* @param \core_analytics\analysable $analysable
* @param int $starttime
* @param int $endtime
* @return float 0 -> accesses, 1 -> no accesses.
* @return float|null 0 -> accesses, 1 -> no accesses.
*/
protected function calculate_sample($sampleid, \core_analytics\analysable $analysable, $starttime = false, $endtime = false) {
if (!$this->enrolment_active_during_analysis_time($sampleid, $starttime, $endtime)) {
// We should not use this sample as the analysis results could be misleading.
return null;
}
$readactions = $this->retrieve('\core\analytics\indicator\any_course_access', $sampleid);
if ($readactions == \core\analytics\indicator\any_course_access::get_min_value()) {
return 1;

View File

@ -17,8 +17,7 @@
/**
* Unit tests for core targets.
*
* @package core
* @category analytics
* @package core_course
* @copyright 2019 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -36,8 +35,7 @@ require_once($CFG->dirroot . '/lib/grade/constants.php');
/**
* Unit tests for core targets.
*
* @package core
* @category analytics
* @package core_course
* @copyright 2019 Victor Deniz <victor@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -186,6 +184,116 @@ class core_analytics_targets_testcase extends advanced_testcase {
];
}
/**
* Provides enrolment params for the {@link self::test_core_target_course_completion_samples()} method.
*
* @return array
*/
public function active_during_analysis_time_provider() {
$now = time();
return [
'enrol-after-end' => [
'starttime' => $now,
'endtime' => $now + WEEKSECS,
'timestart' => $now + (WEEKSECS * 2),
'timeend' => $now + (WEEKSECS * 3),
'nullcalculation' => true,
],
'enrol-before-start' => [
'starttime' => $now + (WEEKSECS * 2),
'endtime' => $now + (WEEKSECS * 3),
'timestart' => $now,
'timeend' => $now + WEEKSECS,
'nullcalculation' => true,
],
'enrol-active-exact-match' => [
'starttime' => $now,
'endtime' => $now + (WEEKSECS * 1),
'timestart' => $now,
'timeend' => $now + (WEEKSECS * 1),
'nullcalculation' => false,
],
'enrol-active' => [
'starttime' => $now + WEEKSECS,
'endtime' => $now + (WEEKSECS * 2),
'timestart' => $now,
'timeend' => $now + (WEEKSECS * 3),
'nullcalculation' => false,
],
'enrol-during-analysis-active-just-for-a-while' => [
'starttime' => $now,
'endtime' => $now + (WEEKSECS * 10),
'timestart' => $now + WEEKSECS,
'timeend' => $now + (WEEKSECS * 2),
'nullcalculation' => true,
],
'enrol-during-analysis-mostly-active' => [
'starttime' => $now,
'endtime' => $now + (WEEKSECS * 20),
'timestart' => $now + WEEKSECS,
'timeend' => $now + (WEEKSECS * 19),
'nullcalculation' => false,
],
'enrol-partly-active-starts-before' => [
'starttime' => $now + WEEKSECS,
'endtime' => $now + (WEEKSECS * 10),
'timestart' => $now,
'timeend' => $now + (WEEKSECS * 2),
'nullcalculation' => true,
],
'enrol-mostly-active-starts-before' => [
'starttime' => $now + WEEKSECS,
'endtime' => $now + (WEEKSECS * 10),
'timestart' => $now,
'timeend' => $now + (WEEKSECS * 9),
'nullcalculation' => false,
],
'enrol-partly-active-ends-afterwards' => [
'starttime' => $now,
'endtime' => $now + (WEEKSECS * 9),
'timestart' => $now + (WEEKSECS * 10),
'timeend' => $now + (WEEKSECS * 11),
'nullcalculation' => true,
],
'enrol-mostly-active-ends-afterwards' => [
'starttime' => $now,
'endtime' => $now + (WEEKSECS * 10),
'timestart' => $now + WEEKSECS,
'timeend' => $now + (WEEKSECS * 11),
'nullcalculation' => false,
],
'enrol-partly-active-no-enrolment-end' => [
'starttime' => $now,
'endtime' => $now + (WEEKSECS * 10),
'timestart' => $now + (WEEKSECS * 9),
'timeend' => false,
'nullcalculation' => true,
],
'enrol-mostly-active-no-enrolment-end' => [
'starttime' => $now,
'endtime' => $now + (WEEKSECS * 10),
'timestart' => $now + WEEKSECS,
'timeend' => false,
'nullcalculation' => true,
],
'no-start' => [
'starttime' => 0,
'endtime' => $now + (WEEKSECS * 2),
'timestart' => $now + WEEKSECS,
'timeend' => $now + (WEEKSECS * 3),
'nullcalculation' => false,
],
'no-end' => [
'starttime' => $now,
'endtime' => 0,
'timestart' => $now + (WEEKSECS * 2),
'timeend' => $now + (WEEKSECS * 3),
'nullcalculation' => false,
]
];
}
/**
* Test the conditions of a valid analysable, both common and specific to this target (course_completion).
*
@ -279,6 +387,48 @@ class core_analytics_targets_testcase extends advanced_testcase {
$this->assertEquals($isvalidforprediction, $target->is_valid_sample($sampleid, $analysable, false));
}
/**
* Test the conditions of a valid calculation (course_completion).
*
* @dataProvider active_during_analysis_time_provider
* @param int $starttime Analysis start time
* @param int $endtime Analysis end time
* @param int $timestart Enrol start date
* @param int $timeend Enrol end date
* @param boolean $nullcalculation Whether the calculation should be null or not
*/
public function test_core_target_course_completion_active_during_analysis_time($starttime, $endtime, $timestart, $timeend,
$nullcalculation) {
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user->id, $course->id, null, 'manual', $timestart, $timeend);
$target = new \core_course\analytics\target\course_completion();
$analyser = new \core\analytics\analyser\student_enrolments(1, $target, [], [], []);
$analysable = new \core_analytics\course($course);
$class = new ReflectionClass('\core\analytics\analyser\student_enrolments');
$method = $class->getMethod('get_all_samples');
$method->setAccessible(true);
list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
$target->add_sample_data($samplesdata);
$sampleid = reset($sampleids);
$reftarget = new ReflectionObject($target);
$refmethod = $reftarget->getMethod('calculate_sample');
$refmethod->setAccessible(true);
if ($nullcalculation) {
$this->assertNull($refmethod->invoke($target, $sampleid, $analysable, $starttime, $endtime));
} else {
$this->assertNotNull($refmethod->invoke($target, $sampleid, $analysable, $starttime, $endtime));
}
}
/**
* Setup user, framework, competencies and course competencies.
*/