aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/BlocksCreate.php
blob: d1d3afbe105dc6de984a224168f908948960d6c9 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

namespace JsonApi\Routes\Courseware;

use Courseware\Container;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\UnprocessableEntityException;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\Courseware\Block as BlockSchema;
use JsonApi\Schemas\Courseware\Container as ContainerSchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Studip\Activity\Activity;
use Studip\Activity\CoursewareProvider;

/**
 * Create a block in a container.
 */
class BlocksCreate extends JsonApiController
{
    use ValidationTrait;

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        $json = $this->validate($request);
        $container = $this->getContainerFromJson($json);
        if (!Authority::canCreateBlocks($user = $this->getUser($request), $container)) {
            throw new AuthorizationFailedException();
        }
        $block = $this->createBlock($user, $json, $container);

        return $this->getCreatedResponse($block);
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    protected function validateResourceDocument($json, $data)
    {
        if (!self::arrayHas($json, 'data')) {
            return 'Missing `data` member at document“s top level.';
        }

        if (BlockSchema::TYPE !== self::arrayGet($json, 'data.type')) {
            return 'Wrong `type` member of document“s `data`.';
        }

        if (self::arrayHas($json, 'data.id')) {
            return 'New document must not have an `id`.';
        }

        if (!self::arrayHas($json, 'data.attributes.block-type')) {
            return 'Missing `block-type` attribute.';
        }

        $blockType = self::arrayGet($json, 'data.attributes.block-type');
        if (!$this->validateBlockType($blockType)) {
            return 'Invalid `block-type` attribute.';
        }

        if (!self::arrayHas($json, 'data.relationships.container')) {
            return 'Missing `container` relationship.';
        }

        if (!$this->getContainerFromJson($json)) {
            return 'Invalid `container` relationship.';
        }
    }

    private function validateBlockType(string $blockType)
    {
        return \Courseware\BlockTypes\BlockType::isBlockType($blockType);
    }

    private function getContainerFromJson($json)
    {
        if (!$this->validateResourceObject($json, 'data.relationships.container', ContainerSchema::TYPE)) {
            return null;
        }

        $containerId = self::arrayGet($json, 'data.relationships.container.data.id');

        return \Courseware\Container::find($containerId);
    }

    private function createBlock(\User $user, array $json, \Courseware\Container $container)
    {
        $get = function ($key, $default = '') use ($json) {
            return self::arrayGet($json, $key, $default);
        };

        $block = \Courseware\Block::build([
            'container_id'    => $container->id,
            'owner_id'        => $user->id,
            'editor_id'       => $user->id,
            'edit_blocker_id' => $user->id,
            'position'        => $container->countBlocks(),
            'block_type'      => $get('data.attributes.block-type'),
            'payload'         => '',
            'visible'         => 1,
            'commentable'     => 0
        ]);

        $payload = $get('data.attributes.payload');
        if ($payload) {
            if (!$block->type->validatePayload($payload)) {
                throw new UnprocessableEntityException('Invalid payload for this `block-type`.');
            }
            $block->type->setPayload($payload);
        }

        $block->store();

        return $block;
    }
}