Select Git revision
ContentBoxHelper.php
Forked from
Stud.IP / Stud.IP
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
UnitsCopy.php 1.50 KiB
<?php
namespace JsonApi\Routes\Courseware;
use JsonApi\NonJsonApiController;
use Courseware\Unit;
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 unit into a course or users contents
*
* @author Ron Lucke <lucke@elan-ev.de>
* @license GPL2 or any later version
*
* @since Stud.IP 5.3
*/
class UnitsCopy extends NonJsonApiController
{
public function __invoke(Request $request, Response $response, array $args)
{
$data = $request->getParsedBody()['data'];
$sourceUnit = Unit::find($args['id']);
$user = $this->getUser($request);
$rangeId = $data['rangeId'];
$rangeType = $data['rangeType'];
$modified = $data['modified'];
try {
$range = \RangeFactory::createRange($rangeType, $rangeId);
} catch (\Exception $e) {
throw new RecordNotFoundException('Range could not be found');
}
if (!Authority::canCreateUnit($user, $range)) {
throw new AuthorizationFailedException();
}
$newUnit = $sourceUnit->copy($user, $rangeId, $rangeType, $modified);
$response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write((string) json_encode($newUnit));
return $response;
}
}