ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
UserDataRepository.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 
8 use ilDBConstants;
10 
15 {
17  private $database;
18 
20  private $logger;
21 
23  private $defaultTitle;
24 
26  private $controller;
27 
36  public function __construct(
40  string $defaultTitle = null
41  ) {
42  $this->database = $database;
43  $this->logger = $logger;
44  $this->controller = $controller;
45 
46  if (null === $defaultTitle) {
47  global $DIC;
48  $defaultTitle = $DIC->language()->txt('certificate_no_object_title');
49  }
50  $this->defaultTitle = $defaultTitle;
51  }
52 
58  public function getUserData(UserDataFilter $filter, array $ilCtrlStack) : array
59  {
60  $sql = 'SELECT
61  cert.pattern_certificate_id,
62  cert.obj_id,
63  cert.user_id,
64  cert.user_name,
65  cert.acquired_timestamp,
66  cert.currently_active,
67  cert.id,
68  cert.title,
69  cert.ref_id,
70  usr_data.firstname,
71  usr_data.lastname,
72  usr_data.email,
73  usr_data.login,
74  usr_data.second_email
75 FROM
76 ' . $this->getQuery($filter);
77 
78  $query = $this->database->query($sql);
79 
80  $result = [];
81  while ($row = $this->database->fetchAssoc($query)) {
82  $id = (int) $row['id'];
83 
84  if (isset($result[$id])) {
85  $result[$id]->addRefId((int) $row['ref_id']);
86  continue;
87  }
88 
89  $link = '';
90  if ([] !== $ilCtrlStack) {
91  $ilCtrlStack[] = ilUserCertificateApiGUI::class;
92  $this->controller->setParameterByClass(ilUserCertificateApiGUI::class, 'certificate_id', $id);
93  $link = $this->controller->getLinkTargetByClass($ilCtrlStack, ilUserCertificateApiGUI::CMD_DOWNLOAD);
94  $this->controller->clearParametersByClass(ilUserCertificateApiGUI::class);
95  }
96 
97  $dataObject = new UserCertificateDto(
98  $id,
99  $row['title'] ?? $this->defaultTitle,
100  (int) $row['obj_id'],
101  (int) $row['acquired_timestamp'],
102  (int) $row['user_id'],
103  $row['firstname'],
104  $row['lastname'],
105  $row['login'],
106  (string) $row['email'],
107  (string) $row['second_email'],
108  [(int) $row['ref_id']],
109  $link
110  );
111 
112  $result[$id] = $dataObject;
113  }
114 
115  if ($filter->getLimitOffset() !== null && $filter->getLimitCount() !== null) {
116  $result = array_slice($result, $filter->getLimitOffset(), $filter->getLimitCount());
117  }
118 
119  return $result;
120  }
121 
122 
128  public function getUserCertificateDataMaxCount(UserDataFilter $filter) : int
129  {
130  $sql = 'SELECT
131  COUNT(id) AS count
132 FROM
133 ' . $this->getQuery($filter, true);
134 
135  $query = $this->database->query($sql);
136 
137  $max_count = intval($this->database->fetchAssoc($query)["count"]);
138 
139  return $max_count;
140  }
141 
142 
149  private function getQuery(UserDataFilter $filter, bool $max_count_only = false) : string
150  {
151  $sql = '(
152 SELECT
153  il_cert_user_cert.pattern_certificate_id,
154  il_cert_user_cert.obj_id,
155  il_cert_user_cert.user_id,
156  il_cert_user_cert.user_name,
157  il_cert_user_cert.acquired_timestamp,
158  il_cert_user_cert.currently_active,
159  il_cert_user_cert.id,
160  object_data.title,
161  object_reference.ref_id
162 FROM il_cert_user_cert
163 INNER JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
164 INNER JOIN object_reference ON object_reference.obj_id = il_cert_user_cert.obj_id';
165 
166  if ($filter->shouldIncludeDeletedObjects()) {
167  $sql .= '
168 UNION
169 SELECT
170  il_cert_user_cert.pattern_certificate_id,
171  il_cert_user_cert.obj_id,
172  il_cert_user_cert.user_id,
173  il_cert_user_cert.user_name,
174  il_cert_user_cert.acquired_timestamp,
175  il_cert_user_cert.currently_active,
176  il_cert_user_cert.id,
177  object_data_del.title,
178  NULL AS ref_id
179 FROM il_cert_user_cert
180 INNER JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id';
181  } else {
182  $sql .= '
183 WHERE object_reference.deleted IS NULL';
184  }
185 
186  $sql .= '
187 ) AS cert
188 INNER JOIN usr_data ON usr_data.usr_id = cert.user_id
189 ' . $this->createWhereCondition($filter);
190 
191  if (!$max_count_only) {
192  $sql .= $this->createOrderByClause($filter);
193  }
194 
195  return $sql;
196  }
197 
198 
203  private function createOrderByClause(UserDataFilter $filter) : string
204  {
205  $sorts = $filter->getSorts();
206 
207  if (empty($sorts)) {
208  return '';
209  }
210 
211  $orders = [];
212 
213  foreach ($sorts as [$key, $direction]) {
214  $direction = $direction === UserDataFilter::SORT_DIRECTION_DESC ? ' DESC' : ' ASC';
215 
216  switch (true) {
217  case ($key === UserDataFilter::SORT_FIELD_USR_LOGIN):
218  $orders[] = 'usr_data.login' . $direction;
219  break;
220 
222  $orders[] = 'usr_data.firstname' . $direction;
223  break;
224 
226  $orders[] = 'usr_data.lastname' . $direction;
227  break;
228 
229  case ($key === UserDataFilter::SORT_FIELD_OBJ_TITLE):
230  $orders[] = 'cert.title' . $direction;
231  break;
232 
234  $orders[] = 'cert.acquired_timestamp' . $direction;
235  break;
236 
237  default:
238  break;
239  }
240  }
241 
242  $orderBy = 'ORDER BY ' . implode(', ', $orders);
243 
244  return $orderBy;
245  }
246 
252  private function createWhereCondition(UserDataFilter $filter) : string
253  {
254  $wheres = [];
255 
256  $userIds = $filter->getUserIds();
257  if (!empty($userIds)) {
258  $wheres[] = $this->database->in('cert.user_id', $userIds, false, ilDBConstants::T_INTEGER);
259  }
260 
261  $objIds = $filter->getObjIds();
262  if (!empty($objIds)) {
263  $wheres[] = $this->database->in('cert.obj_id', $objIds, false, ilDBConstants::T_INTEGER);
264  }
265 
266  $firstName = $filter->getUserFirstName();
267  if (!empty($firstName)) {
268  $wheres[] = $this->database->like('usr_data.firstname', ilDBConstants::T_TEXT, '%' . $firstName . '%');
269  }
270 
271  $lastName = $filter->getUserLastName();
272  if (!empty($lastName)) {
273  $wheres[] = $this->database->like('usr_data.lastname', ilDBConstants::T_TEXT, '%' . $lastName . '%');
274  }
275 
276  $login = $filter->getUserLogin();
277  if (!empty($login)) {
278  $wheres[] = $this->database->like('usr_data.login', ilDBConstants::T_TEXT, '%' . $login . '%');
279  }
280 
281  $userEmail = $filter->getUserEmail();
282  if (!empty($userEmail)) {
283  $wheres[] = '(' . $this->database->like('usr_data.email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
284  . ' OR ' . $this->database->like('usr_data.second_email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
285  . ')';
286  }
287 
288  $issuedBeforeTimestamp = $filter->getIssuedBeforeTimestamp();
289  if ($issuedBeforeTimestamp !== null) {
290  $wheres[] = 'cert.acquired_timestamp < ' . $this->database->quote(
291  $issuedBeforeTimestamp,
293  );
294  }
295 
296  $issuedAfterTimestamp = $filter->getIssuedAfterTimestamp();
297  if ($issuedAfterTimestamp !== null) {
298  $wheres[] = 'cert.acquired_timestamp > ' . $this->database->quote(
299  $issuedAfterTimestamp,
301  );
302  }
303 
304  $title = $filter->getObjectTitle();
305  if (!empty($title)) {
306  $wheres[] = $this->database->like('cert.title', ilDBConstants::T_TEXT, '%' . $title . '%');
307  }
308 
309  $onlyCertActive = $filter->isOnlyCertActive();
310  if ($onlyCertActive === true) {
311  $wheres[] = 'cert.currently_active = ' . $this->database->quote(1, ilDBConstants::T_INTEGER);
312  }
313 
314  if (empty($wheres)) {
315  return '';
316  }
317 
318  $sql = 'WHERE ' . implode(' AND ', $wheres);
319 
320  return $sql;
321  }
322 }
getQuery(UserDataFilter $filter, bool $max_count_only=false)
__construct(\ilDBInterface $database, \ilLogger $logger, \ilCtrl $controller, string $defaultTitle=null)
$login
Definition: cron.php:13
This class provides processing control methods.
$result
Interface ilDBInterface.
$query
createWhereCondition(UserDataFilter $filter)
Creating the additional where condition based on the filter object.
getUserData(UserDataFilter $filter, array $ilCtrlStack)
$DIC
Definition: xapitoken.php:46
Component logger with individual log levels by component id.