ILIAS  release_8 Revision v8.24
class.ilUserAvatarResolver.php
Go to the documentation of this file.
1<?php
2
21
27{
28 private int $user_id = 0;
29 private string $login = "";
30 private string $firstname = "";
31 private string $lastname = "";
32 private bool $has_public_profile = false;
33 private bool $has_public_upload = false;
34 private string $uploaded_file = "";
35 private string $abbreviation = "";
36 private bool $force_image = false;
37 private string $size = 'small';
38 protected ilObjUser $user;
39 protected ilLanguage $lng;
40 protected ilDBInterface $db;
41 protected Factory $ui;
43
44 public function __construct(int $user_id)
45 {
46 global $DIC;
47
48 $this->db = $DIC->database();
49 $this->ui = $DIC->ui()->factory();
50 $this->lng = $DIC->language();
51 $this->user = $DIC->user();
52 $this->user_id = $user_id;
53 $this->letter_avatars_activated = (bool) $DIC->settings()->get('letter_avatars');
54 $this->init();
55 }
56
57 private function init(): void
58 {
59 $in = $this->db->in('usr_pref.keyword', array('public_upload', 'public_profile'), false, 'text');
60 $res = $this->db->queryF(
61 "
62 SELECT usr_pref.*, ud.login, ud.firstname, ud.lastname
63 FROM usr_data ud LEFT JOIN usr_pref ON usr_pref.usr_id = ud.usr_id AND $in
64 WHERE ud.usr_id = %s",
65 array('integer'),
66 array($this->user_id)
67 );
68
69 while ($row = $this->db->fetchAssoc($res)) { // MUST be loop
70 $this->login = $row['login'] ?? '';
71 $this->firstname = $row['firstname'] ?? '';
72 $this->lastname = $row['lastname'] ?? '';
73
74 switch ($row['keyword']) {
75 case 'public_upload':
76 $this->has_public_upload = $row['value'] === 'y';
77 break;
78 case 'public_profile':
79 $this->has_public_profile = ($row['value'] === 'y' || $row['value'] === 'g');
80 break;
81 }
82 }
83
84 // Uploaded file
85 $webspace_dir = '';
86 if (defined('ILIAS_MODULE')) {
87 $webspace_dir = ('.' . $webspace_dir);
88 }
89 $webspace_dir .= ('./' . ltrim(ilFileUtils::getWebspaceDir(), "./"));
90
91 $image_dir = $webspace_dir . '/usr_images';
92 $this->uploaded_file = $image_dir . '/usr_' . $this->user_id . '.jpg';
93
94 if ($this->has_public_profile) {
95 $this->abbreviation = ilStr::subStr($this->firstname, 0, 1) . ilStr::subStr($this->lastname, 0, 1);
96 } else {
97 $this->abbreviation = ilStr::subStr($this->login, 0, 2);
98 }
99 }
100
101 private function useUploadedFile(): bool
102 {
103 return (($this->has_public_upload && $this->has_public_profile) || $this->force_image) && is_file($this->uploaded_file);
104 }
105
111 public function getAvatar(bool $name_as_set_as_text_closely = false): Avatar
112 {
113 if ($name_as_set_as_text_closely) {
114 $alternative_text = $this->lng->txt("user_avatar");
115 } elseif ($this->user_id == $this->user->getId() && !$this->user::_isAnonymous($this->user_id)) {
116 $alternative_text = $this->lng->txt("current_user_avatar");
117 } else {
118 $alternative_text = $this->lng->txt("user_avatar_of") . " " . $this->login;
119 }
120
121 if ($this->useUploadedFile()) {
122 $picture = ilWACSignedPath::signFile($this->uploaded_file);
123 return $this->ui->symbol()->avatar()->picture($picture, $this->login)
124 ->withLabel($alternative_text);
125 }
126
127 if ($this->letter_avatars_activated === false) {
128 return $this->ui->symbol()->avatar()->picture(
129 \ilUtil::getImagePath('no_photo_xsmall.jpg'),
130 ilObjUser::_lookupLogin($this->user_id)
131 );
132 }
133
134 return $this->ui->symbol()->avatar()->letter($this->abbreviation)->withLabel($alternative_text);
135 }
136
137 public function getLegacyPictureURL(): string
138 {
139 global $DIC;
140 if ($this->useUploadedFile()) {
141 return $this->uploaded_file . '?t=' . random_int(1, 99999);
142 }
145 $avatar = $DIC["user.avatar.factory"]->avatar($this->size);
146 $avatar->setName($this->abbreviation);
147 $avatar->setUsrId($this->user_id);
148
149 return $avatar->getUrl();
150 }
151
152 public function setForcePicture(bool $force_image): void
153 {
154 $this->force_image = $force_image;
155 }
156
157 public function setSize(string $size): void
158 {
159 if ($size === 'small' || $size === 'big') {
160 $size = 'xsmall';
161 }
162
163 $this->size = $size;
164 }
165}
static getWebspaceDir(string $mode="filesystem")
get webspace directory
language handling
User class.
static _lookupLogin(int $a_user_id)
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
Class ilUserAvatarResolver.
getAvatar(bool $name_as_set_as_text_closely=false)
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static signFile(string $path_to_file)
global $DIC
Definition: feed.php:28
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