ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjTermsOfServiceGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
39 {
43  private readonly Config $config;
44  private readonly UI $ui;
45  private readonly Settings $tos_settings;
46 
47  public function __construct($a_id = 0, $a_id_type = self::REPOSITORY_NODE_ID, $a_parent_node_id = 0)
48  {
49  global $DIC;
50 
51  $this->dic = $DIC;
52  $this->lng = $DIC->language();
53  $this->error = $DIC['ilErr'];
54 
55  parent::__construct($a_id, $a_id_type, $a_parent_node_id);
56 
57  $this->lng->loadLanguageModule('tos');
58  $this->lng->loadLanguageModule('meta');
59  $config = new Config($this->dic['legalDocuments']->provide(Consumer::ID));
60  if ($this->rbac_system->checkAccess('write', $this->object->getRefId())) {
61  $config = $config->allowEditing();
62  }
63  $this->config = $config;
64  $this->legal_documents = new ilLegalDocumentsAdministrationGUI(self::class, $this->config, $this->afterDocumentDeletion(...));
65  $this->ui = new UI(Consumer::ID, $this->dic->ui(), $this->dic->language());
66  $this->tos_settings = $this->createSettings();
67  }
68 
69  public function getType(): string
70  {
71  return 'tos';
72  }
73 
74  public function executeCommand(): void
75  {
76  if (!$this->rbac_system->checkAccess('read', $this->object->getRefId())) {
77  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
78  }
79  $this->prepareOutput();
80  $next_class = $this->ctrl->getNextClass($this);
81  $cmd = $this->ctrl->getCmd();
82 
83  switch (strtolower($next_class)) {
84  case strtolower(ilLegalDocumentsAdministrationGUI::class):
85  switch ($cmd) {
86  case 'documents': $this->documents();
87  // no break.
88  default: $this->ctrl->forwardCommand($this->legal_documents);
89  }
90  return;
91  case strtolower(ilPermissionGUI::class):
92  $this->tabs_gui->activateTab('permissions');
93  $this->ctrl->forwardCommand(new ilPermissionGUI($this));
94  return;
95  default:
96  switch ($cmd) {
97  case 'confirmReset': $this->confirmReset();
98  return;
99  case 'resetNow': $this->resetNow();
100  return;
101  case 'documents':
102  $this->ctrl->redirectByClass([self::class, get_class($this->legal_documents)], 'documents');
103  return;
104  default: $this->settings();
105  return;
106  }
107  }
108  }
109 
110  public function getAdminTabs(): void
111  {
112  $can_edit_permissions = $this->rbac_system->checkAccess('edit_permission', $this->object->getRefId());
113 
114  // Read right is already required by self::executeCommand.
115  $this->legal_documents->tabs([
116  'documents' => fn() => $this->tabs_gui->addTab('settings', $this->lng->txt('settings'), $this->ctrl->getLinkTarget($this, 'settings')),
117  ]);
118 
119  if ($can_edit_permissions) {
120  $this->tabs_gui->addTab(
121  'permissions',
122  $this->lng->txt('perm_settings'),
123  $this->ctrl->getLinkTargetByClass([self::class, ilPermissionGUI::class], 'perm')
124  );
125  }
126  }
127 
128  public function form(): ILIAS\UI\Component\Input\Container\Form\Standard
129  {
130  $read_only = !$this->rbac_system->checkAccess('write', $this->object->getRefId());
131 
132  $enabled = $this->dic->ui()->factory()->input()->field()->optionalGroup(
133  [
134  'reeval_on_login' => $this->dic->ui()->factory()->input()->field()->checkbox(
135  $this->ui->txt('reevaluate_on_login'),
136  $this->ui->txt('reevaluate_on_login_desc')
137  )
138  ],
139  $this->lng->txt('tos_status_enable'),
140  $this->lng->txt('tos_status_desc')
141  );
142  $enabled = $enabled->withValue($this->tos_settings->enabled()->value() ? ['reeval_on_login' => $this->tos_settings->validateOnLogin()->value()] : null);
143  $enabled = $enabled->withDisabled($read_only);
144 
145  $form = $this->dic->ui()->factory()->input()->container()->form()->standard(
146  $this->ctrl->getFormAction($this, 'saveSettings'),
147  ['enabled' => $enabled]
148  );
149 
150  if ($read_only) {
151  $form = $form->withSubmitLabel($this->lng->txt('refresh'));
152  return $this->legal_documents->admin()->withFormData($form, function () {
153  $this->ctrl->redirect($this, 'settings');
154  });
155  }
156 
157  return $this->legal_documents->admin()->withFormData($form, function (array $data): void {
158  $no_documents = $this->config->legalDocuments()->document()->repository()->countAll() === 0;
159  if ($no_documents && $data['enabled']) {
160  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tos_no_documents_exist_cant_save'), true);
161  $this->ctrl->redirect($this, 'settings');
162  }
163  $this->tos_settings->enabled()->update(isset($data['enabled']));
164  $this->tos_settings->validateOnLogin()->update($data['enabled']['reeval_on_login'] ?? false);
165  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
166  $this->ctrl->redirect($this, 'settings');
167  });
168  }
169 
170  public function afterDocumentDeletion(): void
171  {
172  if ($this->config->legalDocuments()->document()->repository()->countAll() === 0) {
173  $this->tos_settings->enabled()->update(false);
174  }
175  }
176 
177  protected function settings(): void
178  {
179  if (!$this->rbac_system->checkAccess('read', $this->object->getRefId())) {
180  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
181  }
182 
183  $this->tabs_gui->activateTab('settings');
184 
185  $components = [];
186 
187  if ($this->tos_settings->enabled()->value() && $this->config->legalDocuments()->document()->repository()->countAll() === 0) {
188  $components[] = $this->ui->create()->messageBox()->info(
189  $this->ui->txt('no_documents_exist')
190  );
191  }
192 
193  $components[] = $this->legal_documents->admin()->externalSettingsMessage($this->tos_settings->deleteUserOnWithdrawal()->value());
194 
195  $components[] = $this->form();
196  $this->tpl->setContent($this->dic->ui()->renderer()->render($components));
197  }
198 
199  private function documents(): void
200  {
201  $buttons = $this->config->editable() ?
202  [$this->legal_documents->admin()->resetButton($this->dic->ctrl()->getLinkTarget($this, 'confirmReset'))] :
203  [];
204 
205  $reset_date = new DateTimeImmutable('@' . $this->dic->settings()->get('tos_last_reset', '0'));
206 
207  $this->tpl->setCurrentBlock('mess');
208  $this->legal_documents->admin()->setVariable('MESSAGE', $this->legal_documents->admin()->resetBox($reset_date, $buttons));
209  $this->tpl->parseCurrentBlock('mess');
210  }
211 
212  private function confirmReset(): void
213  {
214  $this->legal_documents->admin()->requireEditable();
215  $this->legal_documents->admin()->setContent((new Confirmation($this->dic->language()))->render(
216  $this->dic->ctrl()->getFormAction($this, 'resetNow'),
217  'resetNow',
218  'documents',
219  $this->dic->language()->txt('tos_sure_reset_tos')
220  ));
221  }
222 
223  private function resetNow(): void
224  {
225  $this->legal_documents->admin()->requireEditable();
226  $in = $this->dic->database()->in('usr_id', [ANONYMOUS_USER_ID, SYSTEM_USER_ID], true, 'integer');
227  $this->dic->database()->manipulate("UPDATE usr_data SET agree_date = NULL WHERE $in");
228  $this->tos_settings->lastResetDate()->update((new DataFactory())->clock()->system()->now());
229  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
230  $this->dic->ctrl()->redirectByClass([self::class, get_class($this->legal_documents)], 'documents');
231  }
232 
233  private function createSettings(): Settings
234  {
235  $blocks = new Blocks($this->config->legalDocuments()->id(), $this->dic, $this->config->legalDocuments());
236  return new Settings($blocks->selectSettingsFrom($blocks->globalStore()));
237  }
238 }
bool $enabled
Whether the system instance is enabled to accept connection requests.
Definition: System.php:123
const ANONYMOUS_USER_ID
Definition: constants.php:27
New implementation of ilObjectGUI.
Class ChatMainBarProvider .
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct($a_id=0, $a_id_type=self::REPOSITORY_NODE_ID, $a_parent_node_id=0)
prepareOutput(bool $show_sub_objects=true)
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
readonly ilLegalDocumentsAdministrationGUI $legal_documents
Error Handling & global info handling.
New PermissionGUI (extends from old ilPermission2GUI) RBAC related output.