ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
UserCertificateAPI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Certificate\API;
22
29use ilLogger;
33use ilSetting;
41
43{
47 private readonly ilLogger $logger;
49
50 public function __construct(
54 private readonly ilCertificateTypeClassMap $type_class_map = new ilCertificateTypeClassMap(),
55 ?ilLogger $logger = null,
57 ) {
58 global $DIC;
59
60 $this->logger = $logger ?? $DIC->logger()->cert();
61 $this->object_data_cache = $object_data_cache ?? $DIC['ilObjDataCache'];
62 $this->user_data_repository = $user_data_repository ?? new UserDataRepository(
63 $DIC->database(),
64 $DIC->ctrl()
65 );
66 $this->template_repository = $template_repository ?? new ilCertificateTemplateDatabaseRepository(
67 $DIC->database(),
68 $this->logger
69 );
70 $this->queue_repository = $queue_repository ?? new ilCertificateQueueRepository(
71 $DIC->database(),
72 $this->logger
73 );
74 }
75
76 public function getUserCertificateData(UserDataFilter $filter, array $ilCtrlStack = []): array
77 {
78 return $this->user_data_repository->getUserData($filter, $ilCtrlStack);
79 }
80
82 {
83 return $this->user_data_repository->getUserCertificateDataMaxCount($filter);
84 }
85
86 public function certificateCriteriaMetForGivenTemplate(int $usr_id, ilCertificateTemplate $template): void
87 {
88 if (!$template->isCurrentlyActive()) {
89 $this->logger->debug(sprintf(
90 'Did not trigger certificate achievement for inactive template: usr_id: %s/obj_id: %s/type: %s/template_id: %s',
91 $usr_id,
92 $template->getObjId(),
93 $template->getObjType(),
94 $template->getId()
95 ));
96 return;
97 }
98
99 $this->processEntry(
100 $usr_id,
101 $template
102 );
103 }
104
105 public function certificateCriteriaMet(int $usr_id, int $obj_id): void
106 {
107 $type = $this->object_data_cache->lookupType($obj_id);
108 if (!$this->type_class_map->typeExistsInMap($type)) {
109 throw new ilCertificateConsumerNotSupported(sprintf(
110 "Oject type '%s' is not supported by the certificate component!",
111 $type
112 ));
113 }
114
115 $template = $this->template_repository->fetchCurrentlyActiveCertificate($obj_id);
116
117 $this->certificateCriteriaMetForGivenTemplate($usr_id, $template);
118 }
119
120 public function isActiveCertificateTemplateAvailableFor(int $obj_id): bool
121 {
122 try {
123 return $this->template_repository->fetchCurrentlyActiveCertificate($obj_id)->isCurrentlyActive();
125 return false;
126 }
127 }
128
135 private function processEntry(
136 int $userId,
137 ilCertificateTemplate $template
138 ): void {
139 $this->logger->debug(sprintf(
140 'Trigger persisting certificate achievement for: usr_id: %s/obj_id: %s/type: %s/template_id: %s',
141 $userId,
142 $template->getObjId(),
143 $template->getObjType(),
144 $template->getId()
145 ));
146
147 $entry = new ilCertificateQueueEntry(
148 $template->getObjId(),
149 $userId,
150 $this->type_class_map->getPlaceHolderClassNameByType($template->getObjType()),
152 $template->getId(),
153 time()
154 );
155
156 $mode = (new ilSetting('certificate'))->get(
157 'persistent_certificate_mode',
158 'persistent_certificate_mode_cron'
159 );
160 if ($mode === 'persistent_certificate_mode_instant') {
161 $cronjob = new ilCertificateCron();
162 $cronjob->init();
163 $cronjob->processEntry(0, $entry, []);
164 return;
165 }
166
167 $this->queue_repository->addToQueue($entry);
168 }
169}
readonly ilObjectDataCache $object_data_cache
certificateCriteriaMetForGivenTemplate(int $usr_id, ilCertificateTemplate $template)
__construct(?UserDataRepository $user_data_repository=null, ?ilCertificateTemplateRepository $template_repository=null, ?ilCertificateQueueRepository $queue_repository=null, private readonly ilCertificateTypeClassMap $type_class_map=new ilCertificateTypeClassMap(), ?ilLogger $logger=null, ?ilObjectDataCache $object_data_cache=null,)
readonly ilCertificateTemplateRepository $template_repository
processEntry(int $userId, ilCertificateTemplate $template)
readonly UserDataRepository $user_data_repository
getUserCertificateData(UserDataFilter $filter, array $ilCtrlStack=[])
getUserCertificateDataMaxCount(UserDataFilter $filter)
certificateCriteriaMet(int $usr_id, int $obj_id)
readonly ilCertificateQueueRepository $queue_repository
Component logger with individual log levels by component id.
class ilObjectDataCache
ILIAS Setting Class.
global $DIC
Definition: shib_login.php:26