Skip to content
Snippets Groups Projects
Select Git revision
  • 295da81dc659c62f11376debda146cf6211ae6c6
  • main default protected
  • step-01354-5.5
  • biest-504
  • issue-3215
  • step-03209-bald-rapunzel
  • tic-3123
  • tic-2720-remove-less-compilation-in-plugins
  • step-1800
  • tic-2532
  • biest-561
  • tic-3225
  • 5.3
  • 5.4
  • step-2472
  • step-2660
  • step-1559
  • tic-3094
  • biest-3206
  • biest-3207
  • 5.0
  • v5.3.1
  • v5.2.3
  • v5.1.4
  • v5.0.6
  • v5.3
  • v5.2.2
  • v5.1.3
  • v5.0.5
  • v5.2.1
  • v5.1.2
  • v5.0.4
  • v5.2
  • v5.1.1
  • v5.0.3
  • v5.1
  • v5.0.2
  • v5.0.1
  • v5.0
39 results

CoursewareContentOverviewElements.vue

Blame
  • 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.
    ContainersUpdate.php 2.75 KiB
    <?php
    
    namespace JsonApi\Routes\Courseware;
    
    use Courseware\Container;
    use JsonApi\Errors\AuthorizationFailedException;
    use JsonApi\Errors\RecordNotFoundException;
    use JsonApi\Errors\UnprocessableEntityException;
    use JsonApi\JsonApiController;
    use JsonApi\Routes\ValidationTrait;
    use JsonApi\Schemas\Courseware\Container as ContainerSchema;
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    
    /**
     * Update one Container.
     */
    class ContainersUpdate extends JsonApiController
    {
        use EditBlockAwareTrait;
        use ValidationTrait;
    
        /**
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
        public function __invoke(Request $request, Response $response, $args)
        {
            if (!($resource = Container::find($args['id']))) {
                throw new RecordNotFoundException();
            }
            $json = $this->validate($request, $resource);
            if (!Authority::canUpdateContainer($user = $this->getUser($request), $resource)) {
                throw new AuthorizationFailedException();
            }
            $resource = $this->updateContainer($user, $resource, $json);
    
            return $this->getContentResponse($resource);
        }
    
        /**
         * @SuppressWarnings(PHPMD.UnusedFormalParameters)
         */
        protected function validateResourceDocument($json, $data)
        {
            if (!self::arrayHas($json, 'data')) {
                return 'Missing `data` member at document´s top level.';
            }
    
            if (ContainerSchema::TYPE !== self::arrayGet($json, 'data.type')) {
                return 'Wrong `type` member of document´s `data`.';
            }
    
            if (!self::arrayHas($json, 'data.id')) {
                return 'Document must have an `id`.';
            }
    
            // TODO: Validate everything
        }
    
        private function updateContainer(\User $user, Container $resource, array $json): Container
        {
            return $this->updateLockedResource($user, $resource, function ($user, $resource) use ($json) {
                if ($payload = self::arrayGet($json, 'data.attributes.payload')) {
                    if (!$resource->type->validatePayload((object) $payload)) {
                        throw new UnprocessableEntityException('Invalid payload for this `container-type`.');
                    }
                    $resource->type->setPayload($payload);
                }
    
                if (self::arrayHas($json, 'data.relationships.structural-element.data.id')) {
                    $resource->structural_element_id = self::arrayGet(
                        $json,
                        'data.relationships.structural-element.data.id'
                    );
                }
    
                $resource->position = $json['data']['attributes']['position'];
    
                $resource->editor_id = $user->id;
                $resource->store();
    
                return $resource;
            });
        }
    }