ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilUserCertificateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
30 {
32  private readonly ilCtrlInterface $ctrl;
33  private readonly ilLanguage $language;
35  private readonly ilObjUser $user;
36  private readonly ServerRequestInterface $request;
37  private readonly ilLogger $certificateLogger;
39  protected Factory $uiFactory;
40  protected Renderer $uiRenderer;
42  protected ilHelpGUI $help;
43  final public const SORTATION_SESSION_KEY = 'my_certificates_sorting';
44 
48  protected array $sortationOptions = [
49  'title_ASC' => 'cert_sortable_by_title_asc',
50  'title_DESC' => 'cert_sortable_by_title_desc',
51  'date_ASC' => 'cert_sortable_by_issue_date_asc',
52  'date_DESC' => 'cert_sortable_by_issue_date_desc',
53  ];
54  protected string $defaultSorting = 'date_DESC';
55  private readonly Filesystem $filesystem;
56 
57  public function __construct(
58  ?ilGlobalTemplateInterface $template = null,
59  ?ilCtrlInterface $ctrl = null,
60  ?ilLanguage $language = null,
61  ?ilObjUser $user = null,
62  ?ilUserCertificateRepository $userCertificateRepository = null,
63  ?ServerRequestInterface $request = null,
64  ?ilLogger $certificateLogger = null,
65  ?ilSetting $certificateSettings = null,
66  ?Factory $uiFactory = null,
67  ?Renderer $uiRenderer = null,
68  ?ilAccessHandler $access = null,
69  ?Filesystem $filesystem = null,
70  ?ilHelpGUI $help = null
71  ) {
72  global $DIC;
73 
74  if ($template === null) {
75  $template = $DIC->ui()->mainTemplate();
76  }
77  $this->template = $template;
78 
79  if ($ctrl === null) {
80  $ctrl = $DIC->ctrl();
81  }
82  $this->ctrl = $ctrl;
83 
84  if ($language === null) {
85  $language = $DIC->language();
86  }
87  $this->language = $language;
88 
89  if ($user === null) {
90  $user = $DIC->user();
91  }
92  $this->user = $user;
93 
94  if ($request === null) {
95  $request = $DIC->http()->request();
96  }
97  $this->request = $request;
98 
99  if ($certificateLogger === null) {
100  $certificateLogger = $DIC->logger()->cert();
101  }
102  $this->certificateLogger = $certificateLogger;
103 
104  if ($certificateSettings === null) {
105  $certificateSettings = new ilSetting("certificate");
106  }
107  $this->certificateSettings = $certificateSettings;
108 
109  if (null === $uiFactory) {
110  $uiFactory = $DIC->ui()->factory();
111  }
112  $this->uiFactory = $uiFactory;
113 
114  if (null === $uiRenderer) {
115  $uiRenderer = $DIC->ui()->renderer();
116  }
117  $this->uiRenderer = $uiRenderer;
118 
119  if (null === $access) {
120  $access = $DIC->access();
121  }
122  $this->access = $access;
123 
124  if (null === $filesystem) {
125  $filesystem = $DIC->filesystem()->web();
126  }
127  $this->filesystem = $filesystem;
128 
129  if ($userCertificateRepository === null) {
130  $userCertificateRepository = new ilUserCertificateRepository(null, $this->certificateLogger);
131  }
132  $this->userCertificateRepository = $userCertificateRepository;
133 
134  $this->help = $help ?? $DIC->help();
135 
136  $this->language->loadLanguageModule('cert');
137  }
138 
139  private function getDefaultCommand(): string
140  {
141  return 'listCertificates';
142  }
143 
144  public function executeCommand(): bool
145  {
146  $cmd = $this->ctrl->getCmd();
147 
148  if (!$this->certificateSettings->get('active', '0')) {
149  $this->ctrl->returnToParent($this);
150  }
151 
152  $this->template->setTitle($this->language->txt('obj_cert'));
153  if (!method_exists($this, $cmd)) {
154  $cmd = $this->getDefaultCommand();
155  }
156  $this->{$cmd}();
157 
158  return true;
159  }
160 
165  public function listCertificates(): void
166  {
167  global $DIC;
168 
169  $this->help->setScreenIdComponent('cert');
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 ($data['items'] !== []) {
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('standard/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  foreach ($refIds as $refId) {
249  if ($this->access->checkAccess('read', '', $refId)) {
250  $objectTitle = $this->uiRenderer->render(
251  $this->uiFactory->link()->standard($objectTitle, ilLink::_getLink($refId))
252  );
253  break;
254  }
255  }
256 
257  $sections[] = $this->uiFactory->listing()->descriptive([$this->language->txt('cert_object_label') => implode(
258  '',
259  [
260  $this->uiRenderer->render($objectTypeIcon),
261  $objectTitle
262  ]
263  )
264  ]);
265 
266  $this->ctrl->setParameter($this, 'certificate_id', $certificateData['id']);
267  $downloadHref = $this->ctrl->getLinkTarget($this, 'download');
268  $this->ctrl->clearParameters($this);
269  $sections[] = $this->uiFactory->button()->standard('Download', $downloadHref);
270 
271  $card = $this->uiFactory
272  ->card()
273  ->standard($certificateData['title'], $cardImage)
274  ->withSections($sections);
275 
276  $cards[] = $card;
277  }
278 
279  $deck = $this->uiFactory->deck($cards)->withSmallCardsSize();
280 
281  $uiComponents[] = $this->uiFactory->divider()->horizontal();
282 
283  $uiComponents[] = $deck;
284  } else {
285  $this->template->setOnScreenMessage('info', $this->language->txt('cert_currently_no_certs'));
286  }
287 
288  $this->template->setContent($this->uiRenderer->render($uiComponents));
289  }
290 
291  protected function getCurrentSortation(): string
292  {
293  $sorting = ilSession::get(self::SORTATION_SESSION_KEY);
294  if (!array_key_exists($sorting, $this->sortationOptions)) {
295  $sorting = $this->defaultSorting;
296  }
297 
298  return $sorting;
299  }
300 
305  protected function applySortation(): void
306  {
307  $sorting = $this->request->getQueryParams()['sort_by'] ?? $this->defaultSorting;
308  if (!array_key_exists($sorting, $this->sortationOptions)) {
309  $sorting = $this->defaultSorting;
310  }
311  ilSession::set(self::SORTATION_SESSION_KEY, $sorting);
312 
313  $this->listCertificates();
314  }
315 
319  public function download(): void
320  {
321  $pdfGenerator = new ilPdfGenerator($this->userCertificateRepository);
322 
323  $userCertificateId = (int) $this->request->getQueryParams()['certificate_id'];
324 
325  try {
326  $userCertificate = $this->userCertificateRepository->fetchCertificate($userCertificateId);
327  if ($userCertificate->getUserId() !== $this->user->getId()) {
328  throw new ilException(sprintf(
329  'User "%s" tried to access certificate: "%s"',
330  $this->user->getLogin(),
331  $userCertificateId
332  ));
333  }
334  } catch (ilException $exception) {
335  $this->certificateLogger->warning($exception->getMessage());
336  $this->template->setOnScreenMessage('failure', $this->language->txt('cert_error_no_access'));
337  $this->listCertificates();
338  return;
339  }
340 
341  $pdfAction = new ilCertificatePdfAction(
342  $pdfGenerator,
344  $this->language->txt('error_creating_certificate_pdf')
345  );
346 
347  $pdfAction->downloadPdf($userCertificate->getUserId(), $userCertificate->getObjId());
348 
349  $this->listCertificates();
350  }
351 }
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
ilUserCertificateGUI: ilAchievementsGUI
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)
Help GUI class.
readonly ilLogger $certificateLogger
__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, ?ilHelpGUI $help=null)
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
global $DIC
Definition: feed.php:28
$provider
Definition: ltitoken.php:83
readonly ilCtrlInterface $ctrl
readonly ServerRequestInterface $request
Just a wrapper class to create Unit Test for other classes.
readonly ilGlobalTemplateInterface $template
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.
readonly ilUserCertificateRepository $userCertificateRepository
downloadPdf(int $userId, int $objectId)