ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilCertificateTemplateRepository.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
11{
15 private $database;
16
20 private $logger;
21
26
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
56 public function save(ilCertificateTemplate $certificateTemplate)
57 {
58 $this->logger->info('START - Save new certificate template');
59
60 $objId = $certificateTemplate->getObjId();
61
62 $id = $this->database->nextId('il_cert_template');
63
65
66 $columns = array(
67 'id' => array('integer', $id),
68 'obj_id' => array('integer', $objId),
69 'obj_type' => array('text', $certificateTemplate->getObjType()),
70 'certificate_content' => array('clob', $certificateTemplate->getCertificateContent()),
71 'certificate_hash' => array('text', $certificateTemplate->getCertificateHash()),
72 'template_values' => array('clob', $certificateTemplate->getTemplateValues()),
73 'version' => array('integer', $certificateTemplate->getVersion()),
74 'ilias_version' => array('text', $certificateTemplate->getIliasVersion()),
75 'created_timestamp' => array('integer', $certificateTemplate->getCreatedTimestamp()),
76 'currently_active' => array('integer', (integer) $certificateTemplate->isCurrentlyActive()),
77 'background_image_path' => array('text', $certificateTemplate->getBackgroundImagePath()),
78 'deleted' => array('integer', (integer) $certificateTemplate->isDeleted()),
79 'thumbnail_image_path' => array('text', $certificateTemplate->getThumbnailImagePath())
80 );
81
82 $this->database->insert('il_cert_template', $columns);
83
84 $this->logger->info('END - certificate template saved with columns: ', json_encode($columns));
85 }
86
87
88 public function updateActivity(ilCertificateTemplate $certificateTemplate, bool $currentlyActive)
89 {
90 $sql = 'UPDATE il_cert_template SET currently_active = ' . $this->database->quote($currentlyActive, 'integer') .
91 ' WHERE id = ' . $this->database->quote($certificateTemplate->getId(), 'integer');
92
93 return $this->database->manipulate($sql);
94 }
95
96 public function fetchTemplate(int $templateId) : ilCertificateTemplate
97 {
98 $this->logger->info(sprintf('START - Fetch certificate template with id: "%s"', $templateId));
99
100 $sql = '
101SELECT * FROM
102il_cert_template
103WHERE id = ' . $this->database->quote($templateId, 'integer') . '
104ORDER BY version ASC';
105
106 $query = $this->database->query($sql);
107
108 while ($row = $this->database->fetchAssoc($query)) {
109 return $this->createCertificateTemplate($row);
110 }
111
112 throw new ilException(sprintf('No template with id "%s" found', $templateId));
113 }
114
119 public function fetchCertificateTemplatesByObjId(int $objId) : array
120 {
121 $this->logger->info(sprintf('START - Fetch multiple certificate templates for object: "%s"', $objId));
122
123 $result = array();
124
125 $sql = '
126SELECT * FROM
127il_cert_template
128WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
129AND deleted = 0
130ORDER BY version ASC';
131
132 $query = $this->database->query($sql);
133
134 while ($row = $this->database->fetchAssoc($query)) {
135 $result[] = $this->createCertificateTemplate($row);
136 }
137
138 $this->logger->info(sprintf('END - Fetching of certificate templates for object: "%s" with "%s" results', $objId, count($result)));
139
140 return $result;
141 }
142
148 {
149 $this->logger->info(sprintf('START - Fetch currently active certificate template for object: "%s"', $objId));
150
151 $this->database->setLimit(1);
152
153 $sql = '
154SELECT * FROM il_cert_template
155WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
156AND deleted = 0
157ORDER BY id DESC
158';
159
160 $query = $this->database->query($sql);
161
162 while ($row = $this->database->fetchAssoc($query)) {
163 $this->logger->info(sprintf('END - Found active certificate for: "%s"', $objId));
164
165 return $this->createCertificateTemplate($row);
166 }
167
168 $this->logger->info(sprintf('END - Found NO active certificate for: "%s"', $objId));
169
170 return new ilCertificateTemplate(
171 $objId,
172 $this->objectDataCache->lookUpType($objId),
173 '',
174 '',
175 '',
176 0,
177 0,
178 0,
179 false,
180 '',
181 ''
182 );
183 }
184
191 {
192 $this->logger->info(sprintf('START - Fetch currently active certificate template for object: "%s"', $objId));
193
194 $sql = '
195SELECT * FROM il_cert_template
196WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
197AND deleted = 0
198AND currently_active = 1
199';
200
201 $query = $this->database->query($sql);
202
203 while ($row = $this->database->fetchAssoc($query)) {
204 $this->logger->info(sprintf('END - Found active certificate for: "%s"', $objId));
205
206 return $this->createCertificateTemplate($row);
207 }
208
209 throw new ilException((sprintf('NO active certificate template found for: "%s"', $objId)));
210 }
211
219 {
220 $this->logger->info(sprintf('START - Fetch previous active certificate template for object: "%s"', $objId));
221
222 $templates = $this->fetchCertificateTemplatesByObjId($objId);
223
224 $resultTemplate = new ilCertificateTemplate(
225 $objId,
226 $this->objectDataCache->lookUpType($objId),
227 '',
228 '',
229 '',
230 0,
231 0,
232 0,
233 true,
234 '',
235 ''
236 );
237
238 $version = 0;
239 foreach ($templates as $template) {
240 if ($template->getVersion() > $version) {
241 $version = $template->getVersion();
242 $resultTemplate = $template;
243 }
244 }
245
246 $this->logger->info(sprintf('Latest version active certificate template for object: "%s"', $objId));
247
248 return $resultTemplate;
249 }
250
256 public function deleteTemplate(int $templateId, int $objectId)
257 {
258 $this->logger->info(sprintf('START - Set deleted flag for certificate template("%s") for object: "%s"', $templateId, $objectId));
259
260 $sql = '
261UPDATE il_cert_template
262SET deleted = 1, currently_active = 0
263WHERE id = ' . $this->database->quote($templateId, 'integer') . '
264AND obj_id = ' . $this->database->quote($objectId, 'integer');
265
266 $this->database->manipulate($sql);
267
268 $this->logger->info(sprintf('END - Deleted flag set fo certificate template("%s") for object: "%s"', $templateId, $objectId));
269 }
270
276 public function activatePreviousCertificate(int $objId) : \ilCertificateTemplate
277 {
278 $this->logger->info(sprintf('START - Activate previous certificate template for object: "%s"', $objId));
279
280 $certificates = $this->fetchCertificateTemplatesByObjId($objId);
281
283 $previousCertificate = null;
284 foreach ($certificates as $certificate) {
285 if (null === $previousCertificate) {
286 $previousCertificate = $certificate;
287 } elseif ((int) $certificate->getVersion() > (int) $previousCertificate->getVersion()) {
288 $previousCertificate = $certificate;
289 }
290 }
291
292 $sql = 'UPDATE il_cert_template
293SET currently_active = 1
294WHERE id = ' . $this->database->quote($previousCertificate->getId(), 'integer');
295
296 $this->database->manipulate($sql);
297
298 $this->logger->info(sprintf('END - Previous certificate updated for object: "%s"', $objId));
299
300 return $previousCertificate;
301 }
302
307 public function fetchActiveTemplatesByType(string $type) : array
308 {
309 $this->logger->info(sprintf('START - Fetch all active certificate templates for object type: "%s"', $type));
310
311 $sql = '
312 SELECT il_cert_template.* FROM il_cert_template
313 INNER JOIN object_data od ON od.obj_id = il_cert_template.obj_id
314 WHERE obj_type = ' . $this->database->quote($type, 'text') . '
315 AND currently_active = 1
316 ';
317 $query = $this->database->query($sql);
318
319 $result = array();
320 while ($row = $this->database->fetchAssoc($query)) {
321 $result[] = $this->createCertificateTemplate($row);
322 }
323
324 $this->logger->info(sprintf('END - All certificate templates for object type: "%s": "%s"', $type, json_encode($result)));
325
326 return $result;
327 }
328
335 {
336 $this->logger->info(sprintf('START - Fetch first create certificate template for object: "%s"', $objId));
337
338 $this->database->setLimit(1, 0);
339
340 $sql = 'SELECT * FROM il_cert_template
341WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
342ORDER BY id ASC ';
343
344 $query = $this->database->query($sql);
345
346 while ($row = $this->database->fetchAssoc($query)) {
347 $this->logger->info(sprintf('END - Found first create certificate template for object: "%s"', $objId));
348
349 return $this->createCertificateTemplate($row);
350 }
351
352 throw new ilException('No matching template found. MAY missing DBUpdate. Please check if the correct version is installed.');
353 }
354
360 {
361 $this->logger->info(sprintf('START - Deactivate previous certificate template for object: "%s"', $objId));
362
363 $sql = '
364UPDATE il_cert_template
365SET currently_active = 0
366WHERE obj_id = ' . $this->database->quote($objId, 'integer');
367
368 $this->database->manipulate($sql);
369
370 $this->logger->info(sprintf('END - Certificate template deactivated for object: "%s"', $objId));
371 }
372
373 public function updateDefaultBackgroundImagePaths(string $oldRelativePath, string $newRelativePath) : void
374 {
375 $this->logger->debug(sprintf(
376 'START - Update all default background image paths from "%s" to "%s"',
377 $oldRelativePath,
378 $newRelativePath
379 ));
380
381 $affectedRows = $this->database->manipulateF(
382 'UPDATE il_cert_template SET background_image_path = %s WHERE currently_active = 1 AND (background_image_path = %s OR background_image_path = %s )',
383 [
384 'text',
385 'text',
386 'text'
387 ],
388 [
389 $newRelativePath,
390 $oldRelativePath,
391 '/certificates/default/background.jpg']
392 );
393
394 $this->logger->debug(sprintf(
395 'END - Updated %s certificate templates using old path',
396 $affectedRows
397 ));
398 }
399
400 public function isBackgroundImageUsed(string $relativeImagePath) : bool
401 {
402 $this->logger->debug(sprintf(
403 'START - Checking if any certificate template uses background image path "%s"',
404 $relativeImagePath
405 ));
406
407 $result = $this->database->queryF(
408 'SELECT EXISTS(SELECT 1 FROM il_cert_template WHERE background_image_path = %s AND currently_active = 1) AS does_exist',
409 ['text'],
410 [$relativeImagePath]
411 );
412
413 $exists = (bool) ($this->database->fetchAssoc($result)['does_exist'] ?? false);
414
415 $this->logger->debug(sprintf(
416 'END - Image path "%s" is ' . $exists ? "in use" : "unused",
417 $relativeImagePath
418 ));
419
420 return $exists;
421 }
422
428 {
429 return new ilCertificateTemplate(
430 $row['obj_id'],
431 $row['obj_type'],
432 $row['certificate_content'],
433 $row['certificate_hash'],
434 $row['template_values'],
435 $row['version'],
436 $row['ilias_version'],
437 $row['created_timestamp'],
438 (boolean) $row['currently_active'],
439 $row['background_image_path'],
440 $row['thumbnail_image_path'],
441 $row['id']
442 );
443 }
444}
$result
if(! $in) $columns
Definition: Utf8Test.php:45
An exception for terminatinating execution or to throw for unit testing.
fetchPreviousCertificate(int $objId)
Fetch latest created certificate EVEN IF it is deleted.
updateDefaultBackgroundImagePaths(string $oldRelativePath, string $newRelativePath)
__construct(\ilDBInterface $database, \ilLogger $logger=null, \ilObjectDataCache $objectDataCache=null)
save(ilCertificateTemplate $certificateTemplate)
updateActivity(ilCertificateTemplate $certificateTemplate, bool $currentlyActive)
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.
class ilObjectDataCache
global $DIC
Definition: goto.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
$type
$objId
Definition: xapitoken.php:39