aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/UsersBookmarkedStructuralElementsIndex.php
blob: 618dc2e38e29d5a05909f3d1e61203222c192ead (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

namespace JsonApi\Routes\Courseware;

use Courseware\Bookmark;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
 * Displays the user's bookmarked structural elements.
 */
class UsersBookmarkedStructuralElementsIndex extends JsonApiController
{
    use CoursewareInstancesHelper;

    protected $allowedIncludePaths = [
        'ancestors',
        'containers',
        'containers.blocks',
        'containers.blocks.edit-blocker',
        'containers.blocks.editor',
        'containers.blocks.owner',
        'containers.blocks.user-data-field',
        'containers.blocks.user-progress',
        'course',
        'editor',
        'owner',
        'parent',
    ];

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

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        if (!($user = \User::find($args['id']))) {
            throw new RecordNotFoundException();
        }
        $actor = $this->getUser($request);
        if (!Authority::canIndexBookmarksOfAUser($actor, $user)) {
            throw new AuthorizationFailedException();
        }

        $resources = array_column(Bookmark::findUsersBookmarks($user), 'element');
        $total = count($resources);
        [$offset, $limit] = $this->getOffsetAndLimit();

        return $this->getPaginatedContentResponse(array_slice($resources, $offset, $limit), $total);
    }
}