ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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(
152 'Will not start cron job, because the mode is not set as cron job. Current Mode in settings: "%s"',
153 $currentMode
154 ));
155 return $result;
156 }
157
158 $this->logger->info('START - Begin with cron job to create user certificates from templates');
159
160 $entries = $this->queueRepository->getAllEntriesFromQueue();
161
163
164 $entryCounter = 0;
165 $succeededGenerations = [];
166 foreach ($entries as $entry) {
167 try {
168 $succeededGenerations = $this->processEntry(
169 $entryCounter,
170 $entry,
171 $succeededGenerations
172 );
173
174 ++$entryCounter;
175 } catch (ilInvalidCertificateException $exception) {
176 $this->logger->warning($exception->getMessage());
177 $this->logger->warning('The user MAY not be able to achieve the certificate based on the adapters settings');
178 $this->logger->warning('Due the error, the entry will now be removed from the queue.');
179
180 $this->queueRepository->removeFromQueue($entry->getId());
181
182 continue;
183 } catch (ilException $exception) {
184 $this->logger->warning($exception->getMessage());
185 $this->logger->warning('Due the error, the entry will now be removed from the queue.');
186
187 $this->queueRepository->removeFromQueue($entry->getId());
188 continue;
189 }
190 }
191
192 $result->setStatus($status);
193 if (count($succeededGenerations) > 0) {
194 $result->setMessage(sprintf(
195 'Generated %s certificate(s) in run. Result: %s',
196 count($succeededGenerations),
197 implode(' | ', $succeededGenerations)
198 ));
199 } else {
200 $result->setMessage('0 certificates generated in current run.');
201 }
202
203 return $result;
204 }
205
209 public function getId()
210 {
211 return 'certificate';
212 }
213
217 public function hasAutoActivation()
218 {
219 return true;
220 }
221
225 public function hasFlexibleSchedule()
226 {
227 return true;
228 }
229
233 public function getDefaultScheduleType()
234 {
236 }
237
241 public function getDefaultScheduleValue()
242 {
243 return 1;
244 }
245
255 public function processEntry(int $entryCounter, ilCertificateQueueEntry $entry, array $succeededGenerations) : array
256 {
257 if ($entryCounter > 0 && $entryCounter % 10 === 0) {
258 ilCronManager::ping($this->getId());
259 }
260
261 $this->logger->debug('Entry found will start of processing the entry');
262
264 $class = $entry->getAdapterClass();
265 $this->logger->debug('Adapter class to be executed "' . $class . '"');
266
267 $placeholderValueObject = new $class();
268 if (!$placeholderValueObject instanceof ilCertificatePlaceholderValues) {
269 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.');
270 }
271
272 $objId = $entry->getObjId();
273 $userId = $entry->getUserId();
274 $templateId = $entry->getTemplateId();
275
276 $this->logger->debug(sprintf(
277 'Fetch certificate template for user id: "%s" and object id: "%s" and template id: "%s"',
278 $userId,
279 $objId,
280 $templateId
281 ));
282
283 $template = $this->templateRepository->fetchTemplate($templateId);
284
285 $object = $this->objectHelper->getInstanceByObjId($objId, false);
286 if (!$object instanceof ilObject) {
287 throw new ilException(sprintf(
288 'The given object id: "%s" could not be referred to an actual object',
289 $objId
290 ));
291 }
292
293 $type = $object->getType();
294
295 $userObject = $this->objectHelper->getInstanceByObjId($userId, false);
296 if (!$userObject || !($userObject instanceof \ilObjUser)) {
297 throw new ilException('The given user id"' . $userId . '" could not be referred to an actual user');
298 }
299
300 $this->logger->debug(sprintf(
301 'Object type: "%s"',
302 $type
303 ));
304
305 $certificateContent = $template->getCertificateContent();
306
307 $placeholderValues = $placeholderValueObject->getPlaceholderValues($userId, $objId);
308
309 $this->logger->debug(sprintf(
310 'Values for placeholders: "%s"',
311 json_encode($placeholderValues)
312 ));
313
314 $certificateContent = $this->valueReplacement->replace(
315 $placeholderValues,
316 $certificateContent
317 );
318
319 $thumbnailImagePath = (string) $template->getThumbnailImagePath();
320 $userCertificate = new ilUserCertificate(
321 $template->getId(),
322 $objId,
323 $type,
324 $userId,
325 $userObject->getFullname(),
326 (int) $entry->getStartedTimestamp(),
327 $certificateContent,
328 json_encode($placeholderValues),
329 null,
330 $template->getVersion(),
332 true,
333 $template->getBackgroundImagePath(),
334 $thumbnailImagePath
335 );
336
337 $persistedUserCertificate = $this->userRepository->save($userCertificate);
338
339 $succeededGenerations[] = implode('/', [
340 'obj_id: ' . $objId,
341 'usr_id: ' . $userId
342 ]);
343
344 $this->queueRepository->removeFromQueue($entry->getId());
345
346 $this->dic->event()->raise(
347 'Services/Certificate',
348 'certificateIssued',
349 ['certificate' => $persistedUserCertificate]
350 );
351
352 return $succeededGenerations;
353 }
354}
$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.
const ILIAS_VERSION_NUMERIC
Class HTTPServicesTest.
Class ChatMainBarProvider \MainMenu\Provider.
$type
settings()
Definition: settings.php:2
$DIC
Definition: xapitoken.php:46
$objId
Definition: xapitoken.php:41