ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
4 require_once 'Modules/Forum/classes/class.ilObjForumAccess.php';
5 require_once 'Modules/Forum/classes/class.ilForumAuthorInformationCache.php';
6 
14 {
18  protected $display_id;
19 
23  protected $alias;
24 
28  protected $import_name;
29 
34 
38  protected $author_name;
39 
43  protected $author_short_name;
44 
49 
53  protected $linked_short_name;
54 
58  protected $suffix = '';
59 
63  protected $profilePicture;
64 
68  protected $author;
69 
73  protected $files = array();
74 
78  protected $author_id;
79 
83  protected $lng;
84 
88  protected $globalLng;
89 
93  protected $globalUser;
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  include_once 'Modules/Forum/classes/class.ilObjForumAccess.php';
201 
202  $translationLanguage = $this->globalLng;
203  if ($this->lng instanceof \ilLanguage) {
204  $translationLanguage = $this->lng;
205  }
206 
207  $this->initUserInstance();
208 
209  if ($this->doesAuthorAccountExists()) {
210  if (!$this->isAuthorAnonymous()
211  && (($this->isCurrentUserSessionLoggedIn()
212  && $this->getAuthor()->getPref('public_profile') == 'y')
213  || $this->getAuthor()->getPref('public_profile') == 'g')
214  ) {
215  // 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))
216  $this->author_name = $this->getAuthor()->getPublicName();
217  $this->author_short_name = $this->getAuthor()->getLogin();
218 
219  if ($this->getAuthor()->getPref('public_upload') == 'y') {
220  $this->profilePicture = $this->getUserImagePath($this->getAuthor());
221  } else {
222  $this->profilePicture = $this->getAvatarImageSource(
223  ilStr::subStr($this->getAuthor()->getFirstname(), 0, 1) . ilStr::subStr($this->getAuthor()->getLastname(), 0, 1),
224  $this->getAuthor()->getId()
225  );
226  }
227 
228  if ($this->getAuthor()->getPref('public_gender') != 'y') {
229  $this->getAuthor()->setGender('');
230  }
231 
232  $this->buildAuthorProfileLink(true);
233  } else {
234  $this->getAuthor()->setGender('');
235  $this->author_short_name = $this->author_name = $this->getAuthor()->getLogin();
236  $this->buildAuthorProfileLink(false);
237  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
238  }
239  } elseif ($this->display_id > 0 && !$this->doesAuthorAccountExists() && strlen($this->alias)) {
240  // The author does not use a pseudonym, but the id does not exist anymore (deleted, lost on import etc.)
241  // We have no import name,so we check the pseudonym
242  $this->author_short_name = $this->author_name = $translationLanguage->txt('deleted');
243  $this->suffix = $translationLanguage->txt('deleted');
244  $this->buildAuthorProfileLink(false);
245  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
246  } elseif (strlen($this->import_name)) {
247  // We have no user instance,so we check the import name
248  $this->author_short_name = $this->author_name = $this->import_name . ' (' . $translationLanguage->txt('imported') . ')';
249  $this->suffix = $translationLanguage->txt('imported');
250  $this->buildAuthorProfileLink(false);
251  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
252  } elseif (strlen($this->alias)) {
253  // We have no import name,so we check the pseudonym
254  $this->author_short_name = $this->author_name = $this->alias . ' (' . $translationLanguage->txt('frm_pseudonym') . ')';
255  $this->suffix = $translationLanguage->txt('frm_pseudonym');
256  $this->buildAuthorProfileLink(false);
257  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
258  } else {
259  // If we did not find a pseudonym, the author could not be determined
260  $this->author_short_name = $this->author_name = $translationLanguage->txt('forums_anonymous');
261  $this->buildAuthorProfileLink(false);
262  $this->profilePicture = $this->getAvatarImageSource($this->author_short_name);
263  }
264  }
265 
270  protected function getUserImagePath(\ilObjUser $user)
271  {
272  if (!\ilContext::hasHTML()) {
273  return'';
274  }
275 
276  return $user->getPersonalPicturePath('xsmall');
277  }
278 
284  protected function getAvatarImageSource($name, $usrId = ANONYMOUS_USER_ID)
285  {
286  global $DIC;
287 
288  if (!\ilContext::hasHTML()) {
289  return'';
290  }
291 
293  $avatar = $DIC["user.avatar.factory"]->avatar('xsmall');
294  $avatar->setUsrId($usrId);
295  $avatar->setName(ilStr::subStr($name, 0, 2));
296 
297  return $avatar->getUrl();
298  }
299 
303  public function getProfilePicture()
304  {
305  return $this->profilePicture;
306  }
307 
311  public function getAuthor()
312  {
313  return $this->author;
314  }
315 
320  public function getAuthorName($without_short_name = false)
321  {
322  if (!$without_short_name) {
323  return $this->author_name;
324  } else {
325  return trim(preg_replace('/\(' . preg_quote($this->getAuthorShortName()) . '\)/', '', $this->author_name));
326  }
327  }
328 
332  public function getAuthorShortName()
333  {
335  }
336 
340  public function getLinkedAuthorName()
341  {
343  }
344 
348  public function getLinkedAuthorShortName()
349  {
351  }
352 
356  public function hasSuffix()
357  {
358  return strlen($this->suffix);
359  }
360 
364  public function getSuffix()
365  {
366  return $this->suffix;
367  }
368 }
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)
if($format !==null) $name
Definition: metadata.php:146
buildAuthorProfileLink($with_profile_link=false)
static hasHTML()
Has HTML output.
getPersonalPicturePath($a_size="small", $a_force_pic=false)
Get path to personal picture.
Create styles array
The data for the language used.
language handling