MDL-59638 analytics: Prevent start time to be higher than end time

Other related changes:

- core_analytics\site get_start and get_end returning always integer values
- Defer intval call in define_ranges to refine some seconds
This commit is contained in:
David Monllao
2017-08-08 14:38:11 +02:00
parent 455974b395
commit 42d67f95cd
3 changed files with 17 additions and 9 deletions
@@ -51,11 +51,11 @@ abstract class accumulative_parts extends base {
$nparts = $this->get_number_parts();
$rangeduration = intval(floor(($this->analysable->get_end() - $this->analysable->get_start()) / $nparts));
$rangeduration = ($this->analysable->get_end() - $this->analysable->get_start()) / $nparts;
$ranges = array();
for ($i = 0; $i < $nparts; $i++) {
$end = $this->analysable->get_start() + ($rangeduration * ($i + 1));
$end = $this->analysable->get_start() + intval($rangeduration * ($i + 1));
if ($i === ($nparts - 1)) {
// Better to use the end for the last one as we are using floor above.
$end = $this->analysable->get_end();
@@ -51,15 +51,23 @@ abstract class equal_parts extends base {
$nparts = $this->get_number_parts();
$rangeduration = intval(floor(($this->analysable->get_end() - $this->analysable->get_start()) / $nparts));
$rangeduration = ($this->analysable->get_end() - $this->analysable->get_start()) / $nparts;
if ($rangeduration < $nparts) {
// It is interesting to avoid having a single timestamp belonging to multiple time ranges
// because of things like community of inquiry indicators, where activities have a due date
// that, ideally, would fall only into 1 time range. If the analysable duration is very short
// it is because the model doesn't contain indicators that depend so heavily on time and therefore
// we don't need to worry about timestamps being present in multiple time ranges.
$allowmultipleranges = true;
}
$ranges = array();
for ($i = 0; $i < $nparts; $i++) {
$start = $this->analysable->get_start() + ($rangeduration * $i);
$end = $this->analysable->get_start() + ($rangeduration * ($i + 1));
$start = $this->analysable->get_start() + intval($rangeduration * $i);
$end = $this->analysable->get_start() + intval($rangeduration * ($i + 1));
// Check the end of the previous time range.
if ($i > 0 && $start === $ranges[$i - 1]['end']) {
if (empty($allowmultipleranges) && $i > 0 && $start === $ranges[$i - 1]['end']) {
// We add 1 second so each timestamp only belongs to 1 range.
$start = $start + 1;
}
+2 -2
View File
@@ -83,7 +83,7 @@ class site implements \core_analytics\analysable {
if ($events) {
// There should be just 1 event.
$event = reset($events);
$this->start = $event->timecreated;
$this->start = intval($event->timecreated);
} else {
$this->start = 0;
}
@@ -111,7 +111,7 @@ class site implements \core_analytics\analysable {
if ($events) {
// There should be just 1 event.
$event = reset($events);
$this->end = $event->timecreated;
$this->end = intval($event->timecreated);
} else {
$this->end = time();
}