ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 $this->database->insert(self::TABLE_NAME, $columns);
77
78 $this->logger->debug(
79 sprintf(
80 'END - certificate template saved with columns: %s',
81 json_encode($columns, JSON_THROW_ON_ERROR)
82 )
83 );
84 }
85
86 public function updateActivity(ilCertificateTemplate $certificateTemplate, bool $currentlyActive): int
87 {
88 $sql = 'UPDATE ' . self::TABLE_NAME . ' SET currently_active = ' . $this->database->quote(
89 $currentlyActive,
91 ) .
92 ' WHERE id = ' . $this->database->quote($certificateTemplate->getId(), ilDBConstants::T_INTEGER);
93
94 return $this->database->manipulate($sql);
95 }
96
97 public function fetchTemplate(int $templateId): ilCertificateTemplate
98 {
99 $this->logger->debug(sprintf('START - Fetch certificate template with id: "%s"', $templateId));
100
101 $sql = '
102 SELECT * FROM
103 ' . self::TABLE_NAME . '
104 WHERE id = ' . $this->database->quote($templateId, ilDBConstants::T_INTEGER) . '
105 ORDER BY version ASC
106 ';
107
108 $query = $this->database->query($sql);
109
110 while ($row = $this->database->fetchAssoc($query)) {
111 return $this->createCertificateTemplate($row);
112 }
113
114 throw new ilCouldNotFindCertificateTemplate(sprintf('No template with id "%s" found', $templateId));
115 }
116
120 public function fetchCertificateTemplatesByObjId(int $objId): array
121 {
122 $this->logger->debug(sprintf('START - Fetch multiple certificate templates for object: "%s"', $objId));
123
124 $result = [];
125
126 $sql = '
127 SELECT * FROM ' .
128 self::TABLE_NAME . ' ' .
129 'WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . ' ' .
130 'AND deleted = 0 ' .
131 'ORDER BY version ASC'
132 ;
133
134 $query = $this->database->query($sql);
135
136 while ($row = $this->database->fetchAssoc($query)) {
137 $result[] = $this->createCertificateTemplate($row);
138 }
139
140 $this->logger->debug(
141 sprintf(
142 'END - Fetching of certificate templates for object: "%s" with "%s" results',
143 $objId,
144 count($result)
145 )
146 );
147
148 return $result;
149 }
150
152 {
153 $this->logger->debug(sprintf('START - Fetch currently active certificate template for object: "%s"', $objId));
154
155 $this->database->setLimit(1);
156
157 $sql = '
158 SELECT * FROM ' . self::TABLE_NAME . '
159 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . '
160 AND deleted = 0
161 ORDER BY id DESC
162 ';
163
164 $query = $this->database->query($sql);
165
166 while ($row = $this->database->fetchAssoc($query)) {
167 $this->logger->debug(sprintf('END - Found active certificate for: "%s"', $objId));
168
169 return $this->createCertificateTemplate($row);
170 }
171
172 $this->logger->debug(sprintf('END - Found NO active certificate for: "%s"', $objId));
173
174 return new ilCertificateTemplate(
175 $objId,
176 $this->objectDataCache->lookupType($objId),
177 '',
178 '',
179 '',
180 0,
181 '0',
182 0,
183 false,
184 '',
185 ''
186 );
187 }
188
193 {
194 $this->logger->debug(sprintf('START - Fetch currently active certificate template for object: "%s"', $objId));
195
196 $sql = '
197 SELECT * FROM ' . self::TABLE_NAME . '
198 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . '
199 AND deleted = 0
200 AND currently_active = 1
201 ';
202
203 $query = $this->database->query($sql);
204
205 while ($row = $this->database->fetchAssoc($query)) {
206 $this->logger->debug(sprintf('END - Found active certificate for: "%s"', $objId));
207
208 return $this->createCertificateTemplate($row);
209 }
210
211 throw new ilCouldNotFindCertificateTemplate(sprintf('NO active certificate template found for: "%s"', $objId));
212 }
213
215 {
216 $this->logger->debug(sprintf('START - Fetch previous active certificate template for object: "%s"', $objId));
217
218 $templates = $this->fetchCertificateTemplatesByObjId($objId);
219
220 $resultTemplate = new ilCertificateTemplate(
221 $objId,
222 $this->objectDataCache->lookupType($objId),
223 '',
224 '',
225 '',
226 0,
227 '0',
228 0,
229 true,
230 '',
231 ''
232 );
233
234 $version = 0;
235 foreach ($templates as $template) {
236 if ($template->getVersion() > $version) {
237 $version = $template->getVersion();
238 $resultTemplate = $template;
239 }
240 }
241
242 $this->logger->debug(sprintf('Latest version active certificate template for object: "%s"', $objId));
243
244 return $resultTemplate;
245 }
246
247 public function deleteTemplate(int $templateId, int $objectId): void
248 {
249 $this->logger->debug(
250 sprintf(
251 'START - Set deleted flag for certificate template("%s") for object: "%s"',
252 $templateId,
253 $objectId
254 )
255 );
256
257 $sql = '
258 UPDATE ' . self::TABLE_NAME . '
259 SET deleted = 1, currently_active = 0
260 WHERE id = ' . $this->database->quote($templateId, ilDBConstants::T_INTEGER) . '
261 AND obj_id = ' . $this->database->quote($objectId, ilDBConstants::T_INTEGER);
262
263 $this->database->manipulate($sql);
264
265 $this->logger->debug(
266 sprintf(
267 'END - Deleted flag set fo certificate template("%s") for object: "%s"',
268 $templateId,
269 $objectId
270 )
271 );
272 }
273
275 {
276 $this->logger->debug(sprintf('START - Activate previous certificate template for object: "%s"', $objId));
277
278 $certificates = $this->fetchCertificateTemplatesByObjId($objId);
279
281 $previousCertificate = null;
282 foreach ($certificates as $certificate) {
283 if (null === $previousCertificate) {
284 $previousCertificate = $certificate;
285 } elseif ($certificate->getVersion() > $previousCertificate->getVersion()) {
286 $previousCertificate = $certificate;
287 }
288 }
289
290 $sql = 'UPDATE ' . self::TABLE_NAME . '
291 SET currently_active = 1
292 WHERE id = ' . $this->database->quote($previousCertificate->getId(), ilDBConstants::T_INTEGER);
293
294 $this->database->manipulate($sql);
295
296 $this->logger->debug(sprintf('END - Previous certificate updated for object: "%s"', $objId));
297
298 return $previousCertificate;
299 }
300
302 bool $isGlobalLpEnabled,
303 ?int $forRefId = null
304 ): array {
305 $this->logger->debug(
306 'START - Fetch all active course certificate templates with disabled learning progress: "%s"'
307 );
308
309 $joinLpSettings = '';
310 $whereLpSettings = '';
311 $onSettingsForRefId = '';
312
313 if ($isGlobalLpEnabled) {
314 $joinLpSettings = 'LEFT JOIN ut_lp_settings uls ON uls.obj_id = od.obj_id';
315 $whereLpSettings = sprintf(
316 'AND (uls.u_mode IS NULL OR uls.u_mode = %s)',
317 $this->database->quote(ilLPObjSettings::LP_MODE_DEACTIVATED, 'integer')
318 );
319 }
320
321 if (is_int($forRefId)) {
322 $onSettingsForRefId = " AND settings.value IS NOT NULL AND (JSON_CONTAINS(settings.value, '\"{$forRefId}\"', '$') = 1 OR JSON_CONTAINS(settings.value, '{$forRefId}', '$')) ";
323 }
324
325 $sql = '
326 SELECT ' . self::TABLE_NAME . '.*
327 FROM ' . self::TABLE_NAME . '
328 INNER JOIN object_data od ON od.obj_id = ' . self::TABLE_NAME . ".obj_id
329 INNER JOIN settings ON settings.module = %s AND settings.keyword = {$this->database->concat(
330 [
331 [$this->database->quote('cert_subitems_', 'text'), 'text'],
332 ['od.obj_id', 'text']
333 ],
334 false
335 )} $onSettingsForRefId $joinLpSettings
336 WHERE " . self::TABLE_NAME . '.obj_type = %s
337 AND ' . self::TABLE_NAME . '.currently_active = %s
338 ' . $whereLpSettings;
339 $query = $this->database->queryF(
340 $sql,
341 ['text', 'text', 'integer'],
342 ['crs', 'crs', 1]
343 );
344
345 $result = [];
346 while ($row = $this->database->fetchAssoc($query)) {
347 $result[] = $this->createCertificateTemplate($row);
348 }
349
350 $this->logger->debug(
351 sprintf(
352 'END - All active course certificate templates with disabled learning progress: "%s"',
353 json_encode($result, JSON_THROW_ON_ERROR)
354 )
355 );
356
357 return $result;
358 }
359
364 {
365 $this->logger->debug(sprintf('START - Fetch first create certificate template for object: "%s"', $objId));
366
367 $this->database->setLimit(1, 0);
368
369 $sql = '
370 SELECT * FROM ' . self::TABLE_NAME . '
371 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER) . '
372 ORDER BY id ASC
373 ';
374
375 $query = $this->database->query($sql);
376
377 while ($row = $this->database->fetchAssoc($query)) {
378 $this->logger->debug(sprintf('END - Found first create certificate template for object: "%s"', $objId));
379
380 return $this->createCertificateTemplate($row);
381 }
382
383 throw new ilCouldNotFindCertificateTemplate('No matching template found. MAY missing DBUpdate. Please check if the correct version is installed.');
384 }
385
386 private function deactivatePreviousTemplates(int $objId): void
387 {
388 $this->logger->debug(sprintf('START - Deactivate previous certificate template for object: "%s"', $objId));
389
390 $sql = '
391 UPDATE ' . self::TABLE_NAME . '
392 SET currently_active = 0
393 WHERE obj_id = ' . $this->database->quote($objId, ilDBConstants::T_INTEGER);
394
395 $this->database->manipulate($sql);
396
397 $this->logger->debug(sprintf('END - Certificate template deactivated for object: "%s"', $objId));
398 }
399
400 public function isResourceUsed(string $relative_image_identification): bool
401 {
402 $this->logger->debug(
403 sprintf(
404 'START - Checking if any certificate template uses resource id "%s"',
405 $relative_image_identification
406 )
407 );
408
409 $result = $this->database->queryF(
410 'SELECT EXISTS(SELECT 1 FROM ' . self::TABLE_NAME . ' WHERE
411 (background_image_ident = %s OR tile_image_ident = %s)
412 AND currently_active = 1) AS does_exist',
414 [$relative_image_identification, $relative_image_identification]
415 );
416
417 $exists = (bool) ($this->database->fetchAssoc($result)['does_exist'] ?? false);
418
419 $this->logger->debug(
420 sprintf(
421 'END - Image identification "%s" is ' . $exists ? 'in use' : 'unused',
422 $relative_image_identification
423 )
424 );
425
426 return $exists;
427 }
428
433 {
434 return new ilCertificateTemplate(
435 (int) $row['obj_id'],
436 $row['obj_type'],
437 $row['certificate_content'],
438 $row['certificate_hash'],
439 $row['template_values'],
440 (int) $row['version'],
441 $row['ilias_version'],
442 (int) $row['created_timestamp'],
443 (bool) $row['currently_active'],
444 (string) ($row['background_image_ident'] ?? ''),
445 (string) ($row['tile_image_ident'] ?? ''),
446 isset($row['id']) ? (int) $row['id'] : null
447 );
448 }
449}
$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)
Component logger with individual log levels by component id.
class ilObjectDataCache
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26
$objId
Definition: xapitoken.php:55