ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilLTIProviderObjectSettingGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
12 {
13  const ROLE_ADMIN = 'admin';
14  const ROLE_TUTOR = 'tutor';
15  const ROLE_MEMBER = 'member';
16 
17 
21  protected $ctrl = null;
22 
26  protected $logger = null;
27 
31  protected $lng = null;
32 
36  protected $tpl = null;
37 
41  protected $ref_id = null;
42 
47  protected $custom_roles = [];
48 
52  protected $use_lti_roles = true;
53 
57  public function __construct($a_ref_id)
58  {
59  $this->ref_id = $a_ref_id;
60  $this->logger = $GLOBALS['DIC']->logger()->lti();
61  $this->ctrl = $GLOBALS['DIC']->ctrl();
62  $this->tpl = $GLOBALS['DIC']->ui()->mainTemplate();
63 
64  $this->lng = $GLOBALS['DIC']->language();
65  $this->lng->loadLanguageModule('lti');
66  }
67 
73  public function hasSettingsAccess()
74  {
75  if (!ilObjLTIAdministration::isEnabledForType(ilObject::_lookupType($this->ref_id, true))) {
76  $this->logger->debug('No LTI consumers activated for object type: ' . ilObject::_lookupType($this->ref_id, true));
77  return false;
78  }
79  $access = $GLOBALS['DIC']->rbac()->system();
80  return $access->checkAccess(
81  'release_objects',
83  );
84  }
85 
90  public function setCustomRolesForSelection($a_roles)
91  {
92  $this->custom_roles = $a_roles;
93  }
94 
99  public function offerLTIRolesForSelection($a_stat)
100  {
101  $this->use_lti_roles = $a_stat;
102  }
103 
104 
108  public function executeCommand()
109  {
110  $cmd = $this->ctrl->getCmd('settings');
111  $next_class = $this->ctrl->getNextClass($this);
112 
113  switch ($next_class) {
114  default:
115  $this->$cmd();
116  break;
117  }
118  }
119 
124  protected function settings(ilPropertyFormGUI $form = null)
125  {
126  if (!$form instanceof ilPropertyFormGUI) {
127  $form = $this->initObjectSettingsForm();
128  }
129  $this->tpl->setContent($form->getHTML());
130  }
131 
132 
136  protected function initObjectSettingsForm()
137  {
138  $form = new ilPropertyFormGUI();
139  $form->setFormAction($this->ctrl->getFormAction($this));
140  $form->setTitle($this->lng->txt('lti_object_release_settings_form'));
141 
142  foreach (ilObjLTIAdministration::getEnabledConsumersForType(ilObject::_lookupType($this->ref_id, true)) as $global_consumer) {
143  $object_info = new ilLTIProviderObjectSetting($this->ref_id, $global_consumer->getExtConsumerId());
144 
145  $this->logger->debug($object_info->getAdminRole());
146 
147 
148  // meta data for external consumers
150  $section->setTitle($global_consumer->getTitle());
151  $section->setInfo($global_consumer->getDescription());
152  $form->addItem($section);
153 
154  $connector = new ilLTIDataConnector();
155 
157  $global_consumer->getExtConsumerId(),
159  $connector
160  );
161 
162  $active = new ilCheckboxInputGUI($GLOBALS['lng']->txt('lti_obj_active'), 'lti_active_' . $global_consumer->getExtConsumerId());
163  $active->setInfo($GLOBALS['lng']->txt('lti_obj_active_info'));
164  $active->setValue(1);
165  $form->addItem($active);
166 
167  if ($active_consumer->getEnabled()) { // and enabled
168  $active->setChecked(true);
169 
170  $url = new ilNonEditableValueGUI($this->lng->txt('lti_launch_url'), 'url');
171  $url->setValue(ILIAS_HTTP_PATH . '/lti.php?client_id=' . CLIENT_ID);
172  $active->addSubItem($url);
173 
174  $key = new ilNonEditableValueGUI($this->lng->txt('lti_consumer_key'), 'key');
175  $key->setValue($active_consumer->getKey());
176  $active->addSubItem($key);
177 
178  $secret = new ilNonEditableValueGUI($this->lng->txt('lti_consumer_secret'), 'secret');
179  $secret->setValue($active_consumer->getSecret());
180  $active->addSubItem($secret);
181  }
182 
183  if ($this->custom_roles) {
184  $admin = new ilSelectInputGUI(
185  $this->lng->txt('lti_admin'),
186  'lti_admin_' . $global_consumer->getExtConsumerId()
187  );
188  $admin->setOptions($this->getRoleSelection());
189  $admin->setValue($object_info->getAdminRole() ? $object_info->getAdminRole() : 0);
190  $active->addSubItem($admin);
191 
192  $tutor = new ilSelectInputGUI(
193  $this->lng->txt('lti_tutor'),
194  'lti_tutor_' . $global_consumer->getExtConsumerId()
195  );
196  $tutor->setOptions($this->getRoleSelection());
197  $tutor->setValue($object_info->getTutorRole() ? $object_info->getTutorRole() : 0);
198  $active->addSubItem($tutor);
199 
200  $member = new ilSelectInputGUI(
201  $this->lng->txt('lti_member'),
202  'lti_member_' . $global_consumer->getExtConsumerId()
203  );
204  $member->setOptions($this->getRoleSelection());
205  $member->setValue($object_info->getMemberRole() ? $object_info->getMemberRole() : 0);
206  $active->addSubItem($member);
207  }
208  }
209 
210  $form->addCommandButton('updateSettings', $this->lng->txt('save'));
211  return $form;
212  }
213 
217  protected function updateSettings()
218  {
219  $form = $this->initObjectSettingsForm();
220  if (!$form->checkInput()) {
221  $form->setValuesByPost();
222  $this->settings($form);
223  return;
224  }
225 
226  $connector = new ilLTIDataConnector();
227  foreach (ilObjLTIAdministration::getEnabledConsumersForType(ilObject::_lookupType($this->ref_id, true)) as $global_consumer) {
228  $this->saveRoleSelection($form, $global_consumer->getExtConsumerId());
229 
231  $global_consumer->getExtConsumerId(),
233  $connector
234  );
235  if (!$form->getInput('lti_active_' . $global_consumer->getExtConsumerId())) {
236  // not active anymore => delete consumer
237  if ($consumer->getEnabled()) {
238  $this->logger->info('Deleting lti consumer for object reference: ' . $this->ref_id);
239  $consumer->delete();
240  }
241  } else {
242  // new activation
243  if (!$consumer->getEnabled()) {
244  $this->logger->info('Created new lti release for: ' . $this->ref_id);
245  $consumer->setExtConsumerId($global_consumer->getExtConsumerId());
246  $consumer->createSecret();
247  $consumer->setRefId($this->ref_id);
248  $consumer->setEnabled(true);
249  $consumer->saveLTI($connector);
250  }
251  }
252  }
253 
254  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
255  $this->ctrl->redirect($this, 'settings');
256  }
257 
263  protected function saveRoleSelection(ilPropertyFormGUI $form, $global_consumer_id)
264  {
265  $object_info = new ilLTIProviderObjectSetting($this->ref_id, $global_consumer_id);
266 
267  $admin_role = $form->getInput('lti_admin_' . $global_consumer_id);
268  if ($admin_role > 0) {
269  $object_info->setAdminRole($admin_role);
270  }
271  $tutor_role = $form->getInput('lti_tutor_' . $global_consumer_id);
272  if ($tutor_role > 0) {
273  $object_info->setTutorRole($tutor_role);
274  }
275  $member_role = $form->getInput('lti_member_' . $global_consumer_id);
276  if ($member_role > 0) {
277  $object_info->setMemberRole($member_role);
278  }
279  $object_info->save();
280  }
281 
282 
287  protected function getRoleSelection()
288  {
289  $options = [];
290  $options[0] = $this->lng->txt('select_one');
291  foreach ($this->custom_roles as $role_id) {
293  $options[$role_id] = $title;
294  }
295  return $options;
296  }
297 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
saveRoleSelection(ilPropertyFormGUI $form, $global_consumer_id)
Save role selection for consumer.
This class represents a selection list property in a property form.
This class represents a property form user interface.
$secret
Definition: demo.php:27
Class to represent an LTI Data Connector for ILIAS.
This class represents a section header in a property form.
offerLTIRolesForSelection($a_stat)
Offer LTI roles for mapping.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
This class represents a checkbox property in a property form.
static _lookupTitle($a_id)
lookup object title
static fromGlobalSettingsAndRefId($a_ext_consumer_id, $a_ref_id, ilLTIDataConnector $a_data_connector)
Load consumer from global settings and ref_id.
setInfo($a_info)
Set Information Text.
$section
Definition: Utf8Test.php:83
setCustomRolesForSelection($a_roles)
Set custom roles for mapping to LTI roles.
if(isset($_POST['submit'])) $form
GUI class for LTI provider object settings.
static lookupLTISettingsRefId()
Lookup ref_id.
hasSettingsAccess()
Check if user has access to lti settings.
settings(ilPropertyFormGUI $form=null)
Show settings.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
setOptions($a_options)
Set Options.
$consumer
Definition: demo.php:30
static _getTranslation($a_role_title)
static _lookupType($a_id, $a_reference=false)
lookup object type
updateSettings()
Update settings (activate deactivate lti access)
This class represents a non editable value in a property form.
$url
$key
Definition: croninfo.php:18
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20