ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTermsOfServiceDocumentGUI.php
Go to the documentation of this file.
1<?php
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()
129 {
130 $nextClass = $this->ctrl->getNextClass($this);
131 $cmd = $this->ctrl->getCmd();
132
133 if (!$this->rbacsystem->checkAccess('read', $this->tos->getRefId())) {
134 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
135 }
136
137 switch (strtolower($nextClass)) {
138 default:
139 if ($cmd == '' || !method_exists($this, $cmd)) {
140 $cmd = 'showDocuments';
141 }
142 $this->$cmd();
143 break;
144 }
145 }
146
150 protected function confirmReset()
151 {
152 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
153 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
154 }
155
156 $confirmation = new \ilConfirmationGUI();
157 $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmReset'));
158 $confirmation->setConfirm($this->lng->txt('confirm'), 'reset');
159 $confirmation->setCancel($this->lng->txt('cancel'), 'showDocuments');
160 $confirmation->setHeaderText($this->lng->txt('tos_sure_reset_tos'));
161
162 $this->tpl->setContent($confirmation->getHTML());
163 }
164
168 protected function reset()
169 {
170 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
171 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
172 }
173
174 $this->tos->resetAll();
175
176 $this->log->info('Terms of service reset by ' . $this->user->getId() . ' [' . $this->user->getLogin() . ']');
177 \ilUtil::sendSuccess($this->lng->txt('tos_reset_successful'));
178
179 $this->showDocuments();
180 }
181
185 protected function showDocuments()
186 {
187 if ($this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
188 $addDocumentBtn = \ilLinkButton::getInstance();
189 $addDocumentBtn->setPrimary(true);
190 $addDocumentBtn->setUrl($this->ctrl->getLinkTarget($this, 'showAddDocumentForm'));
191 $addDocumentBtn->setCaption('tos_add_document_btn_label');
192 $this->toolbar->addStickyItem($addDocumentBtn);
193 }
194
195 $documentTableGui = new \ilTermsOfServiceDocumentTableGUI(
196 $this,
197 'showDocuments',
198 $this->criterionTypeFactory,
199 $this->uiFactory,
200 $this->uiRenderer,
201 $this->rbacsystem->checkAccess('write', $this->tos->getRefId())
202 );
203 $documentTableGui->setProvider($this->tableDataProviderFactory->getByContext(\ilTermsOfServiceTableDataProviderFactory::CONTEXT_DOCUMENTS));
204 $documentTableGui->populate();
205
206 $this->tpl->setCurrentBlock('mess');
207 $this->tpl->setVariable('MESSAGE', $this->getResetMessageBoxHtml());
208 $this->tpl->parseCurrentBlock('mess');
209 $this->tpl->setContent($documentTableGui->getHTML());
210 }
211
215 protected function getResetMessageBoxHtml() : string
216 {
217 if ($this->tos->getLastResetDate() && $this->tos->getLastResetDate()->get(IL_CAL_UNIX) != 0) {
220 $resetText = sprintf(
221 $this->lng->txt('tos_last_reset_date'),
222 \ilDatePresentation::formatDate($this->tos->getLastResetDate())
223 );
225 } else {
226 $resetText = $this->lng->txt('tos_never_reset');
227 }
228
229 $buttons = [];
230 if ($this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
231 $buttons = [
232 $this->uiFactory
233 ->button()
234 ->standard($this->lng->txt('tos_reset_tos_for_all_users'), $this->ctrl->getLinkTarget($this, 'confirmReset'))
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
263 $form = new \ilTermsOfServiceDocumentFormGUI(
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()
282 {
283 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
284 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
285 }
286
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()
305 {
306 if (!$this->rbacsystem->checkAccess('write', $this->tos->getRefId())) {
307 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
308 }
309
311 $this->tpl->setContent($form->getHTML());
312 }
313
317 protected function showEditDocumentForm()
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()
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()
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)
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()
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()
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
541 $form = new \ilTermsOfServiceCriterionFormGUI(
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()
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()
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()
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()
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
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
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.
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...
special template class to simplify handling of ITX/PEAR
Class ilTermsOfServiceCriterionFormGUI.
Class ilTermsOfServiceDocumentFormGUI.
Class ilTermsOfServiceDocumentGUI.
getCriterionForm(\ilTermsOfServiceDocument $document, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
getDocumentForm(ilTermsOfServiceDocument $document)
executeCommand()
The implemented class should be \ilCtrl enabled and execute or forward the given command.
__construct(\ilObjTermsOfService $tos, \ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory, \ilTemplate $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.
Class ilTermsOfServiceDocument.
criteria()
\ilTermsOfServiceEvaluableCriterion[]
detachCriterion(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment)
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:16
An entity that renders components to a string output.
Definition: Renderer.php:15
Interface for html sanitizing functionality.
Interface ilTermsOfServiceControllerEnabled.
if(isset($_POST['submit'])) $form
$data
Definition: bench.php:6