ILIAS  trunk Revision v12.0_alpha-413-g215742c0453
UserContextResolver.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\News\Domain;
22
26
32{
33 public function __construct(
34 protected readonly \ilFavouritesDBRepository $favourites_repository,
35 protected readonly \ilAccessHandler $access,
36 protected readonly \ilTree $tree,
37 protected readonly NewsCache $cache
38 ) {
39 }
40
44 public function getAccessibleContexts(\ilObjUser $user, NewsCriteria $criteria): array
45 {
46 // 1. Try cache layer
47 $cached_contexts = $this->cache->getUserContextAccess($user->getId(), $criteria);
48 if ($cached_contexts !== null) {
49 return $cached_contexts;
50 }
51
52 // 2. Resolve contexts
53 $contexts = $this->resolveUserContexts($user, $criteria);
54
55 // 3. Cache the result
56 $this->cache->storeUserContextAccess($user->getId(), $criteria, $contexts);
57
58 return $contexts;
59 }
60
64 private function resolveUserContexts(\ilObjUser $user, NewsCriteria $criteria): array
65 {
66 $contexts = [];
67
68 // Get user's personal desktop items
69 if ($this->shouldIncludePersonalDesktop($user)) {
70 $contexts = array_merge($contexts, $this->getPersonalDesktopContexts($user));
71 }
72
73 // Get user's memberships
74 $contexts = array_merge($contexts, $this->getMembershipContexts($user));
75
76 // Remove duplicates and filter by access
77 return $this->filterContexts($user, $contexts, $criteria);
78 }
79
80 private function shouldIncludePersonalDesktop(\ilObjUser $user): bool
81 {
82 return $user->getPref('pd_items_news') !== 'n';
83 }
84
88 private function getPersonalDesktopContexts(\ilObjUser $user): array
89 {
90 $contexts = [];
91
92 foreach ($this->favourites_repository->getFavouritesOfUser($user->getId()) as $item) {
93 $contexts[] = new NewsContext($item['ref_id'], $item['obj_id'], $item['type']);
94 }
95 return $contexts;
96 }
97
101 private function getMembershipContexts(\ilObjUser $user): array
102 {
103 $contexts = [];
104 $memberships = \ilParticipants::_getMembershipByType($user->getId(), ['crs', 'grp']);
105
106 foreach ($memberships as $obj_id) {
107 $contexts = array_merge(
108 $contexts,
109 array_map(fn($ref_id) => new NewsContext($ref_id, $obj_id), \ilObject::_getAllReferences($obj_id)),
110 );
111 }
112 return $contexts;
113 }
114
121 private function filterContexts(\ilObjUser $user, array $contexts, NewsCriteria $criteria): array
122 {
123 $unique_contexts = [];
124
125 foreach ($contexts as $context) {
126 if (isset($unique_contexts[$context->getRefId()])) {
127 continue;
128 }
129
130 if (
131 !$criteria->isOnlyPublic() &&
132 !$this->access->checkAccessOfUser($user->getId(), 'read', '', $context->getRefId())
133 ) {
134 continue;
135 }
136
137 $unique_contexts[$context->getRefId()] = $context;
138 }
139
140 return array_values($unique_contexts);
141 }
142}
News Context DTO represents a context where news items can be associated with.
Definition: NewsContext.php:29
News Criteria DTO for querying news items supports caching, JSON serialization, and validation.
User Context Resolver resolves which contexts a user can access for news operations.
filterContexts(\ilObjUser $user, array $contexts, NewsCriteria $criteria)
Deduplicate and filter contexts by access.
getAccessibleContexts(\ilObjUser $user, NewsCriteria $criteria)
__construct(protected readonly \ilFavouritesDBRepository $favourites_repository, protected readonly \ilAccessHandler $access, protected readonly \ilTree $tree, protected readonly NewsCache $cache)
resolveUserContexts(\ilObjUser $user, NewsCriteria $criteria)
Multi-Level News Cache Implementation:
Definition: NewsCache.php:36
User class.
getPref(string $keyword)
static _getAllReferences(int $id)
get all reference ids for object ID
static _getMembershipByType(int $a_usr_id, array $a_type, bool $a_only_member_role=false)
get membership by type Get course or group membership
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
$ref_id
Definition: ltiauth.php:66
$context
Definition: webdav.php:31