19 declare(strict_types=1);
50 if ($dic && isset($dic[
'lng'])) {
52 $language->loadLanguageModule(
'certificate');
55 $this->
lng = $language;
60 return $this->
lng->txt(
'cert_cron_task_title');
65 return $this->
lng->txt(
'cert_cron_task_desc');
68 public function init(): void
70 if (null === $this->dic) {
75 $database = $this->dic->database();
77 if (null === $this->
logger) {
78 $this->
logger = $this->dic->logger()->cert();
81 if (null === $this->cronManager) {
82 $this->cronManager = $this->dic->cron()->manager();
85 if (null === $this->queueRepository) {
89 if (null === $this->templateRepository) {
93 if (null === $this->userRepository) {
97 if (null === $this->valueReplacement) {
101 if (null === $this->objectHelper) {
117 $currentMode = $this->
settings->get(
'persistent_certificate_mode',
'persistent_certificate_mode_cron');
118 if ($currentMode !==
'persistent_certificate_mode_cron') {
119 $this->
logger->warning(sprintf(
120 'Will not start cron job, because the mode is not set as cron job. Current Mode in settings: "%s"',
126 $this->
logger->debug(
'START - Begin with cron job to create user certificates from templates');
128 $entries = $this->queueRepository->getAllEntriesFromQueue();
133 $succeededGenerations = [];
134 foreach ($entries as $entry) {
139 $succeededGenerations
144 $this->
logger->warning($exception->getMessage());
145 $this->
logger->warning(
'The user MAY not be able to achieve the certificate based on the adapters settings');
146 $this->
logger->warning(
'Due the error, the entry will now be removed from the queue.');
148 $this->queueRepository->removeFromQueue($entry->getId());
152 $this->
logger->warning($exception->getMessage());
153 $this->
logger->warning(
'Due the error, the entry will now be removed from the queue.');
155 $this->queueRepository->removeFromQueue($entry->getId());
160 $result->setStatus($status);
161 if ($succeededGenerations !== []) {
163 'Generated %s certificate(s) in run. Result: %s',
164 count($succeededGenerations),
165 implode(
' | ', $succeededGenerations)
170 $result->setMessage(
'0 certificates generated in current run.');
178 return 'certificate';
193 return CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES;
209 if ($entryCounter > 0 && $entryCounter % 10 === 0) {
210 $this->cronManager->ping($this->
getId());
213 $this->
logger->debug(
'Entry found will start of processing the entry');
217 $this->
logger->debug(
'Adapter class to be executed "' . $class .
'"');
219 $placeholderValueObject =
new $class();
222 'The given class ' . $class .
' must be an instance of ilCertificateCronAdapter and must ' .
223 'have an accessible namespace. The composer class map should be reloaded.' 231 $this->
logger->debug(sprintf(
232 'Fetch certificate template for user id: "%s" and object id: "%s" and template id: "%s"',
238 $template = $this->templateRepository->fetchTemplate($templateId);
240 $object = $this->objectHelper->getInstanceByObjId(
$objId,
false);
243 'The given object id: "%s" could not be referred to an actual object',
248 $type = $object->getType();
250 $userObject = $this->objectHelper->getInstanceByObjId($userId,
false);
251 if (!($userObject instanceof
ilObjUser)) {
255 $this->
logger->debug(sprintf(
260 $certificateContent = $template->getCertificateContent();
262 $placeholderValues = $placeholderValueObject->getPlaceholderValues($userId,
$objId);
264 $this->
logger->debug(sprintf(
265 'Values for placeholders: "%s"',
266 json_encode($placeholderValues, JSON_THROW_ON_ERROR)
269 $certificateContent = $this->valueReplacement->replace(
274 $thumbnailImagePath = $template->getThumbnailImagePath();
280 $userObject->getFullname(),
283 json_encode($placeholderValues, JSON_THROW_ON_ERROR),
285 $template->getVersion(),
288 $template->getBackgroundImagePath(),
292 $persistedUserCertificate = $this->userRepository->save($userCertificate);
294 $succeededGenerations[] = implode(
'/', [
299 if ($entry->
getId() !== null) {
300 $this->queueRepository->removeFromQueue($entry->
getId());
303 $this->dic->event()->raise(
304 'Services/Certificate',
306 [
'certificate' => $persistedUserCertificate]
309 return $succeededGenerations;
const ILIAS_VERSION_NUMERIC
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Customizing of pimple-DIC for ILIAS.
final const STATUS_NO_ACTION
__construct(private ?ilCertificateQueueRepository $queueRepository=null, private ?ilCertificateTemplateRepository $templateRepository=null, private ?ilUserCertificateRepository $userRepository=null, private ?ilCertificateValueReplacement $valueReplacement=null, private ?ilLogger $logger=null, ?Container $dic=null, ?ilLanguage $language=null, private ?ilCertificateObjectHelper $objectHelper=null, private ?ilSetting $settings=null, private ?ilCronManager $cronManager=null)
language()
Get interface to the i18n service.
processEntry(string $class_name, int $obj_id, int $usr_id, \ilCertificateTemplate $template)
getDefaultScheduleValue()