ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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(
90 \sprintf(
91 'Did not trigger certificate achievement for inactive template: usr_id: %s/obj_id: %s/type: %s/template_id: %s',
92 $usr_id,
93 $template->getObjId(),
94 $template->getObjType(),
95 $template->getId()
96 )
97 );
98 return;
99 }
100
101 $this->processEntry(
102 $usr_id,
103 $template
104 );
105 }
106
107 public function certificateCriteriaMet(int $usr_id, int $obj_id): void
108 {
109 $type = $this->object_data_cache->lookupType($obj_id);
110 if (!$this->type_class_map->typeExistsInMap($type)) {
112 \sprintf(
113 "Oject type '%s' is not supported by the certificate component!",
114 $type
115 )
116 );
117 }
118
119 $template = $this->template_repository->fetchCurrentlyActiveCertificate($obj_id);
120 if ($template->getObjType() !== $type) {
121 $this->logger->error(
122 'Object type mismatch between template and determined type for object with id {obj_id}: '
123 . 'Expected {type} but got {template_type}!',
124 [
125 'obj_id' => $obj_id,
126 'type' => $type,
127 'template_type' => $template->getObjType()
128 ]
129 );
130 }
131
132 $this->certificateCriteriaMetForGivenTemplate($usr_id, $template);
133 }
134
135 public function isActiveCertificateTemplateAvailableFor(int $obj_id): bool
136 {
137 try {
138 return $this->template_repository->fetchCurrentlyActiveCertificate($obj_id)->isCurrentlyActive();
140 return false;
141 }
142 }
143
150 private function processEntry(
151 int $userId,
152 ilCertificateTemplate $template
153 ): void {
154 $this->logger->debug(
155 \sprintf(
156 'Trigger persisting certificate achievement for: usr_id: %s/obj_id: %s/type: %s/template_id: %s',
157 $userId,
158 $template->getObjId(),
159 $template->getObjType(),
160 $template->getId()
161 )
162 );
163
164 $entry = new ilCertificateQueueEntry(
165 $template->getObjId(),
166 $userId,
167 $this->type_class_map->getPlaceHolderClassNameByType($template->getObjType()),
169 $template->getId(),
170 time()
171 );
172
173 $mode = (new ilSetting('certificate'))->get(
174 'persistent_certificate_mode',
175 'persistent_certificate_mode_cron'
176 );
177 if ($mode === 'persistent_certificate_mode_instant') {
178 $cronjob = new ilCertificateCron();
179 $cronjob->init();
180 $cronjob->processEntry(0, $entry, []);
181 return;
182 }
183
184 $this->queue_repository->addToQueue($entry);
185 }
186}
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