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

namespace JsonApi\Routes\Courseware;

use JsonApi\NonJsonApiController;
use Courseware\StructuralElement;
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 an courseware structural element in an courseware structural element
 *
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.0
 */

class StructuralElementsCopy extends NonJsonApiController
{
    public function __invoke(Request $request, Response $response, array $args)
    {
        $data = $request->getParsedBody()['data'];

        $sourceElement = StructuralElement::find($args['id']);
        $newParent = StructuralElement::find($data['parent_id']);
        $user = $this->getUser($request);
        if (!Authority::canCreateStructuralElement($user, $newParent) || !Authority::canUpdateStructuralElement($user, $sourceElement)) {
            throw new AuthorizationFailedException();
        }

        if ($data['migrate']) {
            $newElement = $this->mergeElement($user, $sourceElement, $newParent);
        } else {
            $newElement = $this->copyElement($user, $sourceElement, $newParent);
        }
        if ($data['remove_purpose']) {
            $newElement->purpose = '';
        }
        if (!empty($data['modifications'])) {
            $newElement->title = $data['modifications']['title'] ?? $newElement->title;
            $newElement->payload['color'] = $data['modifications']['color'] ?? 'studip-blue';
            $newElement->payload['description'] = $data['modifications']['description'];
        }

        $newElement->store();

        return $this->redirectToStructuralElement($response, $newElement);
    }

    private function copyElement(\User $user, StructuralElement $sourceElement, StructuralElement $newParent)
    {
        $newElement = $sourceElement->copy($user, $newParent);

        return $newElement;
    }

    private function mergeElement(\User $user, StructuralElement $sourceElement, StructuralElement $targetElement)
    {
        $newElement = $sourceElement->merge($user, $targetElement);

        return $newElement;
    }

    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    private function redirectToStructuralElement(Response $response, StructuralElement $resource): Response
    {
        $pathinfo = $this->getSchema($resource)
            ->getSelfLink($resource)
            ->getStringRepresentation($this->container->get('json-api-integration-urlPrefix'));
        $old = \URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
        $url = \URLHelper::getURL($pathinfo, [], true);
        \URLHelper::setBaseURL($old);

        return $response->withHeader('Location', $url)->withStatus(303);
    }
}