From 206ee17a242dbb4a0fd1b69921b8ee8ad88b7fca Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 28 Dec 2020 10:38:42 +0100 Subject: [PATCH] refs #13 attempt to fix crash when invalid API response received Signed-off-by: Julien Veyssier --- lib/Service/MoodleAPIService.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Service/MoodleAPIService.php b/lib/Service/MoodleAPIService.php index ae4545d..40b322a 100644 --- a/lib/Service/MoodleAPIService.php +++ b/lib/Service/MoodleAPIService.php @@ -65,12 +65,18 @@ class MoodleAPIService { // get courses and set 'time' $courseIds = []; + $recents = []; foreach ($recentItems as $k => $recentItem) { + if (!is_array($recentItem) || !isset($recentItem['timeaccess'])) { + continue; + } if (isset($recentItem['courseid']) && !in_array($recentItem['courseid'], $courseIds)) { $courseIds[] = $recentItem['courseid']; } - $recentItems[$k]['time'] = $recentItem['timeaccess']; - $recentItems[$k]['type'] = 'recent'; + $recent = $recentItem; + $recent['time'] = $recentItem['timeaccess']; + $recent['type'] = 'recent'; + $recents[] = $recent; } // get upcoming events @@ -97,14 +103,14 @@ class MoodleAPIService { // filter by date if (!is_null($recentSince)) { - $recentItems = array_filter($recentItems, function($elem) use ($recentSince) { + $recents = array_filter($recents, function($elem) use ($recentSince) { $ts = intval($elem['time']); return $ts > $recentSince; }); } return [ - 'recents' => array_values($recentItems), + 'recents' => array_values($recents), 'events' => array_values($upcomingEvents) ]; }