ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilUserCertificateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
32 {
37  private ilObjUser $user;
38  private ServerRequestInterface $request;
41  protected Factory $uiFactory;
42  protected Renderer $uiRenderer;
44  public const SORTATION_SESSION_KEY = 'my_certificates_sorting';
45  protected array $sortationOptions = [
46  'title_ASC' => 'cert_sortable_by_title_asc',
47  'title_DESC' => 'cert_sortable_by_title_desc',
48  'date_ASC' => 'cert_sortable_by_issue_date_asc',
49  'date_DESC' => 'cert_sortable_by_issue_date_desc',
50  ];
51  protected string $defaultSorting = 'date_DESC';
53 
54  public function __construct(
55  ?ilGlobalTemplateInterface $template = null,
56  ?ilCtrlInterface $ctrl = null,
57  ?ilLanguage $language = null,
58  ?ilObjUser $user = null,
59  ?ilUserCertificateRepository $userCertificateRepository = null,
60  ?ServerRequestInterface $request = null,
61  ?ilLogger $certificateLogger = null,
62  ?ilSetting $certificateSettings = null,
63  ?Factory $uiFactory = null,
64  ?Renderer $uiRenderer = null,
65  ?ilAccessHandler $access = null,
66  ?Filesystem $filesystem = null
67  ) {
68  global $DIC;
69 
70  $logger = $DIC->logger()->cert();
71 
72  if ($template === null) {
73  $template = $DIC->ui()->mainTemplate();
74  }
75  $this->template = $template;
76 
77  if ($ctrl === null) {
78  $ctrl = $DIC->ctrl();
79  }
80  $this->ctrl = $ctrl;
81 
82  if ($language === null) {
83  $language = $DIC->language();
84  }
85  $this->language = $language;
86 
87  if ($user === null) {
88  $user = $DIC->user();
89  }
90  $this->user = $user;
91 
92  if ($request === null) {
93  $request = $DIC->http()->request();
94  }
95  $this->request = $request;
96 
97  if ($certificateLogger === null) {
98  $certificateLogger = $DIC->logger()->cert();
99  }
100  $this->certificateLogger = $certificateLogger;
101 
102  if ($certificateSettings === null) {
103  $certificateSettings = new ilSetting("certificate");
104  }
105  $this->certificateSettings = $certificateSettings;
106 
107  if (null === $uiFactory) {
108  $uiFactory = $DIC->ui()->factory();
109  }
110  $this->uiFactory = $uiFactory;
111 
112  if (null === $uiRenderer) {
113  $uiRenderer = $DIC->ui()->renderer();
114  }
115  $this->uiRenderer = $uiRenderer;
116 
117  if (null === $access) {
118  $access = $DIC->access();
119  }
120  $this->access = $access;
121 
122  if (null === $filesystem) {
123  $filesystem = $DIC->filesystem()->web();
124  }
125  $this->filesystem = $filesystem;
126 
127  if ($userCertificateRepository === null) {
128  $userCertificateRepository = new ilUserCertificateRepository(null, $this->certificateLogger);
129  }
130  $this->userCertificateRepository = $userCertificateRepository;
131 
132  $this->language->loadLanguageModule('cert');
133  $this->language->loadLanguageModule('cert');
134  }
135 
136  private function getDefaultCommand(): string
137  {
138  return 'listCertificates';
139  }
140 
141  public function executeCommand(): bool
142  {
143  $nextClass = $this->ctrl->getNextClass($this);
144  $cmd = $this->ctrl->getCmd();
145 
146  if (!$this->certificateSettings->get('active', '0')) {
147  $this->ctrl->returnToParent($this);
148  }
149 
150  $this->template->setTitle($this->language->txt('obj_cert'));
151 
152  switch ($nextClass) {
153  default:
154  if (!method_exists($this, $cmd)) {
155  $cmd = $this->getDefaultCommand();
156  }
157  $this->{$cmd}();
158  }
159 
160  return true;
161  }
162 
167  public function listCertificates(): void
168  {
169  global $DIC;
170 
171  if (!$this->certificateSettings->get('active', '0')) {
172  $this->ctrl->redirect($this);
173  }
174 
176  $DIC->database(),
178  $this->language->txt('certificate_no_object_title')
179  );
180 
181  $sorting = $this->getCurrentSortation();
182  $data = $provider->fetchDataSet(
183  $this->user->getId(),
184  [
185  'order_field' => explode('_', $sorting)[0],
186  'order_direction' => explode('_', $sorting)[1],
187  'language' => $this->user->getLanguage()
188  ],
189  []
190  );
191 
192  $uiComponents = [];
193 
194  if (count($data['items']) > 0) {
195  $sortationOptions = [];
196  $cards = [];
197 
198  foreach ($this->sortationOptions as $fieldAndDirection => $lngVariable) {
199  $sortationOptions[$fieldAndDirection] = $this->language->txt($lngVariable);
200  }
201 
202  $sortViewControl = $this->uiFactory
203  ->viewControl()
204  ->sortation($sortationOptions)
205  ->withLabel($this->language->txt($this->sortationOptions[$sorting]))
206  ->withTargetURL($this->ctrl->getLinkTarget($this, 'applySortation'), 'sort_by');
207  $uiComponents[] = $sortViewControl;
208 
209  foreach ($data['items'] as $certificateData) {
210  $thumbnailImagePath = $certificateData['thumbnail_image_path'];
211  $imagePath = ilFileUtils::getWebspaceDir() . $thumbnailImagePath;
212  if ($thumbnailImagePath === null
213  || $thumbnailImagePath === ''
214  || !$this->filesystem->has($thumbnailImagePath)
215  ) {
216  $imagePath = ilUtil::getImagePath('icon_cert.svg');
217  }
218 
219  $cardImage = $this->uiFactory->image()->standard(
220  ilWACSignedPath::signFile($imagePath),
221  $certificateData['title']
222  );
223 
224  $sections = [];
225 
226  if ($certificateData['description'] !== '') {
227  $sections[] = $this->uiFactory->listing()->descriptive([
228  $this->language->txt('cert_description_label') => $certificateData['description']
229  ]);
230  }
231 
232  $oldDatePresentationStatus = ilDatePresentation::useRelativeDates();
234  $sections[] = $this->uiFactory->listing()->descriptive([
235  $this->language->txt('cert_issued_on_label') => ilDatePresentation::formatDate(
236  new ilDateTime($certificateData['date'], IL_CAL_UNIX)
237  )
238  ]);
239  ilDatePresentation::setUseRelativeDates($oldDatePresentationStatus);
240 
241  $objectTypeIcon = $this->uiFactory
242  ->symbol()
243  ->icon()
244  ->standard($certificateData['obj_type'], $certificateData['obj_type']);
245 
246  $objectTitle = $certificateData['title'];
247  $refIds = ilObject::_getAllReferences((int) $certificateData['obj_id']);
248  if (count($refIds) > 0) {
249  foreach ($refIds as $refId) {
250  if ($this->access->checkAccess('read', '', $refId)) {
251  $objectTitle = $this->uiRenderer->render(
252  $this->uiFactory->link()->standard($objectTitle, ilLink::_getLink($refId))
253  );
254  break;
255  }
256  }
257  }
258 
259  $sections[] = $this->uiFactory->listing()->descriptive([$this->language->txt('cert_object_label') => implode(
260  '',
261  [
262  $this->uiRenderer->render($objectTypeIcon),
263  $objectTitle
264  ]
265  )
266  ]);
267 
268  $this->ctrl->setParameter($this, 'certificate_id', $certificateData['id']);
269  $downloadHref = $this->ctrl->getLinkTarget($this, 'download');
270  $this->ctrl->clearParameters($this);
271  $sections[] = $this->uiFactory->button()->standard('Download', $downloadHref);
272 
273  $card = $this->uiFactory
274  ->card()
275  ->standard($certificateData['title'], $cardImage)
276  ->withSections($sections);
277 
278  $cards[] = $card;
279  }
280 
281  $deck = $this->uiFactory->deck($cards)->withSmallCardsSize();
282 
283  $uiComponents[] = $this->uiFactory->divider()->horizontal();
284 
285  $uiComponents[] = $deck;
286  } else {
287  $this->template->setOnScreenMessage('info', $this->language->txt('cert_currently_no_certs'));
288  }
289 
290  $this->template->setContent($this->uiRenderer->render($uiComponents));
291  }
292 
293  protected function getCurrentSortation(): string
294  {
295  $sorting = ilSession::get(self::SORTATION_SESSION_KEY);
296  if (!array_key_exists($sorting, $this->sortationOptions)) {
297  $sorting = $this->defaultSorting;
298  }
299 
300  return $sorting;
301  }
302 
307  protected function applySortation(): void
308  {
309  $sorting = $this->request->getQueryParams()['sort_by'] ?? $this->defaultSorting;
310  if (!array_key_exists($sorting, $this->sortationOptions)) {
311  $sorting = $this->defaultSorting;
312  }
313  ilSession::set(self::SORTATION_SESSION_KEY, $sorting);
314 
315  $this->listCertificates();
316  }
317 
321  public function download(): void
322  {
323  global $DIC;
324 
325  $user = $this->user;
326  $language = $DIC->language();
327 
328  $pdfGenerator = new ilPdfGenerator($this->userCertificateRepository, $this->certificateLogger);
329 
330  $userCertificateId = (int) $this->request->getQueryParams()['certificate_id'];
331 
332  try {
333  $userCertificate = $this->userCertificateRepository->fetchCertificate($userCertificateId);
334  if ($userCertificate->getUserId() !== $user->getId()) {
335  throw new ilException(sprintf(
336  'User "%s" tried to access certificate: "%s"',
337  $user->getLogin(),
338  $userCertificateId
339  ));
340  }
341  } catch (ilException $exception) {
342  $this->certificateLogger->warning($exception->getMessage());
343  $this->template->setOnScreenMessage('failure', $language->txt('cert_error_no_access'));
344  $this->listCertificates();
345  return;
346  }
347 
348  $pdfAction = new ilCertificatePdfAction(
349  $this->certificateLogger,
350  $pdfGenerator,
352  $this->language->txt('error_creating_certificate_pdf')
353  );
354 
355  $pdfAction->downloadPdf($userCertificate->getUserId(), $userCertificate->getObjId());
356 
357  $this->listCertificates();
358  }
359 }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static get(string $a_var)
An entity that renders components to a string output.
Definition: Renderer.php:30
Class ilPdfGeneratorConstantsTest.
ilUserCertificateRepository $userCertificateRepository
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static _getAllReferences(int $id)
get all reference ids for object ID
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
$refId
Definition: xapitoken.php:58
const IL_CAL_UNIX
__construct(?ilGlobalTemplateInterface $template=null, ?ilCtrlInterface $ctrl=null, ?ilLanguage $language=null, ?ilObjUser $user=null, ?ilUserCertificateRepository $userCertificateRepository=null, ?ServerRequestInterface $request=null, ?ilLogger $certificateLogger=null, ?ilSetting $certificateSettings=null, ?Factory $uiFactory=null, ?Renderer $uiRenderer=null, ?ilAccessHandler $access=null, ?Filesystem $filesystem=null)
global $DIC
Definition: feed.php:28
$provider
Definition: ltitoken.php:83
ilGlobalTemplateInterface $template
Just a wrapper class to create Unit Test for other classes.
ServerRequestInterface $request
static signFile(string $path_to_file)
static setUseRelativeDates(bool $a_status)
set use relative dates
static set(string $a_var, $a_val)
Set a value.
Class FlySystemFileAccessTest disabled disabled disabled.
downloadPdf(int $userId, int $objectId)