ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilUserCertificateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
32 {
33  final public const SORTATION_SESSION_KEY = 'my_certificates_sorting';
34 
36  private readonly ilCtrlInterface $ctrl;
37  private readonly ilLanguage $language;
39  private readonly ilObjUser $user;
40  private readonly ServerRequestInterface $request;
41  private readonly ilLogger $certificateLogger;
42  private readonly Factory $uiFactory;
43  private readonly Renderer $uiRenderer;
44  private readonly ilAccessHandler $access;
45  private readonly ilHelpGUI $help;
46  private readonly ilDBInterface $db;
47 
51  protected array $sortationOptions = [
52  'title_ASC' => 'cert_sortable_by_title_asc',
53  'title_DESC' => 'cert_sortable_by_title_desc',
54  'date_ASC' => 'cert_sortable_by_issue_date_asc',
55  'date_DESC' => 'cert_sortable_by_issue_date_desc',
56  ];
57  protected string $defaultSorting = 'date_DESC';
58  private readonly Filesystem $filesystem;
59 
60  public function __construct(
61  ?ilGlobalTemplateInterface $template = null,
62  ?ilCtrlInterface $ctrl = null,
63  ?ilLanguage $language = null,
64  ?ilObjUser $user = null,
65  ?ilUserCertificateRepository $userCertificateRepository = null,
66  ?ServerRequestInterface $request = null,
67  ?ilLogger $certificateLogger = null,
68  private readonly ilSetting $certificateSettings = new ilSetting('certificate'),
69  ?Factory $uiFactory = null,
70  ?Renderer $uiRenderer = null,
71  ?ilAccessHandler $access = null,
72  ?Filesystem $filesystem = null,
73  ?ilHelpGUI $help = null,
74  ?ilDBInterface $db = null,
75  private ?IRSS $irss = null
76  ) {
77  global $DIC;
78 
79  $this->template = $template ?? $DIC->ui()->mainTemplate();
80  $this->ctrl = $ctrl ?? $DIC->ctrl();
81  $this->user = $user ?? $DIC->user();
82  $this->language = $language ?? $DIC->language();
83  $this->request = $request ?? $DIC->http()->request();
84  $this->certificateLogger = $certificateLogger ?? $DIC->logger()->cert();
85  $this->uiFactory = $uiFactory ?? $DIC->ui()->factory();
86  $this->uiRenderer = $uiRenderer ?? $DIC->ui()->renderer();
87  $this->access = $access ?? $DIC->access();
88  $this->filesystem = $filesystem ?? $DIC->filesystem()->web();
89  $this->userCertificateRepository = $userCertificateRepository ?? new ilUserCertificateRepository(null, $this->certificateLogger);
90  $this->help = $help ?? $DIC->help();
91  $this->db = $db ?? $DIC->database();
92  $this->irss = $irss ?? $DIC->resourceStorage();
93 
94  $this->language->loadLanguageModule('cert');
95  }
96 
97  private function getDefaultCommand(): string
98  {
99  return 'listCertificates';
100  }
101 
102  public function executeCommand(): bool
103  {
104  $cmd = $this->ctrl->getCmd();
105 
106  if (!$this->certificateSettings->get('active', '0')) {
107  $this->ctrl->returnToParent($this);
108  }
109 
110  $this->template->setTitle($this->language->txt('obj_cert'));
111  if (!method_exists($this, $cmd)) {
112  $cmd = $this->getDefaultCommand();
113  }
114  $this->{$cmd}();
115 
116  return true;
117  }
118 
123  public function listCertificates(): void
124  {
125  $this->help->setScreenIdComponent('cert');
126 
127  if (!$this->certificateSettings->get('active', '0')) {
128  $this->ctrl->redirect($this);
129  }
130 
132  $this->db,
133  $this->certificateLogger,
134  $this->language->txt('certificate_no_object_title')
135  );
136 
137  $sorting = $this->getCurrentSortation();
138  $data = $provider->fetchDataSet(
139  $this->user->getId(),
140  [
141  'order_field' => explode('_', $sorting)[0],
142  'order_direction' => explode('_', $sorting)[1],
143  'language' => $this->user->getLanguage()
144  ],
145  []
146  );
147 
148  $uiComponents = [];
149 
150  if ($data['items'] !== []) {
151  $sortationOptions = [];
152  $cards = [];
153 
154  foreach ($this->sortationOptions as $fieldAndDirection => $lngVariable) {
155  $sortationOptions[$fieldAndDirection] = $this->language->txt($lngVariable);
156  }
157 
158  $sortViewControl = $this->uiFactory
159  ->viewControl()
160  ->sortation($sortationOptions, $sorting)
161  ->withTargetURL($this->ctrl->getLinkTarget($this, 'applySortation'), 'sort_by');
162 
163  $uiComponents[] = $sortViewControl;
164 
165  foreach ($data['items'] as $certificateData) {
166  $tile_image_identification = $certificateData['tile_image_ident'] ?? '';
167  $imagePath = '';
168  if ($tile_image_identification === '' || $tile_image_identification === '-') {
169  $tile_image_identification = $certificateData['tile_image_path'] ?? '';
170  if ($tile_image_identification !== '' && $this->filesystem->has($tile_image_identification)) {
171  $imagePath = ilWACSignedPath::signFile(
172  ilFileUtils::getWebspaceDir() . $tile_image_identification
173  );
174  }
175  } else {
176  $tile_image_rid = $this->irss->manage()->find($tile_image_identification);
177  if ($tile_image_rid instanceof ResourceIdentification) {
178  $imagePath = $this->irss->consume()->src($tile_image_rid)->getSrc(true);
179  }
180  }
181 
182  if ($imagePath === '') {
183  $imagePath = ilUtil::getImagePath('standard/icon_cert.svg');
184  }
185 
186  $cardImage = $this->uiFactory->image()->standard(
187  $imagePath,
188  $certificateData['title']
189  );
190 
191  $sections = [];
192 
193  if ($certificateData['description'] !== '') {
194  $sections[] = $this->uiFactory->listing()->descriptive([
195  $this->language->txt('cert_description_label') => $certificateData['description']
196  ]);
197  }
198 
199  $oldDatePresentationStatus = ilDatePresentation::useRelativeDates();
201  $sections[] = $this->uiFactory->listing()->descriptive([
202  $this->language->txt('cert_issued_on_label') => ilDatePresentation::formatDate(
203  new ilDateTime($certificateData['date'], IL_CAL_UNIX)
204  )
205  ]);
206  ilDatePresentation::setUseRelativeDates($oldDatePresentationStatus);
207 
208  $objectTypeIcon = $this->uiFactory
209  ->symbol()
210  ->icon()
211  ->standard($certificateData['obj_type'], $certificateData['obj_type'])
212  ;
213 
214  $objectTitle = $certificateData['title'];
215  $refIds = ilObject::_getAllReferences((int) $certificateData['obj_id']);
216  foreach ($refIds as $refId) {
217  if ($this->access->checkAccess('read', '', $refId)) {
218  $objectTitle = $this->uiRenderer->render(
219  $this->uiFactory->link()->standard($objectTitle, ilLink::_getLink($refId))
220  );
221 
222  break;
223  }
224  }
225 
226  $sections[] = $this->uiFactory->listing()->descriptive([$this->language->txt('cert_object_label') => implode(
227  '',
228  [
229  $this->uiRenderer->render($objectTypeIcon),
230  $objectTitle
231  ]
232  )
233  ]);
234 
235  $this->ctrl->setParameter($this, 'certificate_id', $certificateData['id']);
236  $downloadHref = $this->ctrl->getLinkTarget($this, 'download');
237  $this->ctrl->clearParameters($this);
238  $sections[] = $this->uiFactory->button()->standard('Download', $downloadHref);
239 
240  $card = $this->uiFactory
241  ->card()
242  ->standard($certificateData['title'], $cardImage)
243  ->withSections($sections)
244  ;
245 
246  $cards[] = $card;
247  }
248 
249  $deck = $this->uiFactory->deck($cards)->withSmallCardsSize();
250 
251  $uiComponents[] = $this->uiFactory->divider()->horizontal();
252 
253  $uiComponents[] = $deck;
254  } else {
255  $this->template->setOnScreenMessage('info', $this->language->txt('cert_currently_no_certs'));
256  }
257 
258  $this->template->setContent($this->uiRenderer->render($uiComponents));
259  }
260 
261  protected function getCurrentSortation(): string
262  {
263  $sorting = ilSession::get(self::SORTATION_SESSION_KEY);
264  if (!array_key_exists($sorting, $this->sortationOptions)) {
265  $sorting = $this->defaultSorting;
266  }
267 
268  return $sorting;
269  }
270 
275  protected function applySortation(): void
276  {
277  $sorting = $this->request->getQueryParams()['sort_by'] ?? $this->defaultSorting;
278  if (!array_key_exists($sorting, $this->sortationOptions)) {
279  $sorting = $this->defaultSorting;
280  }
281  ilSession::set(self::SORTATION_SESSION_KEY, $sorting);
282 
283  $this->listCertificates();
284  }
285 
289  public function download(): void
290  {
291  $pdfGenerator = new ilPdfGenerator($this->userCertificateRepository);
292 
293  $userCertificateId = (int) $this->request->getQueryParams()['certificate_id'];
294 
295  try {
296  $userCertificate = $this->userCertificateRepository->fetchCertificate($userCertificateId);
297  if ($userCertificate->getUserId() !== $this->user->getId()) {
298  throw new ilException(sprintf('User "%s" tried to access certificate: "%s"', $this->user->getLogin(), $userCertificateId));
299  }
300  } catch (ilException $exception) {
301  $this->certificateLogger->warning($exception->getMessage());
302  $this->template->setOnScreenMessage('failure', $this->language->txt('cert_error_no_access'));
303  $this->listCertificates();
304 
305  return;
306  }
307 
308  $pdfAction = new ilCertificatePdfAction(
309  $pdfGenerator,
311  $this->language->txt('error_creating_certificate_pdf')
312  );
313 
314  $pdfAction->downloadPdf($userCertificate->getUserId(), $userCertificate->getObjId());
315 
316  $this->listCertificates();
317  }
318 }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static get(string $a_var)
static array static setUseRelativeDates(bool $a_status)
set use relative dates
ilUserCertificateGUI: ilAchievementsGUI
readonly ilAccessHandler $access
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getAllReferences(int $id)
get all reference ids for object ID
Help GUI class.
readonly ilLogger $certificateLogger
$refId
Definition: xapitoken.php:58
const IL_CAL_UNIX
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$provider
Definition: ltitoken.php:80
readonly ilCtrlInterface $ctrl
readonly ilDBInterface $db
__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, ?Filesystem $filesystem=null, ?ilHelpGUI $help=null, ?ilDBInterface $db=null, private ?IRSS $irss=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This is how the factory for UI elements looks.
Definition: Factory.php:37
global $DIC
Definition: shib_login.php:22
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
readonly ServerRequestInterface $request
readonly ilGlobalTemplateInterface $template
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
static signFile(string $path_to_file)
language()
description: > Example for rendring a language glyph.
Definition: language.php:41
static set(string $a_var, $a_val)
Set a value.
readonly ilUserCertificateRepository $userCertificateRepository
downloadPdf(int $userId, int $objectId)