diff options
| author | Moritz Strohm <strohm@data-quest.de> | 2025-10-15 14:45:09 +0000 |
|---|---|---|
| committer | Moritz Strohm <strohm@data-quest.de> | 2025-10-15 14:45:09 +0000 |
| commit | 2fffc8a81a1f3c3665efac516aab93e823e6cd14 (patch) | |
| tree | 012bbaa4c868f701c105fc8cd4ee5a551c8cdf73 /lib | |
| parent | 894dd34eed77192d955c32783bc4d874c0716c2c (diff) | |
TIC 2832, closes #2832
Closes #2832
Merge request studip/studip!4453
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/classes/JsonApi/RouteMap.php | 2 | ||||
| -rw-r--r-- | lib/classes/JsonApi/Routes/Vacations/VacationsShow.php | 80 | ||||
| -rw-r--r-- | lib/models/SemesterHoliday.php | 6 |
3 files changed, 85 insertions, 3 deletions
diff --git a/lib/classes/JsonApi/RouteMap.php b/lib/classes/JsonApi/RouteMap.php index ca53a4a..40b535d 100644 --- a/lib/classes/JsonApi/RouteMap.php +++ b/lib/classes/JsonApi/RouteMap.php @@ -7,6 +7,7 @@ use JsonApi\Middlewares\Authentication; use JsonApi\Middlewares\DangerousRouteHandler; use JsonApi\Routes\Consultations\SlotCreationCount; use JsonApi\Routes\Holidays\HolidaysShow; +use JsonApi\Routes\Vacations\VacationsShow; use Slim\Routing\RouteCollectorProxy; /** @@ -154,6 +155,7 @@ class RouteMap \PluginEngine::sendMessage(JsonApiPlugin::class, 'registerUnauthenticatedRoutes', $group); $group->get('/holidays', HolidaysShow::class); + $group->get('/vacations', VacationsShow::class); $group->get('/semesters', Routes\SemestersIndex::class); $group->get('/semesters/{id}', Routes\SemestersShow::class)->setName('get-semester'); diff --git a/lib/classes/JsonApi/Routes/Vacations/VacationsShow.php b/lib/classes/JsonApi/Routes/Vacations/VacationsShow.php new file mode 100644 index 0000000..1e4fa1e --- /dev/null +++ b/lib/classes/JsonApi/Routes/Vacations/VacationsShow.php @@ -0,0 +1,80 @@ +<?php + +namespace JsonApi\Routes\Vacations; + +use JsonApi\NonJsonApiController; +use Psr\Container\ContainerInterface; +use JsonApi\JsonApiIntegration\QueryParserInterface; +use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ServerRequestInterface as Request; +use Neomerx\JsonApi\Schema\Error; +use Neomerx\JsonApi\Schema\ErrorCollection; +use Neomerx\JsonApi\Exceptions\JsonApiException; + +class VacationsShow extends NonJsonApiController +{ + protected $allowed_filtering_parameters = ['year', 'month']; + + public function __construct( + ContainerInterface $container, + private readonly QueryParserInterface $queryParser + ) { + parent::__construct($container); + } + + public function __invoke(Request $request, Response $response, array $args): Response + { + $errors = new ErrorCollection(); + + $filters = $this->queryParser->getFilteringParameters(); + if (isset($filters['month']) && empty($filters['year'])) { + $errors->add(new Error( + 'invalid-filter-value', + title: 'The month filter cannot be used without the year filter.' + )); + } + + if ($errors->count() > 0) { + throw new JsonApiException($errors, JsonApiException::HTTP_CODE_BAD_REQUEST); + } + + if (empty($filters['year'])) { + $filters['year'] = date('Y'); + } + + $start = new \DateTime(); + $start->setTime(0,0,0); + + // Calculate the time span: + if (!empty($filters['month'])) { + // For one month: + $start->setDate($filters['year'], $filters['month'], 1); + $end = clone $start; + $end = $end->add(new \DateInterval('P1M'))->sub(new \DateInterval('PT1S')); + } else { + // For a whole year: + $start->setDate($filters['year'], 1, 1); + $end = clone $start; + $end = $end->add(new \DateInterval('P1Y'))->sub(new \DateInterval('PT1S')); + } + + $vacation_objects = \SemesterHoliday::findByTimestampRange($start->getTimestamp(), $end->getTimestamp()); + + $vacations = []; + foreach ($vacation_objects as $vacation_object) { + $vacations[$vacation_object->id] = [ + 'id' => $vacation_object->id, + 'name' => $vacation_object->name, + 'semester_id' => $vacation_object->semester_id, + 'description' => $vacation_object->description, + 'start' => $vacation_object->beginn, + 'end' => $vacation_object->ende, + 'mkdate' => $vacation_object->mkdate, + 'chdate' => $vacation_object->chdate + ]; + } + + $response->getBody()->write(json_encode($vacations)); + return $response->withHeader('Content-Type', 'application/json'); + } +} diff --git a/lib/models/SemesterHoliday.php b/lib/models/SemesterHoliday.php index ab058e1..ce56e48 100644 --- a/lib/models/SemesterHoliday.php +++ b/lib/models/SemesterHoliday.php @@ -56,9 +56,9 @@ class SemesterHoliday extends SimpleORMap /** * returns all SemesterHoliday between given timestamps (starting AND ending within given timestamps) - * @param integer $timestamp_start - * @param integer $timestamp_end - * @return array of SemesterHoliday + * @param int $timestamp_start + * @param int $timestamp_end + * @return SemesterHoliday[] */ public static function findByTimestampRange($timestamp_start, $timestamp_end) { |
