aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Events/CourseEventsIndex.php
blob: 250d1442427bb1135bf96fb4f571b2c6c0a73942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

namespace JsonApi\Routes\Events;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\JsonApiController;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\Routes\Courses\Authority as CourseAuthority;

class CourseEventsIndex extends JsonApiController
{
    protected $allowedIncludePaths = ['owner'];

    protected $allowedPagingParameters = ['offset', 'limit'];

    public function __invoke(Request $request, Response $response, $args)
    {
        if (!$course = \Course::find($args['id'])) {
            throw new RecordNotFoundException();
        }

        $user = $this->getUser($request);

        if (!CourseAuthority::canShowCourse($user, $course, CourseAuthority::SCOPE_EXTENDED)) {
            throw new AuthorizationFailedException();
        }

        $all_dates = array_merge(
            $course->dates->getArrayCopy(),
            $course->ex_dates->getArrayCopy()
        );
        usort($all_dates, function ($date1, $date2) {
            return intval($date1->date) <=> intval($date2->date);
        });
        list($offset, $limit) = $this->getOffsetAndLimit();

        return $this->getPaginatedContentResponse(array_slice($all_dates, $offset, $limit), count($all_dates));
    }
}