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