Skip to content

Instantly share code, notes, and snippets.

@ericmerrill
Created March 10, 2021 17:11
Show Gist options
  • Select an option

  • Save ericmerrill/6b45e018ba7b4d998ae57c693ac53106 to your computer and use it in GitHub Desktop.

Select an option

Save ericmerrill/6b45e018ba7b4d998ae57c693ac53106 to your computer and use it in GitHub Desktop.
Upcoming week standardized timesplit class (MDL-69413)
<?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 insights for the next week, but standardized in time.
*
* @package local_elis
* @copyright 2020 Oakland University
* @author Eric Merrill <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_elis\analytics\time_splitting;
defined('MOODLE_INTERNAL') || die();
/**
* Time splitting method that generates insights for the next week, but standardized in time.
*
* @package local_elis
* @copyright 2020 Oakland University
* @author Eric Merrill <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class upcoming_standard_week extends \core_analytics\local\time_splitting\upcoming_periodic {
private $dayofweek = 7;
/**
* The time splitting method name.
* @return \lang_string
*/
public static function get_name() : \lang_string {
return new \lang_string('timesplitting:upcomingstandardweek', 'local_elis');
}
/**
* Every two weeks.
* @return \DateInterval
*/
public function periodicity() {
return new \DateInterval('P1W');
}
/**
* 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) {
$time = $time->setTime(3, 0);
$dayofweek = $time->format('N');
// If the day of the date is Thurs or greated, move forward.
if ($dayofweek >= 4) {
$interval = 7 - $dayofweek;
$interval = new \DateInterval("P{$interval}D");
$time = $time->add($interval);
} else {
$interval = new \DateInterval("P{$dayofweek}D");
$time = $time->sub($interval);
}
$start = $time->getTimestamp();
$end = $time->add($this->periodicity())->setTime(11, 0)->getTimestamp();
$reftime = $time->setTime(0, 0)->getTimestamp();
return [
'start' => $start,
'end' => $end,
'time' => $reftime
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment