ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
UserDataRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ilCtrlInterface;
26 use ilDBConstants;
28 use ilDBInterface;
29 use ilLogger;
30 
35 {
37  private ilLogger $logger;
38  private string $defaultTitle;
40 
49  public function __construct(
50  ilDBInterface $database,
51  ilLogger $logger,
52  ilCtrlInterface $ctrl,
53  ?string $defaultTitle = null
54  ) {
55  $this->database = $database;
56  $this->logger = $logger;
57  $this->ctrl = $ctrl;
58 
59  if (null === $defaultTitle) {
60  global $DIC;
61  $defaultTitle = $DIC->language()->txt('certificate_no_object_title');
62  }
63  $this->defaultTitle = $defaultTitle;
64  }
65 
71  public function getUserData(UserDataFilter $filter, array $ilCtrlStack): array
72  {
73  $sql = 'SELECT
74  cert.pattern_certificate_id,
75  cert.obj_id,
76  cert.usr_id,
77  cert.user_name,
78  cert.acquired_timestamp,
79  cert.currently_active,
80  cert.id,
81  cert.title,
82  cert.ref_id,
83  usr_data.firstname,
84  usr_data.lastname,
85  usr_data.email,
86  usr_data.login,
87  usr_data.second_email
88 FROM
89 ' . $this->getQuery($filter);
90 
91  $query = $this->database->query($sql);
92 
93  if ([] !== $ilCtrlStack) {
94  $ilCtrlStack[] = ilUserCertificateApiGUI::class;
95  }
96 
97  $result = [];
98  while ($row = $this->database->fetchAssoc($query)) {
99  $id = (int) $row['id'];
100 
101  if (isset($result[$id])) {
102  $result[$id]->addRefId((int) $row['ref_id']);
103  continue;
104  }
105 
106  $link = '';
107  if ([] !== $ilCtrlStack) {
108  $this->ctrl->setParameterByClass(ilUserCertificateApiGUI::class, 'certificate_id', $id);
109  $link = $this->ctrl->getLinkTargetByClass($ilCtrlStack, ilUserCertificateApiGUI::CMD_DOWNLOAD);
110  $this->ctrl->clearParametersByClass(ilUserCertificateApiGUI::class);
111  }
112 
113  $dataObject = new UserCertificateDto(
114  $id,
115  $row['title'] ?? $this->defaultTitle,
116  (int) $row['obj_id'],
117  (int) $row['acquired_timestamp'],
118  (int) $row['usr_id'],
119  $row['firstname'],
120  $row['lastname'],
121  $row['login'],
122  (string) $row['email'],
123  (string) $row['second_email'],
124  [(int) $row['ref_id']],
125  $link
126  );
127 
128  $result[$id] = $dataObject;
129  }
130 
131  if ($filter->getLimitOffset() !== null && $filter->getLimitCount() !== null) {
132  $result = array_slice($result, $filter->getLimitOffset(), $filter->getLimitCount(), true);
133  }
134 
135  return $result;
136  }
137 
139  {
140  $sql = 'SELECT
141  COUNT(id) AS count
142 FROM
143 ' . $this->getQuery($filter, true);
144 
145  $query = $this->database->query($sql);
146 
147  return (int) $this->database->fetchAssoc($query)["count"];
148  }
149 
155  private function getQuery(UserDataFilter $filter, bool $max_count_only = false): string
156  {
157  $sql = '(
158 SELECT
159  il_cert_user_cert.pattern_certificate_id,
160  il_cert_user_cert.obj_id,
161  il_cert_user_cert.usr_id,
162  il_cert_user_cert.user_name,
163  il_cert_user_cert.acquired_timestamp,
164  il_cert_user_cert.currently_active,
165  il_cert_user_cert.id,
166  object_data.title,
167  object_reference.ref_id
168 FROM il_cert_user_cert
169 INNER JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
170 INNER JOIN object_reference ON object_reference.obj_id = il_cert_user_cert.obj_id';
171 
172  if ($filter->shouldIncludeDeletedObjects()) {
173  $sql .= '
174 UNION
175 SELECT
176  il_cert_user_cert.pattern_certificate_id,
177  il_cert_user_cert.obj_id,
178  il_cert_user_cert.usr_id,
179  il_cert_user_cert.user_name,
180  il_cert_user_cert.acquired_timestamp,
181  il_cert_user_cert.currently_active,
182  il_cert_user_cert.id,
183  object_data_del.title,
184  NULL AS ref_id
185 FROM il_cert_user_cert
186 INNER JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id';
187  } else {
188  $sql .= '
189 WHERE object_reference.deleted IS NULL';
190  }
191 
192  $sql .= '
193 ) AS cert
194 INNER JOIN usr_data ON usr_data.usr_id = cert.usr_id
195 INNER JOIN il_orgu_ua ON il_orgu_ua.user_id = cert.usr_id
196 ' . $this->createWhereCondition($filter);
197 
198  if (!$max_count_only) {
199  $sql .= $this->createOrderByClause($filter);
200  }
201 
202  return $sql;
203  }
204 
205  private function createOrderByClause(UserDataFilter $filter): string
206  {
207  $sorts = $filter->getSorts();
208 
209  if (empty($sorts)) {
210  return '';
211  }
212 
213  $orders = [];
214 
215  foreach ($sorts as [$key, $direction]) {
216  $direction = $direction === UserDataFilter::SORT_DIRECTION_DESC ? ' DESC' : ' ASC';
217 
218  switch (true) {
219  case ($key === UserDataFilter::SORT_FIELD_USR_LOGIN):
220  $orders[] = 'usr_data.login' . $direction;
221  break;
222 
224  $orders[] = 'usr_data.firstname' . $direction;
225  break;
226 
228  $orders[] = 'usr_data.lastname' . $direction;
229  break;
230 
231  case ($key === UserDataFilter::SORT_FIELD_OBJ_TITLE):
232  $orders[] = 'cert.title' . $direction;
233  break;
234 
236  $orders[] = 'cert.acquired_timestamp' . $direction;
237  break;
238 
239  case ($key === UserDataFilter::SORT_FIELD_USR_EMAIL):
240  $orders[] = 'usr_data.email' . $direction;
241  break;
242 
243  default:
244  break;
245  }
246  }
247 
248  return ' ORDER BY ' . implode(', ', $orders);
249  }
250 
256  private function createWhereCondition(UserDataFilter $filter): string
257  {
258  $wheres = [];
259 
260  $userIds = $filter->getUserIds();
261  if (!empty($userIds)) {
262  $wheres[] = $this->database->in('cert.usr_id', $userIds, false, ilDBConstants::T_INTEGER);
263  }
264 
265  $objIds = $filter->getObjIds();
266  if (!empty($objIds)) {
267  $wheres[] = $this->database->in('cert.obj_id', $objIds, false, ilDBConstants::T_INTEGER);
268  }
269 
270  $firstName = $filter->getUserFirstName();
271  if (!empty($firstName)) {
272  $wheres[] = $this->database->like('usr_data.firstname', ilDBConstants::T_TEXT, '%' . $firstName . '%');
273  }
274 
275  $lastName = $filter->getUserLastName();
276  if (!empty($lastName)) {
277  $wheres[] = $this->database->like('usr_data.lastname', ilDBConstants::T_TEXT, '%' . $lastName . '%');
278  }
279 
280  $login = $filter->getUserLogin();
281  if (!empty($login)) {
282  $wheres[] = $this->database->like('usr_data.login', ilDBConstants::T_TEXT, '%' . $login . '%');
283  }
284 
285  $userEmail = $filter->getUserEmail();
286  if (!empty($userEmail)) {
287  $wheres[] = '(' . $this->database->like('usr_data.email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
288  . ' OR ' . $this->database->like('usr_data.second_email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
289  . ')';
290  }
291 
292  $userIdentification = $filter->getUserIdentification();
293  if (!empty($userIdentification)) {
294  $wheres[] = '(' . $this->database->like('usr_data.login', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
295  . ' OR ' . $this->database->like('usr_data.firstname', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
296  . ' OR ' . $this->database->like('usr_data.lastname', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
297  . ' OR ' . $this->database->like('usr_data.email', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
298  . ' OR ' . $this->database->like('usr_data.second_email', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
299  . ')';
300  }
301 
302  $issuedBeforeTimestamp = $filter->getIssuedBeforeTimestamp();
303  if ($issuedBeforeTimestamp !== null) {
304  $wheres[] = 'cert.acquired_timestamp < ' . $this->database->quote(
305  $issuedBeforeTimestamp,
307  );
308  }
309 
310  $issuedAfterTimestamp = $filter->getIssuedAfterTimestamp();
311  if ($issuedAfterTimestamp !== null) {
312  $wheres[] = 'cert.acquired_timestamp > ' . $this->database->quote(
313  $issuedAfterTimestamp,
315  );
316  }
317 
318  $title = $filter->getObjectTitle();
319  if (!empty($title)) {
320  $wheres[] = $this->database->like('cert.title', ilDBConstants::T_TEXT, '%' . $title . '%');
321  }
322 
323  $onlyCertActive = $filter->isOnlyCertActive();
324  if ($onlyCertActive === true) {
325  $wheres[] = 'cert.currently_active = ' . $this->database->quote(1, ilDBConstants::T_INTEGER);
326  }
327 
328  $orgUnitIds = $filter->getOrgUnitIds();
329  if ($orgUnitIds) {
330  $wheres[] = $this->database->in('il_orgu_ua.orgu_id', $orgUnitIds, false, ilDBConstants::T_INTEGER);
331  }
332 
333  if (empty($wheres)) {
334  return '';
335  }
336 
337  return 'WHERE ' . implode(' AND ', $wheres);
338  }
339 }
__construct(ilDBInterface $database, ilLogger $logger, ilCtrlInterface $ctrl, ?string $defaultTitle=null)
getQuery(UserDataFilter $filter, bool $max_count_only=false)
global $DIC
Definition: feed.php:28
string $key
Consumer key/client ID value.
Definition: System.php:193
$query
createWhereCondition(UserDataFilter $filter)
Creating the additional where condition based on the filter object.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getUserData(UserDataFilter $filter, array $ilCtrlStack)