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 periodically.
|
|
|
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 upcoming_periodic extends periodic implements after_now {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Gets the next range with start on the provided time.
|
|
|
40 |
*
|
|
|
41 |
* The next range is based on the upcoming period so we add this
|
|
|
42 |
* range's periodicity to $time.
|
|
|
43 |
*
|
|
|
44 |
* @param \DateTimeImmutable $time
|
|
|
45 |
* @return array
|
|
|
46 |
*/
|
|
|
47 |
protected function get_next_range(\DateTimeImmutable $time) {
|
|
|
48 |
|
|
|
49 |
$start = $time->getTimestamp();
|
|
|
50 |
$end = $time->add($this->periodicity())->getTimestamp();
|
|
|
51 |
return [
|
|
|
52 |
'start' => $start,
|
|
|
53 |
'end' => $end,
|
|
|
54 |
'time' => $start
|
|
|
55 |
];
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Whether to cache or not the indicator calculations.
|
|
|
60 |
* @return bool
|
|
|
61 |
*/
|
|
|
62 |
public function cache_indicator_calculations(): bool {
|
|
|
63 |
return false;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Overriden as these time-splitting methods are based on future dates.
|
|
|
68 |
*
|
|
|
69 |
* @return bool
|
|
|
70 |
*/
|
|
|
71 |
public function valid_for_evaluation(): bool {
|
|
|
72 |
return false;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Get the start of the first time range.
|
|
|
77 |
*
|
|
|
78 |
* Overwriten to start generating predictions about upcoming stuff from time().
|
|
|
79 |
*
|
|
|
80 |
* @return int A timestamp.
|
|
|
81 |
*/
|
|
|
82 |
protected function get_first_start() {
|
|
|
83 |
global $DB;
|
|
|
84 |
|
|
|
85 |
$cache = \cache::make('core', 'modelfirstanalyses');
|
|
|
86 |
|
|
|
87 |
$key = $this->modelid . '_' . $this->analysable->get_id();
|
|
|
88 |
$firstanalysis = $cache->get($key);
|
|
|
89 |
if (!empty($firstanalysis)) {
|
|
|
90 |
return $firstanalysis;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// This analysable has not yet been analysed, the start is therefore now.
|
|
|
94 |
return time();
|
|
|
95 |
}
|
|
|
96 |
}
|