ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilUserAvatarResolver.php
Go to the documentation of this file.
1<?php
2
5
11{
15 private $user_id;
19 private $login;
23 private $firstname;
27 private $lastname;
31 private $has_public_profile = false;
35 private $has_public_upload = false;
47 private $force_image = false;
51 private $size = 'small';
55 private $db;
59 private $lng;
63 private $user;
67 protected $ui;
68
73
78 public function __construct(int $user_id)
79 {
80 global $DIC;
81
82 $this->db = $DIC->database();
83 $this->ui = $DIC->ui()->factory();
84 $this->lng = $DIC->language();
85 $this->user = $DIC->user();
86 $this->user_id = $user_id;
87 $this->letter_avatars_activated = (bool) $DIC->settings()->get('letter_avatars');
88 $this->init();
89 }
90
91 private function init() : void
92 {
93 $in = $this->db->in('usr_pref.keyword', array('public_upload', 'public_profile'), false, 'text');
94 $res = $this->db->queryF(
95 "
96 SELECT usr_pref.*, ud.login, ud.firstname, ud.lastname
97 FROM usr_data ud LEFT JOIN usr_pref ON usr_pref.usr_id = ud.usr_id AND $in
98 WHERE ud.usr_id = %s",
99 array('integer'),
100 array($this->user_id)
101 );
102
103 while ($row = $this->db->fetchAssoc($res)) { // MUST be loop
104 $this->login = $row['login'];
105 $this->firstname = $row['firstname'];
106 $this->lastname = $row['lastname'];
107
108 switch ($row['keyword']) {
109 case 'public_upload':
110 $this->has_public_upload = $row['value'] === 'y';
111 break;
112 case 'public_profile':
113 $this->has_public_profile = ($row['value'] === 'y' || $row['value'] === 'g');
114 break;
115 }
116 }
117
118 // Uploaded file
119 $webspace_dir = '';
120 if (defined('ILIAS_MODULE')) {
121 $webspace_dir = ('.' . $webspace_dir);
122 }
123 $webspace_dir .= ('./' . ltrim(ilUtil::getWebspaceDir(), "./"));
124
125 $image_dir = $webspace_dir . '/usr_images';
126 $this->uploaded_file = $image_dir . '/usr_' . $this->user_id . '.jpg';
127
128 if ($this->has_public_profile) {
129 $this->abbreviation = ilStr::subStr($this->firstname, 0, 1) . ilStr::subStr($this->lastname, 0, 1);
130 } else {
131 $this->abbreviation = ilStr::subStr($this->login, 0, 2);
132 }
133 }
134
135 private function useUploadedFile() : bool
136 {
137 return (($this->has_public_upload && $this->has_public_profile) || $this->force_image) && is_file($this->uploaded_file);
138 }
139
146 public function getAvatar(bool $name_as_set_as_text_closely = false) : Avatar
147 {
148 if ($name_as_set_as_text_closely) {
149 $alternative_text = $this->lng->txt("user_avatar");
150 } elseif ($this->user_id == $this->user->getId() && !$this->user::_isAnonymous($this->user_id)) {
151 $alternative_text = $this->lng->txt("current_user_avatar");
152 } else {
153 $alternative_text = $this->lng->txt("user_avatar_of") . " " . $this->login;
154 }
155
156 if ($this->useUploadedFile()) {
157 return $this->ui->symbol()->avatar()->picture($this->uploaded_file, $this->login)
158 ->withAlternativeText($alternative_text);
159 }
160
161 if ($this->letter_avatars_activated === false) {
162 return $this->ui->symbol()->avatar()->picture(
163 \ilUtil::getImagePath('no_photo_xsmall.jpg'),
164 ilObjUser::_lookupLogin($this->user_id)
165 );
166 }
167
168 return $this->ui->symbol()->avatar()->letter($this->abbreviation)->withAlternativeText($alternative_text);
169 }
170
171 public function getLegacyPictureURL() : string
172 {
173 global $DIC;
174 if ($this->useUploadedFile()) {
175 return $this->uploaded_file . '?t=' . rand(1, 99999);
176 }
179 $avatar = $DIC["user.avatar.factory"]->avatar($this->size);
180 $avatar->setName($this->abbreviation);
181 $avatar->setUsrId($this->user_id);
182
183 return $avatar->getUrl();
184 }
185
189 public function setForcePicture(bool $force_image) : void
190 {
191 $this->force_image = $force_image;
192 }
193
197 public function setSize(string $size) : void
198 {
199 if ($size === 'small' || $size === 'big') {
200 $size = 'xsmall';
201 }
202
203 $this->size = $size;
204 }
205}
user()
Definition: user.php:4
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
An exception for terminatinating execution or to throw for unit testing.
static _lookupLogin($a_user_id)
lookup login
static subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
Class ilUserAvatarResolver.
getAvatar(bool $name_as_set_as_text_closely=false)
__construct(int $user_id)
constructor.
static getWebspaceDir($mode="filesystem")
get webspace directory
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
global $DIC
Definition: goto.php:24
This describes how a letter or a picture avatar could be modified during construction of UI.
Definition: Avatar.php:9
This is how the factory for UI elements looks.
Definition: Factory.php:18
foreach($_POST as $key=> $value) $res
login()
Definition: login.php:2
ui()
Definition: ui.php:5