ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilUserAvatarResolver.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
30{
32 private \ILIAS\ResourceStorage\Services $irss;
33 private Factory $ui;
34 private Language $lng;
37 private string $size;
38 private bool $is_current_user;
40 private bool $has_public_upload = false;
41 private bool $has_public_profile = false;
43 private string $abbreviation = '';
44 private ?string $rid = null;
45 private bool $force_image = false;
46
47 public function __construct(
48 private int $user_id
49 ) {
50 global $DIC;
51 $this->is_current_user = $DIC->user()->getId() === $this->user_id;
52 $this->for_user = $this->is_current_user ? $DIC->user() : new ilObjUser($this->user_id);
53
54 $this->db = $DIC->database();
55 $this->irss = $DIC->resourceStorage();
56 $this->ui = $DIC->ui()->factory();
57 $this->lng = $DIC->language();
58 $this->avatar_factory = $DIC["user.avatar.factory"];
59
60 $this->letter_avatars_activated = (bool) $DIC->settings()->get('letter_avatars');
61 $this->flavour_definition = new ilUserProfilePictureDefinition();
62 $this->size = 'small';
63 $this->readUserSettings();
64 }
65
66 private function readUserSettings(): void
67 {
68 $in = $this->db->in('usr_pref.keyword', ['public_upload', 'public_profile'], false, 'text');
69 $res = $this->db->queryF(
70 "
71 SELECT usr_data.rid, usr_pref.*
72 FROM usr_data LEFT JOIN usr_pref ON usr_pref.usr_id = usr_data.usr_id AND $in
73 WHERE usr_data.usr_id = %s",
74 ['integer'],
75 [$this->user_id]
76 );
77
78 while ($row = $this->db->fetchAssoc($res)) { // MUST be loop
79 $this->rid = $row['rid'] ?? null;
80 switch ($row['keyword']) {
81 case 'public_upload':
82 $this->has_public_upload = $row['value'] === 'y';
83 break;
84 case 'public_profile':
85 $this->has_public_profile = ($row['value'] === 'y' || $row['value'] === 'g');
86 break;
87 }
88 }
89
90 if ($this->has_public_profile) {
91 $sub_str_firstname = ilStr::subStr($this->for_user->getFirstname(), 0, 1);
92 $sub_str_lastname = ilStr::subStr($this->for_user->getLastname(), 0, 1);
93 $this->abbreviation = $sub_str_firstname . $sub_str_lastname;
94 } else {
95 $this->abbreviation = ilStr::subStr($this->for_user->getLogin(), 0, 2);
96 }
97 }
98
99 public function hasProfilePicture(): bool
100 {
101 if (!$this->force_image) {
102 if (!$this->has_public_profile || !$this->has_public_upload) {
103 return false;
104 }
105 }
106
107 if ($this->rid !== null && $this->irss->manage()->find($this->rid) !== null) {
108 return true;
109 }
110
111 return false;
112 }
113
114 private function resolveProfilePicturePath(): string
115 {
116 $rid = $this->irss->manage()->find($this->rid);
117 if ($rid === null) {
118 return '';
119 }
120 $flavour = $this->irss->flavours()->get($rid, $this->flavour_definition);
121 $urls = $this->irss->consume()->flavourUrls($flavour)->getURLsAsArray(false);
122
123 $available_sizes = array_flip(array_keys($this->flavour_definition->getSizes()));
124 $size_index = $available_sizes[$this->size];
125
126 return $urls[$size_index] ?? '';
127 }
128
134 public function getAvatar(bool $name_as_set_as_text_closely = false): Avatar
135 {
136 if ($name_as_set_as_text_closely) {
137 $alternative_text = $this->lng->txt("user_avatar");
138 } elseif ($this->is_current_user && !$this->for_user->isAnonymous()) {
139 $alternative_text = $this->lng->txt("current_user_avatar");
140 } else {
141 $alternative_text = $this->lng->txt("user_avatar_of") . " " . $this->for_user->getLogin();
142 }
143
144 if ($this->hasProfilePicture()) {
145 $picture = $this->getLegacyPictureURL();
146 return $this->ui->symbol()->avatar()->picture(
147 $picture,
148 $this->for_user->getLogin()
149 )->withLabel($alternative_text);
150 }
151
152 // Fallback Image
153 if ($this->letter_avatars_activated === false) {
154 return $this->ui->symbol()->avatar()->picture(
155 \ilUtil::getImagePath('placeholder/no_photo_xsmall.jpg'),
156 $this->for_user->getLogin()
157 );
158 }
159
160 return $this->ui->symbol()->avatar()->letter($this->abbreviation)->withLabel($alternative_text);
161 }
162
163 public function getUserPictureForVCard(): array
164 {
165 if (!$this->hasProfilePicture()) {
166 return [null, null];
167 }
168
169 if ($this->rid !== null
170 && $this->rid !== '-'
171 && ($identification = $this->irss->manage()->find($this->rid)) !== null) {
172 $flavour_streams = $this->irss->flavours()
173 ->get($identification, $this->flavour_definition)
174 ->getStreamResolvers();
175 $available_sizes = array_flip(array_keys($this->flavour_definition->getSizes()));
176 $size_index = $available_sizes[$this->size];
177 if (!isset($flavour_streams[$size_index])) {
178 return [null, null];
179 }
180 return [$flavour_streams[$size_index]->getStream()->__toString(), 'image/jpeg'];
181 }
182
183 return [null, null];
184 }
185
193 public function getLegacyPictureURL(): string
194 {
195 if ($this->hasProfilePicture()
196 && $this->rid !== null && $this->rid !== '-') {
197 return $this->resolveProfilePicturePath();
198 }
199
200 // LETTER AVATAR
201 $avatar = $this->avatar_factory->avatar($this->size);
202 $avatar->setName($this->abbreviation);
203 $avatar->setUsrId($this->user_id);
204
205 return $avatar->getUrl();
206 }
207
212 public function setForcePicture(bool $force_image): void
213 {
214 $this->force_image = $force_image;
215 }
216
220 public function setSize(string $size): void
221 {
222 $this->size = $size;
223 }
224}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
User class.
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:21
Class ilUserAvatarFactory.
Class ilUserAvatarResolver.
ilUserProfilePictureDefinition $flavour_definition
getAvatar(bool $name_as_set_as_text_closely=false)
ilUserAvatarFactory $avatar_factory
setSize(string $size)
There are the Sizes 'big', 'small', 'xsmall', 'xxsmall',.
__construct(private int $user_id)
ILIAS ResourceStorage Services $irss
getLegacyPictureURL()
This method returns the URL to the Profile Picture of a User.
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 t...
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
This describes how a letter or a picture avatar could be modified during construction of UI.
Definition: Avatar.php:29
This is how the factory for UI elements looks.
Definition: Factory.php:38
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26