ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilTermsOfServiceDocumentGUI.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9
15{
18
20 protected $tos;
21
23 protected $tpl;
24
26 protected $ctrl;
27
29 protected $lng;
30
32 protected $rbacsystem;
33
35 protected $error;
36
38 protected $user;
39
41 protected $log;
42
44 protected $uiFactory;
45
47 protected $uiRenderer;
48
50 protected $httpState;
51
53 protected $toolbar;
54
56 protected $fileUpload;
57
59 protected $fileSystems;
60
63
66
87 public function __construct(
105 ) {
106 $this->tos = $tos;
107 $this->criterionTypeFactory = $criterionTypeFactory;
108 $this->tpl = $tpl;
109 $this->ctrl = $ctrl;
110 $this->lng = $lng;
111 $this->rbacsystem = $rbacsystem;
112 $this->error = $error;
113 $this->user = $user;
114 $this->log = $log;
115 $this->toolbar = $toolbar;
116 $this->httpState = $httpState;
117 $this->uiFactory = $uiFactory;
118 $this->uiRenderer = $uiRenderer;
119 $this->fileSystems = $fileSystems;
120 $this->fileUpload = $fileUpload;
121 $this->tableDataProviderFactory = $tableDataProviderFactory;
122 $this->documentPurifier = $documentPurifier;
123 }
124
128 public function executeCommand() : void
129 {
130 $cmd = $this->ctrl->getCmd();
131
132 if (!$this->rbacsystem->checkAccess('read', $this->tos->getRefId())) {
133 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
134 }
135
136 if ($cmd == '' || !method_exists($this, $cmd)) {
137 $cmd = 'showDocuments';
138 }
139 $this->$cmd();
140 }
141
145 protected function confirmReset() : void
146 {
147 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
148 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
149 }
150
151 $confirmation = new ilConfirmationGUI();
152 $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmReset'));
153 $confirmation->setConfirm($this->lng->txt('confirm'), 'reset');
154 $confirmation->setCancel($this->lng->txt('cancel'), 'showDocuments');
155 $confirmation->setHeaderText($this->lng->txt('tos_sure_reset_tos'));
156
157 $this->tpl->setContent($confirmation->getHTML());
158 }
159
163 protected function reset() : void
164 {
165 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
166 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
167 }
168
169 $this->tos->resetAll();
170
171 $this->log->info('Terms of service reset by ' . $this->user->getId() . ' [' . $this->user->getLogin() . ']');
172 ilUtil::sendSuccess($this->lng->txt('tos_reset_successful'));
173
174 $this->showDocuments();
175 }
176
181 protected function showDocuments() : void
182 {
183 if ($this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
184 $addDocumentBtn = ilLinkButton::getInstance();
185 $addDocumentBtn->setPrimary(true);
186 $addDocumentBtn->setUrl($this->ctrl->getLinkTarget($this, 'showAddDocumentForm'));
187 $addDocumentBtn->setCaption('tos_add_document_btn_label');
188 $this->toolbar->addStickyItem($addDocumentBtn);
189 }
190
191 $documentTableGui = new ilTermsOfServiceDocumentTableGUI(
192 $this,
193 'showDocuments',
194 $this->criterionTypeFactory,
195 $this->uiFactory,
196 $this->uiRenderer,
197 $this->rbacsystem->checkAccess('write', $this->tos->getRefId())
198 );
199 $documentTableGui->setProvider($this->tableDataProviderFactory->getByContext(ilTermsOfServiceTableDataProviderFactory::CONTEXT_DOCUMENTS));
200 $documentTableGui->populate();
201
202 $this->tpl->setCurrentBlock('mess');
203 $this->tpl->setVariable('MESSAGE', $this->getResetMessageBoxHtml());
204 $this->tpl->parseCurrentBlock('mess');
205 $this->tpl->setContent($documentTableGui->getHTML());
206 }
207
212 protected function getResetMessageBoxHtml() : string
213 {
214 if ($this->tos->getLastResetDate() && $this->tos->getLastResetDate()->get(IL_CAL_UNIX) != 0) {
217 $resetText = sprintf(
218 $this->lng->txt('tos_last_reset_date'),
219 ilDatePresentation::formatDate($this->tos->getLastResetDate())
220 );
222 } else {
223 $resetText = $this->lng->txt('tos_never_reset');
224 }
225
226 $buttons = [];
227 if ($this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
228 $buttons = [
229 $this->uiFactory
230 ->button()
231 ->standard(
232 $this->lng->txt('tos_reset_tos_for_all_users'),
233 $this->ctrl->getLinkTarget($this, 'confirmReset')
234 )
235 ];
236 }
237
238 return $this->uiRenderer->render(
239 $this->uiFactory->messageBox()
240 ->info($resetText)
241 ->withButtons($buttons)
242 );
243 }
244
250 {
251 if ($document->getId() > 0) {
252 $this->ctrl->setParameter($this, 'tos_id', $document->getId());
253 }
254
255 $formAction = $this->ctrl->getFormAction($this, 'saveAddDocumentForm');
256 $saveCommand = 'saveAddDocumentForm';
257
258 if ($document->getId() > 0) {
259 $formAction = $this->ctrl->getFormAction($this, 'saveEditDocumentForm');
260 $saveCommand = 'saveEditDocumentForm';
261 }
262
264 $document,
265 $this->documentPurifier,
266 $this->user,
267 $this->fileSystems->temp(),
268 $this->fileUpload,
269 $formAction,
270 $saveCommand,
271 'showDocuments',
272 $this->rbacsystem->checkAccess('write', $this->tos->getRefId())
273 );
274
275 return $form;
276 }
277
281 protected function saveAddDocumentForm() : void
282 {
283 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
284 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
285 }
286
287 $form = $this->getDocumentForm(new ilTermsOfServiceDocument());
288 if ($form->saveObject()) {
289 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
290 if ($form->hasTranslatedInfo()) {
291 ilUtil::sendInfo($form->getTranslatedInfo(), true);
292 }
293 $this->ctrl->redirect($this, 'showDocuments');
294 } elseif ($form->hasTranslatedError()) {
295 ilUtil::sendFailure($form->getTranslatedError());
296 }
297
298 $this->tpl->setContent($form->getHTML());
299 }
300
304 protected function showAddDocumentForm() : void
305 {
306 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
307 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
308 }
309
310 $form = $this->getDocumentForm(new ilTermsOfServiceDocument());
311 $this->tpl->setContent($form->getHTML());
312 }
313
317 protected function showEditDocumentForm() : void
318 {
319 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
320 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
321 }
322
323 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
324
325 $form = $this->getDocumentForm($document);
326 $this->tpl->setContent($form->getHTML());
327 }
328
332 protected function saveEditDocumentForm() : void
333 {
334 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
335 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
336 }
337
338 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
339
340 $form = $this->getDocumentForm($document);
341 if ($form->saveObject()) {
342 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
343 if ($form->hasTranslatedInfo()) {
344 ilUtil::sendInfo($form->getTranslatedInfo(), true);
345 }
346 $this->ctrl->redirect($this, 'showDocuments');
347 } elseif ($form->hasTranslatedError()) {
348 ilUtil::sendFailure($form->getTranslatedError());
349 }
350
351 $this->tpl->setContent($form->getHTML());
352 }
353
357 protected function getDocumentsByServerRequest() : array
358 {
359 $documents = [];
360
361 $documentIds = $this->httpState->request()->getParsedBody()['tos_id'] ?? [];
362 if (!is_array($documentIds) || 0 === count($documentIds)) {
363 $documentIds = $this->httpState->request()->getQueryParams()['tos_id'] ? [$this->httpState->request()->getQueryParams()['tos_id']] : [];
364 }
365
366 if (0 === count($documentIds)) {
367 return $documents;
368 }
369
371 ['id' => array_filter(array_map('intval', $documentIds))],
372 ['id' => 'IN']
373 )->getArray();
374
375 return $documents;
376 }
377
383 protected function getFirstDocumentFromList(array $documents) : ilTermsOfServiceDocument
384 {
385 if (1 !== count($documents)) {
386 throw new UnexpectedValueException('Expected exactly one document in list');
387 }
388
389 $document = new ilTermsOfServiceDocument(0);
390 $document = $document->buildFromArray(current($documents));
391
392 return $document;
393 }
394
398 protected function deleteDocuments() : void
399 {
400 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
401 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
402 }
403
404 $documents = $this->getDocumentsByServerRequest();
405 if (0 === count($documents)) {
406 $this->showDocuments();
407 return;
408 } else {
409 $documents = array_map(function (array $data) {
410 $document = new ilTermsOfServiceDocument(0);
411 $document = $document->buildFromArray($data);
412
413 return $document;
414 }, $documents);
415 }
416
417 $isDeletionRequest = (bool) ($this->httpState->request()->getQueryParams()['delete'] ?? false);
418 if ($isDeletionRequest) {
419 $this->processDocumentDeletion($documents);
420
421 $this->ctrl->redirect($this);
422 } else {
423 $this->ctrl->setParameter($this, 'delete', 1);
424 $confirmation = new ilConfirmationGUI();
425 $confirmation->setFormAction($this->ctrl->getFormAction($this, 'deleteDocuments'));
426 $confirmation->setConfirm($this->lng->txt('confirm'), 'deleteDocuments');
427 $confirmation->setCancel($this->lng->txt('cancel'), 'showDocuments');
428
429 $confirmation->setHeaderText($this->lng->txt('tos_sure_delete_documents_p'));
430 if (1 === count($documents)) {
431 $confirmation->setHeaderText($this->lng->txt('tos_sure_delete_documents_s'));
432 }
433
434 foreach ($documents as $document) {
436 $confirmation->addItem('tos_id[]', $document->getId(), implode(' | ', [
437 $document->getTitle()
438 ]));
439 }
440
441 $this->tpl->setContent($confirmation->getHTML());
442 }
443 }
444
448 protected function processDocumentDeletion(array $documents) : void
449 {
450 foreach ($documents as $document) {
452 $document->delete();
453 }
454
455 if (0 === ilTermsOfServiceDocument::getCollection()->count()) {
456 $this->tos->saveStatus(false);
457 ilUtil::sendInfo($this->lng->txt('tos_disabled_no_docs_left'), true);
458 }
459
460 ilUtil::sendSuccess($this->lng->txt('tos_deleted_documents_p'), true);
461 if (1 === count($documents)) {
462 ilUtil::sendSuccess($this->lng->txt('tos_deleted_documents_s'), true);
463 }
464 }
465
469 protected function deleteDocument() : void
470 {
471 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
472 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
473 }
474
475 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
476
477 $this->processDocumentDeletion([$document]);
478
479 $this->ctrl->redirect($this);
480 }
481
485 protected function saveDocumentSorting() : void
486 {
487 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
488 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
489 }
490
491 $sorting = $this->httpState->request()->getParsedBody()['sorting'] ?? [];
492 if (!is_array($sorting) || 0 === count($sorting)) {
493 $this->showDocuments();
494 return;
495 }
496
497 asort($sorting, SORT_NUMERIC);
498
499 $position = 0;
500 foreach ($sorting as $documentId => $ignoredSortValue) {
501 if (!is_numeric($documentId)) {
502 continue;
503 }
504
505 try {
506 $document = new ilTermsOfServiceDocument((int) $documentId);
507 $document->setSorting(++$position);
508 $document->store();
509 } catch (ilException $e) {
510 // Empty catch block
511 }
512 }
513
514 ilUtil::sendSuccess($this->lng->txt('tos_saved_sorting'), true);
515 $this->ctrl->redirect($this);
516 }
517
523 protected function getCriterionForm(
524 ilTermsOfServiceDocument $document,
527 $this->ctrl->setParameter($this, 'tos_id', $document->getId());
528
529 if ($criterionAssignment->getId() > 0) {
530 $this->ctrl->setParameter($this, 'crit_id', $criterionAssignment->getId());
531 }
532
533 $formAction = $this->ctrl->getFormAction($this, 'saveAttachCriterionForm');
534 $saveCommand = 'saveAttachCriterionForm';
535
536 if ($criterionAssignment->getId() > 0) {
537 $formAction = $this->ctrl->getFormAction($this, 'saveChangeCriterionForm');
538 $saveCommand = 'saveChangeCriterionForm';
539 }
540
542 $document,
543 $criterionAssignment,
544 $this->criterionTypeFactory,
545 $this->user,
546 $formAction,
547 $saveCommand,
548 'showDocuments'
549 );
550
551 return $form;
552 }
553
557 protected function saveAttachCriterionForm() : void
558 {
559 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
560 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
561 }
562
563 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
564
565 $form = $this->getCriterionForm($document, new ilTermsOfServiceDocumentCriterionAssignment());
566 if ($form->saveObject()) {
567 ilUtil::sendSuccess($this->lng->txt('tos_doc_crit_attached'), true);
568 $this->ctrl->redirect($this, 'showDocuments');
569 } elseif ($form->hasTranslatedError()) {
570 ilUtil::sendFailure($form->getTranslatedError());
571 }
572
573 $this->tpl->setContent($form->getHTML());
574 }
575
579 protected function showAttachCriterionForm() : void
580 {
581 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
582 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
583 }
584
585 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
586
587 $form = $this->getCriterionForm($document, new ilTermsOfServiceDocumentCriterionAssignment());
588 $this->tpl->setContent($form->getHTML());
589 }
590
594 protected function showChangeCriterionForm() : void
595 {
596 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
597 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
598 }
599
600 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
601
602 $criterionId = $this->httpState->request()->getQueryParams()['crit_id'] ?? 0;
603 if (!is_numeric($criterionId) || $criterionId < 1) {
604 $this->showDocuments();
605 return;
606 }
607
608 $criterionAssignment = array_values(array_filter(
609 $document->criteria(),
610 function (ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment) use ($criterionId) {
611 return $criterionAssignment->getId() == $criterionId;
612 }
613 ))[0];
614
615 $form = $this->getCriterionForm($document, $criterionAssignment);
616 $this->tpl->setContent($form->getHTML());
617 }
618
622 protected function saveChangeCriterionForm() : void
623 {
624 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
625 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
626 }
627
628 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
629
630 $criterionId = $this->httpState->request()->getQueryParams()['crit_id'] ?? 0;
631 if (!is_numeric($criterionId) || $criterionId < 1) {
632 $this->showDocuments();
633 return;
634 }
635
636 $criterionAssignment = array_values(array_filter(
637 $document->criteria(),
638 function (ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment) use ($criterionId) {
639 return $criterionAssignment->getId() == $criterionId;
640 }
641 ))[0];
642
643 $form = $this->getCriterionForm($document, $criterionAssignment);
644 if ($form->saveObject()) {
645 ilUtil::sendSuccess($this->lng->txt('tos_doc_crit_changed'), true);
646 $this->ctrl->redirect($this, 'showDocuments');
647 } elseif ($form->hasTranslatedError()) {
648 ilUtil::sendFailure($form->getTranslatedError());
649 }
650
651 $this->tpl->setContent($form->getHTML());
652 }
653
657 public function detachCriterionAssignment() : void
658 {
659 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
660 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
661 }
662
663 $document = $this->getFirstDocumentFromList($this->getDocumentsByServerRequest());
664
665 $criterionId = $this->httpState->request()->getQueryParams()['crit_id'] ?? 0;
666 if (!is_numeric($criterionId) || $criterionId < 1) {
667 $this->showDocuments();
668 return;
669 }
670
671 $criterionAssignment = array_values(array_filter(
672 $document->criteria(),
673 function (ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment) use ($criterionId) {
674 return $criterionAssignment->getId() == $criterionId;
675 }
676 ))[0];
677
678 $document->detachCriterion($criterionAssignment);
679 $document->update();
680
681 ilUtil::sendSuccess($this->lng->txt('tos_doc_crit_detached'), true);
682 $this->ctrl->redirect($this, 'showDocuments');
683 }
684}
user()
Definition: user.php:4
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
error($a_errmsg)
set error message @access public
Confirmation screen class.
This class provides processing control methods.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
static setUseRelativeDates($a_status)
set use relative dates
static useRelativeDates()
check if relative dates are used
Base class for ILIAS Exception handling.
getFormAction()
Get FormAction.
Class ilGlobalPageTemplate.
language handling
static getInstance()
Factory.
Component logger with individual log levels by component id.
Class ilObjTermsOfService.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
Class ilTermsOfServiceDocumentFormGUI.
Class ilTermsOfServiceDocumentGUI.
getCriterionForm(ilTermsOfServiceDocument $document, ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
__construct(ilObjTermsOfService $tos, ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory, ilGlobalPageTemplate $tpl, ilObjUser $user, ilCtrl $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)
ilTermsOfServiceDocumentGUI constructor.
getDocumentForm(ilTermsOfServiceDocument $document)
executeCommand()
The implemented class should be ilCtrl enabled and execute or forward the given command.
Class ilTermsOfServiceDocument.
detachCriterion(ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
criteria()
ilTermsOfServiceEvaluableCriterion[]
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
Interface GlobalHttpState.
This is how the factory for UI elements looks.
Definition: Factory.php:18
An entity that renders components to a string output.
Definition: Renderer.php:15
Interface for html sanitizing functionality.
Interface ilTermsOfServiceControllerEnabled.
$data
Definition: storeScorm.php:23