ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilUserCertificateGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use Psr\Http\Message\ServerRequestInterface;
26
31{
32 final public const string SORTATION_SESSION_KEY = 'my_certificates_sorting';
33
35 private readonly ilCtrlInterface $ctrl;
36 private readonly ilLanguage $language;
38 private readonly ilObjUser $user;
39 private readonly ServerRequestInterface $request;
40 private readonly ilLogger $logger;
41 private readonly Factory $ui_factory;
42 private readonly Renderer $ui_renderer;
43 private readonly ilAccessHandler $access;
44 private readonly ilHelpGUI $help;
45 private readonly ilDBInterface $db;
46
50 protected array $sortation_options = [
51 'title_ASC' => 'cert_sortable_by_title_asc',
52 'title_DESC' => 'cert_sortable_by_title_desc',
53 'date_ASC' => 'cert_sortable_by_issue_date_asc',
54 'date_DESC' => 'cert_sortable_by_issue_date_desc',
55 ];
56 protected string $default_sorting = 'date_DESC';
57
58 public function __construct(
60 ?ilCtrlInterface $ctrl = null,
61 ?ilLanguage $language = null,
62 ?ilObjUser $user = null,
63 ?ilUserCertificateRepository $userCertificateRepository = null,
64 ?ServerRequestInterface $request = null,
65 ?ilLogger $certificateLogger = null,
66 private readonly ilSetting $certificateSettings = new ilSetting('certificate'),
67 ?Factory $uiFactory = null,
68 ?Renderer $uiRenderer = null,
70 ?ilHelpGUI $help = null,
71 ?ilDBInterface $db = null,
72 private ?IRSS $irss = null
73 ) {
74 global $DIC;
75
76 $this->template = $template ?? $DIC->ui()->mainTemplate();
77 $this->ctrl = $ctrl ?? $DIC->ctrl();
78 $this->user = $user ?? $DIC->user();
79 $this->language = $language ?? $DIC->language();
80 $this->request = $request ?? $DIC->http()->request();
81 $this->logger = $certificateLogger ?? $DIC->logger()->cert();
82 $this->ui_factory = $uiFactory ?? $DIC->ui()->factory();
83 $this->ui_renderer = $uiRenderer ?? $DIC->ui()->renderer();
84 $this->access = $access ?? $DIC->access();
85 $this->user_certificate_repo = $userCertificateRepository ?? new ilUserCertificateRepository(null, $this->logger);
86 $this->help = $help ?? $DIC->help();
87 $this->db = $db ?? $DIC->database();
88 $this->irss = $irss ?? $DIC->resourceStorage();
89
90 $this->language->loadLanguageModule('cert');
91 }
92
93 private function getDefaultCommand(): string
94 {
95 return 'listCertificates';
96 }
97
98 public function executeCommand(): bool
99 {
100 $cmd = $this->ctrl->getCmd();
101
102 if (!$this->certificateSettings->get('active', '0')) {
103 $this->ctrl->returnToParent($this);
104 }
105
106 $this->template->setTitle($this->language->txt('obj_cert'));
107 if (!method_exists($this, $cmd)) {
108 $cmd = $this->getDefaultCommand();
109 }
110 $this->{$cmd}();
111
112 return true;
113 }
114
119 public function listCertificates(): void
120 {
121 $this->help->setScreenIdComponent('cert');
122
123 if (!$this->certificateSettings->get('active', '0')) {
124 $this->ctrl->redirect($this);
125 }
126
127 $this->template->setPermanentLink('cert', null, 'list');
128
130 $this->db,
131 $this->logger,
132 $this->language->txt('certificate_no_object_title')
133 );
134
135 $sorting = $this->getCurrentSortation();
136 $data = $provider->fetchDataSet(
137 $this->user->getId(),
138 [
139 'order_field' => explode('_', $sorting)[0],
140 'order_direction' => explode('_', $sorting)[1],
141 'language' => $this->user->getLanguage()
142 ],
143 []
144 );
145
146 $uiComponents = [];
147
148 if ($data['items'] !== []) {
149 $sortationOptions = [];
150 $cards = [];
151
152 foreach ($this->sortation_options as $fieldAndDirection => $lngVariable) {
153 $sortationOptions[$fieldAndDirection] = $this->language->txt($lngVariable);
154 }
155
156 $sortViewControl = $this->ui_factory
157 ->viewControl()
158 ->sortation($sortationOptions, $sorting)
159 ->withTargetURL($this->ctrl->getLinkTarget($this, 'applySortation'), 'sort_by');
160
161 $uiComponents[] = $sortViewControl;
162
163 foreach ($data['items'] as $certificateData) {
164 $tile_image_identification = $certificateData['tile_image_ident'] ?? '';
165 $imagePath = '';
166 $tile_image_rid = $this->irss->manage()->find($tile_image_identification);
167 if ($tile_image_rid instanceof ResourceIdentification) {
168 $imagePath = $this->irss->consume()->src($tile_image_rid)->getSrc(true);
169 }
170
171 if ($imagePath === '') {
172 $imagePath = ilUtil::getImagePath('standard/icon_cert.svg');
173 }
174
175 $cardImage = $this->ui_factory->image()->standard(
176 $imagePath,
177 $certificateData['title']
178 );
179
180 $sections = [];
181
182 if ($certificateData['description'] !== '') {
183 $sections[] = $this->ui_factory->listing()->descriptive([
184 $this->language->txt('cert_description_label') => $certificateData['description']
185 ]);
186 }
187
188 $oldDatePresentationStatus = ilDatePresentation::useRelativeDates();
190 $sections[] = $this->ui_factory->listing()->descriptive([
191 $this->language->txt('cert_issued_on_label') => ilDatePresentation::formatDate(
192 new ilDateTime($certificateData['date'], IL_CAL_UNIX)
193 )
194 ]);
195 ilDatePresentation::setUseRelativeDates($oldDatePresentationStatus);
196
197 $objectTypeIcon = $this->ui_factory
198 ->symbol()
199 ->icon()
200 ->standard($certificateData['obj_type'], $certificateData['obj_type'])
201 ;
202
203 $objectTitle = $certificateData['title'];
204 $refIds = ilObject::_getAllReferences((int) $certificateData['obj_id']);
205 foreach ($refIds as $refId) {
206 if ($this->access->checkAccess('read', '', $refId)) {
207 $objectTitle = $this->ui_renderer->render(
208 $this->ui_factory->link()->standard($objectTitle, ilLink::_getLink($refId))
209 );
210
211 break;
212 }
213 }
214
215 $sections[] = $this->ui_factory->listing()->descriptive([$this->language->txt('cert_object_label') => implode(
216 '',
217 [
218 $this->ui_renderer->render($objectTypeIcon),
219 $objectTitle
220 ]
221 )
222 ]);
223
224 $this->ctrl->setParameter($this, 'certificate_id', $certificateData['id']);
225 $downloadHref = $this->ctrl->getLinkTarget($this, 'download');
226 $this->ctrl->clearParameters($this);
227 $sections[] = $this->ui_factory->button()->standard('Download', $downloadHref);
228
229 $card = $this->ui_factory
230 ->card()
231 ->standard($certificateData['title'], $cardImage)
232 ->withSections($sections)
233 ;
234
235 $cards[] = $card;
236 }
237
238 $deck = $this->ui_factory->deck($cards)->withSmallCardsSize();
239
240 $uiComponents[] = $this->ui_factory->divider()->horizontal();
241
242 $uiComponents[] = $deck;
243 } else {
244 $this->template->setOnScreenMessage('info', $this->language->txt('cert_currently_no_certs'));
245 }
246
247 $this->template->setContent($this->ui_renderer->render($uiComponents));
248 }
249
250 protected function getCurrentSortation(): string
251 {
252 $sorting = ilSession::get(self::SORTATION_SESSION_KEY);
253 if (!array_key_exists($sorting, $this->sortation_options)) {
254 $sorting = $this->default_sorting;
255 }
256
257 return $sorting;
258 }
259
264 protected function applySortation(): void
265 {
266 $sorting = $this->request->getQueryParams()['sort_by'] ?? $this->default_sorting;
267 if (!array_key_exists($sorting, $this->sortation_options)) {
268 $sorting = $this->default_sorting;
269 }
270 ilSession::set(self::SORTATION_SESSION_KEY, $sorting);
271
272 $this->listCertificates();
273 }
274
278 public function download(): void
279 {
280 $certificate_id = (int) $this->request->getQueryParams()['certificate_id'];
281
282 try {
283 $certificate = $this->user_certificate_repo->fetchCertificate($certificate_id);
284 if ($certificate->getUserId() !== $this->user->getId()) {
285 throw new ilException(
286 sprintf('User "%s" tried to access certificate: "%s"', $this->user->getLogin(), $certificate_id)
287 );
288 }
289 } catch (ilException $exception) {
290 $this->logger->warning($exception->getMessage());
291 $this->template->setOnScreenMessage('failure', $this->language->txt('cert_error_no_access'));
292 $this->listCertificates();
293
294 return;
295 }
296
297 $action = (new ilCertificatePdfAction(
298 (new ilPdfGenerator($this->user_certificate_repo))->withLogger($this->logger),
300 $this->language->txt('error_creating_certificate_pdf')
301 ))->withLogger($this->logger);
302 $action->downloadPdf($certificate->getUserId(), $certificate->getObjId());
303
304 $this->listCertificates();
305 }
306}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
const IL_CAL_UNIX
Just a wrapper class to create Unit Test for other classes.
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
@classDescription Date and time handling
Base class for ILIAS Exception handling.
Help GUI class.
language handling
Component logger with individual log levels by component id.
User class.
static _getAllReferences(int $id)
get all reference ids for object ID
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
ILIAS Setting Class.
@ilCtrl_IsCalledBy ilUserCertificateGUI: ilAchievementsGUI
readonly ServerRequestInterface $request
readonly ilAccessHandler $access
readonly ilUserCertificateRepository $user_certificate_repo
final const string SORTATION_SESSION_KEY
readonly ilCtrlInterface $ctrl
__construct(?ilGlobalTemplateInterface $template=null, ?ilCtrlInterface $ctrl=null, ?ilLanguage $language=null, ?ilObjUser $user=null, ?ilUserCertificateRepository $userCertificateRepository=null, ?ServerRequestInterface $request=null, ?ilLogger $certificateLogger=null, private readonly ilSetting $certificateSettings=new ilSetting('certificate'), ?Factory $uiFactory=null, ?Renderer $uiRenderer=null, ?ilAccessHandler $access=null, ?ilHelpGUI $help=null, ?ilDBInterface $db=null, private ?IRSS $irss=null)
readonly ilGlobalTemplateInterface $template
readonly ilDBInterface $db
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilDBInterface.
$provider
Definition: ltitoken.php:80
global $DIC
Definition: shib_login.php:26
$refId
Definition: xapitoken.php:56