Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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   * X parts time splitting method.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2017 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   * X parts time splitting method.
  31   *
  32   * @package   core_analytics
  33   * @copyright 2017 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 equal_parts extends base implements before_now {
  37  
  38      /**
  39       * Returns the number of parts the analyser duration should be split in.
  40       *
  41       * @return int
  42       */
  43      abstract protected function get_number_parts();
  44  
  45      /**
  46       * Splits the analysable duration in X equal parts from the start to the end.
  47       *
  48       * @return array
  49       */
  50      protected function define_ranges() {
  51  
  52          $nparts = $this->get_number_parts();
  53  
  54          $rangeduration = ($this->analysable->get_end() - $this->analysable->get_start()) / $nparts;
  55  
  56          if ($rangeduration < $nparts) {
  57              // It is interesting to avoid having a single timestamp belonging to multiple time ranges
  58              // because of things like community of inquiry indicators, where activities have a due date
  59              // that, ideally, would fall only into 1 time range. If the analysable duration is very short
  60              // it is because the model doesn't contain indicators that depend so heavily on time and therefore
  61              // we don't need to worry about timestamps being present in multiple time ranges.
  62              $allowmultipleranges = true;
  63          }
  64  
  65          $ranges = array();
  66          for ($i = 0; $i < $nparts; $i++) {
  67              $start = $this->analysable->get_start() + intval($rangeduration * $i);
  68              $end = $this->analysable->get_start() + intval($rangeduration * ($i + 1));
  69  
  70              if (empty($allowmultipleranges) && $i > 0 && $start === $ranges[$i - 1]['end']) {
  71                  // We add 1 second so each timestamp only belongs to 1 range.
  72                  $start = $start + 1;
  73              }
  74  
  75              if ($i === ($nparts - 1)) {
  76                  // Better to use the end for the last one as we are using floor above.
  77                  $end = $this->analysable->get_end();
  78              }
  79              $ranges[$i] = array(
  80                  'start' => $start,
  81                  'end' => $end,
  82                  'time' => $end
  83              );
  84          }
  85  
  86          return $ranges;
  87      }
  88  }