first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Range processor splitting the course in parts and accumulating data from the start.
*
* @package core_analytics
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Range processor splitting the course in parts and accumulating data from the start.
*
* @package core_analytics
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class accumulative_parts extends base implements before_now {
/**
* The number of parts to split the analysable duration in.
*
* @return int
*/
abstract protected function get_number_parts();
/**
* define_ranges
*
* @return array
*/
protected function define_ranges() {
$nparts = $this->get_number_parts();
$rangeduration = ($this->analysable->get_end() - $this->analysable->get_start()) / $nparts;
$ranges = array();
for ($i = 0; $i < $nparts; $i++) {
$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();
}
$ranges[$i] = array(
'start' => $this->analysable->get_start(),
'end' => $end,
'time' => $end
);
}
return $ranges;
}
}
@@ -0,0 +1,37 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Interface for time-splitting methods whose ranges' times are after time().
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Interface for time-splitting methods whose ranges' times are after time().
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface after_now {
}
@@ -0,0 +1,111 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Time splitting method that generates predictions X days/weeks/months after the analysable start.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Time splitting method that generates predictions X days/weeks/months after the analysable start.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class after_start extends \core_analytics\local\time_splitting\base implements before_now {
/**
* The period we should wait until we generate predictions for this.
*
* @param \core_analytics\analysable $analysable
* @return \DateInterval
*/
abstract protected function wait_period(\core_analytics\analysable $analysable);
/**
* Returns whether the course can be processed by this time splitting method or not.
*
* @param \core_analytics\analysable $analysable
* @return bool
*/
public function is_valid_analysable(\core_analytics\analysable $analysable) {
if (!$analysable->get_start()) {
return false;
}
$predictionstart = $this->get_prediction_interval_start($analysable);
if ($analysable->get_start() > $predictionstart) {
// We still need to wait.
return false;
}
return true;
}
/**
* This time-splitting method returns one single range, the start to two days before the end.
*
* @return array The list of ranges, each of them including 'start', 'end' and 'time'
*/
protected function define_ranges() {
$now = time();
$ranges = [
[
'start' => $this->analysable->get_start(),
'end' => $now,
'time' => $now,
]
];
return $ranges;
}
/**
* Whether to cache or not the indicator calculations.
*
* @return bool
*/
public function cache_indicator_calculations(): bool {
return false;
}
/**
* Calculates the interval start time backwards, from now.
*
* @param \core_analytics\analysable $analysable
* @return int
*/
protected function get_prediction_interval_start(\core_analytics\analysable $analysable) {
// The prediction time is always time(). We don't want to reuse the firstanalysis time
// because otherwise samples (e.g. students) which start after the analysable (e.g. course)
// start would use an incorrect analysis interval.
$predictionstart = new \DateTime('now');
$predictionstart->sub($this->wait_period($analysable));
return $predictionstart->getTimestamp();
}
}
@@ -0,0 +1,311 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base time splitting method.
*
* @package core_analytics
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Base time splitting method.
*
* @package core_analytics
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base {
/**
* @var string
*/
protected $id;
/**
* The model id.
*
* @var int
*/
protected $modelid;
/**
* @var \core_analytics\analysable
*/
protected $analysable;
/**
* @var array
*/
protected $ranges = [];
/**
* Define the time splitting methods ranges.
*
* 'time' value defines when predictions are executed, their values will be compared with
* the current time in ready_to_predict. The ranges should be sorted by 'time' in
* ascending order.
*
* @return array('start' => time(), 'end' => time(), 'time' => time())
*/
abstract protected function define_ranges();
/**
* Returns a lang_string object representing the name for the time splitting method.
*
* Used as column identificator.
*
* If there is a corresponding '_help' string this will be shown as well.
*
* @return \lang_string
*/
abstract public static function get_name(): \lang_string;
/**
* Returns the time splitting method id.
*
* @return string
*/
public function get_id() {
return '\\' . get_class($this);
}
/**
* Assigns the analysable and updates the time ranges according to the analysable start and end dates.
*
* @param \core_analytics\analysable $analysable
* @return void
*/
public function set_analysable(\core_analytics\analysable $analysable) {
$this->analysable = $analysable;
$this->ranges = $this->define_ranges();
$this->validate_ranges();
}
/**
* Assigns the model id to this time-splitting method it case it needs it.
*
* @param int $modelid
*/
public function set_modelid(int $modelid) {
$this->modelid = $modelid;
}
/**
* get_analysable
*
* @return \core_analytics\analysable
*/
public function get_analysable() {
return $this->analysable;
}
/**
* Returns whether the course can be processed by this time splitting method or not.
*
* @param \core_analytics\analysable $analysable
* @return bool
*/
public function is_valid_analysable(\core_analytics\analysable $analysable) {
return true;
}
/**
* Should we predict this time range now?
*
* @param array $range
* @return bool
*/
public function ready_to_predict($range) {
if ($range['time'] <= time()) {
return true;
}
return false;
}
/**
* Should we use this time range for training?
*
* @param array $range
* @return bool
*/
public function ready_to_train($range) {
$now = time();
if ($range['time'] <= $now && $range['end'] <= $now) {
return true;
}
return false;
}
/**
* Returns the ranges used by this time splitting method.
*
* @return array
*/
public function get_all_ranges() {
return $this->ranges;
}
/**
* By default all ranges are for training.
*
* @return array
*/
public function get_training_ranges() {
return $this->ranges;
}
/**
* Returns the distinct range indexes in this time splitting method.
*
* @return int[]
*/
public function get_distinct_ranges() {
if ($this->include_range_info_in_training_data()) {
return array_keys($this->ranges);
} else {
return [0];
}
}
/**
* Returns the most recent range that can be used to predict.
*
* This method is only called when calculating predictions.
*
* @return array
*/
public function get_most_recent_prediction_range() {
$ranges = $this->get_all_ranges();
// Opposite order as we are interested in the last range that can be used for prediction.
krsort($ranges);
// We already provided the analysable to the time splitting method, there is no need to feed it back.
foreach ($ranges as $rangeindex => $range) {
if ($this->ready_to_predict($range)) {
// We need to maintain the same indexes.
return array($rangeindex => $range);
}
}
return array();
}
/**
* Returns range data by its index.
*
* @param int $rangeindex
* @return array|false Range data or false if the index is not part of the existing ranges.
*/
public function get_range_by_index($rangeindex) {
if (!isset($this->ranges[$rangeindex])) {
return false;
}
return $this->ranges[$rangeindex];
}
/**
* Generates a unique sample id (sample in a range index).
*
* @param int $sampleid
* @param int $rangeindex
* @return string
*/
final public function append_rangeindex($sampleid, $rangeindex) {
return $sampleid . '-' . $rangeindex;
}
/**
* Returns the sample id and the range index from a uniquesampleid.
*
* @param string $uniquesampleid
* @return array array($sampleid, $rangeindex)
*/
final public function infer_sample_info($uniquesampleid) {
return explode('-', $uniquesampleid);
}
/**
* Whether to include the range index in the training data or not.
*
* By default, we consider that the different time ranges included in a time splitting method may not be
* compatible between them (i.e. the indicators calculated at the end of the course can easily
* differ from indicators calculated at the beginning of the course). So we include the range index as
* one of the variables that the machine learning backend uses to generate predictions.
*
* If the indicators calculated using the different time ranges available in this time splitting method
* are comparable you can overwrite this method to return false.
*
* Note that:
* - This is only relevant for models whose predictions are not based on assumptions
* (i.e. the ones using a machine learning backend to generate predictions).
* - The ranges can only be included in the training data when
* we know the final number of ranges the time splitting method will have. E.g.
* We can not know the final number of ranges of a 'daily' time splitting method
* as we will have one new range every day.
* @return bool
*/
public function include_range_info_in_training_data() {
return true;
}
/**
* Whether to cache or not the indicator calculations.
*
* Indicator calculations are stored to be reused across models. The calculations
* are indexed by the calculation start and end time, and these times depend on the
* time-splitting method. You should overwrite this method and return false if the time
* frames generated by your time-splitting method are unique and / or can hardly be
* reused by further models.
*
* @return bool
*/
public function cache_indicator_calculations(): bool {
return true;
}
/**
* Is this method valid to evaluate prediction models?
*
* @return bool
*/
public function valid_for_evaluation(): bool {
return true;
}
/**
* Validates the time splitting method ranges.
*
* @throws \coding_exception
* @return void
*/
protected function validate_ranges() {
foreach ($this->ranges as $key => $range) {
if (!isset($this->ranges[$key]['start']) || !isset($this->ranges[$key]['end']) ||
!isset($this->ranges[$key]['time'])) {
throw new \coding_exception($this->get_id() . ' time splitting method "' . $key .
'" range is not fully defined. We need a start timestamp and an end timestamp.');
}
}
}
}
@@ -0,0 +1,37 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Interface for time-splitting methods whose ranges' times are before time().
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Interface for time-splitting methods whose ranges' times are before time().
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface before_now {
}
@@ -0,0 +1,88 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* X parts time splitting method.
*
* @package core_analytics
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* X parts time splitting method.
*
* @package core_analytics
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class equal_parts extends base implements before_now {
/**
* Returns the number of parts the analyser duration should be split in.
*
* @return int
*/
abstract protected function get_number_parts();
/**
* Splits the analysable duration in X equal parts from the start to the end.
*
* @return array
*/
protected function define_ranges() {
$nparts = $this->get_number_parts();
$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() + intval($rangeduration * $i);
$end = $this->analysable->get_start() + intval($rangeduration * ($i + 1));
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;
}
if ($i === ($nparts - 1)) {
// Better to use the end for the last one as we are using floor above.
$end = $this->analysable->get_end();
}
$ranges[$i] = array(
'start' => $start,
'end' => $end,
'time' => $end
);
}
return $ranges;
}
}
@@ -0,0 +1,97 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Time splitting method that generates predictions regularly.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Time splitting method that generates predictions periodically.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class past_periodic extends periodic implements before_now {
/**
* Gets the next range with start on the provided time.
*
* The next range is based on the past period so we substract this
* range's periodicity from $time.
*
* @param \DateTimeImmutable $time
* @return array
*/
protected function get_next_range(\DateTimeImmutable $time) {
$end = $time->getTimestamp();
$start = $time->sub($this->periodicity())->getTimestamp();
if ($start < $this->analysable->get_start()) {
// We skip the first range generated as its start is prior to the analysable start.
return false;
}
return [
'start' => $start,
'end' => $end,
'time' => $end
];
}
/**
* Get the start of the first time range.
*
* @return int A timestamp.
*/
protected function get_first_start() {
return $this->analysable->get_start();
}
/**
* Guarantees that the last range dates end right now.
*
* @param array $ranges
* @return array
*/
protected function update_last_range(array $ranges) {
$lastrange = end($ranges);
if ($lastrange['time'] > time()) {
// We just need to wait in this case.
return $lastrange;
}
$timetoenddiff = time() - $lastrange['time'];
$ranges[count($ranges) - 1] = [
'start' => $lastrange['start'] + $timetoenddiff,
'end' => $lastrange['end'] + $timetoenddiff,
'time' => $lastrange['time'] + $timetoenddiff,
];
return $ranges;
}
}
@@ -0,0 +1,162 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Time splitting method that generates predictions regularly.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Time splitting method that generates predictions periodically.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class periodic extends base {
/**
* The periodicity of the predictions / training data generation.
*
* @return \DateInterval
*/
abstract protected function periodicity();
/**
* Gets the next range with start on the provided time.
*
* @param \DateTimeImmutable $time
* @return array
*/
abstract protected function get_next_range(\DateTimeImmutable $time);
/**
* Get the start of the first time range.
*
* @return int A timestamp.
*/
abstract protected function get_first_start();
/**
* Returns whether the analysable can be processed by this time splitting method or not.
*
* @param \core_analytics\analysable $analysable
* @return bool
*/
public function is_valid_analysable(\core_analytics\analysable $analysable) {
if (!$analysable->get_start()) {
return false;
}
return true;
}
/**
* define_ranges
*
* @return array
*/
protected function define_ranges() {
$periodicity = $this->periodicity();
if ($this->analysable->get_end()) {
$end = (new \DateTimeImmutable())->setTimestamp($this->analysable->get_end());
}
$nexttime = (new \DateTimeImmutable())->setTimestamp($this->get_first_start());
$now = new \DateTimeImmutable('now', \core_date::get_server_timezone_object());
$range = $this->get_next_range($nexttime);
if (!$range) {
$nexttime = $nexttime->add($periodicity);
$range = $this->get_next_range($nexttime);
if (!$range) {
throw new \coding_exception('The get_next_range implementation is broken. The difference between two consecutive
ranges can not be more than the periodicity.');
}
}
$ranges = [];
$endreached = false;
while (($this->ready_to_predict($range) || $this->ready_to_train($range)) && !$endreached) {
$ranges[] = $range;
$nexttime = $nexttime->add($periodicity);
$range = $this->get_next_range($nexttime);
$endreached = (!empty($end) && $nexttime > $end);
}
if ($ranges && !$endreached) {
// If this analysable is not finished we adjust the start and end of the last element in $ranges
// so that it ends in time().The reason is that the start of these ranges is based on the analysable
// start and the end is calculated based on the start. This is to prevent the same issue we had in MDL-65348.
//
// An example of the situation we want to avoid is:
// A course started on a Monday, in 2015. It has no end date. Now the system is upgraded to Moodle 3.8, which
// includes this code. This happens on Wednesday. Periodic ranges (e.g. weekly) will be calculated from a Monday
// so the data provided by the time-splitting method would be from Monday to Monday, when we really want to
// provide data from Wednesday to the past Wednesday.
$ranges = $this->update_last_range($ranges);
}
return $ranges;
}
/**
* Overwritten as all generated rows are comparable.
*
* @return bool
*/
public function include_range_info_in_training_data() {
return false;
}
/**
* Overwritting as the last range may be for prediction.
*
* @return array
*/
public function get_training_ranges() {
// Cloning the array.
$trainingranges = $this->ranges;
foreach ($trainingranges as $rangeindex => $range) {
if (!$this->ready_to_train($range)) {
unset($trainingranges[$rangeindex]);
}
}
return $trainingranges;
}
/**
* Allows child classes to update the last range provided.
*
* @param array $ranges
* @return array
*/
protected function update_last_range(array $ranges) {
return $ranges;
}
}
@@ -0,0 +1,96 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Time splitting method that generates predictions periodically.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Time splitting method that generates predictions periodically.
*
* @package core_analytics
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class upcoming_periodic extends periodic implements after_now {
/**
* Gets the next range with start on the provided time.
*
* The next range is based on the upcoming period so we add this
* range's periodicity to $time.
*
* @param \DateTimeImmutable $time
* @return array
*/
protected function get_next_range(\DateTimeImmutable $time) {
$start = $time->getTimestamp();
$end = $time->add($this->periodicity())->getTimestamp();
return [
'start' => $start,
'end' => $end,
'time' => $start
];
}
/**
* Whether to cache or not the indicator calculations.
* @return bool
*/
public function cache_indicator_calculations(): bool {
return false;
}
/**
* Overriden as these time-splitting methods are based on future dates.
*
* @return bool
*/
public function valid_for_evaluation(): bool {
return false;
}
/**
* Get the start of the first time range.
*
* Overwriten to start generating predictions about upcoming stuff from time().
*
* @return int A timestamp.
*/
protected function get_first_start() {
global $DB;
$cache = \cache::make('core', 'modelfirstanalyses');
$key = $this->modelid . '_' . $this->analysable->get_id();
$firstanalysis = $cache->get($key);
if (!empty($firstanalysis)) {
return $firstanalysis;
}
// This analysable has not yet been analysed, the start is therefore now.
return time();
}
}