ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
64  $this->deactivatePreviousTemplates($objId);
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 = '
101 SELECT * FROM
102 il_cert_template
103 WHERE id = ' . $this->database->quote($templateId, 'integer') . '
104 ORDER 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 = '
126 SELECT * FROM
127 il_cert_template
128 WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
129 AND deleted = 0
130 ORDER BY version ASC';
131 
132  $query = $this->database->query($sql);
133 
134  while ($row = $this->database->fetchAssoc($query)) {
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 = '
154 SELECT * FROM il_cert_template
155 WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
156 AND deleted = 0
157 ORDER 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 = '
195 SELECT * FROM il_cert_template
196 WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
197 AND deleted = 0
198 AND 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 
218  public function fetchPreviousCertificate(int $objId) : \ilCertificateTemplate
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 = '
261 UPDATE il_cert_template
262 SET deleted = 1, currently_active = 0
263 WHERE id = ' . $this->database->quote($templateId, 'integer') . '
264 AND 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 
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
293 SET currently_active = 1
294 WHERE 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 = 'SELECT * FROM il_cert_template WHERE obj_type = ' . $this->database->quote($type, 'text') . '
312 AND currently_active = 1';
313  $query = $this->database->query($sql);
314 
315  $result = array();
316  while ($row = $this->database->fetchAssoc($query)) {
318  }
319 
320  $this->logger->info(sprintf('END - All certificate templates for object type: "%s": "%s"', $type, json_encode($result)));
321 
322  return $result;
323  }
324 
330  public function fetchFirstCreatedTemplate(int $objId) : \ilCertificateTemplate
331  {
332  $this->logger->info(sprintf('START - Fetch first create certificate template for object: "%s"', $objId));
333 
334  $this->database->setLimit(1, 0);
335 
336  $sql = 'SELECT * FROM il_cert_template
337 WHERE obj_id = ' . $this->database->quote($objId, 'integer') . '
338 ORDER BY id ASC ';
339 
340  $query = $this->database->query($sql);
341 
342  while ($row = $this->database->fetchAssoc($query)) {
343  $this->logger->info(sprintf('END - Found first create certificate template for object: "%s"', $objId));
344 
345  return $this->createCertificateTemplate($row);
346  }
347 
348  throw new ilException('No matching template found. MAY missing DBUpdate. Please check if the correct version is installed.');
349  }
350 
355  private function deactivatePreviousTemplates(int $objId)
356  {
357  $this->logger->info(sprintf('START - Deactivate previous certificate template for object: "%s"', $objId));
358 
359  $sql = '
360 UPDATE il_cert_template
361 SET currently_active = 0
362 WHERE obj_id = ' . $this->database->quote($objId, 'integer');
363 
364  $this->database->manipulate($sql);
365 
366  $this->logger->info(sprintf('END - Certificate template deactivated for object: "%s"', $objId));
367  }
368 
374  {
375  return new ilCertificateTemplate(
376  $row['obj_id'],
377  $row['obj_type'],
378  $row['certificate_content'],
379  $row['certificate_hash'],
380  $row['template_values'],
381  $row['version'],
382  $row['ilias_version'],
383  $row['created_timestamp'],
384  (boolean) $row['currently_active'],
385  $row['background_image_path'],
386  $row['thumbnail_image_path'],
387  $row['id']
388  );
389  }
390 }
fetchPreviousCertificate(int $objId)
Fetch latest created certificate EVEN IF it is deleted.
$result
$template
$type
global $DIC
Definition: saml.php:7
if(!array_key_exists('StateId', $_REQUEST)) $id
$certificates
Definition: metarefresh.php:39
__construct(\ilDBInterface $database, \ilLogger $logger=null, \ilObjectDataCache $objectDataCache=null)
class ilObjectDataCache
$version
Definition: build.php:27
if(@file_exists(dirname(__FILE__).'/lang/eng.php')) $certificate
Definition: example_052.php:77
save(ilCertificateTemplate $certificateTemplate)
$query
$row
updateActivity(ilCertificateTemplate $certificateTemplate, bool $currentlyActive)
Component logger with individual log levels by component id.
if(! $in) $columns
Definition: Utf8Test.php:45