ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilForumAuthorInformation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 private string $author_name = '';
30 private string $author_short_name = '';
31 private string $linked_public_name = '';
32 private string $linked_short_name = '';
33 private string $suffix = '';
34 private string $profilePicture;
35 private ?ilObjUser $author = null;
36 private readonly ilLanguage $globalLng;
37 private readonly ilObjUser $globalUser;
38 private bool $is_deleted = false;
39
40 public function __construct(
41 private readonly int $author_id,
42 private readonly int $display_id,
43 private readonly string $alias,
44 private readonly string $import_name,
45 private readonly array $public_profile_link_attributes = [],
46 private readonly ?ilLanguage $lng = null
47 ) {
48 global $DIC;
49
50 $this->globalUser = $DIC->user();
51 $this->globalLng = $DIC->language();
52
53 $this->init();
54 }
55
56 protected function initUserInstance(): void
57 {
58 if ($this->display_id > 0) {
59 // Try to read user instance from preloaded cache array
60 $this->author = ilForumAuthorInformationCache::getUserObjectById($this->display_id);
61 if (!$this->author instanceof ilObjUser) {
62 // Get a user instance from forum module's cache method
63 $this->author = ilObjForumAccess::getCachedUserInstance($this->display_id);
64 }
65 }
66
67 if (!$this->author instanceof ilObjUser) {
68 $this->author = new ilObjUser();
69 $this->author->setId(0);
70 $this->author->setPref('public_profile', 'n');
71 $this->author->setGender('n');
72 }
73 }
74
75 protected function doesAuthorAccountExists(): bool
76 {
77 return $this->getAuthor() instanceof ilObjUser && $this->getAuthor()->getId();
78 }
79
80 protected function isAuthorAnonymous(): bool
81 {
82 return $this->doesAuthorAccountExists() && $this->getAuthor()->isAnonymous();
83 }
84
85 protected function isCurrentUserSessionLoggedIn(): bool
86 {
87 return !$this->globalUser->isAnonymous();
88 }
89
90 protected function buildAuthorProfileLink(bool $with_profile_link): void
91 {
92 $link = '';
93
94 if ($with_profile_link && $this->public_profile_link_attributes) {
95 $link = '<a';
96
97 foreach ($this->public_profile_link_attributes as $attr => $value) {
98 $link .= ' ' . $attr . '="' . $value . '"';
99 }
100
101 $link .= '>';
102 }
103
104 $linked_login = $link . $this->author_short_name;
105 $link .= $this->author_name;
106
107 if ($with_profile_link && $this->public_profile_link_attributes) {
108 $link .= '</a>';
109 $linked_login .= '</a>';
110 }
111
112 $this->linked_public_name = $link;
113 $this->linked_short_name = $linked_login;
114 }
115
116 protected function init(): void
117 {
118 $translationLanguage = $this->globalLng;
119 if ($this->lng instanceof ilLanguage) {
120 $translationLanguage = $this->lng;
121 }
122
123 $this->initUserInstance();
124
125 if ($this->doesAuthorAccountExists()) {
126 if (!$this->isAuthorAnonymous()
127 && (($this->isCurrentUserSessionLoggedIn()
128 && $this->getAuthor()->getPref('public_profile') === 'y')
129 || $this->getAuthor()->getPref('public_profile') === 'g')
130 ) {
131 // Author is NOT anonymous and (the current user session is logged in and the profile is public (y) or the profile is globally public (g))
132 $this->author_name = $this->getAuthor()->getPublicName();
133 $this->author_short_name = $this->getAuthor()->getLogin();
134
135 if ($this->getAuthor()->getPref('public_upload') === 'y') {
136 $this->profilePicture = $this->getUserImagePath($this->getAuthor());
137 } else {
138 $this->profilePicture = $this->getAvatarImageSource(
140 $this->getAuthor()->getFirstname(),
141 0,
142 1
143 ) . ilStr::subStr($this->getAuthor()->getLastname(), 0, 1),
144 $this->getAuthor()->getId()
145 );
146 }
147
148 if ($this->getAuthor()->getPref('public_gender') !== 'y') {
149 $this->getAuthor()->setGender('');
150 }
151
152 $this->buildAuthorProfileLink(true);
153 } else {
154 $this->getAuthor()->setGender('');
155 $this->author_short_name = $this->author_name = $this->getAuthor()->getLogin();
156 $this->buildAuthorProfileLink(false);
157 $this->profilePicture = $this->getAvatarImageSource(
158 $this->author_short_name,
159 $this->getAuthor()->getId()
160 );
161 }
162 } elseif ($this->display_id > 0 && $this->alias !== '') {
163 // The author did use a pseudonym and the account does not exist anymore (deleted, lost on import etc.)
164 $this->author_short_name = $this->author_name = $translationLanguage->txt('deleted');
165 $this->is_deleted = true;
166 $this->suffix = $translationLanguage->txt('deleted');
167 $this->buildAuthorProfileLink(false);
168 $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
169 } elseif ($this->import_name !== '') {
170 // We have no user instance,so we check the import name
171 $this->author_short_name = $this->author_name = $this->import_name . ' (' . $translationLanguage->txt('imported') . ')';
172 $this->suffix = $translationLanguage->txt('imported');
173 $this->buildAuthorProfileLink(false);
174 $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
175 } elseif ($this->alias !== '') {
176 // We have no import name,so we check the pseudonym
177 $this->author_short_name = $this->author_name = $this->alias . ' (' . $translationLanguage->txt('frm_pseudonym') . ')';
178 $this->suffix = $translationLanguage->txt('frm_pseudonym');
179 $this->buildAuthorProfileLink(false);
180 $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
181 } else {
182 // If we did not find a pseudonym, the author could not be determined
183 $this->author_short_name = $this->author_name = $translationLanguage->txt('forums_anonymous');
184 $this->buildAuthorProfileLink(false);
185 $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
186 }
187 }
188
189 protected function getUserImagePath(ilObjUser $user): string
190 {
191 if (!ilContext::hasHTML()) {
192 return '';
193 }
194
195 return $user->getPersonalPicturePath('xsmall');
196 }
197
198 protected function getAvatarImageSource(string $name, int $usrId = ANONYMOUS_USER_ID): string
199 {
200 global $DIC;
201
202 if (!ilContext::hasHTML()) {
203 return '';
204 }
205
207 $avatar = $DIC["user.avatar.factory"]->avatar('xsmall');
208 $avatar->setUsrId($usrId);
209 $avatar->setName(ilStr::subStr($name, 0, 2));
210
211 return $avatar->getUrl();
212 }
213
214 public function getProfilePicture(): string
215 {
217 }
218
219 public function getAuthor(): ilObjUser
220 {
221 return $this->author;
222 }
223
224 public function getAuthorName(bool $without_short_name = false): string
225 {
226 if (!$without_short_name) {
227 return $this->author_name;
228 }
229
230 return trim(preg_replace('/\‍(' . preg_quote($this->getAuthorShortName(), '/') . '\‍)/', '', $this->author_name));
231 }
232
233 public function getAuthorShortName(): string
234 {
236 }
237
238 public function getLinkedAuthorName(): string
239 {
241 }
242
243 public function getLinkedAuthorShortName(): string
244 {
246 }
247
248 public function hasSuffix(): bool
249 {
250 return $this->suffix !== '';
251 }
252
253 public function getSuffix(): string
254 {
255 return $this->suffix;
256 }
257
258 public function isDeleted(): bool
259 {
260 return $this->is_deleted;
261 }
262
263 public function getAlias(): string
264 {
265 return $this->alias;
266 }
267}
static hasHTML()
Has HTML output.
buildAuthorProfileLink(bool $with_profile_link)
getAuthorName(bool $without_short_name=false)
__construct(private readonly int $author_id, private readonly int $display_id, private readonly string $alias, private readonly string $import_name, private readonly array $public_profile_link_attributes=[], private readonly ?ilLanguage $lng=null)
language handling
static getCachedUserInstance(int $usr_id)
User class.
getPersonalPicturePath(string $a_size='small', bool $a_force_pic=false)
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:21
const ANONYMOUS_USER_ID
Definition: constants.php:27
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26