From 2d06e7577925ae285e95a18617f0cd05947681f6 Mon Sep 17 00:00:00 2001 From: Ivymaster Date: Tue, 28 Jul 2026 10:59:44 +0200 Subject: [PATCH] Exposed API route for fetching lvplan calendar events --- application/config/routes.php | 1 + .../api/frontend/v1/LvPlanCalendarEvents.php | 114 ++++++++++++++++++ tests/cypress/.gitignore | 5 +- 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 application/controllers/api/frontend/v1/LvPlanCalendarEvents.php diff --git a/application/config/routes.php b/application/config/routes.php index a22e94990..31cc7b027 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -120,6 +120,7 @@ $route['api/frontend/v1/stv/[sS]tudents/([WS]S[0-9]{4})/prestudent/(:num)'] = 'a $route['api/frontend/v1/stv/[sS]tudents/([WS]S[0-9]{4})/person/(:num)'] = 'api/frontend/v1/stv/Students/getPerson/$1/$2'; +$route['api/frontend/v1/lvplan/calendar-events'] = 'api/frontend/v1/LvPlanCalendarEvents/getCalendarData'; $route['lvplan/caldav'] = 'LvPlanCalDAV'; $route['lvplan/caldav/.*'] = 'LvPlanCalDAV'; diff --git a/application/controllers/api/frontend/v1/LvPlanCalendarEvents.php b/application/controllers/api/frontend/v1/LvPlanCalendarEvents.php new file mode 100644 index 000000000..acbfecaeb --- /dev/null +++ b/application/controllers/api/frontend/v1/LvPlanCalendarEvents.php @@ -0,0 +1,114 @@ + self::PERM_LOGGED + ]); + } + + /** + * Returns the authenticated user's mapped calendar events. + */ + public function getCalendarData() + { + $this->output->set_header('Cache-Control: no-store'); + + if ($this->input->method() !== 'get') + { + $this->output->set_header('Allow: GET'); + $this->terminateWithError( + 'Method not allowed', + self::ERROR_TYPE_GENERAL, + REST_Controller::HTTP_METHOD_NOT_ALLOWED + ); + } + + $startDate = $this->input->get('start_date', true); + $endDate = $this->input->get('end_date', true); + $validationErrors = array(); + + if (!is_null($startDate) && !$this->isValidDate($startDate)) + $validationErrors['start_date'] = 'The start_date field must use the YYYY-MM-DD format.'; + + if (!is_null($endDate) && !$this->isValidDate($endDate)) + $validationErrors['end_date'] = 'The end_date field must use the YYYY-MM-DD format.'; + + if (!empty($validationErrors)) + $this->terminateWithValidationErrors($validationErrors); + + $today = new DateTimeImmutable('today', new DateTimeZone('Europe/Vienna')); + + if (is_null($startDate)) + $startDate = $today->modify('-14 days')->format('Y-m-d'); + + if (is_null($endDate)) + $endDate = $today->modify('+6 months')->format('Y-m-d'); + + if ($startDate > $endDate) + { + $this->terminateWithValidationErrors(array( + 'end_date' => 'The end_date field must be on or after start_date.' + )); + } + + $userUID = getAuthUID(); + $this->load->model('ressource/Mitarbeiter_model', 'MitarbeiterModel'); + $this->load->library('KalenderLib', ['uid' => $userUID]); + + $isUserEmployeeResult = $this->MitarbeiterModel->isMitarbeiter($userUID); + $isUserEmployee = $this->getDataOrTerminateWithError($isUserEmployeeResult); + + if ($isUserEmployee) + { + $events = $this->kalenderlib->getPlanForLecturerByLecturer( + $startDate, + $endDate, + $userUID + ); + } + else + { + $events = $this->kalenderlib->getPlanForStudentByStudent( + $startDate, + $endDate, + $userUID + ); + } + + if (!is_array($events)) + $events = array(); + + $this->terminateWithSuccess($events); + } + + /** + * Checks whether a date uses the exact YYYY-MM-DD format. + * + * @param mixed $date + * @return bool + */ + private function isValidDate($date) + { + if (!is_string($date) || $date === '') + return false; + + $parsedDate = DateTimeImmutable::createFromFormat('!Y-m-d', $date); + $errors = DateTimeImmutable::getLastErrors(); + + return $parsedDate !== false + && ($errors === false + || ($errors['warning_count'] === 0 && $errors['error_count'] === 0)) + && $parsedDate->format('Y-m-d') === $date; + } +} diff --git a/tests/cypress/.gitignore b/tests/cypress/.gitignore index 48e0213b8..25e6d917c 100644 --- a/tests/cypress/.gitignore +++ b/tests/cypress/.gitignore @@ -1,4 +1,7 @@ node_modules .env .env.* -!.env.example \ No newline at end of file +!.env.example +/cypress/videos +/cypress/screenshots +/results \ No newline at end of file