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