ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilLTIProviderObjectSettingGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 use ceLTIc\LTI\Util;
23 
31 {
32  public const ROLE_ADMIN = 'admin';
33  public const ROLE_TUTOR = 'tutor';
34  public const ROLE_MEMBER = 'member';
35 
39  protected ?ilCtrl $ctrl = null;
40 
44  protected ?ilLogger $logger = null;
45 
49  protected ?ilLanguage $lng = null;
50 
52 
56  protected ?int $ref_id = null;
57 
62  protected array $custom_roles = [];
63 
67  protected bool $use_lti_roles = true;
68 
72 
76  public function __construct(int $a_ref_id)
77  {
78  global $DIC;
79  $this->ref_id = $a_ref_id;
80  $this->logger = ilLoggerFactory::getLogger('ltis');
81  $this->ctrl = $DIC->ctrl();
82  $this->tpl = $DIC['tpl'];
83  $this->rbacSystem = $DIC->rbac()->system();
84  $this->rbacReview = $DIC->rbac()->review();
85  $this->rbacAdmin = $DIC->rbac()->admin();
86 
87  $this->lng = $DIC->language();
88  $this->lng->loadLanguageModule('lti');
89  }
90 
95  public function hasSettingsAccess(): bool
96  {
97  if (!ilObjLTIAdministration::isEnabledForType(ilObject::_lookupType($this->ref_id, true))) {
98  $this->logger->debug('No LTI consumers activated for object type: ' . ilObject::_lookupType($this->ref_id, true));
99  return false;
100  }
101  return $this->rbacSystem->checkAccess(
102  'release_objects',
104  );
105  }
106 
111  public function setCustomRolesForSelection(array $a_roles): void
112  {
113  if (empty($a_roles)) {
114  $this->checkLocalRole();
115  $a_roles = $this->rbacReview->getLocalRoles($this->ref_id);
116  }
117  $this->custom_roles = $a_roles;
118  }
119 
123  public function offerLTIRolesForSelection(bool $a_stat): void
124  {
125  $this->use_lti_roles = $a_stat;
126  }
127 
128 
132  public function executeCommand(): void
133  {
134  $cmd = $this->ctrl->getCmd('settings');
135  $next_class = $this->ctrl->getNextClass($this);
136 
137  // switch ($next_class) {
138  // default:
139  $this->$cmd();
140  // break;
141  // }
142  }
143 
148  protected function settings(?ilPropertyFormGUI $form = null): void
149  {
150  if (!$form instanceof ilPropertyFormGUI) {
151  $form = $this->initObjectSettingsForm();
152  }
153  $this->tpl->setContent($form->getHTML());
154  }
155 
156 
161  {
162  $form = new ilPropertyFormGUI();
163  $form->setFormAction($this->ctrl->getFormAction($this));
164  $form->setTitle($this->lng->txt('lti_object_release_settings_form'));
165 
166  foreach (ilObjLTIAdministration::getEnabledConsumersForType(ilObject::_lookupType($this->ref_id, true)) as $global_consumer) {
167  $object_info = new ilLTIProviderObjectSetting($this->ref_id, $global_consumer->getExtConsumerId());
168 
169  $this->logger->debug((string) $object_info->getAdminRole());
170 
171 
172  // meta data for external consumers
173  $section = new ilFormSectionHeaderGUI();
174  $section->setTitle($global_consumer->getTitle());
175  $section->setInfo($global_consumer->getDescription());
176  $form->addItem($section);
177 
178  $connector = new ilLTIDataConnector();
179 
181  $global_consumer->getExtConsumerId(),
183  $connector
184  );
185  $active = new ilCheckboxInputGUI($this->lng->txt('lti_obj_active'), 'lti_active_' . $global_consumer->getExtConsumerId());
186  $active->setInfo($this->lng->txt('lti_obj_active_info'));
187  $active->setValue("1");//check
188  if ($active_consumer->getEnabled()) { // and enabled
189  $active->setChecked(true);
190  }
191  if ($this->custom_roles) {
192  $admin = new ilSelectInputGUI(
193  $this->lng->txt('lti_admin'),
194  'lti_admin_' . $global_consumer->getExtConsumerId()
195  );
196  $admin->setOptions($this->getRoleSelection());
197  $admin->setValue($object_info->getAdminRole() ? $object_info->getAdminRole() : 0);
198  $active->addSubItem($admin);
199 
200  $tutor = new ilSelectInputGUI(
201  $this->lng->txt('lti_tutor'),
202  'lti_tutor_' . $global_consumer->getExtConsumerId()
203  );
204  $tutor->setOptions($this->getRoleSelection());
205  $tutor->setValue($object_info->getTutorRole() ? $object_info->getTutorRole() : 0);
206  $active->addSubItem($tutor);
207 
208  $member = new ilSelectInputGUI(
209  $this->lng->txt('lti_member'),
210  'lti_member_' . $global_consumer->getExtConsumerId()
211  );
212  $member->setOptions($this->getRoleSelection());
213  $member->setValue($object_info->getMemberRole() ? $object_info->getMemberRole() : 0);
214  $active->addSubItem($member);
215  }
216  $form->addItem($active);
217 
218  $version = new ilRadioGroupInputGUI($this->lng->txt('lti_obj_version'), 'version_' . $global_consumer->getExtConsumerId());
219  $version->setRequired(true);
220  if (!is_null($active_consumer->ltiVersion)) {
221  $version->setValue($active_consumer->ltiVersion->value);
222  }
223  // $version->setInfo($this->lng->txt('lti_obj_version_info'));
224  $op1 = new ilRadioOption($this->lng->txt("lti_obj_version_13"), LtiVersion::V1P3->value);
225  $sh = new ilNonEditableValueGUI($this->lng->txt('lti_13_step1'), '');
226  $sh->setValue($this->lng->txt("lti_13_step1_info"));
227  $op1->addSubItem($sh);
228  $url = new ilNonEditableValueGUI($this->lng->txt('lti_launch_url'), 'url');
229  $url->setValue(ILIAS_HTTP_PATH . '/lti.php?client_id=' . CLIENT_ID);
230  $op1->addSubItem($url);
231  // $url = new ilNonEditableValueGUI($this->lng->txt('lti_13_initiate_url'), 'url');
232  // $url->setValue(ILIAS_HTTP_PATH . '/lti.php?client_id=' . CLIENT_ID);
233  // $version->addSubItem($url);
234  // $url = new ilNonEditableValueGUI($this->lng->txt('lti_13_redirection_url'), 'url');
235  // $url->setValue(ILIAS_HTTP_PATH . '/lti.php?client_id=' . CLIENT_ID);
236  // $active->addSubItem($url);
237  $sh = new ilNonEditableValueGUI($this->lng->txt('lti_13_step2'), '');
238  $sh->setValue($this->lng->txt("lti_13_step2_info"));
239  $op1->addSubItem($sh);
240  $tf = new ilTextInputGUI($this->lng->txt('lti_13_platform_id'), 'platform_id_' . $global_consumer->getExtConsumerId());
241  $tf->setValue($active_consumer->platformId);
242  $op1->addSubItem($tf);
243  $tf = new ilTextInputGUI($this->lng->txt('lti_13_client_id'), 'client_id_' . $global_consumer->getExtConsumerId());
244  $tf->setValue($active_consumer->clientId);
245  $op1->addSubItem($tf);
246  $tf = new ilTextInputGUI($this->lng->txt('lti_13_deployment_id'), 'deployment_id_' . $global_consumer->getExtConsumerId());
247  $tf->setValue($active_consumer->deploymentId);
248  $op1->addSubItem($tf);
249  $tf = new ilTextInputGUI($this->lng->txt('lti_13_keyset_url'), 'keyset_url_' . $global_consumer->getExtConsumerId());
250  $tf->setValue($active_consumer->jku);
251  $op1->addSubItem($tf);
252  $tf = new ilTextInputGUI($this->lng->txt('lti_13_token_url'), 'token_url_' . $global_consumer->getExtConsumerId());
253  $tf->setValue($active_consumer->accessTokenUrl);
254  $op1->addSubItem($tf);
255  $tf = new ilTextInputGUI($this->lng->txt('lti_13_authentication_url'), 'authentication_url_' . $global_consumer->getExtConsumerId());
256  $tf->setValue($active_consumer->authenticationUrl);
257  $op1->addSubItem($tf);
258  $version->addOption($op1);
259 
260  $op0 = new ilRadioOption($this->lng->txt("lti_obj_version_11"), LtiVersion::V1->value);
261  $url = new ilNonEditableValueGUI($this->lng->txt('lti_launch_url'), 'url');
262  $url->setValue(ILIAS_HTTP_PATH . '/lti.php?client_id=' . CLIENT_ID);
263  $op0->addSubItem($url);
264  $key = new ilNonEditableValueGUI($this->lng->txt('lti_consumer_key'), 'key_' . $global_consumer->getExtConsumerId());
265  if (is_null($active_consumer->getKey())) {
266  $active_consumer->setKey(Util::getRandomString(10));//create $id .
267  }
268  $key->setValue($active_consumer->getKey());
269  $op0->addSubItem($key);
270  $secret = new ilNonEditableValueGUI($this->lng->txt('lti_consumer_secret'), 'secret_' . $global_consumer->getExtConsumerId());
271  if (is_null($active_consumer->getSecret())) {
272  $active_consumer->createSecret();
273  }
274  $secret->setValue($active_consumer->getSecret());
275  $op0->addSubItem($secret);
276  $version->addOption($op0);
277 
278  $active->addSubItem($version);
279  }
280 
281  $form->addCommandButton('updateSettings', $this->lng->txt('save'));
282  return $form;
283  }
284 
288  protected function updateSettings(): void
289  {
290  $form = $this->initObjectSettingsForm();
291  if (!$form->checkInput()) {
292  $form->setValuesByPost();
293  $this->settings($form);
294  return;
295  }
296 
297  $connector = new ilLTIDataConnector();
298  foreach (ilObjLTIAdministration::getEnabledConsumersForType(ilObject::_lookupType($this->ref_id, true)) as $global_consumer) {
299  $this->saveRoleSelection($form, (string) $global_consumer->getExtConsumerId());
300 
302  $global_consumer->getExtConsumerId(),
304  $connector
305  );
306  if (!$form->getInput('lti_active_' . $global_consumer->getExtConsumerId())) {
307  // not active anymore
308  if ($consumer->getEnabled()) {
309  $this->logger->info('Deleting lti consumer for object reference: ' . $this->ref_id);
310  $consumer->setEnabled(false);
311  $consumer->saveLTI($connector);
312  }
313  } else {
314  $consumer->ltiVersion = LtiVersion::from($form->getInput('version_' . $global_consumer->getExtConsumerId()));
315  $this->logger->info('Created new lti release for: ' . $this->ref_id);
316  $consumer->setExtConsumerId($global_consumer->getExtConsumerId());
317  $consumer->setKey((string) $form->getInput('key_' . $global_consumer->getExtConsumerId()));
318  $consumer->setSecret((string) $form->getInput('secret_' . $global_consumer->getExtConsumerId()));
319  $consumer->setRefId($this->ref_id);
320  $consumer->setEnabled(true);
321  if ($form->getInput('platform_id_' . $global_consumer->getExtConsumerId())) {
322  $consumer->platformId = (string) $form->getInput('platform_id_' . $global_consumer->getExtConsumerId());
323  } else {
324  $consumer->platformId = null;
325  }
326  if ($form->getInput('client_id_' . $global_consumer->getExtConsumerId())) {
327  $consumer->clientId = $form->getInput('client_id_' . $global_consumer->getExtConsumerId());
328  } else {
329  $consumer->clientId = null;
330  }
331  if ($form->getInput('deployment_id_' . $global_consumer->getExtConsumerId())) {
332  $consumer->deploymentId = $form->getInput('deployment_id_' . $global_consumer->getExtConsumerId());
333  } else {
334  $consumer->deploymentId = null;
335  }
336  if ($form->getInput('keyset_url_' . $global_consumer->getExtConsumerId())) {
337  $consumer->jku = $form->getInput('keyset_url_' . $global_consumer->getExtConsumerId());
338  } else {
339  $consumer->jku = '';
340  }
341  if ($form->getInput('token_url_' . $global_consumer->getExtConsumerId())) {
342  $consumer->accessTokenUrl = $form->getInput('token_url_' . $global_consumer->getExtConsumerId());
343  } else {
344  $consumer->accessTokenUrl = '';
345  }
346  if ($form->getInput('authentication_url_' . $global_consumer->getExtConsumerId())) {
347  $consumer->authenticationUrl = $form->getInput('authentication_url_' . $global_consumer->getExtConsumerId());
348  } else {
349  $consumer->authenticationUrl = '';
350  }
351  $consumer->saveLTI($connector);
352  }
353  }
354 
355  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
356  $this->ctrl->redirect($this, 'settings');
357  }
358 
364  protected function saveRoleSelection(ilPropertyFormGUI $form, string $global_consumer_id): void
365  {
366  $object_info = new ilLTIProviderObjectSetting($this->ref_id, (int) $global_consumer_id);
367 
368  $admin_role = (int) $form->getInput('lti_admin_' . $global_consumer_id);
369  if ($admin_role > 0) {
370  $object_info->setAdminRole($admin_role);
371  }
372  $tutor_role = (int) $form->getInput('lti_tutor_' . $global_consumer_id);
373  if ($tutor_role > 0) {
374  $object_info->setTutorRole($tutor_role);
375  }
376  $member_role = (int) $form->getInput('lti_member_' . $global_consumer_id);
377  if ($member_role > 0) {
378  $object_info->setMemberRole($member_role);
379  }
380  $object_info->save();
381  }
382 
387  protected function getRoleSelection(): array
388  {
389  $options = [];
390  $options[0] = $this->lng->txt('select_one');
391  foreach ($this->custom_roles as $role_id) {
393  $options[$role_id] = $title;
394  }
395  return $options;
396  }
397 
401  protected function checkLocalRole(): void
402  {
403  $a_global_role = ilObject::_getIdsForTitle("il_lti_global_role", "role", false);
404  if (is_array($a_global_role) && !empty($a_global_role)) {
405  if (count($this->rbacReview->getRolesOfObject($this->ref_id, false)) == 0) {
406  $rbacadmin = $this->rbacAdmin;
407  $type = ilObject::_lookupType($this->ref_id, true);
408  $role = new ilObjRole();
409  $role->setTitle("il_lti_learner");
410  $role->setDescription("LTI Learner of " . $type . " obj_no." . ilObject::_lookupObjectId($this->ref_id));
411  $role->create();
412  $rbacadmin->assignRoleToFolder($role->getId(), $this->ref_id, 'y');
413  $rbacadmin->grantPermission($role->getId(), ilRbacReview::_getOperationIdsByName(array('visible','read')), $this->ref_id);
414  // $rbacadmin->setRolePermission($role->getId(), ilObject::_lookupType($this->ref_id, true), [2,3], $this->ref_id);
415  if ($type == "sahs" || $type == "lm" || $type == "svy" || $type == "tst") {
416  $role = new ilObjRole();
417  $role->setTitle("il_lti_instructor");
418  $role->setDescription("LTI Instructor of " . $type . " obj_no." . ilObject::_lookupObjectId($this->ref_id));
419  $role->create();
420  $rbacadmin->assignRoleToFolder($role->getId(), $this->ref_id, 'y');
421  $ops = ilRbacReview::_getOperationIdsByName(array('visible','read','read_learning_progress'));
422  if ($type == "svy") {
423  $ops[] = ilRbacReview::_getOperationIdsByName(array('read_results'))[0];
424  }
425  if ($type == "tst") {
426  $ops[] = ilRbacReview::_getOperationIdsByName(array('tst_results'))[0];
427  }
428  $rbacadmin->grantPermission($role->getId(), $ops, $this->ref_id);
429  }
430  }
431  }
432  }
433 }
Class ilObjRole.
This class represents an option in a radio group.
static getLogger(string $a_component_id)
Get component logger.
$version
Definition: plugin.php:24
This class represents a selection list property in a property form.
static _getOperationIdsByName(array $operations)
get ops_id&#39;s by name.
$url
Definition: shib_logout.php:68
setOptions(array $a_options)
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
static fromGlobalSettingsAndRefId(int $a_ext_consumer_id, int $a_ref_id, ilLTIDataConnector $a_data_connector)
Load consumer from global settings and ref_id.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This class represents a property in a property form.
settings(?ilPropertyFormGUI $form=null)
Show settings.
saveRoleSelection(ilPropertyFormGUI $form, string $global_consumer_id)
Save role selection for consumer.
static _lookupTitle(int $obj_id)
GUI class for LTI provider object settings.
static lookupLTISettingsRefId()
Lookup ref_id.
hasSettingsAccess()
Check if user has access to lti settings.
static _getTranslation(string $a_role_title)
setValue(string $a_value)
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:26
static _lookupObjectId(int $ref_id)
setCustomRolesForSelection(array $a_roles)
Set custom roles for mapping to LTI roles.
static _getIdsForTitle(string $title, string $type='', bool $partial_match=false)
offerLTIRolesForSelection(bool $a_stat)
Offer LTI roles for mapping.
updateSettings()
Update settings (activate deactivate lti access)
checkLocalRole()
check for local roles for lti objects which are not grp or crs
Class ilRbacAdmin Core functions for role based access control.
static _lookupType(int $id, bool $reference=false)