ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPDSelectedItemsBlockViewSettings.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
25{
27 protected static array $availableViews = [
33 ];
35 protected static array $viewNames = [
36 self::VIEW_SELECTED_ITEMS => 'favourites',
37 self::VIEW_RECOMMENDED_CONTENT => 'recommended_content',
38 self::VIEW_MY_MEMBERSHIPS => 'memberships',
39 self::VIEW_LEARNING_SEQUENCES => 'learning_sequences',
40 self::VIEW_MY_STUDYPROGRAMME => 'study_programmes',
41 ];
43 protected static array $availablePresentations = [
46 ];
48 protected static array $availableSortOptions = [
53 ];
55 protected static array $availableSortOptionsByView = [
56 self::VIEW_SELECTED_ITEMS => [
61 ],
62 self::VIEW_RECOMMENDED_CONTENT => [
66 ],
67 self::VIEW_MY_MEMBERSHIPS => [
73 ],
74 self::VIEW_LEARNING_SEQUENCES => [
77 ],
78 self::VIEW_MY_STUDYPROGRAMME => [
81 ],
82 ];
84 protected static array $availablePresentationsByView = [
85 self::VIEW_SELECTED_ITEMS => [
88 ],
89 self::VIEW_RECOMMENDED_CONTENT => [
92 ],
93 self::VIEW_MY_MEMBERSHIPS => [
96 ],
97 self::VIEW_LEARNING_SEQUENCES => [
100 ],
101 self::VIEW_MY_STUDYPROGRAMME => [
104 ],
105
106 ];
107 private static array $has_data = [self::SORT_MANUALLY];
108 private static array $has_options = [self::SORT_MANUALLY];
109 private static array $options_by_sortation = [
110 self::SORT_MANUALLY => ['top', 'bot'],
111 ];
113 protected ilObjUser $actor;
114 protected array $validViews = [];
119
120 public function __construct(
122 int $view = self::VIEW_SELECTED_ITEMS,
123 ?Setting $settings = null,
125 ) {
126 global $DIC;
127
128 $this->settings = $settings ?? $DIC->settings();
129
130 $this->actor = $actor;
131 $this->currentView = $view;
132 $this->access = $access ?? new DashboardAccess();
133 }
134
135 public function getMembershipsView(): int
136 {
138 }
139
140 public function getSelectedItemsView(): int
141 {
143 }
144
145 public function getStudyProgrammeView(): int
146 {
148 }
149
150 public function getLearningSequenceView(): int
151 {
153 }
154
156 {
158 }
159
160 public function getListPresentationMode(): string
161 {
163 }
164
165 public function getTilePresentationMode(): string
166 {
168 }
169
170 public function isMembershipsViewActive(): bool
171 {
172 return $this->currentView === $this->getMembershipsView();
173 }
174
175 public function isRecommendedContentViewActive(): bool
176 {
177 return $this->currentView === self::VIEW_RECOMMENDED_CONTENT;
178 }
179
180 public function isSelectedItemsViewActive(): bool
181 {
182 return $this->currentView === $this->getSelectedItemsView();
183 }
184
185 public function isStudyProgrammeViewActive(): bool
186 {
187 return $this->currentView === $this->getStudyProgrammeView();
188 }
189
190 public function isLearningSequenceViewActive(): bool
191 {
192 return $this->currentView === self::VIEW_LEARNING_SEQUENCES;
193 }
194
195 public function getSortByStartDateMode(): string
196 {
198 }
199
200 public function getSortByLocationMode(): string
201 {
203 }
204
205 public function getSortByTypeMode(): string
206 {
207 return self::SORT_BY_TYPE;
208 }
209
210 public function getSortByAlphabetMode(): string
211 {
213 }
214
215 public function getAvailableSortOptionsByView(int $view): array
216 {
217 return self::$availableSortOptionsByView[$view] ?? [];
218 }
219
220 public function getDefaultSortingByView(int $view): string
221 {
222 $sorting = $this->settings->get('pd_def_sort_view_' . $view, self::SORT_BY_LOCATION);
223 if (!in_array($sorting, $this->getAvailableSortOptionsByView($view), true)) {
224 return $this->getAvailableSortOptionsByView($view)[0];
225 }
226 return $sorting;
227 }
228
232 public function getPresentationViews(): array
233 {
235 }
236
240 public function getAvailablePresentationsByView(int $view): array
241 {
242 return self::$availablePresentationsByView[$view];
243 }
244
245 public function storeViewSorting(int $view, string $type, array $active): void
246 {
247 if (!in_array($type, $active, true)) {
248 $active[] = $type;
249 }
250
251 assert(in_array($type, $this->getAvailableSortOptionsByView($view), true));
252
253 $this->settings->set('pd_def_sort_view_' . $view, $type);
254 $this->settings->set('pd_active_sort_view_' . $view, serialize($active));
255 }
256
257 public function storeViewSortingOptions(int $view, array $options): void
258 {
259 foreach ($options as $sorting => $option) {
260 if (in_array($sorting, $this->getAvailableSortOptionsByView($view), true)) {
261 $this->settings->set(
262 'pd_sort_options_' . $view . '_' . $sorting,
263 json_encode($option)
264 );
265 }
266 }
267 }
271 public function getActiveSortingsByView(int $view): array
272 {
273 $val = $this->settings->get('pd_active_sort_view_' . $view);
274 if ($val === '' || $val === null) {
275 $active_sortings = $this->getAvailableSortOptionsByView($view);
276 } else {
277 $active_sortings = unserialize($val, ['allowed_classes' => false]);
278 }
279 return array_filter(
280 $active_sortings,
281 fn(string $sorting): bool => in_array(
282 $sorting,
283 $this->getAvailableSortOptionsByView($view),
284 true
285 )
286 );
287 }
288
292 public function storeViewPresentation(int $view, string $default, array $active): void
293 {
294 if (!in_array($default, $active, true)) {
295 $active[] = $default;
296 }
297 $this->settings->set('pd_def_pres_view_' . $view, $default);
298 $this->settings->set('pd_active_pres_view_' . $view, serialize($active));
299 }
300
301 public function getDefaultPresentationByView(int $view): string
302 {
303 return $this->settings->get('pd_def_pres_view_' . $view, 'list');
304 }
305
309 public function getActivePresentationsByView(int $view): array
310 {
311 $val = $this->settings->get('pd_active_pres_view_' . $view, '');
312
313 return (!$val)
314 ? $this->getAvailablePresentationsByView($view)
315 : unserialize($val, ['allowed_classes' => false]);
316 }
317
321 public function setViewPositions(array $positions): void
322 {
323 $this->settings->set('pd_view_positions', serialize($positions));
324 }
325
329 public function getViewPositions(): array
330 {
331 $val = $this->settings->get('pd_view_positions', '');
332 return (!$val)
334 : unserialize($val, ['allowed_classes' => false]);
335 }
336
337 public function isViewEnabled(int $view): bool
338 {
339 switch ($view) {
340 case $this->getMembershipsView():
341 return $this->enabledMemberships();
342 case $this->getSelectedItemsView():
343 return $this->enabledSelectedItems();
344 case $this->getStudyProgrammeView():
345 return $this->enabledStudyProgrammes();
346 case $this->getRecommendedContentView():
347 return $this->enabledRecommendedContent();
348 case $this->getLearningSequenceView():
349 return $this->enabledLearningSequences();
350 default:
351 return false;
352 }
353 }
354
355 public function enableView(int $view, bool $status): void
356 {
357 switch ($view) {
358 case $this->getMembershipsView():
359 $this->enableMemberships($status);
360 break;
361 case $this->getSelectedItemsView():
362 $this->enableSelectedItems($status);
363 break;
364 case $this->getStudyProgrammeView():
365 $this->enableStudyProgrammes($status);
366 break;
367 case $this->getRecommendedContentView():
368 break;
369 case $this->getLearningSequenceView():
370 $this->enableLearningSequences($status);
371 break;
372 default:
373 throw new InvalidArgumentException('Unknown view: $view');
374 }
375 }
376
377 public function enabledMemberships(): bool
378 {
379 return (int) $this->settings->get('disable_my_memberships', '0') === 0;
380 }
381
382 public function enabledSelectedItems(): bool
383 {
384 return (int) $this->settings->get('disable_my_offers', '0') === 0;
385 }
386
387 public function enableMemberships(bool $status): void
388 {
389 $this->settings->set('disable_my_memberships', $status ? '0' : '1');
390 }
391
392 public function enableSelectedItems(bool $status): void
393 {
394 $this->settings->set('disable_my_offers', $status ? '0' : '1');
395 }
396
397 public function allViewsEnabled(): bool
398 {
399 return $this->enabledMemberships() && $this->enabledSelectedItems();
400 }
401
402 protected function allViewsDisabled(): bool
403 {
404 return !$this->enabledMemberships() && !$this->enabledSelectedItems();
405 }
406
407 public function getDefaultView(): int
408 {
409 return (int) $this->settings->get('personal_items_default_view', (string) $this->getSelectedItemsView());
410 }
411
412 public function storeDefaultView(int $view): void
413 {
414 $this->settings->set('personal_items_default_view', (string) $view);
415 }
416
417 public function parse(): void
418 {
419 $this->validViews = self::$availableViews;
420
421 $this->currentSortOption = $this->getEffectiveSortingMode();
422 $this->currentPresentationOption = $this->getEffectivePresentationMode();
423 }
424
425 public function getEffectivePresentationMode(): string
426 {
427 $mode = $this->actor->getPref('pd_view_pres_' . $this->currentView);
428
429 if (!in_array($mode, $this->getSelectablePresentationModes(), true)) {
430 $mode = $this->getDefaultPresentationByView($this->currentView);
431 }
432
433 return $mode;
434 }
435
436 public function getEffectiveSortingMode(): string
437 {
438 $mode = $this->actor->getPref('pd_order_items_' . $this->currentView);
439
440 if (!in_array($mode, $this->getSelectableSortingModes(), true)) {
441 $mode = $this->getDefaultSortingByView($this->currentView);
442 }
443
444 return $mode;
445 }
446
450 public function getSelectableSortingModes(): array
451 {
452 return array_intersect(
453 $this->getActiveSortingsByView($this->currentView),
454 $this->getAvailableSortOptionsByView($this->currentView)
455 );
456 }
457
461 public function getSelectablePresentationModes(): array
462 {
463 if (!$this->access->canChangePresentation($this->actor->getId())) {
464 return [$this->getDefaultPresentationByView($this->currentView)];
465 }
466 return array_intersect(
467 $this->getActivePresentationsByView($this->currentView),
468 $this->getAvailablePresentationsByView($this->currentView)
469 );
470 }
471
472 public function storeActorPresentationMode(string $presentationMode): void
473 {
474 if (in_array($presentationMode, $this->getSelectablePresentationModes())) {
475 $this->actor->writePref(
476 'pd_view_pres_' . $this->currentView,
477 $presentationMode
478 );
479 }
480 }
481
482 public function storeActorSortingMode(string $sortingMode): void
483 {
484 if (in_array($sortingMode, $this->getSelectableSortingModes())) {
485 $this->actor->writePref(
486 'pd_order_items_' . $this->currentView,
487 $sortingMode
488 );
489 }
490 }
491
492 public function getActor(): ilObjUser
493 {
494 return $this->actor;
495 }
496
497 public function getCurrentView(): int
498 {
499 return $this->currentView;
500 }
501
502 public function getCurrentSortOption(): string
503 {
505 }
506
507 public function isValidView(int $view): bool
508 {
509 return in_array($view, $this->validViews, true);
510 }
511
512 public function getDefaultSorting(): string
513 {
514 return $this->settings->get('dash_def_sort', $this->getSortByLocationMode());
515 }
516
517 public function isSortedByType(): bool
518 {
519 return $this->currentSortOption === $this->getSortByTypeMode();
520 }
521
522 public function isSortedByAlphabet(): bool
523 {
524 return $this->currentSortOption === $this->getSortByAlphabetMode();
525 }
526
527 public function isSortedByLocation(): bool
528 {
529 return $this->currentSortOption === $this->getSortByLocationMode();
530 }
531
532 public function isSortedByStartDate(): bool
533 {
534 return $this->currentSortOption === $this->getSortByStartDateMode();
535 }
536
537 public function isTilePresentation(): bool
538 {
539 return $this->currentPresentationOption === $this->getTilePresentationMode();
540 }
541
542 public function isListPresentation(): bool
543 {
544 return $this->currentPresentationOption === $this->getListPresentationMode();
545 }
546
547 public function enabledRecommendedContent(): bool
548 {
549 return true;
550 }
551
552 public function enabledLearningSequences(): bool
553 {
554 return (int) $this->settings->get('disable_learning_sequences', '1') === 0;
555 }
556
557 public function enabledStudyProgrammes(): bool
558 {
559 return (int) $this->settings->get('disable_study_programmes', '1') === 0;
560 }
561
562 public function enableLearningSequences(bool $status): void
563 {
564 $this->settings->set('disable_learning_sequences', $status ? '0' : '1');
565 }
566
567 public function enableStudyProgrammes(bool $status): void
568 {
569 $this->settings->set('disable_study_programmes', $status ? '0' : '1');
570 }
571
572 public function getViewName(int $view): string
573 {
574 return self::$viewNames[$view];
575 }
576
580 public function getEffectiveSortingData(): ?array
581 {
582 $mode = $this->getEffectiveSortingMode();
583 $key = 'pd_order_data_' . $this->getCurrentView() . '_' . $mode;
584 return in_array($mode, self::$has_data, true) ?
585 json_decode($this->actor->getPref($key) ?: '[]', true, 2) :
586 null;
587 }
588
589 public function getEffectiveSortingOptions(): array
590 {
591 return $this->getSortingOptionsByView($this->getCurrentView())[$this->getEffectiveSortingMode()] ?? [];
592 }
593
594 public function storeActorSortingData(array $data): void
595 {
596 $mode = $this->getEffectiveSortingMode();
597 if (in_array($mode, self::$has_options, true)) {
598 $this->actor->writePref('pd_order_data_' . $this->getCurrentView() . '_' . $mode, json_encode($data));
599 }
600 }
601
602 public function getAvailableOptionsBySortation(string $sortation): array
603 {
604 return self::$options_by_sortation[$sortation] ?? [];
605 }
606
610 public function getSortingOptionsByView(int $view): array
611 {
612 return array_merge(...array_map(fn($sorting) => [
613 $sorting => json_decode(
614 $this->settings->get('pd_sort_options_' . $view . '_' . $sorting) ?: '[]',
615 true,
616 2
617 ),
618 ], self::$has_options));
619 }
620}
User class.
storeViewSorting(int $view, string $type, array $active)
__construct(ilObjUser $actor, int $view=self::VIEW_SELECTED_ITEMS, ?Setting $settings=null, ?DashboardAccess $access=null)
storeViewPresentation(int $view, string $default, array $active)
global $DIC
Definition: shib_login.php:26