ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilECSUserConsentModalGUI.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
24 
34 {
35  public const CMD_RENDER_MODAL = 'renderConsentModal';
36  public const CMD_SAVE_CONSENT = 'saveConsent';
37 
38  protected const TRIGGER_TYPE_SHY = 1;
39  protected const TRIGGER_TYPE_STANDARD = 2;
40  protected const TRIGGER_TYPE_CARD = 3;
41 
42  private int $usr_id;
43  private int $ref_id;
44  private int $obj_id;
45  private int $mid;
46  private int $server_id;
47 
53 
58  protected ilLanguage $lng;
61 
62  public function __construct(
63  int $a_usr_id,
64  int $a_ref_id,
65  ilRemoteObjectBaseGUI $remote_gui = null
66  ) {
67  global $DIC;
68 
69  $this->usr_id = $a_usr_id;
70  $this->ref_id = $a_ref_id;
71  $this->remote_gui = $remote_gui;
72  $this->consents = ilECSUserConsents::getInstanceByUserId($this->usr_id);
73  $this->importManager = ilECSImportManager::getInstance();
74  $this->exportManager = ilECSExportManager::getInstance();
75 
76  $this->ui_factory = $DIC->ui()->factory();
77  $this->ui_renderer = $DIC->ui()->renderer();
78  $this->request = $DIC->http()->request();
79  $this->lng = $DIC->language();
80  $this->lng->loadLanguageModule('ecs');
81  $this->ctrl = $DIC->ctrl();
82  $this->objDefinition = $DIC['objDefinition'];
83 
84  $this->remote_object = $this->initRemoteObject();
85  $this->initMidAndServer();
86  $this->obj_id = $this->remote_object->getId();
87  }
88 
89  public function hasConsented(): bool
90  {
91  return $this->consents->hasConsented($this->server_id, $this->mid);
92  }
93 
94  protected function initMidAndServer(): void
95  {
96  $this->mid = $this->remote_object->getMID();
97  $this->server_id = ilECSImportManager::getInstance()->lookupServerId($this->remote_object->getId());
98  }
99 
100  protected function lookupOrganization(): string
101  {
102  return $this->remote_object->getOrganization();
103  }
104 
105  protected function isLocalObject(): bool
106  {
107  return $this->remote_object->isLocalObject();
108  }
109 
110  public function getTitleLink(): string
111  {
112  if (
113  $this->usr_id === ANONYMOUS_USER_ID ||
114  $this->isLocalObject() ||
115  $this->hasConsented()
116  ) {
117  return '';
118  }
119  $components = $this->getConsentModalComponents(self::TRIGGER_TYPE_SHY);
120  return $this->ui_renderer->render($components);
121  }
122 
123  public function addLinkToToolbar(ilToolbarGUI $toolbar): void
124  {
125  if (
126  $this->usr_id === ANONYMOUS_USER_ID ||
127  $this->isLocalObject()
128  ) {
129  return;
130  }
131  if ($this->hasConsented()) {
132  $this->addRemoteLinkToToolbar($toolbar);
133  } else {
134  $this->addConsentModalToToolbar($toolbar);
135  }
136  }
137 
138  protected function addRemoteLinkToToolbar(ilToolbarGUI $toolbar): void
139  {
140  $button = $this->ui_factory
141  ->button()
142  ->standard(
143  $this->lng->txt($this->remote_object->getType() . '_call'),
144  $this->ctrl->getLinkTarget($this->remote_gui, 'call')
145  );
146  $toolbar->addComponent($button);
147  }
148 
149  public function addConsentModalToToolbar(ilToolbarGUI $toolbar): void
150  {
151  $components = $this->getConsentModalComponents();
152  foreach ($components as $component) {
153  $toolbar->addComponent($component);
154  }
155  }
156 
157  public function addConsentModalToCard(
158  RepositoryObject $card
159  ): RepositoryObject {
160  $components = $this->getConsentModalComponents(self::TRIGGER_TYPE_CARD);
161  foreach ($components as $component) {
162  if ($component === null) {
163  continue;
164  }
165  $this->toolbar->addComponent($component);
166 
167  $image = $card->getImage();
168  $image = $image->withOnClick($component->getShowSignal());
169  $card = $card
170  ->withImage($image)
171  ->withTitleAction($component->getShowSignal());
172  }
173  return $card;
174  }
175 
176  protected function getConsentModalComponents(
177  int $a_trigger_type = self::TRIGGER_TYPE_STANDARD
178  ): array {
179  $form = $this->initConsentForm();
180  $form_id = 'form_' . $form->getId();
181  $agree = $this->ui_factory->button()
182  ->primary('Agree and Proceed', '#')
183  ->withOnLoadCode(
184  function ($id) use ($form_id) {
185  return "$('#$id').click(function() { $('#$form_id').submit(); return false; });";
186  }
187  );
188 
189  $submitted = (string) ($this->request->getParsedBody()['cmd'] ?? '');
190  $valid = true;
191  $error_html = '';
192  if (strcmp($submitted, 'submit') === 0) {
193  if (!$this->saveConsent($form)) {
194  $form->setValuesByPost();
195  $error = $this->ui_factory->messageBox()->failure(
196  $this->lng->txt('ecs_consent_required')
197  );
198  $error_html = $this->ui_renderer->render([$error]);
199  $valid = false;
200  }
201  }
202 
203  $modal = $this->ui_factory->modal()->roundtrip(
204  $this->lng->txt('ecs_consent_modal_title'),
205  $this->ui_factory->legacy(
206  $error_html .
207  $form->getHTML()
208  )
209  )->withActionButtons([$agree]);
210  if (!$valid) {
211  $modal = $modal->withOnLoad($modal->getShowSignal());
212  }
213 
214  $button = null;
215  if ($a_trigger_type === self::TRIGGER_TYPE_STANDARD) {
216  $button = $this->ui_factory->button()->standard(
217  $this->lng->txt($this->remote_object->getType() . '_call'),
218  '#'
219  )->withOnClick(
220  $modal->getShowSignal()
221  );
222  } elseif ($a_trigger_type === self::TRIGGER_TYPE_SHY) {
223  $button = $this->ui_factory->button()->shy(
224  $this->remote_object->getTitle(),
225  '#'
226  )->withOnClick(
227  $modal->getShowSignal()
228  );
229  }
230  return [$button, $modal];
231  }
232 
233  protected function saveConsent(ilPropertyFormGUI $form): bool
234  {
235  $consented = (bool) ($this->request->getParsedBody()['consent'] ?? 0);
236  $ref_id_hidden = (int) ($this->request->getParsedBody()['consented_ref_id'] ?? 0);
237  $ref_type_hidden = (string) ($this->request->getParsedBody()['consented_type'] ?? '');
238  if ($consented) {
239  $this->consents->add($this->server_id, $this->mid);
240  $this->ctrl->setParameterByClass(
241  $ref_type_hidden,
242  'ref_id',
243  $ref_id_hidden
244  );
245  $this->ctrl->redirectToURL(
246  $this->ctrl->getLinkTargetByClass(
247  [
248  ilRepositoryGUI::class,
249  $ref_type_hidden
250  ],
251  'call'
252  )
253  );
254  return true;
255  }
256  return false;
257  }
258 
259  protected function initConsentForm(): ilPropertyFormGUI
260  {
261  $form = new ilPropertyFormGUI();
262  $form->setId(uniqid('form', false));
263  $form->setFormAction('#');
264 
265  $ref_id_hidden = new ilHiddenInputGUI('consented_ref_id');
266  $ref_id_hidden->setValue((string) $this->ref_id);
267  $form->addItem($ref_id_hidden);
268  $ref_type_hidden = new ilHiddenInputGUI('consented_type');
269  $ref_type_hidden->setValue($this->getGUIClassName());
270  $form->addItem($ref_type_hidden);
271  $title = new ilNonEditableValueGUI(
272  $this->lng->txt('title'),
273  'title'
274  );
275  $title->setValue(
276  ilObject::_lookupTitle($this->obj_id)
277  );
278  $form->addItem($title);
279 
280  $target = new ilNonEditableValueGUI(
281  $this->lng->txt('ecs_form_target_platform'),
282  'organisation'
283  );
284  $target->setValue($this->lookupOrganization());
285  $form->addItem($target);
286 
287  // provider
288  $organisation = $this->getOrganisation();
289  if ($organisation instanceof ilECSOrganisation) {
291  $this->lng->txt('organization'),
292  'provider'
293  );
294  $provider->setValue($organisation->getName());
295  $form->addItem($provider);
296  }
297 
298  $consent = new ilCheckboxInputGUI(
299  $this->lng->txt('ecs_form_consent'),
300  'consent'
301  );
302  $consent->setValue("1");
303  $consent->setChecked($this->consents->hasConsented($this->server_id, $this->mid));
304  $consent->setRequired(true);
305  $form->addItem($consent);
306 
307  $user_data_fields = [];
308  foreach (['login',
309  'firstname',
310  'lastname',
311  'email',
312  'institution'
313  ] as $field) {
314  $user_data_fields[] = $this->lng->txt('ecs_' . $field);
315  }
316  $listing = $this->ui_factory->listing()->unordered($user_data_fields);
317  $listing_html = $this->ui_renderer->render([$listing]);
318  $consent->setOptionTitle(
319  $this->lng->txt(
320  'ecs_form_consent_option_title'
321  ) . '<br />' . $listing_html
322  );
323  $submit = new ilHiddenInputGUI('cmd');
324  $submit->setValue('submit');
325  $form->addItem($submit);
326  return $form;
327  }
328 
329  protected function getOrganisation(): ?ilECSOrganisation
330  {
331  $server_id = $this->importManager->lookupServerId($this->obj_id);
332  if (0 === $server_id) {
333  return null;
334  }
336  $server_id
337  );
338  try {
339  $part = $community_reader->getParticipantByMID($this->mid);
340  if ($part instanceof ilECSParticipant) {
341  return $part->getOrganisation();
342  }
343  return null;
344  } catch (ilECSConnectorException $e) {
345  return null;
346  }
347  }
348 
349  protected function getGUIClassName(): string
350  {
351  return get_class($this->remote_gui);
352  }
353 
358  protected function initRemoteObject(): ilRemoteObjectBase
359  {
360  $remote = ilObjectFactory::getInstanceByRefId($this->ref_id);
361  if (!$remote instanceof ilRemoteObjectBase) {
362  throw new ilObjectNotFoundException(
363  'Invalid ref_id given: ' . $this->ref_id
364  );
365  }
366  return $remote;
367  }
368 }
const ANONYMOUS_USER_ID
Definition: constants.php:27
addConsentModalToCard(RepositoryObject $card)
static getInstance()
Get the singelton instance of this ilECSExportManager.
$valid
addComponent(\ILIAS\UI\Component\Component $a_comp)
__construct(int $a_usr_id, int $a_ref_id, ilRemoteObjectBaseGUI $remote_gui=null)
Class ilECSUserConsents.
This class represents a checkbox property in a property form.
Manage the ECS imported contents.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
getImage()
Get the image of the card.
$provider
Definition: ltitoken.php:83
parses the objects.xml it handles the xml-description of all ilias objects
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstance()
Get the singleton instance of this ilECSImportManager.
static _lookupTitle(int $obj_id)
addConsentModalToToolbar(ilToolbarGUI $toolbar)
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
static getInstanceByUserId(int $a_usr_id)
Remote object app base class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withImage(Image $image)
Set the image of the card.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
addRemoteLinkToToolbar(ilToolbarGUI $toolbar)
Manage the ECS exported contents.
getConsentModalComponents(int $a_trigger_type=self::TRIGGER_TYPE_STANDARD)