ILIAS  release_7 Revision v7.30-3-g800a261c036
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
10
15{
17 private $database;
18
20 private $logger;
21
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
75FROM
76' . $this->getQuery($filter);
77
78 $query = $this->database->query($sql);
79
80 if ([] !== $ilCtrlStack) {
81 $ilCtrlStack[] = ilUserCertificateApiGUI::class;
82 }
83
84 $result = [];
85 while ($row = $this->database->fetchAssoc($query)) {
86 $id = (int) $row['id'];
87
88 if (isset($result[$id])) {
89 $result[$id]->addRefId((int) $row['ref_id']);
90 continue;
91 }
92
93 $link = '';
94 if ([] !== $ilCtrlStack) {
95 $this->controller->setParameterByClass(ilUserCertificateApiGUI::class, 'certificate_id', $id);
96 $link = $this->controller->getLinkTargetByClass($ilCtrlStack, ilUserCertificateApiGUI::CMD_DOWNLOAD);
97 $this->controller->clearParametersByClass(ilUserCertificateApiGUI::class);
98 }
99
100 $dataObject = new UserCertificateDto(
101 $id,
102 $row['title'] ?? $this->defaultTitle,
103 (int) $row['obj_id'],
104 (int) $row['acquired_timestamp'],
105 (int) $row['user_id'],
106 $row['firstname'],
107 $row['lastname'],
108 $row['login'],
109 (string) $row['email'],
110 (string) $row['second_email'],
111 [(int) $row['ref_id']],
112 $link
113 );
114
115 $result[$id] = $dataObject;
116 }
117
118 if ($filter->getLimitOffset() !== null && $filter->getLimitCount() !== null) {
119 $result = array_slice($result, $filter->getLimitOffset(), $filter->getLimitCount());
120 }
121
122 return $result;
123 }
124
125
131 public function getUserCertificateDataMaxCount(UserDataFilter $filter) : int
132 {
133 $sql = 'SELECT
134 COUNT(id) AS count
135FROM
136' . $this->getQuery($filter, true);
137
138 $query = $this->database->query($sql);
139
140 $max_count = intval($this->database->fetchAssoc($query)["count"]);
141
142 return $max_count;
143 }
144
145
152 private function getQuery(UserDataFilter $filter, bool $max_count_only = false) : string
153 {
154 $sql = '(
155SELECT
156 il_cert_user_cert.pattern_certificate_id,
157 il_cert_user_cert.obj_id,
158 il_cert_user_cert.user_id,
159 il_cert_user_cert.user_name,
160 il_cert_user_cert.acquired_timestamp,
161 il_cert_user_cert.currently_active,
162 il_cert_user_cert.id,
163 object_data.title,
164 object_reference.ref_id
165FROM il_cert_user_cert
166INNER JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
167INNER JOIN object_reference ON object_reference.obj_id = il_cert_user_cert.obj_id';
168
169 if ($filter->shouldIncludeDeletedObjects()) {
170 $sql .= '
171UNION
172SELECT
173 il_cert_user_cert.pattern_certificate_id,
174 il_cert_user_cert.obj_id,
175 il_cert_user_cert.user_id,
176 il_cert_user_cert.user_name,
177 il_cert_user_cert.acquired_timestamp,
178 il_cert_user_cert.currently_active,
179 il_cert_user_cert.id,
180 object_data_del.title,
181 NULL AS ref_id
182FROM il_cert_user_cert
183INNER JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id';
184 } else {
185 $sql .= '
186WHERE object_reference.deleted IS NULL';
187 }
188
189 $sql .= '
190) AS cert
191INNER JOIN usr_data ON usr_data.usr_id = cert.user_id
192INNER JOIN il_orgu_ua ON il_orgu_ua.user_id = cert.user_id
193' . $this->createWhereCondition($filter);
194
195 if (!$max_count_only) {
196 $sql .= $this->createOrderByClause($filter);
197 }
198
199 return $sql;
200 }
201
202
207 private function createOrderByClause(UserDataFilter $filter) : string
208 {
209 $sorts = $filter->getSorts();
210
211 if (empty($sorts)) {
212 return '';
213 }
214
215 $orders = [];
216
217 foreach ($sorts as [$key, $direction]) {
218 $direction = $direction === UserDataFilter::SORT_DIRECTION_DESC ? ' DESC' : ' ASC';
219
220 switch (true) {
222 $orders[] = 'usr_data.login' . $direction;
223 break;
224
226 $orders[] = 'usr_data.firstname' . $direction;
227 break;
228
230 $orders[] = 'usr_data.lastname' . $direction;
231 break;
232
234 $orders[] = 'cert.title' . $direction;
235 break;
236
238 $orders[] = 'cert.acquired_timestamp' . $direction;
239 break;
240
242 $orders[] = 'usr_data.email' . $direction;
243 break;
244
245 default:
246 break;
247 }
248 }
249
250 $orderBy = ' ORDER BY ' . implode(', ', $orders);
251
252 return $orderBy;
253 }
254
260 private function createWhereCondition(UserDataFilter $filter) : string
261 {
262 $wheres = [];
263
264 $userIds = $filter->getUserIds();
265 if (!empty($userIds)) {
266 $wheres[] = $this->database->in('cert.user_id', $userIds, false, ilDBConstants::T_INTEGER);
267 }
268
269 $objIds = $filter->getObjIds();
270 if (!empty($objIds)) {
271 $wheres[] = $this->database->in('cert.obj_id', $objIds, false, ilDBConstants::T_INTEGER);
272 }
273
274 $firstName = $filter->getUserFirstName();
275 if (!empty($firstName)) {
276 $wheres[] = $this->database->like('usr_data.firstname', ilDBConstants::T_TEXT, '%' . $firstName . '%');
277 }
278
279 $lastName = $filter->getUserLastName();
280 if (!empty($lastName)) {
281 $wheres[] = $this->database->like('usr_data.lastname', ilDBConstants::T_TEXT, '%' . $lastName . '%');
282 }
283
284 $login = $filter->getUserLogin();
285 if (!empty($login)) {
286 $wheres[] = $this->database->like('usr_data.login', ilDBConstants::T_TEXT, '%' . $login . '%');
287 }
288
289 $userEmail = $filter->getUserEmail();
290 if (!empty($userEmail)) {
291 $wheres[] = '(' . $this->database->like('usr_data.email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
292 . ' OR ' . $this->database->like('usr_data.second_email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
293 . ')';
294 }
295
296 $userIdentification = $filter->getUserIdentification();
297 if (!empty($userIdentification)) {
298 $wheres[] = '(' . $this->database->like('usr_data.login', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
299 . ' OR ' . $this->database->like('usr_data.firstname', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
300 . ' OR ' . $this->database->like('usr_data.lastname', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
301 . ' OR ' . $this->database->like('usr_data.email', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
302 . ' OR ' . $this->database->like('usr_data.second_email', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
303 . ')';
304 }
305
306 $issuedBeforeTimestamp = $filter->getIssuedBeforeTimestamp();
307 if ($issuedBeforeTimestamp !== null) {
308 $wheres[] = 'cert.acquired_timestamp < ' . $this->database->quote(
309 $issuedBeforeTimestamp,
311 );
312 }
313
314 $issuedAfterTimestamp = $filter->getIssuedAfterTimestamp();
315 if ($issuedAfterTimestamp !== null) {
316 $wheres[] = 'cert.acquired_timestamp > ' . $this->database->quote(
317 $issuedAfterTimestamp,
319 );
320 }
321
322 $title = $filter->getObjectTitle();
323 if (!empty($title)) {
324 $wheres[] = $this->database->like('cert.title', ilDBConstants::T_TEXT, '%' . $title . '%');
325 }
326
327 $onlyCertActive = $filter->isOnlyCertActive();
328 if ($onlyCertActive === true) {
329 $wheres[] = 'cert.currently_active = ' . $this->database->quote(1, ilDBConstants::T_INTEGER);
330 }
331
332 $orgUnitIds = $filter->getOrgUnitIds();
333 if ($orgUnitIds) {
334 $wheres[] = $this->database->in('il_orgu_ua.orgu_id', $orgUnitIds, false, ilDBConstants::T_INTEGER);
335 }
336
337 if (empty($wheres)) {
338 return '';
339 }
340
341 $sql = 'WHERE ' . implode(' AND ', $wheres);
342
343 return $sql;
344 }
345}
$result
An exception for terminatinating execution or to throw for unit testing.
getQuery(UserDataFilter $filter, bool $max_count_only=false)
getUserData(UserDataFilter $filter, array $ilCtrlStack)
createWhereCondition(UserDataFilter $filter)
Creating the additional where condition based on the filter object.
__construct(\ilDBInterface $database, \ilLogger $logger, \ilCtrl $controller, string $defaultTitle=null)
This class provides processing control methods.
Class ilDBConstants.
Component logger with individual log levels by component id.
$login
Definition: cron.php:13
global $DIC
Definition: goto.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query