ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilUserAvatarResolver.php
Go to the documentation of this file.
1 <?php
2 
4 
10 {
14  private $user_id;
18  private $login;
22  private $firstname;
26  private $lastname;
30  private $has_public_profile = false;
34  private $has_public_upload = false;
38  private $uploaded_file;
42  private $abbreviation;
46  private $force_image = false;
50  private $size = 'small';
55 
60  public function __construct(int $user_id)
61  {
62  global $DIC;
63  $this->db = $DIC->database();
64  $this->ui = $DIC->ui()->factory();
65  $this->user_id = $user_id;
66  $this->letter_avatars_activated = (bool) $DIC->settings()->get('letter_avatars');
67  $this->init();
68  }
69 
70  private function init() : void
71  {
72  $in = $this->db->in('usr_pref.keyword', array('public_upload', 'public_profile'), false, 'text');
73  $res = $this->db->queryF(
74  "
75  SELECT usr_pref.*, ud.login, ud.firstname, ud.lastname
76  FROM usr_data ud LEFT JOIN usr_pref ON usr_pref.usr_id = ud.usr_id AND $in
77  WHERE ud.usr_id = %s",
78  array('integer'),
79  array($this->user_id)
80  );
81 
82  while ($row = $this->db->fetchAssoc($res)) {
83  $this->login = $row['login'];
84  $this->firstname = $row['firstname'];
85  $this->lastname = $row['lastname'];
86 
87  switch ($row['keyword']) {
88  case 'public_upload':
89  $this->has_public_upload = $row['value'] === 'y';
90  break;
91  case 'public_profile':
92  $this->has_public_profile = ($row['value'] === 'y' || $row['value'] === 'g');
93  break;
94  }
95  }
96 
97  // Uploaded file
98  $webspace_dir = '';
99  if (defined('ILIAS_MODULE')) {
100  $webspace_dir = ('.' . $webspace_dir);
101  }
102  $webspace_dir .= ('./' . ltrim(ilUtil::getWebspaceDir(), "./"));
103 
104  $image_dir = $webspace_dir . '/usr_images';
105  $this->uploaded_file = $image_dir . '/usr_' . $this->user_id . '.jpg';
106 
107  if ($this->has_public_profile) {
108  $this->abbreviation = ilStr::subStr($this->firstname, 0, 1) . ilStr::subStr($this->lastname, 0, 1);
109  } else {
110  $this->abbreviation = ilStr::subStr($this->login, 0, 2);
111  }
112  }
113 
114  private function useUploadedFile() : bool
115  {
116  return (($this->has_public_upload && $this->has_public_profile) || $this->force_image) && is_file($this->uploaded_file);
117  }
118 
119  public function getAvatar() : Avatar
120  {
121  if ($this->useUploadedFile()) {
122  return $this->ui->symbol()->avatar()->picture($this->uploaded_file, $this->abbreviation);
123  }
124 
125  if ($this->letter_avatars_activated === false) {
126  return $this->ui->symbol()->avatar()->picture(
127  \ilUtil::getImagePath('no_photo_xsmall.jpg'),
128  ilObjUser::_lookupLogin($this->user_id)
129  );
130  }
131  return $this->ui->symbol()->avatar()->letter($this->abbreviation);
132  }
133 
134  public function getLegacyPictureURL() : string
135  {
136  global $DIC;
137  if ($this->useUploadedFile()) {
138  return $this->uploaded_file . '?t=' . rand(1, 99999);
139  ;
140  }
143  $avatar = $DIC["user.avatar.factory"]->avatar($this->size);
144  $avatar->setName($this->abbreviation);
145  $avatar->setUsrId($this->user_id);
146 
147  return $avatar->getUrl();
148  }
149 
153  public function setForcePicture(bool $force_image) : void
154  {
155  $this->force_image = $force_image;
156  }
157 
161  public function setSize(string $size) : void
162  {
163  if ($size === 'small' || $size === 'big') {
164  $size = 'xsmall';
165  }
166 
167  $this->size = $size;
168  }
169 }
static _lookupLogin($a_user_id)
lookup login
login()
Definition: login.php:2
__construct(int $user_id)
constructor.
static subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
foreach($_POST as $key=> $value) $res
This describes how a letter or a picture avatar could be modified during construction of UI...
Definition: Avatar.php:8
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
ui()
Definition: ui.php:5
Class ilUserAvatarResolver.
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
$DIC
Definition: xapitoken.php:46
static getWebspaceDir($mode="filesystem")
get webspace directory