MDL-59212 analytics: Abstract equal parts and accum time splittings

Part of MDL-57791 epic.
This commit is contained in:
David Monllao
2017-07-24 08:37:00 +02:00
parent f67bd6af70
commit fbc18acda0
6 changed files with 174 additions and 91 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 {
/**
* 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 = intval(floor(($this->analysable->get_end() - $this->analysable->get_start()) / $nparts));
$ranges = array();
for ($i = 0; $i < $nparts; $i++) {
$end = $this->analysable->get_start() + ($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;
}
}
@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class deciles extends base {
class deciles extends equal_parts {
/**
* get_name
@@ -45,27 +45,11 @@ class deciles extends base {
}
/**
* define_ranges
* 10 parts.
*
* @return array
* @return int
*/
protected function define_ranges() {
$rangeduration = floor(($this->analysable->get_end() - $this->analysable->get_start()) / 10);
$ranges = array();
for ($i = 0; $i < 10; $i++) {
$start = $this->analysable->get_start() + ($rangeduration * $i);
$end = $this->analysable->get_start() + ($rangeduration * ($i + 1));
if ($i === 9) {
$end = $this->analysable->get_end();
}
$ranges[] = array(
'start' => $start,
'end' => $end,
'time' => $end
);
}
return $ranges;
protected function get_number_parts() {
return 10;
}
}
@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class deciles_accum extends base {
class deciles_accum extends accumulative_parts {
/**
* get_name
@@ -45,26 +45,11 @@ class deciles_accum extends base {
}
/**
* define_ranges
* 10 parts.
*
* @return array
* @return int
*/
protected function define_ranges() {
$rangeduration = floor(($this->analysable->get_end() - $this->analysable->get_start()) / 10);
$ranges = array();
for ($i = 0; $i < 10; $i++) {
$end = $this->analysable->get_start() + ($rangeduration * ($i + 1));
if ($i === 9) {
$end = $this->analysable->get_end();
}
$ranges[] = array(
'start' => $this->analysable->get_start(),
'end' => $end,
'time' => $end
);
}
return $ranges;
protected function get_number_parts() {
return 10;
}
}
@@ -0,0 +1,80 @@
<?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 {
/**
* 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 = intval(floor(($this->analysable->get_end() - $this->analysable->get_start()) / $nparts));
$ranges = array();
for ($i = 0; $i < $nparts; $i++) {
$start = $this->analysable->get_start() + ($rangeduration * $i);
$end = $this->analysable->get_start() + ($rangeduration * ($i + 1));
// Check the end of the previous time range.
if ($i > 0 && $start === $ranges[$i - 1]['end']) {
// We deduct 1 second from the previous end so each timestamp only belongs to 1 range.
$ranges[$i - 1]['end'] = $ranges[$i - 1]['end'] - 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;
}
}
@@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 4 quarters time splitting method.
* Quarters time splitting method.
*
* @package core_analytics
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
@@ -27,13 +27,13 @@ namespace core_analytics\local\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* 4 quarters time splitting method.
* Quarters 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
*/
class quarters extends base {
class quarters extends equal_parts {
/**
* get_name
@@ -45,30 +45,11 @@ class quarters extends base {
}
/**
* define_ranges
* 4 parts
*
* @return array
* @return int
*/
protected function define_ranges() {
$duration = floor(($this->analysable->get_end() - $this->analysable->get_start()) / 4);
return [
[
'start' => $this->analysable->get_start(),
'end' => $this->analysable->get_start() + $duration,
'time' => $this->analysable->get_start() + $duration
], [
'start' => $this->analysable->get_start() + $duration,
'end' => $this->analysable->get_start() + ($duration * 2),
'time' => $this->analysable->get_start() + ($duration * 2)
], [
'start' => $this->analysable->get_start() + ($duration * 2),
'end' => $this->analysable->get_start() + ($duration * 3),
'time' => $this->analysable->get_start() + ($duration * 3)
], [
'start' => $this->analysable->get_start() + ($duration * 3),
'end' => $this->analysable->get_end(),
'time' => $this->analysable->get_end()
]
];
protected function get_number_parts() {
return 4;
}
}
@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quarters_accum extends base {
class quarters_accum extends accumulative_parts {
/**
* get_name
@@ -45,30 +45,11 @@ class quarters_accum extends base {
}
/**
* define_ranges
* 4 parts.
*
* @return array
* @return int
*/
protected function define_ranges() {
$duration = floor(($this->analysable->get_end() - $this->analysable->get_start()) / 4);
return [
[
'start' => $this->analysable->get_start(),
'end' => $this->analysable->get_start() + $duration,
'time' => $this->analysable->get_start() + $duration
], [
'start' => $this->analysable->get_start(),
'end' => $this->analysable->get_start() + ($duration * 2),
'time' => $this->analysable->get_start() + ($duration * 2)
], [
'start' => $this->analysable->get_start(),
'end' => $this->analysable->get_start() + ($duration * 3),
'time' => $this->analysable->get_start() + ($duration * 3)
], [
'start' => $this->analysable->get_start(),
'end' => $this->analysable->get_end(),
'time' => $this->analysable->get_end()
]
];
protected function get_number_parts() {
return 4;
}
}