1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Time splitting method that generates predictions regularly.
|
|
|
19 |
*
|
|
|
20 |
* @package core_analytics
|
|
|
21 |
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_analytics\local\time_splitting;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Time splitting method that generates predictions periodically.
|
|
|
31 |
*
|
|
|
32 |
* @package core_analytics
|
|
|
33 |
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
abstract class past_periodic extends periodic implements before_now {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Gets the next range with start on the provided time.
|
|
|
40 |
*
|
|
|
41 |
* The next range is based on the past period so we substract this
|
|
|
42 |
* range's periodicity from $time.
|
|
|
43 |
*
|
|
|
44 |
* @param \DateTimeImmutable $time
|
|
|
45 |
* @return array
|
|
|
46 |
*/
|
|
|
47 |
protected function get_next_range(\DateTimeImmutable $time) {
|
|
|
48 |
|
|
|
49 |
$end = $time->getTimestamp();
|
|
|
50 |
$start = $time->sub($this->periodicity())->getTimestamp();
|
|
|
51 |
|
|
|
52 |
if ($start < $this->analysable->get_start()) {
|
|
|
53 |
// We skip the first range generated as its start is prior to the analysable start.
|
|
|
54 |
return false;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
return [
|
|
|
58 |
'start' => $start,
|
|
|
59 |
'end' => $end,
|
|
|
60 |
'time' => $end
|
|
|
61 |
];
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Get the start of the first time range.
|
|
|
66 |
*
|
|
|
67 |
* @return int A timestamp.
|
|
|
68 |
*/
|
|
|
69 |
protected function get_first_start() {
|
|
|
70 |
return $this->analysable->get_start();
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Guarantees that the last range dates end right now.
|
|
|
75 |
*
|
|
|
76 |
* @param array $ranges
|
|
|
77 |
* @return array
|
|
|
78 |
*/
|
|
|
79 |
protected function update_last_range(array $ranges) {
|
|
|
80 |
$lastrange = end($ranges);
|
|
|
81 |
|
|
|
82 |
if ($lastrange['time'] > time()) {
|
|
|
83 |
// We just need to wait in this case.
|
|
|
84 |
return $lastrange;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$timetoenddiff = time() - $lastrange['time'];
|
|
|
88 |
|
|
|
89 |
$ranges[count($ranges) - 1] = [
|
|
|
90 |
'start' => $lastrange['start'] + $timetoenddiff,
|
|
|
91 |
'end' => $lastrange['end'] + $timetoenddiff,
|
|
|
92 |
'time' => $lastrange['time'] + $timetoenddiff,
|
|
|
93 |
];
|
|
|
94 |
|
|
|
95 |
return $ranges;
|
|
|
96 |
}
|
|
|
97 |
}
|