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