Skip to content
Snippets Groups Projects
Select Git revision
  • 00b4e32c6dcff8ca9b038c59b81e93b11737ac25
  • 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

BlocksByRangeIndex.php

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.
    BlocksByRangeIndex.php 1.99 KiB
    <?php
    namespace JsonApi\Routes\Consultations;
    
    use JsonApi\Errors\AuthorizationFailedException;
    use JsonApi\Errors\RecordNotFoundException;
    use JsonApi\JsonApiController;
    use JsonApi\Schemas\ConsultationBlock;
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    
    /**
     * Displays all consultation blocks of a range
     */
    class BlocksByRangeIndex extends JsonApiController
    {
        use FilterTrait;
    
        protected $allowedIncludePaths = [
            ConsultationBlock::REL_SLOTS,
            ConsultationBlock::REL_RANGE,
        ];
        protected $allowedPagingParameters = ['offset', 'limit'];
        protected $allowedFilteringParameters = ['current', 'expired'];
    
        public function __invoke(Request $request, Response $response, $args)
        {
            $this->validateFilters();
    
            $range_id = $args['id'];
            $range_type = substr($args['type'], 0, -1); // Strips trailing plural s
    
            $range = \RangeFactory::createRange($range_type, $range_id);
            if ($range->isNew()) {
                throw new RecordNotFoundException();
            }
    
            if (!Authority::canShowRange($this->getUser($request), $range)) {
                throw new AuthorizationFailedException();
            }
    
            [$offset, $limit] = $this->getOffsetAndLimit();
    
            $filters = $this->getFilters();
            $blocks = $this->getBlocks($range, $filters);
    
            return $this->getPaginatedContentResponse(
                $blocks->limit($offset, $limit)->getArrayCopy(),
                count($blocks)
            );
        }
    
        private function getBlocks(\Range $range, array $filters): \SimpleCollection
        {
            if (!$filters['current'] && !$filters['expired']) {
                return \SimpleCollection::createFromArray([]);
            }
    
            if ($filters['current'] && $filters['expired']) {
                return $range->consultation_blocks;
            }
    
            $blocks = \ConsultationBlock::findByRange($range, 'ORDER BY start', $filters['expired']);
            return \SimpleCollection::createFromArray($blocks);
        }
    }