ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilUserCertificateRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
27  private ilLogger $logger;
28  private string $defaultTitle;
29 
30  public function __construct(
31  ?ilDBInterface $database = null,
32  ?ilLogger $logger = null,
33  ?string $defaultTitle = null
34  ) {
35  if (null === $database) {
36  global $DIC;
37  $database = $DIC->database();
38  }
39  $this->database = $database;
40 
41  if (null === $logger) {
42  global $DIC;
43  $logger = $DIC->logger()->cert();
44  }
45  $this->logger = $logger;
46 
47  if (null === $defaultTitle) {
48  global $DIC;
49  $defaultTitle = $DIC->language()->txt('certificate_no_object_title');
50  }
51  $this->defaultTitle = $defaultTitle;
52  }
53 
59  public function save(ilUserCertificate $userCertificate): ilUserCertificate
60  {
61  $this->logger->debug('START - saving of user certificate');
62 
63  $version = (int) $this->fetchLatestVersion($userCertificate->getObjId(), $userCertificate->getUserId());
64  ++$version;
65 
66  $id = $this->database->nextId('il_cert_user_cert');
67 
68  $objId = $userCertificate->getObjId();
69  $userId = $userCertificate->getUserId();
70 
71  $this->deactivatePreviousCertificates($objId, $userId);
72 
73  $columns = [
74  'id' => ['integer', $id],
75  'pattern_certificate_id' => ['integer', $userCertificate->getPatternCertificateId()],
76  'obj_id' => ['integer', $objId],
77  'obj_type' => ['text', $userCertificate->getObjType()],
78  'usr_id' => ['integer', $userId],
79  'user_name' => ['text', $userCertificate->getUserName()],
80  'acquired_timestamp' => ['integer', $userCertificate->getAcquiredTimestamp()],
81  'certificate_content' => ['clob', $userCertificate->getCertificateContent()],
82  'template_values' => ['clob', $userCertificate->getTemplateValues()],
83  'valid_until' => ['integer', $userCertificate->getValidUntil()],
84  'version' => ['integer', $version],
85  'ilias_version' => ['text', $userCertificate->getIliasVersion()],
86  'currently_active' => ['integer', (int) $userCertificate->isCurrentlyActive()],
87  'background_image_path' => ['text', $userCertificate->getBackgroundImagePath()],
88  'thumbnail_image_path' => ['text', $userCertificate->getThumbnailImagePath()]
89  ];
90 
91  $this->logger->debug(sprintf(
92  'END - Save certificate with following values: %s',
93  json_encode($columns, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
94  ));
95 
96  $this->database->insert('il_cert_user_cert', $columns);
97 
98  return $userCertificate->withId($id)->withVersion($version);
99  }
100 
105  public function fetchActiveCertificates(int $userId): array
106  {
107  $this->logger->debug(sprintf('START - Fetching all active certificates for user: "%s"', $userId));
108 
109  $sql = '
110 SELECT
111  il_cert_user_cert.pattern_certificate_id,
112  il_cert_user_cert.obj_id,
113  il_cert_user_cert.obj_type,
114  il_cert_user_cert.usr_id,
115  il_cert_user_cert.user_name,
116  il_cert_user_cert.acquired_timestamp,
117  il_cert_user_cert.certificate_content,
118  il_cert_user_cert.template_values,
119  il_cert_user_cert.valid_until,
120  il_cert_user_cert.version,
121  il_cert_user_cert.ilias_version,
122  il_cert_user_cert.currently_active,
123  il_cert_user_cert.background_image_path,
124  il_cert_user_cert.id,
125  il_cert_user_cert.thumbnail_image_path,
126  COALESCE(object_data.title, object_data_del.title, ' . $this->database->quote($this->defaultTitle, 'text') . ') AS title
127 FROM il_cert_user_cert
128 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
129 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
130 WHERE usr_id = ' . $this->database->quote($userId, 'integer') . '
131 AND currently_active = 1';
132 
133  $query = $this->database->query($sql);
134 
135  $result = [];
136  while ($row = $this->database->fetchAssoc($query)) {
137  $userCertificate = $this->createUserCertificate($row);
138 
139  $presentation = new ilUserCertificatePresentation(
140  (int) $row['obj_id'],
141  (string) $row['obj_type'],
142  $userCertificate,
143  $row['title'],
144  ''
145  );
146  $result[] = $presentation;
147  }
148 
149  $this->logger->debug(sprintf('Actual results: "%s"', json_encode($result, JSON_THROW_ON_ERROR)));
150  $this->logger->debug(sprintf(
151  'END - All active certificates for user: "%s" total: "%s"',
152  $userId,
153  count($result)
154  ));
155 
156  return $result;
157  }
158 
166  int $userId,
167  int $startTimestamp,
168  int $endTimeStamp
169  ): array {
170  $this->logger->debug(sprintf('START - Fetching all active certificates for user: "%s"', $userId));
171 
172  $sql = '
173 SELECT
174  il_cert_user_cert.pattern_certificate_id,
175  il_cert_user_cert.obj_id,
176  il_cert_user_cert.obj_type,
177  il_cert_user_cert.usr_id,
178  il_cert_user_cert.user_name,
179  il_cert_user_cert.acquired_timestamp,
180  il_cert_user_cert.certificate_content,
181  il_cert_user_cert.template_values,
182  il_cert_user_cert.valid_until,
183  il_cert_user_cert.version,
184  il_cert_user_cert.ilias_version,
185  il_cert_user_cert.currently_active,
186  il_cert_user_cert.background_image_path,
187  il_cert_user_cert.id,
188  il_cert_user_cert.thumbnail_image_path,
189  COALESCE(object_data.title, object_data_del.title, ' . $this->database->quote($this->defaultTitle, 'text') . ') AS title
190 FROM il_cert_user_cert
191 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
192 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
193 WHERE usr_id = ' . $this->database->quote($userId, 'integer') . '
194 AND currently_active = 1
195 AND acquired_timestamp >= ' . $this->database->quote($startTimestamp, 'integer') . '
196 AND acquired_timestamp <= ' . $this->database->quote($endTimeStamp, 'integer');
197 
198  $query = $this->database->query($sql);
199 
200  $result = [];
201  while ($row = $this->database->fetchAssoc($query)) {
202  $userCertificate = $this->createUserCertificate($row);
203 
204  $presentation = new ilUserCertificatePresentation(
205  (int) $row['obj_id'],
206  (string) $row['obj_type'],
207  $userCertificate,
208  $row['title'],
209  ''
210  );
211  $result[] = $presentation;
212  }
213 
214  $this->logger->debug(sprintf('Actual results: "%s"', json_encode($result, JSON_THROW_ON_ERROR)));
215  $this->logger->debug(sprintf(
216  'END - All active certificates for user: "%s" total: "%s"',
217  $userId,
218  count($result)
219  ));
220 
221  return $result;
222  }
223 
230  public function fetchActiveCertificate(int $userId, int $objectId): ilUserCertificate
231  {
232  $this->logger->debug(sprintf(
233  'START - Fetching all active certificates for user: "%s" and object: "%s"',
234  $userId,
235  $objectId
236  ));
237 
238  $sql = 'SELECT *
239 FROM il_cert_user_cert
240 WHERE usr_id = ' . $this->database->quote($userId, 'integer') . '
241 AND obj_id = ' . $this->database->quote($objectId, 'integer') . '
242 AND currently_active = 1';
243 
244  $query = $this->database->query($sql);
245 
246  while ($row = $this->database->fetchAssoc($query)) {
247  $this->logger->debug(sprintf('Active certificate values: %s', json_encode($row, JSON_THROW_ON_ERROR)));
248 
249  $this->logger->debug(sprintf(
250  'END -Found active user certificate for user: "%s" and object: "%s"',
251  $userId,
252  $objectId
253  ));
254 
255  return $this->createUserCertificate($row);
256  }
257 
258  throw new ilException(sprintf(
259  'There is no active entry for user id: "%s" and object id: "%s"',
260  $userId,
261  $objectId
262  ));
263  }
264 
271  public function fetchActiveCertificateForPresentation(int $userId, int $objectId): ilUserCertificatePresentation
272  {
273  $this->logger->debug(sprintf(
274  'START - Fetching all active certificates for user: "%s" and object: "%s"',
275  $userId,
276  $objectId
277  ));
278 
279  $sql = 'SELECT
280  il_cert_user_cert.pattern_certificate_id,
281  il_cert_user_cert.obj_id,
282  il_cert_user_cert.obj_type,
283  il_cert_user_cert.usr_id,
284  il_cert_user_cert.user_name,
285  il_cert_user_cert.acquired_timestamp,
286  il_cert_user_cert.certificate_content,
287  il_cert_user_cert.template_values,
288  il_cert_user_cert.valid_until,
289  il_cert_user_cert.version,
290  il_cert_user_cert.ilias_version,
291  il_cert_user_cert.currently_active,
292  il_cert_user_cert.background_image_path,
293  il_cert_user_cert.id,
294  il_cert_user_cert.thumbnail_image_path,
295  usr_data.lastname,
296  COALESCE(object_data.title, object_data_del.title, ' . $this->database->quote($this->defaultTitle, 'text') . ') AS title
297 FROM il_cert_user_cert
298 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
299 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
300 LEFT JOIN usr_data ON usr_data.usr_id = il_cert_user_cert.usr_id
301 WHERE il_cert_user_cert.usr_id = ' . $this->database->quote($userId, 'integer') . '
302 AND il_cert_user_cert.obj_id = ' . $this->database->quote($objectId, 'integer') . '
303 AND il_cert_user_cert.currently_active = 1';
304 
305  $query = $this->database->query($sql);
306 
307  while ($row = $this->database->fetchAssoc($query)) {
308  $this->logger->debug(sprintf('Active certificate values: %s', json_encode($row, JSON_THROW_ON_ERROR)));
309 
310  $this->logger->debug(sprintf(
311  'END -Found active user certificate for user: "%s" and object: "%s"',
312  $userId,
313  $objectId
314  ));
315 
316  $userCertificate = $this->createUserCertificate($row);
318  (int) $row['obj_id'],
319  (string) $row['obj_type'],
320  $userCertificate,
321  $row['title'],
322  '',
323  $row['lastname']
324  );
325  }
326 
327  throw new ilException(sprintf(
328  'There is no active entry for user id: "%s" and object id: "%s"',
329  $userId,
330  $objectId
331  ));
332  }
333 
339  public function fetchActiveCertificatesByTypeForPresentation(int $userId, string $type): array
340  {
341  $this->logger->debug(sprintf(
342  'START - Fetching all active certificates for user: "%s" and type: "%s"',
343  $userId,
344  $type
345  ));
346 
347  $sql = 'SELECT
348  il_cert_user_cert.pattern_certificate_id,
349  il_cert_user_cert.obj_id,
350  il_cert_user_cert.obj_type,
351  il_cert_user_cert.usr_id,
352  il_cert_user_cert.user_name,
353  il_cert_user_cert.acquired_timestamp,
354  il_cert_user_cert.certificate_content,
355  il_cert_user_cert.template_values,
356  il_cert_user_cert.valid_until,
357  il_cert_user_cert.version,
358  il_cert_user_cert.ilias_version,
359  il_cert_user_cert.currently_active,
360  il_cert_user_cert.background_image_path,
361  il_cert_user_cert.id,
362  il_cert_user_cert.thumbnail_image_path,
363  COALESCE(object_data.title, object_data_del.title, ' . $this->database->quote($this->defaultTitle, 'text') . ') AS title
364 FROM il_cert_user_cert
365 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
366 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
367 WHERE usr_id = ' . $this->database->quote($userId, 'integer') . '
368  AND obj_type = ' . $this->database->quote($type, 'text') . '
369  AND currently_active = 1';
370 
371  $query = $this->database->query($sql);
372 
373  $result = [];
374  while ($row = $this->database->fetchAssoc($query)) {
375  $userCertificate = $this->createUserCertificate($row);
376 
377  $presentation = new ilUserCertificatePresentation(
378  (int) $row['obj_id'],
379  (string) $row['obj_type'],
380  $userCertificate,
381  $row['title'],
382  ''
383  );
384  $result[] = $presentation;
385  }
386 
387  $this->logger->debug(sprintf(
388  'END - Fetching all active certificates for user: "%s" and type: "%s"',
389  $userId,
390  $type
391  ));
392 
393  return $result;
394  }
395 
401  public function fetchCertificate(int $id): ilUserCertificate
402  {
403  $this->logger->debug(sprintf('START - Fetch certificate by id: "%s"', $id));
404 
405  $sql = 'SELECT * FROM il_cert_user_cert WHERE id = ' . $this->database->quote($id, 'integer');
406 
407  $query = $this->database->query($sql);
408 
409  while ($row = $this->database->fetchAssoc($query)) {
410  $this->logger->debug(sprintf('Fetched certificate: "%s"', json_encode($row, JSON_THROW_ON_ERROR)));
411 
412  $this->logger->debug(sprintf('END - Fetch certificate by id: "%s"', $id));
413 
414  return $this->createUserCertificate($row);
415  }
416 
417  throw new ilException('No certificate found for user certificate id: ' . $id);
418  }
419 
425  public function fetchObjectIdsWithCertificateForUser(int $userId, array $objectIds): array
426  {
427  $this->logger->debug(sprintf(
428  'START - Fetch certificate for user("%s") and ids: "%s"',
429  $userId,
430  json_encode($objectIds, JSON_THROW_ON_ERROR)
431  ));
432 
433  if (0 === count($objectIds)) {
434  return [];
435  }
436 
437  $inStatementObjectIds = $this->database->in(
438  'obj_id',
439  $objectIds,
440  false,
441  'integer'
442  );
443 
444  $sql = 'SELECT obj_id FROM il_cert_user_cert
445  WHERE usr_id = ' . $this->database->quote($userId, 'integer') .
446  ' AND ' . $inStatementObjectIds .
447  ' AND currently_active = ' . $this->database->quote(1, 'integer');
448 
449  $query = $this->database->query($sql);
450 
451  $result = [];
452 
453  while ($row = $this->database->fetchAssoc($query)) {
454  $this->logger->debug(sprintf('Fetched certificate: "%s"', json_encode($row, JSON_THROW_ON_ERROR)));
455  $result[] = (int) $row['obj_id'];
456  }
457 
458  return $result;
459  }
460 
465  public function fetchUserIdsWithCertificateForObject(int $objectId): array
466  {
467  $this->logger->debug(sprintf('START - Fetch certificate for object("%s")"', $objectId));
468 
469  $sql = 'SELECT usr_id FROM il_cert_user_cert
470 WHERE obj_id = ' . $this->database->quote($objectId, 'integer') . '
471  AND currently_active = ' . $this->database->quote(1, 'integer');
472 
473  $query = $this->database->query($sql);
474 
475  $result = [];
476 
477  while ($row = $this->database->fetchAssoc($query)) {
478  $this->logger->debug(sprintf('Fetched certificate: "%s"', json_encode($row, JSON_THROW_ON_ERROR)));
479  $result[] = (int) $row['usr_id'];
480  }
481 
482  return $result;
483  }
484 
485  public function deleteUserCertificates(int $userId): void
486  {
487  $this->logger->debug(sprintf('START - Delete certificate for user("%s")"', $userId));
488 
489  $sql = 'DELETE FROM il_cert_user_cert WHERE usr_id = ' . $this->database->quote($userId, 'integer');
490 
491  $this->database->manipulate($sql);
492 
493  $this->logger->debug(sprintf('END - Successfully deleted certificate for user("%s")"', $userId));
494  }
495 
501  private function fetchCertificatesOfObject(int $objId, int $userId): array
502  {
503  $this->logger->debug(sprintf(
504  'START - fetching all certificates of object(user id: "%s", object id: "%s")',
505  $userId,
506  $objId
507  ));
508 
509  $sql = 'SELECT * FROM il_cert_user_cert
510 WHERE usr_id = ' . $this->database->quote($userId, 'integer') . '
511 AND obj_id = ' . $this->database->quote($objId, 'integer');
512 
513  $query = $this->database->query($sql);
514 
515  $result = [];
516  while ($row = $this->database->fetchAssoc($query)) {
517  $this->logger->debug(sprintf(
518  'Certificate found: "%s")',
519  json_encode($row, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
520  ));
521 
522  $this->logger->debug(sprintf('Certificate: "%s"', json_encode($row, JSON_THROW_ON_ERROR)));
523 
524  $result[] = $this->createUserCertificate($row);
525  }
526 
527  $this->logger->debug(sprintf(
528  'END - fetching all certificates of object(user id: "%s", object id: "%s")',
529  $userId,
530  $objId
531  ));
532 
533  return $result;
534  }
535 
536  private function fetchLatestVersion(int $objId, int $userId): string
537  {
538  $this->logger->debug(sprintf(
539  'START - fetching of latest certificates of object(user id: "%s", object id: "%s")',
540  $userId,
541  $objId
542  ));
543 
544  $templates = $this->fetchCertificatesOfObject($objId, $userId);
545 
546  $version = 0;
547  foreach ($templates as $template) {
548  if ($template->getVersion() > $version) {
549  $version = $template->getVersion();
550  }
551  }
552 
553  $this->logger->debug(sprintf(
554  'END - fetching of latest certificates of object(user id: "%s", object id: "%s") with version "%s"',
555  $userId,
556  $objId,
557  $version
558  ));
559 
560  return (string) $version;
561  }
562 
563  private function deactivatePreviousCertificates(int $objId, int $userId): void
564  {
565  $this->logger->debug(sprintf(
566  'START - deactivating previous certificates for user id: "%s" and object id: "%s"',
567  $userId,
568  $objId
569  ));
570 
571  $sql = '
572 UPDATE il_cert_user_cert
573 SET currently_active = 0
574 WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
575 AND usr_id = ' . $this->database->quote($userId, 'integer');
576 
577  $this->database->manipulate($sql);
578 
579  $this->logger->debug(sprintf(
580  'END - deactivating previous certificates for user id: "%s" and object id: "%s"',
581  $userId,
582  $objId
583  ));
584  }
585 
586  public function isBackgroundImageUsed(string $relative_image_path): bool
587  {
588  $this->logger->debug(sprintf(
589  'START - Checking if any certificate template uses background image path "%s"',
590  $relative_image_path
591  ));
592 
593  $result = $this->database->queryF(
594  'SELECT EXISTS(SELECT 1 FROM il_cert_user_cert WHERE background_image_path = %s AND currently_active = 1) AS does_exist',
595  ['text'],
596  [$relative_image_path]
597  );
598 
599  $exists = (bool) ($this->database->fetchAssoc($result)['does_exist'] ?? false);
600 
601  $this->logger->debug(sprintf(
602  'END - Image path "%s" is ' . $exists ? "in use" : "unused",
603  $relative_image_path
604  ));
605 
606  return $exists;
607  }
608 
613  private function createUserCertificate(array $row): ilUserCertificate
614  {
615  return new ilUserCertificate(
616  (int) $row['pattern_certificate_id'],
617  (int) $row['obj_id'],
618  $row['obj_type'],
619  (int) $row['usr_id'],
620  $row['user_name'],
621  (int) $row['acquired_timestamp'],
622  $row['certificate_content'],
623  $row['template_values'],
624  (int) $row['valid_until'],
625  (int) $row['version'],
626  $row['ilias_version'],
627  (bool) $row['currently_active'],
628  (string) $row['background_image_path'],
629  (string) $row['thumbnail_image_path'],
630  isset($row['id']) ? (int) $row['id'] : null
631  );
632  }
633 }
fetchActiveCertificatesByTypeForPresentation(int $userId, string $type)
isBackgroundImageUsed(string $relative_image_path)
$type
fetchActiveCertificatesInIntervalForPresentation(int $userId, int $startTimestamp, int $endTimeStamp)
save(ilUserCertificate $userCertificate)
$objId
Definition: xapitoken.php:57
fetchActiveCertificate(int $userId, int $objectId)
global $DIC
Definition: feed.php:28
deactivatePreviousCertificates(int $objId, int $userId)
__construct(?ilDBInterface $database=null, ?ilLogger $logger=null, ?string $defaultTitle=null)
$query
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
fetchObjectIdsWithCertificateForUser(int $userId, array $objectIds)
$version
Definition: plugin.php:24
fetchActiveCertificateForPresentation(int $userId, int $objectId)