Skip to content

Commit

Permalink
MDL-75928 format_week: improve get_section_dates one week calc
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze committed Sep 22, 2023
1 parent 8e931f2 commit 3b6acb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 17 additions & 6 deletions course/format/weeks/lib.php
Expand Up @@ -406,14 +406,25 @@ public function get_section_dates($section, $startdate = false) {
} else {
$sectionnum = $section;
}
$oneweekseconds = 604800;
// Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight
// savings and the date changes.
$startdate = $startdate + 7200;

// Create a DateTime object for the start date.
$startdateobj = new DateTime("@$startdate");

// Calculate the interval for one week.
$oneweekinterval = new DateInterval('P7D');

// Calculate the interval for the specified number of sections.
for ($i = 1; $i < $sectionnum; $i++) {
$startdateobj->add($oneweekinterval);
}

// Calculate the end date.
$enddateobj = clone $startdateobj;
$enddateobj->add($oneweekinterval);

$dates = new stdClass();
$dates->start = $startdate + ($oneweekseconds * ($sectionnum - 1));
$dates->end = $dates->start + $oneweekseconds;
$dates->start = $startdateobj->getTimestamp();
$dates->end = $enddateobj->getTimestamp();

return $dates;
}
Expand Down
4 changes: 2 additions & 2 deletions course/format/weeks/tests/format_weeks_test.php
Expand Up @@ -219,8 +219,8 @@ public function test_default_course_enddate() {
$courseform = new \testable_course_edit_form(null, $args);
$courseform->definition_after_data();

// format_weeks::get_section_dates is adding 2h to avoid DST problems, we need to replicate it here.
$enddate = $params['startdate'] + (WEEKSECS * $params['numsections']) + 7200;
// Calculate the expected end date.
$enddate = $params['startdate'] + (WEEKSECS * $params['numsections']);

$weeksformat = course_get_format($course->id);
$this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form()));
Expand Down

0 comments on commit 3b6acb9

Please sign in to comment.