ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilUserAvatarResolver.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
29 {
30  private ilDBInterface $db;
31  private \ILIAS\ResourceStorage\Services $irss;
32  private Factory $ui;
33  private ilLanguage $lng;
36  private string $size;
37  private bool $is_current_user;
39  private bool $has_public_upload = false;
40  private bool $has_public_profile = false;
42  private string $abbreviation = '';
43  private ?string $rid = null;
44  private bool $force_image = false;
45 
46  public function __construct(
47  private int $user_id
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  }
64 
65  private function readUserSettings(): 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  }
97 
98  public function hasProfilePicture(): 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  }
113 
114  private function resolveLegacyPicturePath(): 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  }
121 
122  private function resolveProfilePicturePath(): 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  }
136 
142  public function getAvatar(bool $name_as_set_as_text_closely = false): 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  }
170 
171  public function getUserPictureForVCard(): 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  }
207 
215  public function getLegacyPictureURL(): 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  }
233 
238  public function setForcePicture(bool $force_image): void
239  {
240  $this->force_image = $force_image;
241  }
242 
246  public function setSize(string $size): void
247  {
248  $this->size = $size;
249  }
250 }
Class ilUserAvatarFactory.
static getWebspaceDir(string $mode="filesystem")
get webspace directory
$res
Definition: ltiservices.php:69
ilUserAvatarFactory $avatar_factory
setSize(string $size)
There are the Sizes &#39;big&#39;, &#39;small&#39;, &#39;xsmall&#39;, &#39;xxsmall&#39;,.
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
global $DIC
Definition: feed.php:28
getLegacyPictureURL()
This method returns the URL to the Profile Picture of a User.
static getMimeType(string $a_file, bool $a_external=false)
get mime type for file
This describes how a letter or a picture avatar could be modified during construction of UI...
Definition: Avatar.php:28
__construct(private int $user_id)
ilUserProfilePictureDefinition $flavour_definition
Class ilUserAvatarResolver.
ILIAS ResourceStorage Services $irss
static signFile(string $path_to_file)
getAvatar(bool $name_as_set_as_text_closely=false)
setForcePicture(bool $force_image)
There are places where we want wo show the Profile Picture of a User, even if the user doesn&#39;t want t...