19 declare(strict_types=1);
68 if ($dic && isset($dic[
'lng'])) {
70 $language->loadLanguageModule(
'certificate');
73 $this->
lng = $language;
78 return $this->
lng->txt(
'cert_cron_task_title');
83 return $this->
lng->txt(
'cert_cron_task_desc');
86 public function init(): void
88 if (null === $this->dic) {
93 $database = $this->dic->database();
95 if (null === $this->
logger) {
96 $this->
logger = $this->dic->logger()->cert();
99 if (null === $this->cronManager) {
100 $this->cronManager = $this->dic->cron()->manager();
103 if (null === $this->queueRepository) {
107 if (null === $this->templateRepository) {
111 if (null === $this->userRepository) {
115 if (null === $this->valueReplacement) {
119 if (null === $this->objectHelper) {
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"',
144 $this->
logger->debug(
'START - Begin with cron job to create user certificates from templates');
146 $entries = $this->queueRepository->getAllEntriesFromQueue();
151 $succeededGenerations = [];
152 foreach ($entries as $entry) {
154 $succeededGenerations = $this->processEntry(
157 $succeededGenerations
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.');
166 $this->queueRepository->removeFromQueue($entry->getId());
170 $this->
logger->warning($exception->getMessage());
171 $this->
logger->warning(
'Due the error, the entry will now be removed from the queue.');
173 $this->queueRepository->removeFromQueue($entry->getId());
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)
186 $result->setMessage(
'0 certificates generated in current run.');
194 return 'certificate';
209 return self::SCHEDULE_TYPE_IN_MINUTES;
227 public function processEntry(
int $entryCounter,
ilCertificateQueueEntry $entry, array $succeededGenerations): array
229 if ($entryCounter > 0 && $entryCounter % 10 === 0) {
230 $this->cronManager->ping($this->
getId());
233 $this->
logger->debug(
'Entry found will start of processing the entry');
237 $this->
logger->debug(
'Adapter class to be executed "' . $class .
'"');
239 $placeholderValueObject =
new $class();
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.');
248 $this->
logger->debug(sprintf(
249 'Fetch certificate template for user id: "%s" and object id: "%s" and template id: "%s"',
255 $template = $this->templateRepository->fetchTemplate($templateId);
257 $object = $this->objectHelper->getInstanceByObjId(
$objId,
false);
260 'The given object id: "%s" could not be referred to an actual object',
265 $type = $object->getType();
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');
272 $this->
logger->debug(sprintf(
277 $certificateContent = $template->getCertificateContent();
279 $placeholderValues = $placeholderValueObject->getPlaceholderValues($userId,
$objId);
281 $this->
logger->debug(sprintf(
282 'Values for placeholders: "%s"',
283 json_encode($placeholderValues, JSON_THROW_ON_ERROR)
286 $certificateContent = $this->valueReplacement->replace(
291 $thumbnailImagePath = $template->getThumbnailImagePath();
297 $userObject->getFullname(),
300 json_encode($placeholderValues, JSON_THROW_ON_ERROR),
302 $template->getVersion(),
305 $template->getBackgroundImagePath(),
309 $persistedUserCertificate = $this->userRepository->save($userCertificate);
311 $succeededGenerations[] = implode(
'/', [
316 if ($entry->
getId() !== null) {
317 $this->queueRepository->removeFromQueue($entry->
getId());
320 $this->dic->event()->raise(
321 'Services/Certificate',
323 [
'certificate' => $persistedUserCertificate]
326 return $succeededGenerations;
ilUserCertificateRepository $userRepository
ilCertificateObjectHelper $objectHelper
const ILIAS_VERSION_NUMERIC
ilCronManager $cronManager
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.
ilCertificateValueReplacement $valueReplacement
const DEFAULT_SCHEDULE_HOURS
language()
Get interface to the i18n service.
ilCertificateTemplateRepository $templateRepository
getDefaultScheduleValue()