ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilCertificateCron.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
10
12 protected $lng;
13
16
19
22
24 private $logger;
25
28
31
33 private $dic;
34
36 private $settings;
37
49 public function __construct(
54 ilLogger $logger = null,
55 \ILIAS\DI\Container $dic = null,
56 ilLanguage $language = null,
58 ilSetting $setting = null
59 ) {
60 if (null === $dic) {
61 global $DIC;
62 $dic = $DIC;
63 }
64 $this->dic = $dic;
65
66 $this->queueRepository = $queueRepository;
67 $this->templateRepository = $templateRepository;
68 $this->userRepository = $userRepository;
69 $this->valueReplacement = $valueReplacement;
70 $this->logger = $logger;
71 $this->objectHelper = $objectHelper;
72 $this->settings = $setting;
73
74 if ($dic) {
75 if (isset($dic['lng'])) {
76 $language = $dic->language();
77 $language->loadLanguageModule('certificate');
78 }
79 }
80
81 $this->lng = $language;
82 }
83
87 public function getTitle()
88 {
89 return $this->lng->txt('cert_cron_task_title');
90 }
91
95 public function getDescription()
96 {
97 return $this->lng->txt('cert_cron_task_desc');
98 }
99
100 public function init()
101 {
102 if (null === $this->dic) {
103 global $DIC;
104 $this->dic = $DIC;
105 }
106
107 $database = $this->dic->database();
108
109 if (null === $this->logger) {
110 $this->logger = $this->dic->logger()->cert();
111 }
112
113 if (null === $this->queueRepository) {
114 $this->queueRepository = new ilCertificateQueueRepository($database, $this->logger);
115 }
116
117 if (null === $this->templateRepository) {
118 $this->templateRepository = new ilCertificateTemplateRepository($database, $this->logger);
119 }
120
121 if (null === $this->userRepository) {
122 $this->userRepository = new ilUserCertificateRepository($database, $this->logger);
123 }
124
125 if (null === $this->valueReplacement) {
126 $this->valueReplacement = new ilCertificateValueReplacement();
127 }
128
129 if (null === $this->objectHelper) {
130 $this->objectHelper = new ilCertificateObjectHelper();
131 }
132
133 if (null === $this->settings) {
134 $this->settings = new ilSetting('certificate');
135 }
136 }
137
142 public function run()
143 {
144 $this->init();
145
146 $result = new ilCronJobResult();
148
149 $currentMode = $this->settings->get('persistent_certificate_mode', 'persistent_certificate_mode_cron');
150 if ($currentMode !== 'persistent_certificate_mode_cron') {
151 $this->logger->warning(sprintf('Will not start cron job, because the mode is not set as cron job. Current Mode in settings: "%s"', $currentMode));
152 return $result;
153 }
154
155 $this->logger->info('START - Begin with cron job to create user certificates from templates');
156
157 $entries = $this->queueRepository->getAllEntriesFromQueue();
158
160
161 $entryCounter = 0;
162 $succeededGenerations = [];
163 foreach ($entries as $entry) {
164 try {
165 $succeededGenerations = $this->processEntry(
166 $entryCounter,
167 $entry,
168 $succeededGenerations
169 );
170
171 ++$entryCounter;
172 } catch (ilInvalidCertificateException $exception) {
173 $this->logger->warning($exception->getMessage());
174 $this->logger->warning('The user MAY not be able to achieve the certificate based on the adapters settings');
175 $this->logger->warning('Due the error, the entry will now be removed from the queue.');
176
177 $this->queueRepository->removeFromQueue($entry->getId());
178
179 continue;
180 } catch (ilException $exception) {
181 $this->logger->warning($exception->getMessage());
182 $this->logger->warning('Due the error, the entry will now be removed from the queue.');
183
184 $this->queueRepository->removeFromQueue($entry->getId());
185 continue;
186 }
187 }
188
189 $result->setStatus($status);
190 if (count($succeededGenerations) > 0) {
191 $result->setMessage(sprintf(
192 'Generated %s certificate(s) in run. Result: %s',
193 count($succeededGenerations),
194 implode(' | ', $succeededGenerations)
195 ));
196 } else {
197 $result->setMessage('0 certificates generated in current run.');
198 }
199
200 return $result;
201 }
202
206 public function getId()
207 {
208 return 'certificate';
209 }
210
214 public function hasAutoActivation()
215 {
216 return true;
217 }
218
222 public function hasFlexibleSchedule()
223 {
224 return true;
225 }
226
230 public function getDefaultScheduleType()
231 {
233 }
234
238 public function getDefaultScheduleValue()
239 {
240 return 1;
241 }
242
252 public function processEntry(int $entryCounter, ilCertificateQueueEntry $entry, array $succeededGenerations) : array
253 {
254 if ($entryCounter > 0 && $entryCounter % 10 === 0) {
255 ilCronManager::ping($this->getId());
256 }
257
258 $this->logger->debug('Entry found will start of processing the entry');
259
261 $class = $entry->getAdapterClass();
262 $this->logger->debug('Adapter class to be executed "' . $class . '"');
263
264 $placeholderValueObject = new $class();
265 if (!$placeholderValueObject instanceof ilCertificatePlaceholderValues) {
266 throw new ilException('The given class ' . $class . ' MUST be an instance of ilCertificateCronAdapter and MUST have an accessible namespace. The class map MAY be reloader.');
267 }
268
269 $objId = $entry->getObjId();
270 $userId = $entry->getUserId();
271 $templateId = $entry->getTemplateId();
272
273 $this->logger->debug(sprintf(
274 'Fetch certificate template for user id: "%s" and object id: "%s" and template id: "%s"',
275 $userId,
276 $objId,
277 $templateId
278 ));
279
280 $template = $this->templateRepository->fetchTemplate($templateId);
281
282 $object = $this->objectHelper->getInstanceByObjId($objId, false);
283 if (!$object instanceof ilObject) {
284 throw new ilException(sprintf('The given object id: "%s" could not be referred to an actual object', $objId));
285 }
286
287 $type = $object->getType();
288
289 $userObject = $this->objectHelper->getInstanceByObjId($userId, false);
290 if (!$userObject || !($userObject instanceof \ilObjUser)) {
291 throw new ilException('The given user id"' . $userId . '" could not be referred to an actual user');
292 }
293
294 $this->logger->debug(sprintf(
295 'Object type: "%s"',
296 $type
297 ));
298
299 $certificateContent = $template->getCertificateContent();
300
301 $placeholderValues = $placeholderValueObject->getPlaceholderValues($userId, $objId);
302
303 $this->logger->debug(sprintf(
304 'Values for placeholders: "%s"',
305 json_encode($placeholderValues)
306 ));
307
308 $certificateContent = $this->valueReplacement->replace(
309 $placeholderValues,
310 $certificateContent
311 );
312
313 $thumbnailImagePath = (string) $template->getThumbnailImagePath();
314 $userCertificate = new ilUserCertificate(
315 $template->getId(),
316 $objId,
317 $type,
318 $userId,
319 $userObject->getFullname(),
320 (int) $entry->getStartedTimestamp(),
321 $certificateContent,
322 json_encode($placeholderValues),
323 null,
324 $template->getVersion(),
326 true,
327 $template->getBackgroundImagePath(),
328 $thumbnailImagePath
329 );
330
331 $persistedUserCertificate = $this->userRepository->save($userCertificate);
332
333 $succeededGenerations[] = implode('/', [
334 'obj_id: ' . $objId,
335 'usr_id: ' . $userId
336 ]);
337
338 $this->queueRepository->removeFromQueue($entry->getId());
339
340 $this->dic->event()->raise(
341 'Services/Certificate',
342 'certificateIssued',
343 ['certificate' => $persistedUserCertificate]
344 );
345
346 return $succeededGenerations;
347 }
348}
$result
An exception for terminatinating execution or to throw for unit testing.
getDefaultScheduleType()
Get schedule type.int
getDescription()
Get description.string
getDefaultScheduleValue()
Get schedule value.int|array
getTitle()
Get title.string
hasAutoActivation()
Is to be activated on "installation".boolean
$queueRepository
@var ilCertificateQueueRepository
hasFlexibleSchedule()
Can the schedule be configured?boolean
__construct(ilCertificateQueueRepository $queueRepository=null, ilCertificateTemplateRepository $templateRepository=null, ilUserCertificateRepository $userRepository=null, ilCertificateValueReplacement $valueReplacement=null, ilLogger $logger=null, \ILIAS\DI\Container $dic=null, ilLanguage $language=null, ilCertificateObjectHelper $objectHelper=null, ilSetting $setting=null)
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_IN_MINUTES
static ping($a_job_id)
Keep cron job alive.
Base class for ILIAS Exception handling.
language handling
Component logger with individual log levels by component id.
Class ilObject Basic functions for all objects.
ILIAS Setting Class.
$template
const ILIAS_VERSION_NUMERIC
Class HTTPServicesTest.
Class BaseForm.
$type
global $DIC
Definition: saml.php:7
settings()
Definition: settings.php:2