ILIAS  release_8 Revision v8.24
class.ilTermsOfServiceDocumentGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24use ILIAS\Refinery\Factory as Refinery;
27
33{
38 protected ilLanguage $lng;
41 protected ilObjUser $user;
42 protected ilLogger $log;
51 protected Refinery $refinery;
52
53 public function __construct(
71 Refinery $refinery
72 ) {
73 $this->tos = $tos;
74 $this->criterionTypeFactory = $criterionTypeFactory;
75 $this->tpl = $tpl;
76 $this->ctrl = $ctrl;
77 $this->lng = $lng;
78 $this->rbacsystem = $rbacsystem;
79 $this->error = $error;
80 $this->user = $user;
81 $this->log = $log;
82 $this->toolbar = $toolbar;
83 $this->httpState = $httpState;
84 $this->uiFactory = $uiFactory;
85 $this->uiRenderer = $uiRenderer;
86 $this->fileSystems = $fileSystems;
87 $this->fileUpload = $fileUpload;
88 $this->tableDataProviderFactory = $tableDataProviderFactory;
89 $this->documentPurifier = $documentPurifier;
90 $this->refinery = $refinery;
91 }
92
93 public function executeCommand(): void
94 {
95 $cmd = $this->ctrl->getCmd();
96
97 if (!$this->rbacsystem->checkAccess('read', $this->tos->getRefId())) {
98 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
99 }
100
101 if ($cmd === null || $cmd === '' || !method_exists($this, $cmd)) {
102 $cmd = 'showDocuments';
103 }
104 $this->$cmd();
105 }
106
107 protected function confirmReset(): void
108 {
109 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
110 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
111 }
112
113 $confirmation = new ilConfirmationGUI();
114 $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmReset'));
115 $confirmation->setConfirm($this->lng->txt('confirm'), 'reset');
116 $confirmation->setCancel($this->lng->txt('cancel'), 'showDocuments');
117 $confirmation->setHeaderText($this->lng->txt('tos_sure_reset_tos'));
118
119 $this->tpl->setContent($confirmation->getHTML());
120 }
121
122 protected function reset(): void
123 {
124 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
125 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
126 }
127
128 $this->tos->resetAll();
129
130 $this->log->info('Terms of service reset by ' . $this->user->getId() . ' [' . $this->user->getLogin() . ']');
131 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tos_reset_successful'));
132
133 $this->showDocuments();
134 }
135
136 protected function showDocuments(): void
137 {
138 if ($this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
139 $addDocumentBtn = ilLinkButton::getInstance();
140 $addDocumentBtn->setPrimary(true);
141 $addDocumentBtn->setUrl($this->ctrl->getLinkTarget($this, 'showAddDocumentForm'));
142 $addDocumentBtn->setCaption('tos_add_document_btn_label');
143 $this->toolbar->addStickyItem($addDocumentBtn);
144 }
145
146 $documentTableGui = new ilTermsOfServiceDocumentTableGUI(
147 $this,
148 'showDocuments',
149 $this->criterionTypeFactory,
150 $this->uiFactory,
151 $this->uiRenderer,
152 $this->rbacsystem->checkAccess('write', $this->tos->getRefId())
153 );
154 $documentTableGui->setProvider($this->tableDataProviderFactory->getByContext(ilTermsOfServiceTableDataProviderFactory::CONTEXT_DOCUMENTS));
155 $documentTableGui->populate();
156
157 $this->tpl->setCurrentBlock('mess');
158 $this->tpl->setVariable('MESSAGE', $this->getResetMessageBoxHtml());
159 $this->tpl->parseCurrentBlock('mess');
160 $this->tpl->setContent($documentTableGui->getHTML());
161 }
162
163 protected function getResetMessageBoxHtml(): string
164 {
165 if (((int) $this->tos->getLastResetDate()->get(IL_CAL_UNIX)) !== 0) {
168 $resetText = sprintf(
169 $this->lng->txt('tos_last_reset_date'),
170 ilDatePresentation::formatDate($this->tos->getLastResetDate())
171 );
173 } else {
174 $resetText = $this->lng->txt('tos_never_reset');
175 }
176
177 $buttons = [];
178 if ($this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
179 $buttons = [
180 $this->uiFactory
181 ->button()
182 ->standard(
183 $this->lng->txt('tos_reset_tos_for_all_users'),
184 $this->ctrl->getLinkTarget($this, 'confirmReset')
185 )
186 ];
187 }
188
189 return $this->uiRenderer->render(
190 $this->uiFactory->messageBox()
191 ->info($resetText)
192 ->withButtons($buttons)
193 );
194 }
195
197 {
198 if ($document->getId() > 0) {
199 $this->ctrl->setParameter($this, 'tos_id', $document->getId());
200 }
201
202 $formAction = $this->ctrl->getFormAction($this, 'saveAddDocumentForm');
203 $saveCommand = 'saveAddDocumentForm';
204
205 if ($document->getId() > 0) {
206 $formAction = $this->ctrl->getFormAction($this, 'saveEditDocumentForm');
207 $saveCommand = 'saveEditDocumentForm';
208 }
209
211 $document,
212 $this->documentPurifier,
213 $this->user,
214 $this->fileSystems->temp(),
215 $this->fileUpload,
216 $formAction,
217 $saveCommand,
218 'showDocuments',
219 $this->rbacsystem->checkAccess('write', $this->tos->getRefId())
220 );
221
222 return $form;
223 }
224
225 protected function saveAddDocumentForm(): void
226 {
227 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
228 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
229 }
230
231 $form = $this->getDocumentForm(new ilTermsOfServiceDocument());
232 if ($form->saveObject()) {
233 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
234 if ($form->hasTranslatedInfo()) {
235 $this->tpl->setOnScreenMessage('info', $form->getTranslatedInfo(), true);
236 }
237 $this->ctrl->redirect($this, 'showDocuments');
238 } elseif ($form->hasTranslatedError()) {
239 $this->tpl->setOnScreenMessage('failure', $form->getTranslatedError());
240 }
241
242 $this->tpl->setContent($form->getHTML());
243 }
244
245 protected function showAddDocumentForm(): void
246 {
247 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
248 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
249 }
250
251 $form = $this->getDocumentForm(new ilTermsOfServiceDocument());
252 $this->tpl->setContent($form->getHTML());
253 }
254
255 protected function showEditDocumentForm(): void
256 {
257 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
258 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
259 }
260
261 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
262
263 $form = $this->getDocumentForm($document);
264 $this->tpl->setContent($form->getHTML());
265 }
266
267 protected function saveEditDocumentForm(): void
268 {
269 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
270 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
271 }
272
273 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
274
275 $form = $this->getDocumentForm($document);
276 if ($form->saveObject()) {
277 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
278 if ($form->hasTranslatedInfo()) {
279 $this->tpl->setOnScreenMessage('info', $form->getTranslatedInfo(), true);
280 }
281 $this->ctrl->redirect($this, 'showDocuments');
282 } elseif ($form->hasTranslatedError()) {
283 $this->tpl->setOnScreenMessage('failure', $form->getTranslatedError());
284 }
285
286 $this->tpl->setContent($form->getHTML());
287 }
288
292 protected function getDocumentsByServerRequest(): array
293 {
294 $documentIds = [];
295
296 if ($this->httpState->wrapper()->post()->has('tos_id')) {
297 $documentIds = $this->httpState->wrapper()->post()->retrieve(
298 'tos_id',
299 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
300 );
301 }
302
303 if ($documentIds === [] && $this->httpState->wrapper()->query()->has('tos_id')) {
304 $documentIds = [$this->httpState->wrapper()->query()->retrieve(
305 'tos_id',
306 $this->refinery->kindlyTo()->int()
307 )];
308 }
309
310 if ($documentIds === []) {
311 return $documentIds;
312 }
313
315 ['id' => array_filter(array_map('intval', $documentIds))],
316 ['id' => 'IN']
317 )->getArray();
318
319 return $documents;
320 }
321
322 protected function getFirstDocumentFromList(array $documents): ilTermsOfServiceDocument
323 {
324 if (1 !== count($documents)) {
325 throw new UnexpectedValueException('Expected exactly one document in list');
326 }
327
328 $document = new ilTermsOfServiceDocument(0);
329 $document = $document->buildFromArray(current($documents));
330
331 return $document;
332 }
333
334 protected function deleteDocuments(): void
335 {
336 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
337 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
338 }
339
340 $documents = $this->getDocumentsByServerRequest();
341 if (0 === count($documents)) {
342 $this->showDocuments();
343 return;
344 }
345
346 $documents = array_map(static function (array $data): ilTermsOfServiceDocument {
347 $document = new ilTermsOfServiceDocument(0);
348 $document = $document->buildFromArray($data);
349
350 return $document;
351 }, $documents);
352
353 $isDeletionRequest = (bool) ($this->httpState->request()->getQueryParams()['delete'] ?? false);
354 if ($isDeletionRequest) {
355 $this->processDocumentDeletion($documents);
356
357 $this->ctrl->redirect($this);
358 } else {
359 $this->ctrl->setParameter($this, 'delete', 1);
360 $confirmation = new ilConfirmationGUI();
361 $confirmation->setFormAction($this->ctrl->getFormAction($this, 'deleteDocuments'));
362 $confirmation->setConfirm($this->lng->txt('confirm'), 'deleteDocuments');
363 $confirmation->setCancel($this->lng->txt('cancel'), 'showDocuments');
364
365 $confirmation->setHeaderText($this->lng->txt('tos_sure_delete_documents_p'));
366 if (1 === count($documents)) {
367 $confirmation->setHeaderText($this->lng->txt('tos_sure_delete_documents_s'));
368 }
369
370 foreach ($documents as $document) {
372 $confirmation->addItem('tos_id[]', (string) $document->getId(), implode(' | ', [
373 $document->getTitle()
374 ]));
375 }
376
377 $this->tpl->setContent($confirmation->getHTML());
378 }
379 }
380
384 protected function processDocumentDeletion(array $documents): void
385 {
386 foreach ($documents as $document) {
388 $document->delete();
389 }
390
391 if (0 === ilTermsOfServiceDocument::getCollection()->count()) {
392 $this->tos->saveStatus(false);
393 $this->tpl->setOnScreenMessage('info', $this->lng->txt('tos_disabled_no_docs_left'), true);
394 }
395
396 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tos_deleted_documents_p'), true);
397 if (1 === count($documents)) {
398 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tos_deleted_documents_s'), true);
399 }
400 }
401
402 protected function deleteDocument(): void
403 {
404 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
405 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
406 }
407
408 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
409
410 $this->processDocumentDeletion([$document]);
411
412 $this->ctrl->redirect($this);
413 }
414
415 protected function saveDocumentSorting(): void
416 {
417 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
418 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
419 }
420
421 $sorting = $this->httpState->request()->getParsedBody()['sorting'] ?? [];
422 if (!is_array($sorting) || 0 === count($sorting)) {
423 $this->showDocuments();
424 return;
425 }
426
427 asort($sorting, SORT_NUMERIC);
428
429 $position = 0;
430 foreach ($sorting as $documentId => $ignoredSortValue) {
431 if (!is_numeric($documentId)) {
432 continue;
433 }
434
435 try {
436 $document = new ilTermsOfServiceDocument((int) $documentId);
437 $document->setSorting(++$position);
438 $document->store();
439 } catch (ilException $e) {
440 // Empty catch block
441 }
442 }
443
444 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tos_saved_sorting'), true);
445 $this->ctrl->redirect($this);
446 }
447
448 protected function getCriterionForm(
449 ilTermsOfServiceDocument $document,
452 $this->ctrl->setParameter($this, 'tos_id', $document->getId());
453
454 if ($criterionAssignment->getId() > 0) {
455 $this->ctrl->setParameter($this, 'crit_id', $criterionAssignment->getId());
456 }
457
458 $formAction = $this->ctrl->getFormAction($this, 'saveAttachCriterionForm');
459 $saveCommand = 'saveAttachCriterionForm';
460
461 if ($criterionAssignment->getId() > 0) {
462 $formAction = $this->ctrl->getFormAction($this, 'saveChangeCriterionForm');
463 $saveCommand = 'saveChangeCriterionForm';
464 }
465
467 $document,
468 $criterionAssignment,
469 $this->criterionTypeFactory,
470 $this->user,
471 $formAction,
472 $saveCommand,
473 'showDocuments'
474 );
475
476 return $form;
477 }
478
479 protected function saveAttachCriterionForm(): void
480 {
481 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
482 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
483 }
484
485 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
486
487 $form = $this->getCriterionForm($document, new ilTermsOfServiceDocumentCriterionAssignment());
488 if ($form->saveObject()) {
489 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tos_doc_crit_attached'), true);
490 $this->ctrl->redirect($this, 'showDocuments');
491 } elseif ($form->hasTranslatedError()) {
492 $this->tpl->setOnScreenMessage('failure', $form->getTranslatedError());
493 }
494
495 $this->tpl->setContent($form->getHTML());
496 }
497
498 protected function showAttachCriterionForm(): void
499 {
500 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
501 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
502 }
503
504 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
505
506 $form = $this->getCriterionForm($document, new ilTermsOfServiceDocumentCriterionAssignment());
507 $this->tpl->setContent($form->getHTML());
508 }
509
510 protected function showChangeCriterionForm(): void
511 {
512 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
513 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
514 }
515
516 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
517
518 $criterionId = $this->httpState->request()->getQueryParams()['crit_id'] ?? 0;
519 if (!is_numeric($criterionId) || $criterionId < 1) {
520 $this->showDocuments();
521 return;
522 }
523
524 $criterionAssignment = array_values(array_filter(
525 $document->criteria(),
526 static function (ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment) use (
527 $criterionId
528 ): bool {
529 return $criterionAssignment->getId() == $criterionId;
530 }
531 ))[0];
532
533 $form = $this->getCriterionForm($document, $criterionAssignment);
534 $this->tpl->setContent($form->getHTML());
535 }
536
537 protected function saveChangeCriterionForm(): void
538 {
539 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
540 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
541 }
542
543 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
544
545 $criterionId = $this->httpState->request()->getQueryParams()['crit_id'] ?? 0;
546 if (!is_numeric($criterionId) || $criterionId < 1) {
547 $this->showDocuments();
548 return;
549 }
550
551 $criterionAssignment = array_values(array_filter(
552 $document->criteria(),
553 static function (ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment) use (
554 $criterionId
555 ): bool {
556 return $criterionAssignment->getId() == $criterionId;
557 }
558 ))[0];
559
560 $form = $this->getCriterionForm($document, $criterionAssignment);
561 if ($form->saveObject()) {
562 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tos_doc_crit_changed'), true);
563 $this->ctrl->redirect($this, 'showDocuments');
564 } elseif ($form->hasTranslatedError()) {
565 $this->tpl->setOnScreenMessage('failure', $form->getTranslatedError());
566 }
567
568 $this->tpl->setContent($form->getHTML());
569 }
570
571 public function detachCriterionAssignment(): void
572 {
573 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
574 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
575 }
576
577 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
578
579 $criterionId = $this->httpState->request()->getQueryParams()['crit_id'] ?? 0;
580 if (!is_numeric($criterionId) || $criterionId < 1) {
581 $this->showDocuments();
582 return;
583 }
584
585 $criterionAssignment = array_values(array_filter(
586 $document->criteria(),
587 static function (ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment) use (
588 $criterionId
589 ): bool {
590 return $criterionAssignment->getId() == $criterionId;
591 }
592 ))[0];
593
594 $document->detachCriterion($criterionAssignment);
595 $document->update();
596
597 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tos_doc_crit_detached'), true);
598 $this->ctrl->redirect($this, 'showDocuments');
599 }
600}
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
static where($where, $operator=null)
Builds data types.
Definition: Factory.php:21
const IL_CAL_UNIX
error(string $a_errmsg)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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)
Error Handling & global info handling uses PEAR error class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
Component logger with individual log levels by component id.
Class ilObjTermsOfService.
User class.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
Class ilTermsOfServiceDocumentFormGUI.
Class ilTermsOfServiceDocumentGUI.
ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory
getCriterionForm(ilTermsOfServiceDocument $document, ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
ilTermsOfServiceTableDataProviderFactory $tableDataProviderFactory
getDocumentForm(ilTermsOfServiceDocument $document)
__construct(ilObjTermsOfService $tos, ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory, ilGlobalTemplateInterface $tpl, ilObjUser $user, ilCtrlInterface $ctrl, ilLanguage $lng, ilRbacSystem $rbacsystem, ilErrorHandling $error, ilLogger $log, ilToolbarGUI $toolbar, GlobalHttpState $httpState, Factory $uiFactory, Renderer $uiRenderer, Filesystems $fileSystems, FileUpload $fileUpload, ilTermsOfServiceTableDataProviderFactory $tableDataProviderFactory, ilHtmlPurifierInterface $documentPurifier, Refinery $refinery)
executeCommand()
The implemented class should be ilCtrlInterface enabled and execute or forward the given command.
Class ilTermsOfServiceDocument.
detachCriterion(ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface GlobalHttpState.
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface for html sanitizing functionality.
Interface ilTermsOfServiceControllerEnabled.