aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/BlocksCopy.php
blob: d8bb1c5304f384e64a60f97f24f561e11cb08c17 (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
<?php

namespace JsonApi\Routes\Courseware;

use JsonApi\NonJsonApiController;
use Courseware\Block;
use Courseware\Container;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\BadRequestException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\Errors\UnprocessableEntityException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
 * Copy a courseware block in a courseware container
 *
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.0
 */

class BlocksCopy extends NonJsonApiController
{
    public function __invoke(Request $request, Response $response, array $args)
    {

        $data = $request->getParsedBody()['data'];

        $block = \Courseware\Block::find($data['block']['id']);
        if (!$block) {
            throw new RecordNotFoundException();
        }
        $container = \Courseware\Container::find($data['parent_id']);
        if (!$container) {
            throw new RecordNotFoundException();
        }
        $sectionIndex = $data['section'];
        $user = $this->getUser($request);

        if (!Authority::canCreateBlocks($user, $container) || !Authority::canUpdateBlock($user, $block)) {
            throw new AuthorizationFailedException();
        }

        $new_block = $this->copyBlock($user, $block, $container, $sectionIndex);

        $response = $response->withHeader('Content-Type', 'application/json');
        $response->getBody()->write((string) json_encode($new_block));

        return $response;
    }

    private function copyBlock(\User $user, \Courseware\Block $remote_block, \Courseware\Container $container, $sectionIndex)
    {
        return $remote_block->copy($user, $container, $sectionIndex);
    }
}