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