Skip to content

Commit

Permalink
System: adjust the timetable calendar caching to only save necessary …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
SKuipers committed Sep 29, 2024
1 parent f321dbe commit 6b96ff0
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions modules/Timetable/moduleFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ function getCalendarEvents($connection2, $guid, $xml, $startDayStamp, $endDaySta
$settingGateway = $container->get(SettingGateway::class);
$ssoMicrosoft = $settingGateway->getSettingByScope('System Admin', 'ssoMicrosoft');
$ssoMicrosoft = json_decode($ssoMicrosoft, true);
$calendarEventsCache = 'calendarEvents-'.date('W', $startDayStamp).'-'.substr($xml, 0, 24);
$calendarEventsCache = 'calendarCache-'.date('W', $startDayStamp).'-'.substr($xml, 0, 24);

if ($session->has($calendarEventsCache)) {
return $session->get($calendarEventsCache);
}

if (!empty($ssoMicrosoft) && $ssoMicrosoft['enabled'] == 'Y' && $session->has('microsoftAPIAccessToken')) {
$eventsSchool = array();
Expand Down Expand Up @@ -222,19 +226,13 @@ function getCalendarEvents($connection2, $guid, $xml, $startDayStamp, $endDaySta

$getEventsUrl = '/me/calendarView?'.http_build_query($queryParams);

if ($session->has($calendarEventsCache)) {
$events = $session->get($calendarEventsCache);
} else {
$events = $graph->createRequest('GET', $getEventsUrl)
// Add the user's timezone to the Prefer header
->addHeaders(array(
'Prefer' => 'outlook.timezone="'."China Standard Time".'"'
))
->setReturnType(Event::class)
->execute();

$session->set($calendarEventsCache, $events);
}
$events = $graph->createRequest('GET', $getEventsUrl)
// Add the user's timezone to the Prefer header
->addHeaders(array(
'Prefer' => 'outlook.timezone="'."China Standard Time".'"'
))
->setReturnType(Event::class)
->execute();

foreach ($events as $event) {
$properties = $event->getProperties();
Expand Down Expand Up @@ -270,17 +268,11 @@ function getCalendarEvents($connection2, $guid, $xml, $startDayStamp, $endDaySta

$calendarListEntry = array();

if ($session->has($calendarEventsCache)) {
$calendarListEntry = $session->get($calendarEventsCache);
} else {
try {
$optParams = array('timeMin' => $start.'+00:00', 'timeMax' => $end.'+00:00', 'singleEvents' => true);
$calendarListEntry = $service->events->listEvents($xml, $optParams);
} catch (Exception $e) {
$getFail = true;
}

$session->set($calendarEventsCache, $calendarListEntry);
try {
$optParams = array('timeMin' => $start.'+00:00', 'timeMax' => $end.'+00:00', 'singleEvents' => true);
$calendarListEntry = $service->events->listEvents($xml, $optParams);
} catch (Exception $e) {
$getFail = true;
}

if ($getFail) {
Expand Down Expand Up @@ -350,6 +342,8 @@ function getCalendarEvents($connection2, $guid, $xml, $startDayStamp, $endDaySta
$eventsSchool = false;
}

$session->set($calendarEventsCache, $eventsSchool);

return $eventsSchool;
}

Expand Down

0 comments on commit 6b96ff0

Please sign in to comment.