Skip to content

Commit

Permalink
MDL-41792 core_calendar: fixed issues when using multiple calendar types
Browse files Browse the repository at this point in the history
1) No longer assume that the end of the current month in Gregorian will be
the end of the month for the calendar type being used.
2) Need to take into account the hours and minutes a calendar type may vary
in order to generate timestamps.
3) When generating the previous and next month links start at the beginning
of the month, not the current day.
4) Need to convert to Gregorian when creating timestamps during calendar
exports.
  • Loading branch information
mdjnelson committed Oct 11, 2013
1 parent 1b7930f commit 1032966
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
30 changes: 20 additions & 10 deletions calendar/export_execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,20 @@
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
}
$timestart = make_timestamp($startyear, $startmonth, $startmonthday);
$gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);

$endmonthday = $startmonthday + $numberofdaysinweek;
$endmonth = $startmonth;
$endyear = $startyear;
if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
}
$timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
$gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
$timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);
break;
case 'weeknext':
$startweekday = calendar_get_starting_weekday();
Expand All @@ -110,23 +115,28 @@
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
}
$timestart = make_timestamp($startyear, $startmonth, $startmonthday);
$gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);

$endmonthday = $startmonthday + $numberofdaysinweek;
$endmonth = $startmonth;
$endyear = $startyear;
if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
}
$timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
$gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
$timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);
break;
case 'monthnow':
// Convert to gregorian.
$gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);

$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day']);
$timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'],
calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);
$timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS);
break;
case 'monthnext':
// Get the next month for this calendar.
Expand All @@ -136,9 +146,9 @@
$gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);

// Create the timestamps.
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day']);
$timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'],
calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);
$timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS);
break;
case 'recentupcoming':
//Events in the last 5 or next 60 days
Expand Down
16 changes: 9 additions & 7 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ function calendar_get_mini($courses, $groups, $users, $calmonth = false, $calyea

list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display.

// Get Gregorian date.
// Get Gregorian date for the start of the month.
$gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);

// Store the gregorian year and month to be used later.
// Store the gregorian date values to be used later.
list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);

Expand All @@ -249,7 +249,7 @@ function calendar_get_mini($courses, $groups, $users, $calmonth = false, $calyea

// These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
$display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
$display->tend = make_timestamp($gy, $gm, $display->maxdays, 23, 59, 59);
$display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;

// Align the starting weekday to fall in our display range
// This is simple, not foolproof.
Expand Down Expand Up @@ -860,12 +860,14 @@ function calendar_top_controls($type, $data) {
// We need to get the previous and next months in certain cases.
if ($type == 'frontpage' || $type == 'course' || $type == 'month') {
$prevmonth = calendar_sub_month($date['mon'], $date['year']);
$prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], $date['mday']);
$prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day']);
$prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
$prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
$prevmonthtime['hour'], $prevmonthtime['minute']);

$nextmonth = calendar_add_month($date['mon'], $date['year']);
$nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], $date['mday']);
$nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day']);
$nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
$nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
$nextmonthtime['hour'], $nextmonthtime['minute']);
}

switch ($type) {
Expand Down
17 changes: 9 additions & 8 deletions calendar/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,14 @@ public function fake_block_threemonths(calendar_information $calendar) {
$date = $calendartype->timestamp_to_date_array($calendar->time);

$prevmonth = calendar_sub_month($date['mon'], $date['year']);
$prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], $date['mday']);
$prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day']);
$prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
$prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
$prevmonthtime['hour'], $prevmonthtime['minute']);

$nextmonth = calendar_add_month($date['mon'], $date['year']);
$nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], $date['mday']);
$nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day']);
$nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
$nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
$nextmonthtime['hour'], $nextmonthtime['minute']);

$content = html_writer::start_tag('div', array('class' => 'minicalendarblock'));
$content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $prevmonthtime);
Expand Down Expand Up @@ -417,10 +419,9 @@ public function show_month_detailed(calendar_information $calendar, moodle_url $
$calendar->time = time();
}

// Get Gregorian date.
// Get Gregorian date for the start of the month.
$gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);

// Store the gregorian year and month to be used later.
// Store the gregorian date values to be used later.
list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);

Expand All @@ -437,7 +438,7 @@ public function show_month_detailed(calendar_information $calendar, moodle_url $

// These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
$display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
$display->tend = make_timestamp($gy, $gm, $display->maxdays, 23, 59, 59);
$display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;

// Align the starting weekday to fall in our display range
// This is simple, not foolproof.
Expand Down

0 comments on commit 1032966

Please sign in to comment.