ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCertificateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
32 
41 {
43  protected ilTree $tree;
44  protected ILIAS $ilias;
46  protected ilLanguage $lng;
47  protected int $ref_id;
50  private readonly WrapperFactory $httpWrapper;
51  private readonly Factory $refinery;
52  private IRSS $irss;
57  private readonly ilXlsFoParser $xlsFoParser;
61  private readonly FileUpload $fileUpload;
62  private readonly Filesystem $file_system;
63  private readonly string $certificatePath;
64  private readonly ilPageFormats $pageFormats;
65  private readonly ilLogger $logger;
67  private readonly ilDBInterface $database;
68 
69  public function __construct(
70  private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject,
71  ilCertificatePlaceholderValues $placeholderValuesObject,
72  private readonly int $objectId,
73  string $certificatePath,
74  ?ilCertificateFormRepository $settingsFormFactory = null,
75  ?ilCertificateDeleteAction $deleteAction = null,
76  ?ilCertificateTemplateRepository $templateRepository = null,
77  ?ilPageFormats $pageFormats = null,
78  ?ilXlsFoParser $xlsFoParser = null,
79  ?ilCertificateTemplateExportAction $exportAction = null,
80  ?ilCertificateTemplatePreviewAction $previewAction = null,
81  ?FileUpload $fileUpload = null,
82  private readonly ilSetting $settings = new ilSetting('certificate'),
83  ?Filesystem $file_system = null,
84  ?Filesystem $tmp_file_system = null
85  ) {
86  global $DIC;
87 
88  $this->httpWrapper = $DIC->http()->wrapper();
89  $this->refinery = $DIC->refinery();
90  $this->lng = $DIC['lng'];
91  $this->tpl = $DIC['tpl'];
92  $this->ctrl = $DIC['ilCtrl'];
93  $this->ilias = $DIC['ilias'];
94 
95  $this->irss = $DIC->resourceStorage();
96  $this->stakeholder = new ilCertificateTemplateStakeholder($this->objectId);
97 
98  $this->tree = $DIC['tree'];
99  $this->access = $DIC['ilAccess'];
100  $this->toolbar = $DIC['ilToolbar'];
101 
102  $this->global_certificate_settings = new ilObjCertificateSettings();
103  $this->lng->loadLanguageModule('certificate');
104  $this->lng->loadLanguageModule('cert');
105  $this->lng->loadLanguageModule('trac');
106 
107  $this->ref_id = (int) $DIC->http()->wrapper()->query()->retrieve('ref_id', $DIC->refinery()->kindlyTo()->int());
108 
109  $this->logger = $DIC->logger()->cert();
110 
111  $this->settingsFormFactory = $settingsFormFactory ?? new ilCertificateSettingsFormRepository(
112  $this->objectId,
113  $certificatePath,
114  $this->lng,
115  $this->tpl,
116  $this->ctrl,
117  $this->access,
118  $this->toolbar,
119  $placeholderDescriptionObject,
120  $DIC->ui()->factory(),
121  $DIC->ui()->renderer()
122  );
123  $this->templateRepository = $templateRepository ?? new ilCertificateTemplateDatabaseRepository(
124  $DIC->database(),
126  );
127  $this->deleteAction = $deleteAction ?? new ilCertificateTemplateDeleteAction($this->templateRepository);
128  $this->pageFormats = $pageFormats ?? new ilPageFormats($DIC->language());
129  $this->xlsFoParser = $xlsFoParser ?? new ilXlsFoParser($DIC->settings(), $this->pageFormats);
130  $this->exportAction = $exportAction ?? new ilCertificateTemplateExportAction(
131  $this->objectId,
132  $certificatePath,
133  $this->templateRepository,
134  $this->irss
135  );
136  $this->previewAction = $previewAction ?? new ilCertificateTemplatePreviewAction(
137  $this->templateRepository,
138  $placeholderValuesObject,
139  $this->irss
140  );
141  $this->fileUpload = $fileUpload ?? $DIC->upload();
142  $this->file_system = $file_system ?? $DIC->filesystem()->web();
143  $this->database = $DIC->database();
144  }
145 
156  public function executeCommand()
157  {
158  $cmd = $this->ctrl->getCmd();
159  $next_class = $this->ctrl->getNextClass($this);
160 
161  $ret = null;
162 
163  $cmd = $this->getCommand($cmd);
164  switch ($next_class) {
165  case 'ilpropertyformgui':
166  $form = $this->getEditorForm();
167  $this->ctrl->forwardCommand($form);
168 
169  break;
170  default:
171  $ret = $this->$cmd();
172 
173  break;
174  }
175 
176  return $ret;
177  }
178 
179  public function getCommand($cmd)
180  {
181  return $cmd;
182  }
183 
184  public function certificateImport(): void
185  {
186  $this->certificateEditor();
187  }
188 
189  public function certificatePreview(): void
190  {
191  try {
192  $this->previewAction->createPreviewPdf($this->objectId);
193  } catch (Exception) {
194  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'));
195  $this->certificateEditor();
196  }
197  }
198 
205  public function certificateExportFO(): void
206  {
207  $this->exportAction->export();
208  }
209 
210  public function certificateDelete(): void
211  {
212  // display confirmation message
213  $cgui = new ilConfirmationGUI();
214  $cgui->setFormAction($this->ctrl->getFormAction($this, 'certificateEditor'));
215  $cgui->setHeaderText($this->lng->txt('certificate_confirm_deletion_text'));
216  $cgui->setCancel($this->lng->txt('no'), 'certificateEditor');
217  $cgui->setConfirm($this->lng->txt('yes'), 'certificateDeleteConfirm');
218 
219  $this->tpl->setContent($cgui->getHTML());
220  }
221 
225  public function certificateDeleteConfirm(): void
226  {
227  $template = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
228  $templateId = $template->getId();
229 
230  $this->deleteAction->delete($templateId, $this->objectId);
231  $this->ctrl->redirect($this, 'certificateEditor');
232  }
233 
243  public function certificateSave(): void
244  {
245  global $DIC;
246 
247  $form = $this->settingsFormFactory->createForm(
248  $this
249  );
250 
251  $form->setValuesByPost();
252 
253  $request = $DIC->http()->request();
254 
255  $formFields = $request->getParsedBody();
256 
257  $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
258 
259  $this->saveCertificate($form, $formFields, $this->objectId);
260  }
261 
271  public function certificateUpload(): void
272  {
273  $this->certificateEditor();
274  }
275 
286  public function certificateEditor(): void
287  {
288  $form = $this->getEditorForm();
289  $enabledGlobalLearningProgress = ilObjUserTracking::_enabledLearningProgress();
290 
291  $messageBoxHtml = '';
292  if ($enabledGlobalLearningProgress) {
293  $objectLearningProgressSettings = new ilLPObjSettings($this->objectId);
294  $mode = $objectLearningProgressSettings->getMode();
295 
297  $object = ilObjectFactory::getInstanceByObjId($this->objectId);
298  if (ilLPObjSettings::LP_MODE_DEACTIVATED === $mode && $object->getType() !== 'crs') {
299  global $DIC;
300 
301  $renderer = $DIC->ui()->renderer();
302  $messageBox = $DIC->ui()
303  ->factory()
304  ->messageBox()
305  ->info($this->lng->txt('learning_progress_deactivated'))
306  ;
307 
308  $messageBoxHtml = $renderer->render($messageBox);
309  $form->clearCommandButtons();
310  }
311  }
312 
313  $formHtml = $form->getHTML();
314 
315  $this->tpl->setVariable('ADM_CONTENT', $messageBoxHtml . $formHtml);
316  }
317 
326  private function getEditorForm(): ilPropertyFormGUI
327  {
328  $certificateTemplate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
329 
330  $form = $this->settingsFormFactory->createForm(
331  $this
332  );
333 
334  $formFields = $this->createFormatArray($certificateTemplate);
335 
336  $formFields['active'] = $certificateTemplate->isCurrentlyActive();
337 
338  $form->setValuesByArray($formFields);
339 
340  return $form;
341  }
342 
343  private function saveCertificate(ilPropertyFormGUI $form, array $form_fields, int $objId): void
344  {
345  $certificate_handler = new CertificateResourceHandler(
346  new ilUserCertificateRepository($this->database),
347  new ilCertificateTemplateDatabaseRepository($this->database),
348  $this->irss,
349  $this->global_certificate_settings,
350  $this->stakeholder,
351  );
352  $current_template = $this->templateRepository->fetchPreviousCertificate($objId);
353  $currentVersion = $current_template->getVersion();
354  $nextVersion = $currentVersion + 1;
355  $current_background_rid = $this->irss->manageContainer()->find(
356  $current_template->getBackgroundImageIdentification()
357  );
358  $current_tile_image_rid = $this->irss->manageContainer()->find(
359  $current_template->getTileImageIdentification()
360  );
361  $old_background_image = $current_background_rid === null
362  ? $current_template->getBackgroundImagePath() :
363  '';
364  $old_tile_image = $current_tile_image_rid === null
365  ? $current_template->getTileImagePath() :
366  '';
367 
368  $should_delete_background =
369  $this->httpWrapper->post()->retrieve(
370  'background_delete',
371  $this->refinery->byTrying([
372  $this->refinery->kindlyTo()->bool(),
373  $this->refinery->always(false)
374  ])
375  );
376  $should_delete_tile_image =
377  $this->httpWrapper->post()->retrieve(
378  'certificate_card_tile_image_delete',
379  $this->refinery->byTrying([
380  $this->refinery->kindlyTo()->bool(),
381  $this->refinery->always(false)
382  ])
383  );
384 
385  $new_background_rid = $current_background_rid && !$should_delete_background ? $current_background_rid :
386  $this->global_certificate_settings->getBackgroundImageIdentification();
387  if (
388  is_string($new_background_rid) &&
389  is_string($this->global_certificate_settings->getBackgroundImageIdentification()) &&
390  $new_background_rid === $this->global_certificate_settings->getBackgroundImageIdentification()
391  ) {
392  if ($this->file_system->has($new_background_rid)) {
393  $new_background_rid = $this->irss->manage()->stream(
394  $this->file_system->readStream($new_background_rid),
396  );
397  } else {
398  $old_background_image = $new_background_rid;
399  $new_background_rid = null;
400  }
401  }
402 
403  $new_tile_rid = !$should_delete_tile_image ? $current_tile_image_rid : null;
404  if ($form->checkInput()) {
405  try {
406  $this->settingsFormFactory->save($form_fields);
407 
408  $templateValues = $this->placeholderDescriptionObject->getPlaceholderDescriptions();
409 
410  if ($this->fileUpload->hasUploads()) {
411  if (!$this->fileUpload->hasBeenProcessed()) {
412  $this->fileUpload->process();
413  }
414  $new_background = $form->getInput('background')['tmp_name'] ?? '';
415  $new_tile_image = $form->getInput('certificate_card_tile_image')['tmp_name'] ?? '';
416  $results = $this->fileUpload->getResults();
417 
418  if ($new_background !== '') {
419  $new_background_rid = $this->irss->manage()->upload(
420  $results[$new_background],
421  $this->stakeholder
422  );
423  }
424 
425  if ($new_tile_image !== '') {
426  $new_tile_rid = $this->irss->manage()->upload(
427  $results[$new_tile_image],
428  $this->stakeholder
429  );
430  }
431  }
432 
433  $jsonEncodedTemplateValues = json_encode($templateValues, JSON_THROW_ON_ERROR);
434 
435  if (isset($new_background_rid)) {
436  $old_background_image = '';
437  }
438  if (isset($new_tile_rid)) {
439  $old_tile_image = '';
440  }
441 
442  $xslfo = $this->xlsFoParser->parse($form_fields);
443  $newHashValue = hash(
444  'sha256',
445  implode('', [
446  $xslfo,
447  isset($new_background_rid) ? $new_background_rid->serialize() : '',
448  $jsonEncodedTemplateValues,
449  isset($new_tile_rid) ? $new_background_rid->serialize() : '',
450  $old_background_image, $old_tile_image
451  ])
452  );
453 
454  $active = (bool) ($form_fields['active'] ?? false);
455 
456  if ($newHashValue !== $current_template->getCertificateHash()) {
457  $certificateTemplate = new ilCertificateTemplate(
458  $objId,
459  ilObject::_lookupType($objId),
460  $xslfo,
461  $newHashValue,
462  $jsonEncodedTemplateValues,
463  $nextVersion,
465  time(),
466  $active,
467  $old_background_image,
468  $old_tile_image,
469  isset($new_background_rid) ? $new_background_rid->serialize() : '',
470  isset($new_tile_rid) ? $new_tile_rid->serialize() : '',
471  );
472  $this->templateRepository->save($certificateTemplate);
473 
474  if ($current_background_rid instanceof ResourceIdentification) {
475  $certificate_handler->handleResourceChange($current_background_rid);
476  }
477  if ($current_tile_image_rid instanceof ResourceIdentification) {
478  $certificate_handler->handleResourceChange($current_tile_image_rid);
479  }
480 
481  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
482  $this->ctrl->redirect($this, 'certificateEditor');
483  }
484 
485  if (
486  $current_template->getId() !== null &&
487  $current_template->isCurrentlyActive() !== $active
488  ) {
489  $this->templateRepository->updateActivity($current_template, $active);
490  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_change_active_status'), true);
491  $this->ctrl->redirect($this, 'certificateEditor');
492  }
493 
494  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_same_not_saved'), true);
495  $this->ctrl->redirect($this, 'certificateEditor');
496  } catch (Exception $e) {
497  $this->tpl->setOnScreenMessage(
498  'failure',
499  $e->getMessage()
500  );
501  $this->logger->error($e->getTraceAsString());
502  }
503  }
504 
505  $form->setValuesByPost();
506 
507  $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
508  }
509 
510  private function createFormatArray(ilCertificateTemplate $certificateTemplate): array
511  {
512  if ('' === $certificateTemplate->getCertificateHash()) {
513  $format = $this->settings->get('pageformat', '');
514  $formats = $this->pageFormats->fetchPageFormats();
515 
516  return [
517  'pageformat' => $format,
518  'pagewidth' => $formats['width'] ?? '',
519  'pageheight' => $formats['height'] ?? '',
520  'margin_body_top' => ilPageFormats::DEFAULT_MARGIN_BODY_TOP,
521  'margin_body_right' => ilPageFormats::DEFAULT_MARGIN_BODY_RIGHT,
522  'margin_body_bottom' => ilPageFormats::DEFAULT_MARGIN_BODY_BOTTOM,
523  'margin_body_left' => ilPageFormats::DEFAULT_MARGIN_BODY_LEFT,
524  'certificate_text' => $certificateTemplate->getCertificateContent()
525  ];
526  }
527 
528  return $this->settingsFormFactory->fetchFormFieldData($certificateTemplate->getCertificateContent());
529  }
530 }
certificateSave()
Saves the certificate.
final const DEFAULT_MARGIN_BODY_TOP
readonly ilPageFormats $pageFormats
final const DEFAULT_MARGIN_BODY_RIGHT
readonly ilDBInterface $database
$renderer
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilCertificateTemplateStakeholder $stakeholder
Interface Observer Contains several chained tasks and infos about them.
$objId
Definition: xapitoken.php:57
createFormatArray(ilCertificateTemplate $certificateTemplate)
readonly Filesystem $file_system
readonly WrapperFactory $httpWrapper
readonly ilCertificateFormRepository $settingsFormFactory
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
readonly ilCertificateTemplateExportAction $exportAction
certificateDeleteConfirm()
Deletes the certificate and all its data.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
const ILIAS_VERSION_NUMERIC
readonly FileUpload $fileUpload
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
certificateExportFO()
Exports the certificate.
global $DIC
Definition: shib_login.php:22
Class ilObjForumAdministration.
$results
readonly string $certificatePath
Class FileUpload.
Definition: FileUpload.php:37
readonly ilCertificateTemplatePreviewAction $previewAction
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
readonly ilLogger $logger
saveCertificate(ilPropertyFormGUI $form, array $form_fields, int $objId)
ilCertificateTemplateDatabaseRepository $certificate_repo
readonly ilCertificateTemplateRepository $templateRepository
readonly ilXlsFoParser $xlsFoParser
final const DEFAULT_MARGIN_BODY_LEFT
__construct(private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject, ilCertificatePlaceholderValues $placeholderValuesObject, private readonly int $objectId, string $certificatePath, ?ilCertificateFormRepository $settingsFormFactory=null, ?ilCertificateDeleteAction $deleteAction=null, ?ilCertificateTemplateRepository $templateRepository=null, ?ilPageFormats $pageFormats=null, ?ilXlsFoParser $xlsFoParser=null, ?ilCertificateTemplateExportAction $exportAction=null, ?ilCertificateTemplatePreviewAction $previewAction=null, ?FileUpload $fileUpload=null, private readonly ilSetting $settings=new ilSetting('certificate'), ?Filesystem $file_system=null, ?Filesystem $tmp_file_system=null)
readonly ilCertificateDeleteAction $deleteAction
static _lookupType(int $id, bool $reference=false)
readonly ilObjCertificateSettings $global_certificate_settings
final const DEFAULT_MARGIN_BODY_BOTTOM
readonly Factory $refinery
ilGlobalPageTemplate $tpl