ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 did use a pseudonym and the account does not exist anymore (deleted, lost on import etc.)
239  $this->author_short_name = $this->author_name = $translationLanguage->txt('deleted');
240  $this->is_deleted = true;
241  $this->suffix = $translationLanguage->txt('deleted');
242  $this->buildAuthorProfileLink(false);
243  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
244  } elseif (strlen($this->import_name)) {
245  // We have no user instance,so we check the import name
246  $this->author_short_name = $this->author_name = $this->import_name . ' (' . $translationLanguage->txt('imported') . ')';
247  $this->suffix = $translationLanguage->txt('imported');
248  $this->buildAuthorProfileLink(false);
249  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
250  } elseif (strlen($this->alias)) {
251  // We have no import name,so we check the pseudonym
252  $this->author_short_name = $this->author_name = $this->alias . ' (' . $translationLanguage->txt('frm_pseudonym') . ')';
253  $this->suffix = $translationLanguage->txt('frm_pseudonym');
254  $this->buildAuthorProfileLink(false);
255  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
256  } else {
257  // If we did not find a pseudonym, the author could not be determined
258  $this->author_short_name = $this->author_name = $translationLanguage->txt('forums_anonymous');
259  $this->buildAuthorProfileLink(false);
260  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
261  }
262  }
263 
268  protected function getUserImagePath(\ilObjUser $user)
269  {
270  if (!\ilContext::hasHTML()) {
271  return'';
272  }
273 
274  return $user->getPersonalPicturePath('xsmall');
275  }
276 
282  protected function getAvatarImageSource($name, $usrId = ANONYMOUS_USER_ID)
283  {
284  global $DIC;
285 
286  if (!\ilContext::hasHTML()) {
287  return'';
288  }
289 
291  $avatar = $DIC["user.avatar.factory"]->avatar('xsmall');
292  $avatar->setUsrId($usrId);
293  $avatar->setName(ilStr::subStr($name, 0, 2));
294 
295  return $avatar->getUrl();
296  }
297 
301  public function getProfilePicture()
302  {
303  return $this->profilePicture;
304  }
305 
309  public function getAuthor()
310  {
311  return $this->author;
312  }
313 
318  public function getAuthorName($without_short_name = false)
319  {
320  if (!$without_short_name) {
321  return $this->author_name;
322  } else {
323  return trim(preg_replace('/\(' . preg_quote($this->getAuthorShortName()) . '\)/', '', $this->author_name));
324  }
325  }
326 
330  public function getAuthorShortName()
331  {
333  }
334 
338  public function getLinkedAuthorName()
339  {
341  }
342 
346  public function getLinkedAuthorShortName()
347  {
349  }
350 
354  public function hasSuffix()
355  {
356  return strlen($this->suffix);
357  }
358 
362  public function getSuffix()
363  {
364  return $this->suffix;
365  }
366 
370  public function isDeleted()
371  {
372  return $this->is_deleted;
373  }
374 
378  public function getAlias()
379  {
380  return $this->alias;
381  }
382 }
static getCachedUserInstance($usr_id)
const ANONYMOUS_USER_ID
Definition: constants.php:25
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)
if($format !==null) $name
Definition: metadata.php:230
buildAuthorProfileLink($with_profile_link=false)
static hasHTML()
Has HTML output.
global $DIC
Definition: goto.php:24
getPersonalPicturePath($a_size="small", $a_force_pic=false)
Get path to personal picture.