ILIAS  release_8 Revision v8.23
class.ilCertificateCron.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 {
28  public const DEFAULT_SCHEDULE_HOURS = 1;
29 
30  protected ?ilLanguage $lng;
34  private ?ilLogger $logger;
37  private ?Container $dic = null;
38  private ?ilSetting $settings;
40 
41  public function __construct(
42  ?ilCertificateQueueRepository $queueRepository = null,
43  ?ilCertificateTemplateRepository $templateRepository = null,
44  ?ilUserCertificateRepository $userRepository = null,
45  ?ilCertificateValueReplacement $valueReplacement = null,
46  ?ilLogger $logger = null,
47  ?Container $dic = null,
48  ?ilLanguage $language = null,
49  ?ilCertificateObjectHelper $objectHelper = null,
50  ?ilSetting $setting = null,
51  ?ilCronManager $cronManager = null
52  ) {
53  if (null === $dic) {
54  global $DIC;
55  $dic = $DIC;
56  }
57  $this->dic = $dic;
58 
59  $this->queueRepository = $queueRepository;
60  $this->templateRepository = $templateRepository;
61  $this->userRepository = $userRepository;
62  $this->valueReplacement = $valueReplacement;
63  $this->logger = $logger;
64  $this->objectHelper = $objectHelper;
65  $this->settings = $setting;
66  $this->cronManager = $cronManager;
67 
68  if ($dic && isset($dic['lng'])) {
69  $language = $dic->language();
70  $language->loadLanguageModule('certificate');
71  }
72 
73  $this->lng = $language;
74  }
75 
76  public function getTitle(): string
77  {
78  return $this->lng->txt('cert_cron_task_title');
79  }
80 
81  public function getDescription(): string
82  {
83  return $this->lng->txt('cert_cron_task_desc');
84  }
85 
86  public function init(): void
87  {
88  if (null === $this->dic) {
89  global $DIC;
90  $this->dic = $DIC;
91  }
92 
93  $database = $this->dic->database();
94 
95  if (null === $this->logger) {
96  $this->logger = $this->dic->logger()->cert();
97  }
98 
99  if (null === $this->cronManager) {
100  $this->cronManager = $this->dic->cron()->manager();
101  }
102 
103  if (null === $this->queueRepository) {
104  $this->queueRepository = new ilCertificateQueueRepository($database, $this->logger);
105  }
106 
107  if (null === $this->templateRepository) {
108  $this->templateRepository = new ilCertificateTemplateDatabaseRepository($database, $this->logger);
109  }
110 
111  if (null === $this->userRepository) {
112  $this->userRepository = new ilUserCertificateRepository($database, $this->logger);
113  }
114 
115  if (null === $this->valueReplacement) {
116  $this->valueReplacement = new ilCertificateValueReplacement();
117  }
118 
119  if (null === $this->objectHelper) {
120  $this->objectHelper = new ilCertificateObjectHelper();
121  }
122 
123  if (null === $this->settings) {
124  $this->settings = new ilSetting('certificate');
125  }
126  }
127 
128  public function run(): ilCronJobResult
129  {
130  $this->init();
131 
132  $result = new ilCronJobResult();
133  $result->setStatus(ilCronJobResult::STATUS_NO_ACTION);
134 
135  $currentMode = $this->settings->get('persistent_certificate_mode', 'persistent_certificate_mode_cron');
136  if ($currentMode !== 'persistent_certificate_mode_cron') {
137  $this->logger->warning(sprintf(
138  'Will not start cron job, because the mode is not set as cron job. Current Mode in settings: "%s"',
139  $currentMode
140  ));
141  return $result;
142  }
143 
144  $this->logger->debug('START - Begin with cron job to create user certificates from templates');
145 
146  $entries = $this->queueRepository->getAllEntriesFromQueue();
147 
148  $status = ilCronJobResult::STATUS_OK;
149 
150  $entryCounter = 0;
151  $succeededGenerations = [];
152  foreach ($entries as $entry) {
153  try {
154  $succeededGenerations = $this->processEntry(
155  $entryCounter,
156  $entry,
157  $succeededGenerations
158  );
159 
160  ++$entryCounter;
161  } catch (ilInvalidCertificateException $exception) {
162  $this->logger->warning($exception->getMessage());
163  $this->logger->warning('The user MAY not be able to achieve the certificate based on the adapters settings');
164  $this->logger->warning('Due the error, the entry will now be removed from the queue.');
165 
166  $this->queueRepository->removeFromQueue($entry->getId());
167 
168  continue;
169  } catch (ilException $exception) {
170  $this->logger->warning($exception->getMessage());
171  $this->logger->warning('Due the error, the entry will now be removed from the queue.');
172 
173  $this->queueRepository->removeFromQueue($entry->getId());
174  continue;
175  }
176  }
177 
178  $result->setStatus($status);
179  if (count($succeededGenerations) > 0) {
180  $result->setMessage(sprintf(
181  'Generated %s certificate(s) in run. Result: %s',
182  count($succeededGenerations),
183  implode(' | ', $succeededGenerations)
184  ));
185  } else {
186  $result->setMessage('0 certificates generated in current run.');
187  }
188 
189  return $result;
190  }
191 
192  public function getId(): string
193  {
194  return 'certificate';
195  }
196 
197  public function hasAutoActivation(): bool
198  {
199  return true;
200  }
201 
202  public function hasFlexibleSchedule(): bool
203  {
204  return true;
205  }
206 
207  public function getDefaultScheduleType(): int
208  {
209  return self::SCHEDULE_TYPE_IN_MINUTES;
210  }
211 
212  public function getDefaultScheduleValue(): ?int
213  {
214  return 1;
215  }
216 
227  public function processEntry(int $entryCounter, ilCertificateQueueEntry $entry, array $succeededGenerations): array
228  {
229  if ($entryCounter > 0 && $entryCounter % 10 === 0) {
230  $this->cronManager->ping($this->getId());
231  }
232 
233  $this->logger->debug('Entry found will start of processing the entry');
234 
236  $class = $entry->getAdapterClass();
237  $this->logger->debug('Adapter class to be executed "' . $class . '"');
238 
239  $placeholderValueObject = new $class();
240  if (!$placeholderValueObject instanceof ilCertificatePlaceholderValues) {
241  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.');
242  }
243 
244  $objId = $entry->getObjId();
245  $userId = $entry->getUserId();
246  $templateId = $entry->getTemplateId();
247 
248  $this->logger->debug(sprintf(
249  'Fetch certificate template for user id: "%s" and object id: "%s" and template id: "%s"',
250  $userId,
251  $objId,
252  $templateId
253  ));
254 
255  $template = $this->templateRepository->fetchTemplate($templateId);
256 
257  $object = $this->objectHelper->getInstanceByObjId($objId, false);
258  if (!$object instanceof ilObject) {
259  throw new ilException(sprintf(
260  'The given object id: "%s" could not be referred to an actual object',
261  $objId
262  ));
263  }
264 
265  $type = $object->getType();
266 
267  $userObject = $this->objectHelper->getInstanceByObjId($userId, false);
268  if (!($userObject instanceof ilObjUser)) {
269  throw new ilException('The given user id"' . $userId . '" could not be referred to an actual user');
270  }
271 
272  $this->logger->debug(sprintf(
273  'Object type: "%s"',
274  $type
275  ));
276 
277  $certificateContent = $template->getCertificateContent();
278 
279  $placeholderValues = $placeholderValueObject->getPlaceholderValues($userId, $objId);
280 
281  $this->logger->debug(sprintf(
282  'Values for placeholders: "%s"',
283  json_encode($placeholderValues, JSON_THROW_ON_ERROR)
284  ));
285 
286  $certificateContent = $this->valueReplacement->replace(
287  $placeholderValues,
288  $certificateContent
289  );
290 
291  $thumbnailImagePath = $template->getThumbnailImagePath();
292  $userCertificate = new ilUserCertificate(
293  $template->getId(),
294  $objId,
295  $type,
296  $userId,
297  $userObject->getFullname(),
298  $entry->getStartedTimestamp(),
299  $certificateContent,
300  json_encode($placeholderValues, JSON_THROW_ON_ERROR),
301  null,
302  $template->getVersion(),
304  true,
305  $template->getBackgroundImagePath(),
306  $thumbnailImagePath
307  );
308 
309  $persistedUserCertificate = $this->userRepository->save($userCertificate);
310 
311  $succeededGenerations[] = implode('/', [
312  'obj_id: ' . $objId,
313  'usr_id: ' . $userId
314  ]);
315 
316  if ($entry->getId() !== null) {
317  $this->queueRepository->removeFromQueue($entry->getId());
318  }
319 
320  $this->dic->event()->raise(
321  'Services/Certificate',
322  'certificateIssued',
323  ['certificate' => $persistedUserCertificate]
324  );
325 
326  return $succeededGenerations;
327  }
328 }
ilUserCertificateRepository $userRepository
ilCertificateObjectHelper $objectHelper
const ILIAS_VERSION_NUMERIC
$type
$objId
Definition: xapitoken.php:57
ilCertificateQueueRepository $queueRepository
__construct(?ilCertificateQueueRepository $queueRepository=null, ?ilCertificateTemplateRepository $templateRepository=null, ?ilUserCertificateRepository $userRepository=null, ?ilCertificateValueReplacement $valueReplacement=null, ?ilLogger $logger=null, ?Container $dic=null, ?ilLanguage $language=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilSetting $setting=null, ?ilCronManager $cronManager=null)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:31
global $DIC
Definition: feed.php:28
ilCertificateValueReplacement $valueReplacement
language()
Get interface to the i18n service.
Definition: Container.php:86
ilCertificateTemplateRepository $templateRepository