blob: eea3d5aa10df13d9cda4dca38afd159af774856f (
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 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 a courseware container in an courseware structural element
*
* @author Ron Lucke <lucke@elan-ev.de>
* @license GPL2 or any later version
*
* @since Stud.IP 5.0
*/
class ContainersCopy extends NonJsonApiController
{
public function __invoke(Request $request, Response $response, array $args)
{
$data = $request->getParsedBody()['data'];
$container = \Courseware\Container::find($data['container']['id']);
if (!$container) {
throw new RecordNotFoundException();
}
$element = \Courseware\StructuralElement::find($data['parent_id']);
if (!$element) {
throw new RecordNotFoundException();
}
$user = $this->getUser($request);
if (!Authority::canCreateContainer($user, $element) || !Authority::canUpdateContainer($user, $container)) {
throw new AuthorizationFailedException();
}
$new_container = $this->copyContainer($user, $container, $element);
$response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write((string) json_encode($new_container));
return $response;
}
private function copyContainer(\User $user, \Courseware\Container $remote_container, \Courseware\StructuralElement $element)
{
list($container, $blockMapObjs) = $remote_container->copy($user, $element);
return $container;
}
}
|