Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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 X days/weeks/months after the analysable start.
  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 X days/weeks/months after the analysable start.
  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 after_start extends \core_analytics\local\time_splitting\base implements before_now {
  37  
  38      /**
  39       * The period we should wait until we generate predictions for this.
  40       *
  41       * @param  \core_analytics\analysable $analysable
  42       * @return \DateInterval
  43       */
  44      abstract protected function wait_period(\core_analytics\analysable $analysable);
  45  
  46      /**
  47       * Returns whether the course can be processed by this time splitting method or not.
  48       *
  49       * @param \core_analytics\analysable $analysable
  50       * @return bool
  51       */
  52      public function is_valid_analysable(\core_analytics\analysable $analysable) {
  53  
  54          if (!$analysable->get_start()) {
  55              return false;
  56          }
  57  
  58          $predictionstart = $this->get_prediction_interval_start($analysable);
  59          if ($analysable->get_start() > $predictionstart) {
  60              // We still need to wait.
  61              return false;
  62          }
  63  
  64          return true;
  65      }
  66  
  67      /**
  68       * This time-splitting method returns one single range, the start to two days before the end.
  69       *
  70       * @return array The list of ranges, each of them including 'start', 'end' and 'time'
  71       */
  72      protected function define_ranges() {
  73  
  74          $now = time();
  75          $ranges = [
  76              [
  77                  'start' => $this->analysable->get_start(),
  78                  'end' => $now,
  79                  'time' => $now,
  80              ]
  81          ];
  82  
  83          return $ranges;
  84      }
  85  
  86      /**
  87       * Whether to cache or not the indicator calculations.
  88       *
  89       * @return bool
  90       */
  91      public function cache_indicator_calculations(): bool {
  92          return false;
  93      }
  94  
  95      /**
  96       * Calculates the interval start time backwards, from now.
  97       *
  98       * @param  \core_analytics\analysable $analysable
  99       * @return int
 100       */
 101      protected function get_prediction_interval_start(\core_analytics\analysable $analysable) {
 102  
 103          // The prediction time is always time(). We don't want to reuse the firstanalysis time
 104          // because otherwise samples (e.g. students) which start after the analysable (e.g. course)
 105          // start would use an incorrect analysis interval.
 106          $predictionstart = new \DateTime('now');
 107          $predictionstart->sub($this->wait_period($analysable));
 108  
 109          return $predictionstart->getTimestamp();
 110      }
 111  }