ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilForumAuthorInformation.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
15  protected $display_id;
16 
20  protected $alias;
21 
25  protected $import_name;
26 
30  protected $public_profile_link_attributes = array();
31 
35  protected $author_name;
36 
40  protected $author_short_name;
41 
46 
50  protected $linked_short_name;
51 
55  protected $suffix = '';
56 
60  protected $profilePicture;
61 
65  protected $author;
66 
70  protected $files = array();
71 
75  protected $author_id;
76 
80  protected $lng;
81 
85  protected $globalLng;
86 
90  protected $globalUser;
91 
93  protected $is_deleted = false;
94 
104  {
105  global $DIC;
106 
107  $this->globalUser = $DIC->user();
108  $this->globalLng = $DIC->language();
109 
110  $this->author_id = $author_id;
111  $this->display_id = $display_id;
112  $this->alias = $alias;
113  $this->import_name = $import_name;
114  $this->public_profile_link_attributes = $public_profile_link_attributes;
115  $this->lng = $lng;
116 
117  $this->init();
118  }
119 
123  protected function initUserInstance()
124  {
125  if (is_numeric($this->display_id) && $this->display_id > 0) {
126  // Try to read user instance from preloaded cache array
127  $this->author = ilForumAuthorInformationCache::getUserObjectById($this->display_id);
128  if (!$this->author) {
129  // Get a user instance from forum module's cache method
130  $this->author = ilObjForumAccess::getCachedUserInstance($this->display_id);
131  }
132  }
133 
134  if (!$this->author) {
135  $this->author = new ilObjUser();
136  $this->author->setId(0);
137  $this->author->setPref('public_profile', 'n');
138  $this->author->setGender('');
139  }
140  }
141 
145  protected function doesAuthorAccountExists()
146  {
147  return $this->getAuthor() instanceof ilObjUser && $this->getAuthor()->getId();
148  }
149 
153  protected function isAuthorAnonymous()
154  {
155  return $this->doesAuthorAccountExists() && $this->getAuthor()->isAnonymous();
156  }
157 
161  protected function isCurrentUserSessionLoggedIn()
162  {
163  return !$this->globalUser->isAnonymous();
164  }
165 
169  protected function buildAuthorProfileLink($with_profile_link = false)
170  {
171  $link = '';
172 
173  if ($with_profile_link && $this->public_profile_link_attributes) {
174  $link = '<a';
175 
176  foreach ($this->public_profile_link_attributes as $attr => $value) {
177  $link .= ' ' . $attr . '="' . $value . '"';
178  }
179 
180  $link .= '>';
181  }
182 
183  $linked_login = $link . $this->author_short_name;
184  $link .= $this->author_name;
185 
186  if ($with_profile_link && $this->public_profile_link_attributes) {
187  $link .= '</a>';
188  $linked_login .= '</a>';
189  }
190 
191  $this->linked_public_name = $link;
192  $this->linked_short_name = $linked_login;
193  }
194 
198  protected function init()
199  {
200  $translationLanguage = $this->globalLng;
201  if ($this->lng instanceof \ilLanguage) {
202  $translationLanguage = $this->lng;
203  }
204 
205  $this->initUserInstance();
206 
207  if ($this->doesAuthorAccountExists()) {
208  if (!$this->isAuthorAnonymous()
209  && (($this->isCurrentUserSessionLoggedIn()
210  && $this->getAuthor()->getPref('public_profile') == 'y')
211  || $this->getAuthor()->getPref('public_profile') == 'g')
212  ) {
213  // 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))
214  $this->author_name = $this->getAuthor()->getPublicName();
215  $this->author_short_name = $this->getAuthor()->getLogin();
216 
217  if ($this->getAuthor()->getPref('public_upload') == 'y') {
218  $this->profilePicture = $this->getUserImagePath($this->getAuthor());
219  } else {
220  $this->profilePicture = $this->getAvatarImageSource(
221  ilStr::subStr($this->getAuthor()->getFirstname(), 0, 1) . ilStr::subStr($this->getAuthor()->getLastname(), 0, 1),
222  $this->getAuthor()->getId()
223  );
224  }
225 
226  if ($this->getAuthor()->getPref('public_gender') != 'y') {
227  $this->getAuthor()->setGender('');
228  }
229 
230  $this->buildAuthorProfileLink(true);
231  } else {
232  $this->getAuthor()->setGender('');
233  $this->author_short_name = $this->author_name = $this->getAuthor()->getLogin();
234  $this->buildAuthorProfileLink(false);
235  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name, $this->getAuthor()->getId());
236  }
237  } elseif ($this->display_id > 0 && !$this->doesAuthorAccountExists() && strlen($this->alias)) {
238  // The author does not use a pseudonym, but the id does not exist anymore (deleted, lost on import etc.)
239  // We have no import name,so we check the pseudonym
240  $this->author_short_name = $this->author_name = $translationLanguage->txt('deleted');
241  $this->is_deleted = true;
242  $this->suffix = $translationLanguage->txt('deleted');
243  $this->buildAuthorProfileLink(false);
244  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
245  } elseif (strlen($this->import_name)) {
246  // We have no user instance,so we check the import name
247  $this->author_short_name = $this->author_name = $this->import_name . ' (' . $translationLanguage->txt('imported') . ')';
248  $this->suffix = $translationLanguage->txt('imported');
249  $this->buildAuthorProfileLink(false);
250  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
251  } elseif (strlen($this->alias)) {
252  // We have no import name,so we check the pseudonym
253  $this->author_short_name = $this->author_name = $this->alias . ' (' . $translationLanguage->txt('frm_pseudonym') . ')';
254  $this->suffix = $translationLanguage->txt('frm_pseudonym');
255  $this->buildAuthorProfileLink(false);
256  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
257  } else {
258  // If we did not find a pseudonym, the author could not be determined
259  $this->author_short_name = $this->author_name = $translationLanguage->txt('forums_anonymous');
260  $this->buildAuthorProfileLink(false);
261  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
262  }
263  }
264 
269  protected function getUserImagePath(\ilObjUser $user)
270  {
271  if (!\ilContext::hasHTML()) {
272  return'';
273  }
274 
275  return $user->getPersonalPicturePath('xsmall');
276  }
277 
283  protected function getAvatarImageSource($name, $usrId = ANONYMOUS_USER_ID)
284  {
285  global $DIC;
286 
287  if (!\ilContext::hasHTML()) {
288  return'';
289  }
290 
292  $avatar = $DIC["user.avatar.factory"]->avatar('xsmall');
293  $avatar->setUsrId($usrId);
294  $avatar->setName(ilStr::subStr($name, 0, 2));
295 
296  return $avatar->getUrl();
297  }
298 
302  public function getProfilePicture()
303  {
304  return $this->profilePicture;
305  }
306 
310  public function getAuthor()
311  {
312  return $this->author;
313  }
314 
319  public function getAuthorName($without_short_name = false)
320  {
321  if (!$without_short_name) {
322  return $this->author_name;
323  } else {
324  return trim(preg_replace('/\(' . preg_quote($this->getAuthorShortName()) . '\)/', '', $this->author_name));
325  }
326  }
327 
331  public function getAuthorShortName()
332  {
334  }
335 
339  public function getLinkedAuthorName()
340  {
342  }
343 
347  public function getLinkedAuthorShortName()
348  {
350  }
351 
355  public function hasSuffix()
356  {
357  return strlen($this->suffix);
358  }
359 
363  public function getSuffix()
364  {
365  return $this->suffix;
366  }
367 
371  public function isDeleted()
372  {
373  return $this->is_deleted;
374  }
375 
379  public function getAlias()
380  {
381  return $this->alias;
382  }
383 }
static getCachedUserInstance($usr_id)
global $DIC
Definition: saml.php:7
getAuthorName($without_short_name=false)
static subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
__construct($author_id, $display_id, $alias, $import_name, array $public_profile_link_attributes=array(), \ilLanguage $lng=null)
buildAuthorProfileLink($with_profile_link=false)
static hasHTML()
Has HTML output.
$user
Definition: migrateto20.php:57
getPersonalPicturePath($a_size="small", $a_force_pic=false)
Get path to personal picture.
language handling