ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilUserAvatarResolver Class Reference

Class ilUserAvatarResolver. More...

+ Collaboration diagram for ilUserAvatarResolver:

Public Member Functions

 __construct (private int $user_id)
 
 hasProfilePicture ()
 
 getAvatar (bool $name_as_set_as_text_closely=false)
 
 getUserPictureForVCard ()
 
 getLegacyPictureURL ()
 This method returns the URL to the Profile Picture of a User. More...
 
 setForcePicture (bool $force_image)
 There are places where we want wo show the Profile Picture of a User, even if the user doesn't want to show it. More...
 
 setSize (string $size)
 There are the Sizes 'big', 'small', 'xsmall', 'xxsmall',. More...
 

Private Member Functions

 readUserSettings ()
 
 resolveLegacyPicturePath ()
 
 resolveProfilePicturePath ()
 

Private Attributes

ilDBInterface $db
 
ILIAS ResourceStorage Services $irss
 
Factory $ui
 
ilLanguage $lng
 
bool $letter_avatars_activated
 
ilUserProfilePictureDefinition $flavour_definition
 
string $size
 
bool $is_current_user
 
ilObjUser $for_user
 
bool $has_public_upload = false
 
bool $has_public_profile = false
 
ilUserAvatarFactory $avatar_factory
 
string $abbreviation = ''
 
string $rid = null
 
bool $force_image = false
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilUserAvatarResolver::__construct ( private int  $user_id)

Definition at line 46 of file class.ilUserAvatarResolver.php.

References $DIC, ILIAS\Repository\lng(), readUserSettings(), and ILIAS\Repository\ui().

48  {
49  global $DIC;
50  $this->is_current_user = $DIC->user()->getId() === $this->user_id;
51  $this->for_user = $this->is_current_user ? $DIC->user() : new ilObjUser($this->user_id);
52 
53  $this->db = $DIC->database();
54  $this->irss = $DIC->resourceStorage();
55  $this->ui = $DIC->ui()->factory();
56  $this->lng = $DIC->language();
57  $this->avatar_factory = $DIC["user.avatar.factory"];
58 
59  $this->letter_avatars_activated = (bool) $DIC->settings()->get('letter_avatars');
60  $this->flavour_definition = new ilUserProfilePictureDefinition();
61  $this->size = 'small';
62  $this->readUserSettings();
63  }
global $DIC
Definition: feed.php:28
+ Here is the call graph for this function:

Member Function Documentation

◆ getAvatar()

ilUserAvatarResolver::getAvatar ( bool  $name_as_set_as_text_closely = false)
Parameters
bool$name_as_set_as_text_closelyif the name is set as text close to the Avatar, the alternative text for screenreaders will be set differently, to reduce redundancy for screenreaders. See rules on the Avatar Symbol in the UI Components

Definition at line 142 of file class.ilUserAvatarResolver.php.

References ilUtil\getImagePath(), getLegacyPictureURL(), hasProfilePicture(), ILIAS\Repository\lng(), and ILIAS\Repository\ui().

142  : Avatar
143  {
144  if ($name_as_set_as_text_closely) {
145  $alternative_text = $this->lng->txt("user_avatar");
146  } elseif ($this->is_current_user && !$this->for_user->isAnonymous()) {
147  $alternative_text = $this->lng->txt("current_user_avatar");
148  } else {
149  $alternative_text = $this->lng->txt("user_avatar_of") . " " . $this->for_user->getLogin();
150  }
151 
152  if ($this->hasProfilePicture()) {
153  $picture = $this->getLegacyPictureURL();
154  return $this->ui->symbol()->avatar()->picture(
155  $picture,
156  $this->for_user->getLogin()
157  )->withLabel($alternative_text);
158  }
159 
160  // Fallback Image
161  if ($this->letter_avatars_activated === false) {
162  return $this->ui->symbol()->avatar()->picture(
163  \ilUtil::getImagePath('placeholder/no_photo_xsmall.jpg'),
164  $this->for_user->getLogin()
165  );
166  }
167 
168  return $this->ui->symbol()->avatar()->letter($this->abbreviation)->withLabel($alternative_text);
169  }
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
getLegacyPictureURL()
This method returns the URL to the Profile Picture of a User.
This describes how a letter or a picture avatar could be modified during construction of UI...
Definition: Avatar.php:28
+ Here is the call graph for this function:

◆ getLegacyPictureURL()

ilUserAvatarResolver::getLegacyPictureURL ( )

This method returns the URL to the Profile Picture of a User.

Depending on Settings and the Availability of a Prodile Picture, there's a Fallback to the Letter Avatar as well (as data-URL).

Deprecated:
use getAvatar() instead

Definition at line 215 of file class.ilUserAvatarResolver.php.

References hasProfilePicture(), resolveLegacyPicturePath(), resolveProfilePicturePath(), and ilWACSignedPath\signFile().

Referenced by getAvatar().

215  : string
216  {
217  if ($this->hasProfilePicture() && $this->rid !== '-') {
218  if ($this->rid !== null) {
219  return $this->resolveProfilePicturePath();
220  }
221 
222  // LEGACY
224  }
225 
226  // LETTER AVATAR
227  $avatar = $this->avatar_factory->avatar($this->size);
228  $avatar->setName($this->abbreviation);
229  $avatar->setUsrId($this->user_id);
230 
231  return $avatar->getUrl();
232  }
static signFile(string $path_to_file)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getUserPictureForVCard()

ilUserAvatarResolver::getUserPictureForVCard ( )

Definition at line 171 of file class.ilUserAvatarResolver.php.

References $size, ilObjMediaObject\getMimeType(), hasProfilePicture(), and resolveLegacyPicturePath().

171  : array
172  {
173  if (!$this->hasProfilePicture()
174  || $this->rid === '-') {
175  return [null, null];
176  }
177 
178  if ($this->rid !== null
179  && ($identification = $this->irss->manage()->find($this->rid)) !== null) {
180  $flavour_streams = $this->irss->flavours()
181  ->get($identification, $this->flavour_definition)
182  ->getStreamResolvers();
183  $available_sizes = array_flip(array_keys($this->flavour_definition->getSizes()));
184  $size_index = $available_sizes[$this->size];
185  if (!isset($flavour_streams[$size_index])) {
186  return [null, null];
187  }
188  return [$flavour_streams[$size_index]->getStream()->__toString(), 'image/jpeg'];
189  }
190 
191  $fh = fopen($this->resolveLegacyPicturePath(), 'rb');
192  if (!fh) {
193  return [null, null];
194  }
195 
196  $image = fread($fh, filesize($imagefile));
197  fclose($fh);
198  $mimetype = ilObjMediaObject::getMimeType($imagefile);
199 
200  $type = '';
201  if (0 === strpos($mimetype, 'image')) {
202  $type = $mimetype;
203  }
204 
205  return [$image, $type];
206  }
static getMimeType(string $a_file, bool $a_external=false)
get mime type for file
+ Here is the call graph for this function:

◆ hasProfilePicture()

ilUserAvatarResolver::hasProfilePicture ( )

Definition at line 98 of file class.ilUserAvatarResolver.php.

References resolveLegacyPicturePath().

Referenced by getAvatar(), getLegacyPictureURL(), and getUserPictureForVCard().

98  : bool
99  {
100  if (!$this->force_image) {
101  if (!$this->has_public_profile || !$this->has_public_upload) {
102  return false;
103  }
104  }
105 
106  // IRSS
107  if ($this->rid !== null && $this->irss->manage()->find($this->rid) !== null) {
108  return true;
109  }
110  // LEGACY
111  return is_file($this->resolveLegacyPicturePath());
112  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readUserSettings()

ilUserAvatarResolver::readUserSettings ( )
private

Definition at line 65 of file class.ilUserAvatarResolver.php.

References $res, and ilStr\subStr().

Referenced by __construct().

65  : void
66  {
67  $in = $this->db->in('usr_pref.keyword', ['public_upload', 'public_profile'], false, 'text');
68  $res = $this->db->queryF(
69  "
70  SELECT usr_data.rid, usr_pref.*
71  FROM usr_data LEFT JOIN usr_pref ON usr_pref.usr_id = usr_data.usr_id AND $in
72  WHERE usr_data.usr_id = %s",
73  ['integer'],
74  [$this->user_id]
75  );
76 
77  while ($row = $this->db->fetchAssoc($res)) { // MUST be loop
78  $this->rid = $row['rid'] ?? null;
79  switch ($row['keyword']) {
80  case 'public_upload':
81  $this->has_public_upload = $row['value'] === 'y';
82  break;
83  case 'public_profile':
84  $this->has_public_profile = ($row['value'] === 'y' || $row['value'] === 'g');
85  break;
86  }
87  }
88 
89  if ($this->has_public_profile) {
90  $sub_str_firstname = ilStr::subStr($this->for_user->getFirstname(), 0, 1);
91  $sub_str_lastname = ilStr::subStr($this->for_user->getLastname(), 0, 1);
92  $this->abbreviation = $sub_str_firstname . $sub_str_lastname;
93  } else {
94  $this->abbreviation = ilStr::subStr($this->for_user->getLogin(), 0, 2);
95  }
96  }
$res
Definition: ltiservices.php:69
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resolveLegacyPicturePath()

ilUserAvatarResolver::resolveLegacyPicturePath ( )
private

Definition at line 114 of file class.ilUserAvatarResolver.php.

References ilFileUtils\getWebspaceDir().

Referenced by getLegacyPictureURL(), getUserPictureForVCard(), and hasProfilePicture().

114  : string
115  {
116  // Legacy Uploaded file - can be removed in ILIAS 10
117  $webspace_dir = ('./' . ltrim(ilFileUtils::getWebspaceDir(), "./"));
118  $image_dir = $webspace_dir . '/usr_images';
119  return $image_dir . '/usr_' . $this->user_id . '.jpg';
120  }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resolveProfilePicturePath()

ilUserAvatarResolver::resolveProfilePicturePath ( )
private

Definition at line 122 of file class.ilUserAvatarResolver.php.

References $size.

Referenced by getLegacyPictureURL().

122  : string
123  {
124  $rid = $this->irss->manage()->find($this->rid);
125  if ($rid === null) {
126  return '';
127  }
128  $flavour = $this->irss->flavours()->get($rid, $this->flavour_definition);
129  $urls = $this->irss->consume()->flavourUrls($flavour)->getURLsAsArray(false);
130 
131  $available_sizes = array_flip(array_keys($this->flavour_definition->getSizes()));
132  $size_index = $available_sizes[$this->size];
133 
134  return $urls[$size_index] ?? '';
135  }
+ Here is the caller graph for this function:

◆ setForcePicture()

ilUserAvatarResolver::setForcePicture ( bool  $force_image)

There are places where we want wo show the Profile Picture of a User, even if the user doesn't want to show it.

(e.g. in Administration)

Definition at line 238 of file class.ilUserAvatarResolver.php.

References $force_image.

238  : void
239  {
240  $this->force_image = $force_image;
241  }

◆ setSize()

ilUserAvatarResolver::setSize ( string  $size)

There are the Sizes 'big', 'small', 'xsmall', 'xxsmall',.

See also
ilUserProfilePictureDefinition

Definition at line 246 of file class.ilUserAvatarResolver.php.

References $size.

246  : void
247  {
248  $this->size = $size;
249  }

Field Documentation

◆ $abbreviation

string ilUserAvatarResolver::$abbreviation = ''
private

Definition at line 42 of file class.ilUserAvatarResolver.php.

◆ $avatar_factory

ilUserAvatarFactory ilUserAvatarResolver::$avatar_factory
private

Definition at line 41 of file class.ilUserAvatarResolver.php.

◆ $db

ilDBInterface ilUserAvatarResolver::$db
private

Definition at line 30 of file class.ilUserAvatarResolver.php.

◆ $flavour_definition

ilUserProfilePictureDefinition ilUserAvatarResolver::$flavour_definition
private

Definition at line 35 of file class.ilUserAvatarResolver.php.

◆ $for_user

ilObjUser ilUserAvatarResolver::$for_user
private

Definition at line 38 of file class.ilUserAvatarResolver.php.

◆ $force_image

bool ilUserAvatarResolver::$force_image = false
private

Definition at line 44 of file class.ilUserAvatarResolver.php.

Referenced by setForcePicture().

◆ $has_public_profile

bool ilUserAvatarResolver::$has_public_profile = false
private

Definition at line 40 of file class.ilUserAvatarResolver.php.

◆ $has_public_upload

bool ilUserAvatarResolver::$has_public_upload = false
private

Definition at line 39 of file class.ilUserAvatarResolver.php.

◆ $irss

ILIAS ResourceStorage Services ilUserAvatarResolver::$irss
private

Definition at line 31 of file class.ilUserAvatarResolver.php.

◆ $is_current_user

bool ilUserAvatarResolver::$is_current_user
private

Definition at line 37 of file class.ilUserAvatarResolver.php.

◆ $letter_avatars_activated

bool ilUserAvatarResolver::$letter_avatars_activated
private

Definition at line 34 of file class.ilUserAvatarResolver.php.

◆ $lng

ilLanguage ilUserAvatarResolver::$lng
private

Definition at line 33 of file class.ilUserAvatarResolver.php.

◆ $rid

string ilUserAvatarResolver::$rid = null
private

Definition at line 43 of file class.ilUserAvatarResolver.php.

◆ $size

string ilUserAvatarResolver::$size
private

◆ $ui

Factory ilUserAvatarResolver::$ui
private

Definition at line 32 of file class.ilUserAvatarResolver.php.


The documentation for this class was generated from the following file: