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