ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCertificateTemplateDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const TABLE_NAME = 'il_cert_template';
29 private readonly ilLogger $logger;
31
32 public function __construct(
33 private readonly ilDBInterface $database,
34 ?ilLogger $logger = null,
36 ) {
37 if (null === $logger) {
38 global $DIC;
39 $logger = $DIC->logger()->cert();
40 }
41 $this->logger = $logger;
42
43 if (null === $objectDataCache) {
44 global $DIC;
45 $objectDataCache = $DIC['ilObjDataCache'];
46 }
48 }
49
50 public function save(ilCertificateTemplate $certificateTemplate): void
51 {
52 $this->logger->debug('START - Save new certificate template');
53
54 $objId = $certificateTemplate->getObjId();
55
56 $id = $this->database->nextId(self::TABLE_NAME);
57
59
60 $columns = [
61 'id' => ['integer', $id],
62 'obj_id' => ['integer', $objId],
63 'obj_type' => ['text', $certificateTemplate->getObjType()],
64 'certificate_content' => ['clob', $certificateTemplate->getCertificateContent()],
65 'certificate_hash' => ['text', $certificateTemplate->getCertificateHash()],
66 'template_values' => ['clob', $certificateTemplate->getTemplateValues()],
67 'version' => ['integer', $certificateTemplate->getVersion()],
68 'ilias_version' => ['text', $certificateTemplate->getIliasVersion()],
69 'created_timestamp' => ['integer', $certificateTemplate->getCreatedTimestamp()],
70 'currently_active' => ['integer', (int) $certificateTemplate->isCurrentlyActive()],
71 'deleted' => ['integer', (int) $certificateTemplate->isDeleted()],
72 'background_image_ident' => [ilDBConstants::T_TEXT, $certificateTemplate->getBackgroundImageIdentification()],
73 'tile_image_ident' => [ilDBConstants::T_TEXT, $certificateTemplate->getTileImageIdentification()]
74 ];
75
76 if (
77 $this->database->tableColumnExists('il_cert_user_cert', 'background_image_path') &&
78 $this->database->tableColumnExists('il_cert_user_cert', 'tile_image_path')
79 ) {
80 $columns['background_image_path'] = [ilDBConstants::T_TEXT, $certificateTemplate->getBackgroundImagePath()];
81 $columns['tile_image_path'] = [ilDBConstants::T_TEXT, $certificateTemplate->getTileImagePath()];
82 }
83
84 $this->database->insert(self::TABLE_NAME, $columns);
85
86 $this->logger->debug(
87 sprintf(
88 'END - certificate template saved with columns: %s',
89 json_encode($columns, JSON_THROW_ON_ERROR)
90 )
91 );
92 }
93
94 public function updateActivity(ilCertificateTemplate $certificateTemplate, bool $currentlyActive): int
95 {
96 $sql = 'UPDATE ' . self::TABLE_NAME . ' SET currently_active = ' . $this->database->quote(
97 $currentlyActive,
99 ) .
100 ' WHERE id = ' . $this->database->quote($certificateTemplate->getId(), ilDBConstants::T_INTEGER);
101
102 return $this->database->manipulate($sql);
103 }
104
105 public function fetchTemplate(int $templateId): ilCertificateTemplate
106 {
107 $this->logger->debug(sprintf('START - Fetch certificate template with id: "%s"', $templateId));
108
109 $sql = '
110 SELECT * FROM
111 ' . self::TABLE_NAME . '
112 WHERE id = ' . $this->database->quote($templateId, ilDBConstants::T_INTEGER) . '
113 ORDER BY version ASC
114 ';
115
116 $query = $this->database->query($sql);
117
118 while ($row = $this->database->fetchAssoc($query)) {
119 return $this->createCertificateTemplate($row);
120 }
121
122 throw new ilCouldNotFindCertificateTemplate(sprintf('No template with id "%s" found', $templateId));
123 }
124
128 public function fetchCertificateTemplatesByObjId(int $objId): array
129 {
130 $this->logger->debug(sprintf('START - Fetch multiple certificate templates for object: "%s"', $objId));
131
132 $result = [];
133
134 $sql = '
135 SELECT * FROM ' .
136 self::TABLE_NAME . ' ' .
137 'WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . ' ' .
138 'AND deleted = 0 ' .
139 'ORDER BY version ASC'
140 ;
141
142 $query = $this->database->query($sql);
143
144 while ($row = $this->database->fetchAssoc($query)) {
145 $result[] = $this->createCertificateTemplate($row);
146 }
147
148 $this->logger->debug(
149 sprintf(
150 'END - Fetching of certificate templates for object: "%s" with "%s" results',
151 $objId,
152 count($result)
153 )
154 );
155
156 return $result;
157 }
158
160 {
161 $this->logger->debug(sprintf('START - Fetch currently active certificate template for object: "%s"', $objId));
162
163 $this->database->setLimit(1);
164
165 $sql = '
166 SELECT * FROM ' . self::TABLE_NAME . '
167 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . '
168 AND deleted = 0
169 ORDER BY id DESC
170 ';
171
172 $query = $this->database->query($sql);
173
174 while ($row = $this->database->fetchAssoc($query)) {
175 $this->logger->debug(sprintf('END - Found active certificate for: "%s"', $objId));
176
177 return $this->createCertificateTemplate($row);
178 }
179
180 $this->logger->debug(sprintf('END - Found NO active certificate for: "%s"', $objId));
181
182 return new ilCertificateTemplate(
183 $objId,
184 $this->objectDataCache->lookupType($objId),
185 '',
186 '',
187 '',
188 0,
189 '0',
190 0,
191 false,
192 '',
193 '',
194 '',
195 ''
196 );
197 }
198
203 {
204 $this->logger->debug(sprintf('START - Fetch currently active certificate template for object: "%s"', $objId));
205
206 $sql = '
207 SELECT * FROM ' . self::TABLE_NAME . '
208 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . '
209 AND deleted = 0
210 AND currently_active = 1
211 ';
212
213 $query = $this->database->query($sql);
214
215 while ($row = $this->database->fetchAssoc($query)) {
216 $this->logger->debug(sprintf('END - Found active certificate for: "%s"', $objId));
217
218 return $this->createCertificateTemplate($row);
219 }
220
221 throw new ilCouldNotFindCertificateTemplate(sprintf('NO active certificate template found for: "%s"', $objId));
222 }
223
225 {
226 $this->logger->debug(sprintf('START - Fetch previous active certificate template for object: "%s"', $objId));
227
228 $templates = $this->fetchCertificateTemplatesByObjId($objId);
229
230 $resultTemplate = new ilCertificateTemplate(
231 $objId,
232 $this->objectDataCache->lookupType($objId),
233 '',
234 '',
235 '',
236 0,
237 '0',
238 0,
239 true,
240 '',
241 '',
242 '',
243 ''
244 );
245
246 $version = 0;
247 foreach ($templates as $template) {
248 if ($template->getVersion() > $version) {
249 $version = $template->getVersion();
250 $resultTemplate = $template;
251 }
252 }
253
254 $this->logger->debug(sprintf('Latest version active certificate template for object: "%s"', $objId));
255
256 return $resultTemplate;
257 }
258
259 public function deleteTemplate(int $templateId, int $objectId): void
260 {
261 $this->logger->debug(
262 sprintf(
263 'START - Set deleted flag for certificate template("%s") for object: "%s"',
264 $templateId,
265 $objectId
266 )
267 );
268
269 $sql = '
270 UPDATE ' . self::TABLE_NAME . '
271 SET deleted = 1, currently_active = 0
272 WHERE id = ' . $this->database->quote($templateId, ilDBConstants::T_INTEGER) . '
273 AND obj_id = ' . $this->database->quote($objectId, ilDBConstants::T_INTEGER);
274
275 $this->database->manipulate($sql);
276
277 $this->logger->debug(
278 sprintf(
279 'END - Deleted flag set fo certificate template("%s") for object: "%s"',
280 $templateId,
281 $objectId
282 )
283 );
284 }
285
287 {
288 $this->logger->debug(sprintf('START - Activate previous certificate template for object: "%s"', $objId));
289
290 $certificates = $this->fetchCertificateTemplatesByObjId($objId);
291
293 $previousCertificate = null;
294 foreach ($certificates as $certificate) {
295 if (null === $previousCertificate) {
296 $previousCertificate = $certificate;
297 } elseif ($certificate->getVersion() > $previousCertificate->getVersion()) {
298 $previousCertificate = $certificate;
299 }
300 }
301
302 $sql = 'UPDATE ' . self::TABLE_NAME . '
303 SET currently_active = 1
304 WHERE id = ' . $this->database->quote($previousCertificate->getId(), ilDBConstants::T_INTEGER);
305
306 $this->database->manipulate($sql);
307
308 $this->logger->debug(sprintf('END - Previous certificate updated for object: "%s"', $objId));
309
310 return $previousCertificate;
311 }
312
314 bool $isGlobalLpEnabled,
315 ?int $forRefId = null
316 ): array {
317 $this->logger->debug(
318 'START - Fetch all active course certificate templates with disabled learning progress: "%s"'
319 );
320
321 $joinLpSettings = '';
322 $whereLpSettings = '';
323 $onSettingsForRefId = '';
324
325 if ($isGlobalLpEnabled) {
326 $joinLpSettings = 'LEFT JOIN ut_lp_settings uls ON uls.obj_id = od.obj_id';
327 $whereLpSettings = sprintf(
328 'AND (uls.u_mode IS NULL OR uls.u_mode = %s)',
329 $this->database->quote(ilLPObjSettings::LP_MODE_DEACTIVATED, 'integer')
330 );
331 }
332
333 if (is_int($forRefId)) {
334 $onSettingsForRefId = " AND settings.value IS NOT NULL AND (JSON_CONTAINS(settings.value, '\"{$forRefId}\"', '$') = 1 OR JSON_CONTAINS(settings.value, '{$forRefId}', '$')) ";
335 }
336
337 $sql = '
338 SELECT ' . self::TABLE_NAME . '.*
339 FROM ' . self::TABLE_NAME . '
340 INNER JOIN object_data od ON od.obj_id = ' . self::TABLE_NAME . ".obj_id
341 INNER JOIN settings ON settings.module = %s AND settings.keyword = {$this->database->concat(
342 [
343 [$this->database->quote('cert_subitems_', 'text'), 'text'],
344 ['od.obj_id', 'text']
345 ],
346 false
347 )} $onSettingsForRefId $joinLpSettings
348 WHERE " . self::TABLE_NAME . '.obj_type = %s
349 AND ' . self::TABLE_NAME . '.currently_active = %s
350 ' . $whereLpSettings;
351 $query = $this->database->queryF(
352 $sql,
353 ['text', 'text', 'integer'],
354 ['crs', 'crs', 1]
355 );
356
357 $result = [];
358 while ($row = $this->database->fetchAssoc($query)) {
359 $result[] = $this->createCertificateTemplate($row);
360 }
361
362 $this->logger->debug(
363 sprintf(
364 'END - All active course certificate templates with disabled learning progress: "%s"',
365 json_encode($result, JSON_THROW_ON_ERROR)
366 )
367 );
368
369 return $result;
370 }
371
376 {
377 $this->logger->debug(sprintf('START - Fetch first create certificate template for object: "%s"', $objId));
378
379 $this->database->setLimit(1, 0);
380
381 $sql = '
382 SELECT * FROM ' . self::TABLE_NAME . '
383 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . '
384 ORDER BY id ASC
385 ';
386
387 $query = $this->database->query($sql);
388
389 while ($row = $this->database->fetchAssoc($query)) {
390 $this->logger->debug(sprintf('END - Found first create certificate template for object: "%s"', $objId));
391
392 return $this->createCertificateTemplate($row);
393 }
394
395 throw new ilCouldNotFindCertificateTemplate('No matching template found. MAY missing DBUpdate. Please check if the correct version is installed.');
396 }
397
398 private function deactivatePreviousTemplates(int $objId): void
399 {
400 $this->logger->debug(sprintf('START - Deactivate previous certificate template for object: "%s"', $objId));
401
402 $sql = '
403 UPDATE ' . self::TABLE_NAME . '
404 SET currently_active = 0
405 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER);
406
407 $this->database->manipulate($sql);
408
409 $this->logger->debug(sprintf('END - Certificate template deactivated for object: "%s"', $objId));
410 }
411
412 public function updateDefaultBackgroundImagePaths(string $old_relative_path, string $new_relative_path): void
413 {
414 $this->logger->debug(
415 sprintf(
416 'START - Update all default background image paths from "%s" to "%s"',
417 $old_relative_path,
418 $new_relative_path
419 )
420 );
421
422 $affected_rows = $this->database->manipulateF(
423 'UPDATE ' . self::TABLE_NAME . ' SET background_image_ident = %s ' .
424 'WHERE currently_active = 1 AND (background_image_ident = %s OR background_image_ident = %s )',
425 [
426 'text',
427 'text',
428 'text'
429 ],
430 [
431 $new_relative_path,
432 $old_relative_path,
433 '/certificates/default/background.jpg'
434 ]
435 );
436
437 $this->logger->debug(
438 sprintf(
439 'END - Updated %s certificate templates using old path',
440 $affected_rows
441 )
442 );
443 }
444
445 public function isResourceUsed(string $relative_image_identification): bool
446 {
447 $this->logger->debug(
448 sprintf(
449 'START - Checking if any certificate template uses resource id "%s"',
450 $relative_image_identification
451 )
452 );
453
454 $result = $this->database->queryF(
455 'SELECT EXISTS(SELECT 1 FROM ' . self::TABLE_NAME . ' WHERE
456 (background_image_ident = %s OR tile_image_ident = %s)
457 AND currently_active = 1) AS does_exist',
459 [$relative_image_identification, $relative_image_identification]
460 );
461
462 $exists = (bool) ($this->database->fetchAssoc($result)['does_exist'] ?? false);
463
464 $this->logger->debug(
465 sprintf(
466 'END - Image path "%s" is ' . $exists ? 'in use' : 'unused',
467 $relative_image_identification
468 )
469 );
470
471 return $exists;
472 }
473
478 {
479 return new ilCertificateTemplate(
480 (int) $row['obj_id'],
481 $row['obj_type'],
482 $row['certificate_content'],
483 $row['certificate_hash'],
484 $row['template_values'],
485 (int) $row['version'],
486 $row['ilias_version'],
487 (int) $row['created_timestamp'],
488 (bool) $row['currently_active'],
489 (string) ($row['background_image_path'] ?? ''),
490 (string) ($row['tile_image_path'] ?? ''),
491 (string) ($row['background_image_ident'] ?? ''),
492 (string) ($row['tile_image_ident'] ?? ''),
493 isset($row['id']) ? (int) $row['id'] : null
494 );
495 }
496}
$version
Definition: plugin.php:24
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(private readonly ilDBInterface $database, ?ilLogger $logger=null, ?ilObjectDataCache $objectDataCache=null)
updateActivity(ilCertificateTemplate $certificateTemplate, bool $currentlyActive)
fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(bool $isGlobalLpEnabled, ?int $forRefId=null)
updateDefaultBackgroundImagePaths(string $old_relative_path, string $new_relative_path)
Component logger with individual log levels by component id.
class ilObjectDataCache
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26
$objId
Definition: xapitoken.php:57