Skip to content
Snippets Groups Projects
Select Git revision
  • 844fc84f3e5764c8e3826dd0012815718c29b09f
  • main default protected
  • studip-rector
  • ci-opt
  • course-members-export-as-word
  • data-vue-app
  • pipeline-improvements
  • webpack-optimizations
  • rector
  • icon-renewal
  • http-client-and-factories
  • jsonapi-atomic-operations
  • vueify-messages
  • tic-2341
  • 135-translatable-study-areas
  • extensible-sorm-action-parameters
  • sorm-configuration-trait
  • jsonapi-mvv-routes
  • docblocks-for-magic-methods
19 results

ContainersCopy.php

Blame
  • Forked from Stud.IP / Stud.IP
    2671 commits behind the upstream repository.
    Ron Lucke's avatar
    Ron Lucke authored
    Closes #2448 and #2675
    
    Merge request studip/studip!1574
    844fc84f
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ContainersCopy.php 1.85 KiB
    <?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;
        }
    }