19 declare(strict_types=1);
53 if ($dic && isset($dic[
'lng'])) {
55 $language->loadLanguageModule(
'certificate');
58 $this->
lng = $language;
63 return $this->
lng->txt(
'cert_cron_task_title');
68 return $this->
lng->txt(
'cert_cron_task_desc');
71 public function init(): void
73 if (
null === $this->dic) {
78 $database = $this->dic->database();
81 $this->
logger = $this->dic->logger()->cert();
84 if (
null === $this->cronManager) {
85 $this->cronManager = $this->dic->cron()->manager();
88 if (
null === $this->queueRepository) {
92 if (
null === $this->templateRepository) {
96 if (
null === $this->userRepository) {
100 if (
null === $this->valueReplacement) {
104 if (
null === $this->objectHelper) {
118 $result->setStatus(JobResult::STATUS_NO_ACTION);
120 $currentMode = $this->
settings->get(
'persistent_certificate_mode',
'persistent_certificate_mode_cron');
121 if ($currentMode !==
'persistent_certificate_mode_cron') {
122 $this->
logger->warning(sprintf(
123 'Will not start cron job, because the mode is not set as cron job. Current Mode in settings: "%s"',
130 $this->
logger->debug(
'START - Begin with cron job to create user certificates from templates');
132 $entries = $this->queueRepository->getAllEntriesFromQueue();
134 $status = JobResult::STATUS_OK;
137 $succeededGenerations = [];
138 foreach ($entries as $entry) {
143 $succeededGenerations
148 $this->
logger->warning($exception->getMessage());
149 $this->
logger->warning(
'The user MAY not be able to achieve the certificate based on the adapters settings');
150 $this->
logger->warning(
'Due the error, the entry will now be removed from the queue.');
152 $this->queueRepository->removeFromQueue($entry->getId());
156 $this->
logger->warning($exception->getMessage());
157 $this->
logger->warning(
'Due the error, the entry will now be removed from the queue.');
159 $this->queueRepository->removeFromQueue($entry->getId());
165 $result->setStatus($status);
166 if ($succeededGenerations !== []) {
168 'Generated %s certificate(s) in run. Result: %s',
169 count($succeededGenerations),
170 implode(
' | ', $succeededGenerations)
175 $result->setMessage(
'0 certificates generated in current run.');
183 return 'certificate';
198 return JobScheduleType::IN_MINUTES;
214 if ($entryCounter > 0 && $entryCounter % 10 === 0) {
215 $this->cronManager->ping($this->
getId());
218 $this->
logger->debug(
'Entry found will start of processing the entry');
222 $this->
logger->debug(
'Adapter class to be executed "' . $class .
'"');
224 $placeholderValueObject =
new $class();
227 'The given class ' . $class .
' must be an instance of ilCertificateCronAdapter and must ' .
228 'have an accessible namespace. The composer class map should be reloaded.' 236 $this->
logger->debug(sprintf(
237 'Fetch certificate template for user id: "%s" and object id: "%s" and template id: "%s"',
243 $template = $this->templateRepository->fetchTemplate($templateId);
245 $object = $this->objectHelper->getInstanceByObjId(
$objId,
false);
248 'The given object id: "%s" could not be referred to an actual object',
253 $type = $object->getType();
255 $userObject = $this->objectHelper->getInstanceByObjId($userId,
false);
260 $this->
logger->debug(sprintf(
265 $cert_id = $this->userRepository->requestIdentity();
266 $certificateContent = $template->getCertificateContent();
268 $placeholderValues = $placeholderValueObject->getPlaceholderValues($userId,
$objId);
269 $placeholderValues[
'CERTIFICATE_ID'] = $cert_id->asString();
271 $this->
logger->debug(sprintf(
272 'Values for placeholders: "%s"',
273 json_encode($placeholderValues, JSON_THROW_ON_ERROR)
276 $certificateContent = $this->valueReplacement->replace(
286 $userObject->getFullname(),
289 json_encode($placeholderValues, JSON_THROW_ON_ERROR),
291 $template->getVersion(),
295 $template->getBackgroundImagePath(),
296 $template->getTileImagePath(),
297 $template->getBackgroundImageIdentification(),
298 $template->getTileImageIdentification(),
302 $persistedUserCertificate = $this->userRepository->save($userCertificate);
304 $succeededGenerations[] = implode(
'/', [
310 $this->queueRepository->removeFromQueue($entry->
getId());
313 $this->dic->event()->raise(
314 'components/ILIAS/Certificate',
316 [
'certificate' => $persistedUserCertificate]
319 return $succeededGenerations;
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Customizing of pimple-DIC for ILIAS.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
const ILIAS_VERSION_NUMERIC
language()
Get interface to the i18n service.
processEntry(string $class_name, int $obj_id, int $usr_id, \ilCertificateTemplate $template)
getDefaultScheduleValue()
__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 ?JobManager $cronManager=null,)