ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator 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 
34 {
35  private readonly string $defaultTitle;
36 
42  public function __construct(
43  private readonly ilDBInterface $database,
44  private readonly ilCtrlInterface $ctrl,
45  ?string $defaultTitle = null
46  ) {
47  if (null === $defaultTitle) {
48  global $DIC;
49  $defaultTitle = $DIC->language()->txt('certificate_no_object_title');
50  }
51  $this->defaultTitle = $defaultTitle;
52  }
53 
58  public function getUserData(UserDataFilter $filter, array $ilCtrlStack): array
59  {
60  $sql = 'SELECT
61  cert.pattern_certificate_id,
62  cert.obj_id,
63  cert.usr_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  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->ctrl->setParameterByClass(ilUserCertificateApiGUI::class, 'certificate_id', $id);
96  $link = $this->ctrl->getLinkTargetByClass($ilCtrlStack, ilUserCertificateApiGUI::CMD_DOWNLOAD);
97  $this->ctrl->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['usr_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(), true);
120  }
121 
122  return $result;
123  }
124 
126  {
127  $sql = 'SELECT
128  COUNT(id) AS count
129 FROM
130 ' . $this->getQuery($filter, true);
131 
132  $query = $this->database->query($sql);
133 
134  return (int) $this->database->fetchAssoc($query)["count"];
135  }
136 
137  private function getQuery(UserDataFilter $filter, bool $max_count_only = false): string
138  {
139  $sql = '(
140 SELECT
141  il_cert_user_cert.pattern_certificate_id,
142  il_cert_user_cert.obj_id,
143  il_cert_user_cert.usr_id,
144  il_cert_user_cert.user_name,
145  il_cert_user_cert.acquired_timestamp,
146  il_cert_user_cert.currently_active,
147  il_cert_user_cert.id,
148  object_data.title,
149  object_reference.ref_id
150 FROM il_cert_user_cert
151 INNER JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
152 INNER JOIN object_reference ON object_reference.obj_id = il_cert_user_cert.obj_id';
153 
154  if ($filter->shouldIncludeDeletedObjects()) {
155  $sql .= '
156 UNION
157 SELECT
158  il_cert_user_cert.pattern_certificate_id,
159  il_cert_user_cert.obj_id,
160  il_cert_user_cert.usr_id,
161  il_cert_user_cert.user_name,
162  il_cert_user_cert.acquired_timestamp,
163  il_cert_user_cert.currently_active,
164  il_cert_user_cert.id,
165  object_data_del.title,
166  NULL AS ref_id
167 FROM il_cert_user_cert
168 INNER JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id';
169  } else {
170  $sql .= '
171 WHERE object_reference.deleted IS NULL';
172  }
173 
174  $sql .= '
175 ) AS cert
176 INNER JOIN usr_data ON usr_data.usr_id = cert.usr_id
177 INNER JOIN il_orgu_ua ON il_orgu_ua.user_id = cert.usr_id
178 ' . $this->createWhereCondition($filter);
179 
180  if (!$max_count_only) {
181  $sql .= $this->createOrderByClause($filter);
182  }
183 
184  return $sql;
185  }
186 
187  private function createOrderByClause(UserDataFilter $filter): string
188  {
189  $sorts = $filter->getSorts();
190 
191  if (empty($sorts)) {
192  return '';
193  }
194 
195  $orders = [];
196 
197  foreach ($sorts as [$key, $direction]) {
198  $direction = $direction === UserDataFilter::SORT_DIRECTION_DESC ? ' DESC' : ' ASC';
199 
200  switch (true) {
201  case ($key === UserDataFilter::SORT_FIELD_USR_LOGIN):
202  $orders[] = 'usr_data.login' . $direction;
203  break;
204 
206  $orders[] = 'usr_data.firstname' . $direction;
207  break;
208 
210  $orders[] = 'usr_data.lastname' . $direction;
211  break;
212 
213  case ($key === UserDataFilter::SORT_FIELD_OBJ_TITLE):
214  $orders[] = 'cert.title' . $direction;
215  break;
216 
218  $orders[] = 'cert.acquired_timestamp' . $direction;
219  break;
220 
221  case ($key === UserDataFilter::SORT_FIELD_USR_EMAIL):
222  $orders[] = 'usr_data.email' . $direction;
223  break;
224 
225  default:
226  break;
227  }
228  }
229 
230  return ' ORDER BY ' . implode(', ', $orders);
231  }
232 
236  private function createWhereCondition(UserDataFilter $filter): string
237  {
238  $wheres = [];
239 
240  $userIds = $filter->getUserIds();
241  if (!empty($userIds)) {
242  $wheres[] = $this->database->in('cert.usr_id', $userIds, false, ilDBConstants::T_INTEGER);
243  }
244 
245  $objIds = $filter->getObjIds();
246  if (!empty($objIds)) {
247  $wheres[] = $this->database->in('cert.obj_id', $objIds, false, ilDBConstants::T_INTEGER);
248  }
249 
250  $firstName = $filter->getUserFirstName();
251  if (!empty($firstName)) {
252  $wheres[] = $this->database->like('usr_data.firstname', ilDBConstants::T_TEXT, '%' . $firstName . '%');
253  }
254 
255  $lastName = $filter->getUserLastName();
256  if (!empty($lastName)) {
257  $wheres[] = $this->database->like('usr_data.lastname', ilDBConstants::T_TEXT, '%' . $lastName . '%');
258  }
259 
260  $login = $filter->getUserLogin();
261  if (!empty($login)) {
262  $wheres[] = $this->database->like('usr_data.login', ilDBConstants::T_TEXT, '%' . $login . '%');
263  }
264 
265  $userEmail = $filter->getUserEmail();
266  if (!empty($userEmail)) {
267  $wheres[] = '(' . $this->database->like('usr_data.email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
268  . ' OR ' . $this->database->like('usr_data.second_email', ilDBConstants::T_TEXT, '%' . $userEmail . '%')
269  . ')';
270  }
271 
272  $userIdentification = $filter->getUserIdentification();
273  if (!empty($userIdentification)) {
274  $wheres[] = '(' . $this->database->like('usr_data.login', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
275  . ' OR ' . $this->database->like('usr_data.firstname', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
276  . ' OR ' . $this->database->like('usr_data.lastname', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
277  . ' OR ' . $this->database->like('usr_data.email', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
278  . ' OR ' . $this->database->like('usr_data.second_email', ilDBConstants::T_TEXT, '%' . $userIdentification . '%')
279  . ')';
280  }
281 
282  $issuedBeforeTimestamp = $filter->getIssuedBeforeTimestamp();
283  if ($issuedBeforeTimestamp !== null) {
284  $wheres[] = 'cert.acquired_timestamp < ' . $this->database->quote(
285  $issuedBeforeTimestamp,
287  );
288  }
289 
290  $issuedAfterTimestamp = $filter->getIssuedAfterTimestamp();
291  if ($issuedAfterTimestamp !== null) {
292  $wheres[] = 'cert.acquired_timestamp > ' . $this->database->quote(
293  $issuedAfterTimestamp,
295  );
296  }
297 
298  $title = $filter->getObjectTitle();
299  if (!empty($title)) {
300  $wheres[] = $this->database->like('cert.title', ilDBConstants::T_TEXT, '%' . $title . '%');
301  }
302 
303  $onlyCertActive = $filter->isOnlyCertActive();
304  if ($onlyCertActive) {
305  $wheres[] = 'cert.currently_active = ' . $this->database->quote(1, ilDBConstants::T_INTEGER);
306  }
307 
308  $orgUnitIds = $filter->getOrgUnitIds();
309  if ($orgUnitIds) {
310  $wheres[] = $this->database->in('il_orgu_ua.orgu_id', $orgUnitIds, false, ilDBConstants::T_INTEGER);
311  }
312 
313  if (empty($wheres)) {
314  return '';
315  }
316 
317  return 'WHERE ' . implode(' AND ', $wheres);
318  }
319 }
__construct(private readonly ilDBInterface $database, private readonly ilCtrlInterface $ctrl, ?string $defaultTitle=null)
getQuery(UserDataFilter $filter, bool $max_count_only=false)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:22
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)