ILIAS  trunk Revision v11.0_alpha-2658-ge2404539063
Sequence.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 use ILIAS\Data\URI;
34 
35 class Sequence implements ISequence\Sequence
36 {
37  use ComponentHelper;
38 
39  private const PARAM_POSITION = 'p';
40  public const STORAGE_ID_PREFIX = self::class . '_';
41 
46  protected ?array $actions = null;
47  protected int $position = 0;
48  protected ?string $id = null;
49 
50  public function __construct(
51  protected DataFactory $data_factory,
52  protected Refinery $refinery,
53  protected Storage $storage,
54  protected ISequence\SegmentRetrieval $segment_retrieval
55  ) {
56  }
57 
58  public function getSegmentRetrieval(): ISequence\SegmentRetrieval
59  {
60  return $this->segment_retrieval;
61  }
62 
63  public function getCurrentPosition(): int
64  {
65  return $this->position;
66  }
67 
68  public function withCurrentPosition(int $position): static
69  {
70  $clone = clone $this;
71  $clone->position = $position;
72  return $clone;
73  }
74 
75  public function withViewControls(ViewControlContainer $viewcontrols): static
76  {
77  $clone = clone $this;
78  $clone->viewcontrols = $viewcontrols;
79  return $clone;
80  }
81 
83  {
84  return $this->viewcontrols;
85  }
86 
87  public function withActions(...$actions): static
88  {
89  $clone = clone $this;
90  $clone->actions = $actions;
91  return $clone;
92  }
93 
94  public function getActions(): ?array
95  {
96  return $this->actions;
97  }
98 
99  protected function checkRequest(): void
100  {
101  if (! $this->request) {
102  throw new \LogicException('no request was set on the sequence');
103  }
104  }
105 
106  public function withRequest(ServerRequestInterface $request): static
107  {
108  $clone = clone $this;
109  $clone->request = $request;
110  $clone->initFromRequest();
111  return $clone;
112  }
113 
115  {
116  $this->checkRequest();
117  return $this->request;
118  }
119 
120  protected function initFromRequest(): void
121  {
122  $base_uri = $this->data_factory->uri($this->request->getUri()->__toString());
123  $namespace = ['sequence_' . $this->getId() ?? ''];
124  $url_builder = new URLBuilder($base_uri);
125  list(
126  $this->url_builder,
127  $this->token_position
128  ) = $url_builder->acquireParameters(
129  $namespace,
130  self::PARAM_POSITION
131  );
132 
133  $query = new \ILIAS\HTTP\Wrapper\ArrayBasedRequestWrapper($this->request->getQueryParams());
134  $this->position = $query->retrieve(
135  $this->token_position->getName(),
136  $this->refinery->byTrying([
137  $this->refinery->kindlyTo()->int(),
138  $this->refinery->always(0)
139  ])
140  );
141 
142  if ($this->viewcontrols) {
143  $this->viewcontrols = $this->applyValuesToViewcontrols(
144  $this->viewcontrols,
145  $this->request
146  );
147  }
148  }
149 
150  public function getNext(int $direction): URI
151  {
152  $this->checkRequest();
153  return $this->url_builder
154  ->withParameter($this->token_position, (string) ($this->position + $direction))
155  ->buildURI();
156  }
157 
158  protected function getStorageData(): ?array
159  {
160  if (null !== ($storage_id = $this->getStorageId())) {
161  return $this->storage[$storage_id] ?? null;
162  }
163  return null;
164  }
165 
166  protected function setStorageData(array $data): void
167  {
168  if (null !== ($storage_id = $this->getStorageId())) {
169  $this->storage[$storage_id] = $data;
170  }
171  }
172 
173  protected function getStorageId(): ?string
174  {
175  if (null !== ($id = $this->getId())) {
176  return static::STORAGE_ID_PREFIX . $id;
177  }
178  return null;
179  }
180 
181  public function withId(string $id): static
182  {
183  $clone = clone $this;
184  $clone->id = $id;
185  return $clone;
186  }
187 
188  protected function getId(): ?string
189  {
190  return $this->id;
191  }
192 
193  protected function applyValuesToViewcontrols(
194  ViewControlContainer $view_controls,
195  ServerRequestInterface $request
197  $stored_values = new ArrayInputData($this->getStorageData() ?? []);
198  $view_controls = $view_controls
199  ->withStoredInput($stored_values)
200  ->withRequest($request);
201  $this->setStorageData($view_controls->getComponentInternalValues());
202  return $view_controls;
203  }
204 
205 }
if($err=$client->getError()) $namespace
withRequest(ServerRequestInterface $request)
Rendering the sequence must be done using the current request: it (the request) will carry parameters...
Definition: Sequence.php:106
applyValuesToViewcontrols(ViewControlContainer $view_controls, ServerRequestInterface $request)
Definition: Sequence.php:193
__construct(protected DataFactory $data_factory, protected Refinery $refinery, protected Storage $storage, protected ISequence\SegmentRetrieval $segment_retrieval)
Definition: Sequence.php:50
withViewControls(ViewControlContainer $viewcontrols)
You may add view controls to the sequences&#39;s player that alter the way, order or focus in which the s...
Definition: Sequence.php:75
acquireParameters(array $namespace, string ... $names)
Definition: URLBuilder.php:138
withId(string $id)
The Sequence comes with a storage to keep ViewControl-settings throughout requests.
Definition: Sequence.php:181
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Storage is simple key/value store without further schema definition.
Definition: Storage.php:29
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
URLBuilder.
Definition: URLBuilder.php:40