ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilUserCertificateGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
6
13{
15 private $template;
16
18 private $controller;
19
21 private $language;
22
25
27 private $user;
28
30 private $request;
31
34
37
39 protected $uiFactory;
40
42 protected $uiRenderer;
43
45 protected $access;
46
47 const SORTATION_SESSION_KEY = 'my_certificates_sorting';
48
52 protected $sortationOptions = [
53 'title_ASC' => 'cert_sortable_by_title_asc',
54 'title_DESC' => 'cert_sortable_by_title_desc',
55 'date_ASC' => 'cert_sortable_by_issue_date_asc',
56 'date_DESC' => 'cert_sortable_by_issue_date_desc',
57 ];
58
60 protected $defaultSorting = 'date_DESC';
61
63 private $filesystem;
64
79 public function __construct(
80 ilTemplate $template = null,
81 ilCtrl $controller = null,
82 ilLanguage $language = null,
83 ilObjUser $user = null,
85 GuzzleHttp\Psr7\Request $request = null,
88 Factory $uiFactory = null,
89 Renderer $uiRenderer = null,
92 ) {
93 global $DIC;
94
95 $logger = $DIC->logger()->cert();
96
97 if ($template === null) {
98 $template = $DIC->ui()->mainTemplate();
99 }
100 $this->template = $template;
101
102 if ($controller === null) {
103 $controller = $DIC->ctrl();
104 }
105 $this->controller = $controller;
106
107 if ($language === null) {
108 $language = $DIC->language();
109 }
110 $this->language = $language;
111
112 if ($user === null) {
113 $user = $DIC->user();
114 }
115 $this->user = $user;
116
117
118 if ($request === null) {
119 $request = $DIC->http()->request();
120 }
121 $this->request = $request;
122
123 if ($certificateLogger === null) {
124 $certificateLogger = $DIC->logger()->cert();
125 }
126 $this->certificateLogger = $certificateLogger;
127
128 if ($certificateSettings === null) {
129 $certificateSettings = new ilSetting("certificate");
130 }
131 $this->certificateSettings = $certificateSettings;
132
133 if (null === $uiFactory) {
134 $uiFactory = $DIC->ui()->factory();
135 }
136 $this->uiFactory = $uiFactory;
137
138 if (null === $uiRenderer) {
139 $uiRenderer = $DIC->ui()->renderer();
140 }
141 $this->uiRenderer = $uiRenderer;
142
143 if (null === $access) {
144 $access = $DIC->access();
145 }
146 $this->access = $access;
147
148 if (null === $filesystem) {
149 $filesystem = $DIC->filesystem()->web();
150 }
151 $this->filesystem = $filesystem;
152
153 if ($userCertificateRepository === null) {
154 $userCertificateRepository = new ilUserCertificateRepository(null, $this->certificateLogger);
155 }
156 $this->userCertificateRepository = $userCertificateRepository;
157
158 $this->language->loadLanguageModule('cert');
159 $this->language->loadLanguageModule('cert');
160 }
161
165 private function getDefaultCommand() : string
166 {
167 return 'listCertificates';
168 }
169
175 public function executeCommand()
176 {
177 $nextClass = $this->controller->getNextClass($this);
178 $cmd = $this->controller->getCmd();
179
180 if (!$this->certificateSettings->get('active')) {
181 $this->controller->returnToParent($this);
182 }
183
184 $this->template->setTitle($this->language->txt('obj_cert'));
185
186 switch ($nextClass) {
187 default:
188 if (!method_exists($this, $cmd)) {
189 $cmd = $this->getDefaultCommand();
190 }
191 $this->{$cmd}();
192 }
193
194 return true;
195 }
196
201 public function listCertificates()
202 {
203 global $DIC;
204
205 if (!$this->certificateSettings->get('active')) {
206 $this->controller->redirect($this);
207 return;
208 }
209
210 $provider = new ilUserCertificateTableProvider(
211 $DIC->database(),
212 $this->certificateLogger,
213 $this->controller,
214 $this->language->txt('certificate_no_object_title')
215 );
216
217 $sorting = $this->getCurrentSortation();
218 $data = $provider->fetchDataSet(
219 $this->user->getId(),
220 [
221 'order_field' => explode('_', $sorting)[0],
222 'order_direction' => explode('_', $sorting)[1],
223 'language' => $this->user->getLanguage()
224 ],
225 []
226 );
227
228 $uiComponents = [];
229
230 if (count($data['items']) > 0) {
232 $cards = [];
233
234 foreach ($this->sortationOptions as $fieldAndDirection => $lngVariable) {
235 $sortationOptions[$fieldAndDirection] = $this->language->txt($lngVariable);
236 }
237
238 $sortViewControl = $this->uiFactory
239 ->viewControl()
240 ->sortation($sortationOptions)
241 ->withLabel($this->language->txt($this->sortationOptions[$sorting]))
242 ->withTargetURL($this->controller->getLinkTarget($this, 'applySortation'), 'sort_by');
243 $uiComponents[] = $sortViewControl;
244
245 foreach ($data['items'] as $certificateData) {
246 $thumbnailImagePath = $certificateData['thumbnail_image_path'];
247 $imagePath = ilUtil::getWebspaceDir() . $thumbnailImagePath;
248 if ($thumbnailImagePath === null
249 || $thumbnailImagePath === ''
250 || !$this->filesystem->has($thumbnailImagePath)
251 ) {
252 $imagePath = \ilUtil::getImagePath('icon_cert.svg');
253 }
254
255 $cardImage = $this->uiFactory->image()->standard(
256 ilWACSignedPath::signFile($imagePath),
257 $certificateData['title']
258 );
259
260
261 $sections = [];
262
263 if (strlen($certificateData['description']) > 0) {
264 $sections[] = $this->uiFactory->listing()->descriptive([
265 $this->language->txt('cert_description_label') => $certificateData['description']
266 ]);
267 }
268
269
270 $oldDatePresentationStatus = \ilDatePresentation::useRelativeDates();
272 $sections[] = $this->uiFactory->listing()->descriptive([
273 $this->language->txt('cert_issued_on_label') => \ilDatePresentation::formatDate(
274 new \ilDateTime($certificateData['date'], \IL_CAL_UNIX)
275 )
276 ]);
277 \ilDatePresentation::setUseRelativeDates($oldDatePresentationStatus);
278
279 $objectTypeIcon = $this->uiFactory
280 ->symbol()
281 ->icon()
282 ->standard($certificateData['obj_type'], $certificateData['obj_type'], 'small');
283
284 $objectTitle = $certificateData['title'];
285 $refIds = \ilObject::_getAllReferences($certificateData['obj_id']);
286 if (count($refIds) > 0) {
287 foreach ($refIds as $refId) {
288 if ($this->access->checkAccess('read', '', $refId)) {
289 $objectTitle = $this->uiRenderer->render(
290 $this->uiFactory->link()->standard($objectTitle, \ilLink::_getLink($refId))
291 );
292 break;
293 }
294 }
295 }
296
297 $sections[] = $this->uiFactory->listing()->descriptive([$this->language->txt('cert_object_label') => implode('', [
298 $this->uiRenderer->render($objectTypeIcon),
299 $objectTitle
300 ])]);
301
302 $this->controller->setParameter($this, 'certificate_id', $certificateData['id']);
303 $downloadHref = $this->controller->getLinkTarget($this, 'download');
304 $this->controller->clearParameters($this);
305 $sections[] = $this->uiFactory->button()->standard('Download', $downloadHref);
306
307 $card = $this->uiFactory
308 ->card()
309 ->standard($certificateData['title'], $cardImage)
310 ->withSections($sections);
311
312 $cards[] = $card;
313 }
314
315 $deck = $this->uiFactory->deck($cards)->withNormalCardsSize();
316
317 $uiComponents[] = $this->uiFactory->divider()->horizontal();
318
319 $uiComponents[] = $deck;
320 } else {
321 \ilUtil::sendInfo($this->language->txt('cert_currently_no_certs'));
322 }
323
324 $this->template->setContent($this->uiRenderer->render($uiComponents));
325 }
326
330 protected function getCurrentSortation() : string
331 {
332 $sorting = \ilSession::get(self::SORTATION_SESSION_KEY);
333 if (!array_key_exists($sorting, $this->sortationOptions)) {
334 $sorting = $this->defaultSorting;
335 }
336
337 return $sorting;
338 }
339
343 protected function applySortation()
344 {
345 $sorting = $this->request->getQueryParams()['sort_by'] ?? $this->defaultSorting;
346 if (!array_key_exists($sorting, $this->sortationOptions)) {
347 $sorting = $this->defaultSorting;
348 }
349 \ilSession::set(self::SORTATION_SESSION_KEY, $sorting);
350
351 $this->listCertificates();
352 }
353
357 public function download()
358 {
359 global $DIC;
360
361 $user = $DIC->user();
362 $language = $DIC->language();
363
364 $pdfGenerator = new ilPdfGenerator($this->userCertificateRepository, $this->certificateLogger);
365
366 $userCertificateId = (int) $this->request->getQueryParams()['certificate_id'];
367
368 try {
369 $userCertificate = $this->userCertificateRepository->fetchCertificate($userCertificateId);
370 if ((int) $userCertificate->getUserId() !== (int) $user->getId()) {
371 throw new ilException(sprintf('User "%s" tried to access certificate: "%s"', $user->getLogin(), $userCertificateId));
372 }
373 } catch (ilException $exception) {
374 $this->certificateLogger->warning($exception->getMessage());
375 ilUtil::sendFailure($language->txt('cert_error_no_access'));
376 $this->listCertificates();
377 return;
378 }
379
380 $pdfAction = new ilCertificatePdfAction(
381 $this->certificateLogger,
382 $pdfGenerator,
384 $this->language->txt('error_creating_certificate_pdf')
385 );
386
387 $pdfAction->downloadPdf($userCertificate->getUserId(), $userCertificate->getObjId());
388
389 $this->listCertificates();
390 }
391}
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
Just a wrapper class to create Unit Test for other classes.
This class provides processing control methods.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
static setUseRelativeDates($a_status)
set use relative dates
static useRelativeDates()
check if relative dates are used
@classDescription Date and time handling
Base class for ILIAS Exception handling.
language handling
Component logger with individual log levels by component id.
static _getAllReferences($a_id)
get all reference ids of object
static set($a_var, $a_val)
Set a value.
static get($a_var)
Get a value.
ILIAS Setting Class.
special template class to simplify handling of ITX/PEAR
__construct(ilTemplate $template=null, ilCtrl $controller=null, ilLanguage $language=null, ilObjUser $user=null, ilUserCertificateRepository $userCertificateRepository=null, GuzzleHttp\Psr7\Request $request=null, ilLogger $certificateLogger=null, ilSetting $certificateSettings=null, Factory $uiFactory=null, Renderer $uiRenderer=null, \ilAccessHandler $access=null, \ILIAS\Filesystem\Filesystem $filesystem=null)
static getWebspaceDir($mode="filesystem")
get webspace directory
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
static signFile($path_to_file)
This is how the factory for UI elements looks.
Definition: Factory.php:18
An entity that renders components to a string output.
Definition: Renderer.php:15
Interface ilAccessHandler.
language()
Definition: language.php:2
Class FlySystemFileAccessTest.
Class ChatMainBarProvider \MainMenu\Provider.
Class ilPdfGeneratorConstantsTest.
$data
Definition: storeScorm.php:23
$DIC
Definition: xapitoken.php:46
$refId
Definition: xapitoken.php:42