ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilPDSelectedItemsBlockViewSettings.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
25{
26 private static array $has_data = [self::SORT_MANUALLY];
27 private static array $has_options = [self::SORT_MANUALLY];
28 private static array $options_by_sortation = [
29 self::SORT_MANUALLY => ['top', 'bot'],
30 ];
31
32 protected Setting $settings;
35
36 public function __construct(
37 protected readonly ilObjUser $actor,
38 protected readonly int $view = self::VIEW_SELECTED_ITEMS,
39 ?Setting $settings = null,
40 protected readonly DashboardAccess $access = new DashboardAccess()
41 ) {
42 global $DIC;
43 $this->settings = $settings ?? $DIC->settings();
44 }
45
46 final public function getMembershipsView(): int
47 {
49 }
50
51 final public function getSelectedItemsView(): int
52 {
54 }
55
56 final public function getStudyProgrammeView(): int
57 {
59 }
60
61 final public function getLearningSequenceView(): int
62 {
64 }
65
66 final public function getRecommendedContentView(): int
67 {
69 }
70
71 final public function getListPresentationMode(): string
72 {
74 }
75
76 final public function getTilePresentationMode(): string
77 {
79 }
80
81 public function isMembershipsViewActive(): bool
82 {
83 return $this->view === $this->getMembershipsView();
84 }
85
86 final public function isRecommendedContentViewActive(): bool
87 {
88 return $this->view === self::VIEW_RECOMMENDED_CONTENT;
89 }
90
91 public function isSelectedItemsViewActive(): bool
92 {
93 return $this->view === $this->getSelectedItemsView();
94 }
95
96 public function isStudyProgrammeViewActive(): bool
97 {
98 return $this->view === $this->getStudyProgrammeView();
99 }
100
101 final public function isLearningSequenceViewActive(): bool
102 {
103 return $this->view === self::VIEW_LEARNING_SEQUENCES;
104 }
105
106 final public function getSortByStartDateMode(): string
107 {
109 }
110
111 final public function getSortByLocationMode(): string
112 {
114 }
115
116 final public function getSortByTypeMode(): string
117 {
118 return self::SORT_BY_TYPE;
119 }
120
121 final public function getSortByAlphabetMode(): string
122 {
124 }
125
129 final public function getAvailableSortOptionsByView(int $view): array
130 {
131 return self::AVAILABLE_SORT_OPTIONS_BY_VIEW[$view] ?? [];
132 }
133
134 public function getDefaultSortingByView(int $view): string
135 {
136 $sorting = $this->settings->get('pd_def_sort_view_' . $view, self::SORT_BY_LOCATION);
137 if (!in_array($sorting, $this->getAvailableSortOptionsByView($view), true)) {
138 return $this->getAvailableSortOptionsByView($view)[0];
139 }
140 return $sorting;
141 }
142
146 final public function getPresentationViews(): array
147 {
149 }
150
154 final public function getAvailablePresentationsByView(int $view): array
155 {
156 return self::AVAILABLE_PRESENTATION_BY_VIEW[$view];
157 }
158
159 public function storeViewSorting(int $view, string $type, array $active): void
160 {
161 if (!in_array($type, $active, true)) {
162 $active[] = $type;
163 }
164
165 assert(in_array($type, $this->getAvailableSortOptionsByView($view), true));
166
167 $this->settings->set('pd_def_sort_view_' . $view, $type);
168 $this->settings->set('pd_active_sort_view_' . $view, serialize($active));
169 }
170
171 public function storeViewSortingOptions(int $view, array $options): void
172 {
173 foreach ($options as $sorting => $option) {
174 if (in_array($sorting, $this->getAvailableSortOptionsByView($view), true)) {
175 $this->settings->set(
176 'pd_sort_options_' . $view . '_' . $sorting,
177 json_encode($option)
178 );
179 }
180 }
181 }
185 public function getActiveSortingsByView(int $view): array
186 {
187 $val = $this->settings->get('pd_active_sort_view_' . $view);
188 if ($val === '' || $val === null) {
189 $active_sortings = $this->getAvailableSortOptionsByView($view);
190 } else {
191 $active_sortings = unserialize($val, ['allowed_classes' => false]);
192 }
193 return array_filter(
194 $active_sortings,
195 fn(string $sorting): bool => in_array(
196 $sorting,
197 $this->getAvailableSortOptionsByView($view),
198 true
199 )
200 );
201 }
202
206 public function storeViewPresentation(int $view, string $default, array $active): void
207 {
208 if (!in_array($default, $active, true)) {
209 $active[] = $default;
210 }
211 $this->settings->set('pd_def_pres_view_' . $view, $default);
212 $this->settings->set('pd_active_pres_view_' . $view, serialize($active));
213 }
214
215 public function getDefaultPresentationByView(int $view): string
216 {
217 return $this->settings->get('pd_def_pres_view_' . $view, 'list');
218 }
219
223 public function getActivePresentationsByView(int $view): array
224 {
225 $val = $this->settings->get('pd_active_pres_view_' . $view, '');
226
227 return (!$val)
228 ? $this->getAvailablePresentationsByView($view)
229 : unserialize($val, ['allowed_classes' => false]);
230 }
231
235 public function setViewPositions(array $positions): void
236 {
237 $this->settings->set('pd_view_positions', serialize($positions));
238 }
239
243 public function getViewPositions(): array
244 {
245 $val = $this->settings->get('pd_view_positions', '');
246 return (!$val)
248 : unserialize($val, ['allowed_classes' => false]);
249 }
250
251 public function isViewEnabled(int $view): bool
252 {
253 switch ($view) {
254 case $this->getMembershipsView():
255 return $this->enabledMemberships();
256 case $this->getSelectedItemsView():
257 return $this->enabledSelectedItems();
258 case $this->getStudyProgrammeView():
259 return $this->enabledStudyProgrammes();
260 case $this->getRecommendedContentView():
261 return $this->enabledRecommendedContent();
262 case $this->getLearningSequenceView():
263 return $this->enabledLearningSequences();
264 default:
265 return false;
266 }
267 }
268
269 public function enableView(int $view, bool $status): void
270 {
271 switch ($view) {
272 case $this->getMembershipsView():
273 $this->enableMemberships($status);
274 break;
275 case $this->getSelectedItemsView():
276 $this->enableSelectedItems($status);
277 break;
278 case $this->getStudyProgrammeView():
279 $this->enableStudyProgrammes($status);
280 break;
281 case $this->getRecommendedContentView():
282 break;
283 case $this->getLearningSequenceView():
284 $this->enableLearningSequences($status);
285 break;
286 default:
287 throw new InvalidArgumentException('Unknown view: $view');
288 }
289 }
290
291 public function enabledMemberships(): bool
292 {
293 return (int) $this->settings->get('disable_my_memberships', '0') === 0;
294 }
295
296 public function enabledSelectedItems(): bool
297 {
298 return (int) $this->settings->get('disable_my_offers', '0') === 0;
299 }
300
301 public function enableMemberships(bool $status): void
302 {
303 $this->settings->set('disable_my_memberships', $status ? '0' : '1');
304 }
305
306 public function enableSelectedItems(bool $status): void
307 {
308 $this->settings->set('disable_my_offers', $status ? '0' : '1');
309 }
310
311 public function allViewsEnabled(): bool
312 {
313 return $this->enabledMemberships() && $this->enabledSelectedItems();
314 }
315
316 protected function allViewsDisabled(): bool
317 {
318 return !$this->enabledMemberships() && !$this->enabledSelectedItems();
319 }
320
321 public function getDefaultView(): int
322 {
323 return (int) $this->settings->get('personal_items_default_view', (string) $this->getSelectedItemsView());
324 }
325
326 public function storeDefaultView(int $view): void
327 {
328 $this->settings->set('personal_items_default_view', (string) $view);
329 }
330
331 public function parse(): void
332 {
333 $this->current_sort_option = $this->getEffectiveSortingMode();
334 $this->current_presentation_option = $this->getEffectivePresentationMode();
335 }
336
337 public function getEffectivePresentationMode(): string
338 {
339 $mode = $this->actor->getPref('pd_view_pres_' . $this->view);
340
341 if (!in_array($mode, $this->getSelectablePresentationModes(), true)) {
342 $mode = $this->getDefaultPresentationByView($this->view);
343 }
344
345 return $mode;
346 }
347
348 public function getEffectiveSortingMode(): string
349 {
350 $mode = $this->actor->getPref('pd_order_items_' . $this->view);
351
352 if (!in_array($mode, $this->getSelectableSortingModes(), true)) {
353 $mode = $this->getDefaultSortingByView($this->view);
354 }
355
356 return $mode;
357 }
358
362 public function getSelectableSortingModes(): array
363 {
364 return array_intersect(
365 $this->getActiveSortingsByView($this->view),
366 $this->getAvailableSortOptionsByView($this->view)
367 );
368 }
369
373 public function getSelectablePresentationModes(): array
374 {
375 if (!$this->access->canChangePresentation($this->actor->getId())) {
376 return [$this->getDefaultPresentationByView($this->view)];
377 }
378 return array_intersect(
379 $this->getActivePresentationsByView($this->view),
380 $this->getAvailablePresentationsByView($this->view)
381 );
382 }
383
384 public function storeActorPresentationMode(string $presentationMode): void
385 {
386 if (in_array($presentationMode, $this->getSelectablePresentationModes())) {
387 $this->actor->writePref(
388 'pd_view_pres_' . $this->view,
389 $presentationMode
390 );
391 }
392 }
393
394 public function storeActorSortingMode(string $sortingMode): void
395 {
396 if (in_array($sortingMode, $this->getSelectableSortingModes())) {
397 $this->actor->writePref(
398 'pd_order_items_' . $this->view,
399 $sortingMode
400 );
401 }
402 }
403
404 final public function getActor(): ilObjUser
405 {
406 return $this->actor;
407 }
408
409 final public function getView(): int
410 {
411 return $this->view;
412 }
413
414 final public function getCurrentSortOption(): string
415 {
417 }
418
419 final public function isValidView(int $view): bool
420 {
421 return in_array($view, self::AVAILABLE_VIEWS, true);
422 }
423
424 public function getDefaultSorting(): string
425 {
426 return $this->settings->get('dash_def_sort', $this->getSortByLocationMode());
427 }
428
429 public function isSortedByType(): bool
430 {
431 return $this->current_sort_option === $this->getSortByTypeMode();
432 }
433
434 public function isSortedByAlphabet(): bool
435 {
436 return $this->current_sort_option === $this->getSortByAlphabetMode();
437 }
438
439 public function isSortedByLocation(): bool
440 {
441 return $this->current_sort_option === $this->getSortByLocationMode();
442 }
443
444 public function isSortedByStartDate(): bool
445 {
446 return $this->current_sort_option === $this->getSortByStartDateMode();
447 }
448
449 public function isTilePresentation(): bool
450 {
451 return $this->current_presentation_option === $this->getTilePresentationMode();
452 }
453
454 public function isListPresentation(): bool
455 {
456 return $this->current_presentation_option === $this->getListPresentationMode();
457 }
458
459 final public function enabledRecommendedContent(): bool
460 {
461 return true;
462 }
463
464 public function enabledLearningSequences(): bool
465 {
466 return (int) $this->settings->get('disable_learning_sequences', '1') === 0;
467 }
468
469 public function enabledStudyProgrammes(): bool
470 {
471 return (int) $this->settings->get('disable_study_programmes', '1') === 0;
472 }
473
474 public function enableLearningSequences(bool $status): void
475 {
476 $this->settings->set('disable_learning_sequences', $status ? '0' : '1');
477 }
478
479 public function enableStudyProgrammes(bool $status): void
480 {
481 $this->settings->set('disable_study_programmes', $status ? '0' : '1');
482 }
483
484 final public function getViewName(int $view): string
485 {
486 return self::VIEW_NAMES[$view];
487 }
488
492 public function getEffectiveSortingData(): ?array
493 {
494 $mode = $this->getEffectiveSortingMode();
495 $key = 'pd_order_data_' . $this->getView() . '_' . $mode;
496 return in_array($mode, self::$has_data, true) ?
497 json_decode($this->actor->getPref($key) ?: '[]', true, 2) :
498 null;
499 }
500
501 public function getEffectiveSortingOptions(): array
502 {
503 return $this->getSortingOptionsByView($this->getView())[$this->getEffectiveSortingMode()] ?? [];
504 }
505
506 public function storeActorSortingData(array $data): void
507 {
508 $mode = $this->getEffectiveSortingMode();
509 if (in_array($mode, self::$has_options, true)) {
510 $this->actor->writePref('pd_order_data_' . $this->getView() . '_' . $mode, json_encode($data));
511 }
512 }
513
514 public function getAvailableOptionsBySortation(string $sortation): array
515 {
516 return self::$options_by_sortation[$sortation] ?? [];
517 }
518
522 public function getSortingOptionsByView(int $view): array
523 {
524 return array_merge(...array_map(fn($sorting) => [
525 $sorting => json_decode(
526 $this->settings->get('pd_sort_options_' . $view . '_' . $sorting) ?: '[]',
527 true,
528 2
529 ),
530 ], self::$has_options));
531 }
532}
User class.
storeViewSorting(int $view, string $type, array $active)
storeViewPresentation(int $view, string $default, array $active)
__construct(protected readonly ilObjUser $actor, protected readonly int $view=self::VIEW_SELECTED_ITEMS, ?Setting $settings=null, protected readonly DashboardAccess $access=new DashboardAccess())
global $DIC
Definition: shib_login.php:26