ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjTermsOfServiceGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Object/classes/class.ilObject2GUI.php';
5 require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
6 require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
7 require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceTableDataProviderFactory.php';
8 require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceAgreementByLanguageTableGUI.php';
9 require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceAcceptanceHistoryTableGUI.php';
10 
18 {
22  protected $form;
23 
27  public $lng;
28 
32  public $ctrl;
33 
37  public $object;
38 
42  public $tabs_gui;
43 
47  public $factory;
48 
54  public function __construct($a_id = 0, $a_id_type = self::REPOSITORY_NODE_ID, $a_parent_node_id = 0)
55  {
60  global $lng, $ilDB;
61 
62  parent::__construct($a_id, $a_id_type, $a_parent_node_id);
63 
64  $this->lng->loadLanguageModule('tos');
65 
66  $this->factory = new ilTermsOfServiceTableDataProviderFactory();
67  $this->factory->setLanguageAdapter($lng);
68  $this->factory->setDatabaseAdapter($ilDB);
69  }
70 
74  public function getType()
75  {
76  return 'tos';
77  }
78 
82  public function executeCommand()
83  {
84  $next_class = $this->ctrl->getNextClass($this);
85  $cmd = $this->ctrl->getCmd();
86  $this->prepareOutput();
87 
88  switch($next_class)
89  {
90  case 'ilpermissiongui':
91  $this->tabs_gui->setTabActive('perm_settings');
92  require_once 'Services/AccessControl/classes/class.ilPermissionGUI.php';
93  $perm_gui = new ilPermissionGUI($this);
94  $this->ctrl->forwardCommand($perm_gui);
95  break;
96 
97  default:
98  if($cmd == '' || $cmd == 'view')
99  {
100  $cmd = 'settings';
101  }
102  $this->$cmd();
103  break;
104  }
105  }
106 
110  public function getAdminTabs(ilTabsGUI $tabs_gui)
111  {
115  global $rbacsystem;
116 
117  if($rbacsystem->checkAccess('read', $this->object->getRefId()))
118  {
119  $tabs_gui->addTarget('settings', $this->ctrl->getLinkTarget($this, 'settings'), array('saveSettings', 'settings', '', 'view'), '', '');
120  }
121 
122  if($rbacsystem->checkAccess('read', $this->object->getRefId()))
123  {
124  $tabs_gui->addTarget('tos_agreement_by_lng', $this->ctrl->getLinkTarget($this, 'showAgreementByLanguage'), array('reset', 'confirmReset', 'showAgreementByLanguage', 'resetAgreementByLanguageFilter', 'applyAgreementByLanguageFilter'), '', '');
125  }
126 
127  if($rbacsystem->checkAccess('read', $this->object->getRefId()) &&
128  $rbacsystem->checkAccess('read', USER_FOLDER_ID)
129  )
130  {
131  $tabs_gui->addTarget('tos_acceptance_history', $this->ctrl->getLinkTarget($this, 'showAcceptanceHistory'), array('showAcceptanceHistory', 'resetAcceptanceHistoryFilter', 'applyAcceptanceHistoryFilter'), '', '');
132  }
133 
134  if($rbacsystem->checkAccess('edit_permission', $this->object->getRefId()))
135  {
136  $tabs_gui->addTarget('perm_settings', $this->ctrl->getLinkTargetByClass(array(get_class($this), 'ilpermissiongui'), 'perm'), array('perm', 'info', 'owner'), 'ilpermissiongui');
137  }
138  }
139 
143  protected function initSettingsForm()
144  {
145  if(null == $this->form)
146  {
147  $this->form = new ilPropertyFormGUI();
148  $this->form->setTitle($this->lng->txt('tos_tos_settings'));
149  $this->form->setFormAction($this->ctrl->getFormAction($this, 'saveSettings'));
150 
151  $status = new ilCheckboxInputGUI($this->lng->txt('tos_status_enable'), 'tos_status');
152  $status->setInfo($this->lng->txt('tos_status_desc'));
153  $this->form->addItem($status);
154 
155  $this->form->addCommandButton('saveSettings', $this->lng->txt('save'));
156  }
157  }
158 
162  protected function saveSettings()
163  {
168  global $rbacsystem, $ilErr;
169 
170  if(!$rbacsystem->checkAccess('write', $this->object->getRefId()))
171  {
172  $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
173  }
174 
175  $provider = $this->factory->getByContext(ilTermsOfServiceTableDataProviderFactory::CONTEXT_AGRREMENT_BY_LANGUAGE);
176  $list = $provider->getList(array(), array());
177 
178  $has_documents = false;
179  foreach($list['items'] as $item)
180  {
181  if($item['agreement_document'])
182  {
183  $has_documents = true;
184  break;
185  }
186  }
187 
188  $this->initSettingsForm();
189  if($this->form->checkInput())
190  {
191  if($has_documents || !(int)$this->form->getInput('tos_status'))
192  {
193  $this->object->saveStatus((int)$this->form->getInput('tos_status'));
194  ilUtil::sendSuccess($this->lng->txt('saved_successfully'));
195  }
196  }
197 
198  if(
199  !$has_documents &&
200  (int)$this->form->getInput('tos_status') &&
201  !$this->object->getStatus()
202  )
203  {
204  $_POST['tos_status'] = 0;
205  ilUtil::sendFailure($this->lng->txt('tos_no_documents_exist_cant_save'));
206  }
207 
208  $this->settings(false);
209  }
210 
214  protected function settings($init_from_database = true)
215  {
221  global $rbacsystem, $ilErr, $tpl;
222 
223  if(!$rbacsystem->checkAccess('read', $this->object->getRefId()))
224  {
225  $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
226  }
227 
228  $this->showMissingDocuments();
229 
230  $this->initSettingsForm();
231  if($init_from_database)
232  {
233  $this->form->setValuesByArray(array(
234  'tos_status' => $this->object->getStatus()
235  ));
236  }
237  else
238  {
239  $this->form->setValuesByPost();
240  }
241 
242  $tpl->setContent($this->form->getHtml());
243  }
244 
248  protected function confirmReset()
249  {
255  global $rbacsystem, $ilErr, $tpl;
256 
257  if(!$rbacsystem->checkAccess('write', $this->object->getRefId()))
258  {
259  $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
260  }
261 
262  $confirmation = new ilConfirmationGUI();
263  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmReset'));
264  $confirmation->setConfirm($this->lng->txt('confirm'), 'reset');
265  $confirmation->setCancel($this->lng->txt('cancel'), 'showAgreementByLanguage');
266  $confirmation->setHeaderText($this->lng->txt('tos_sure_reset_tos'));
267 
268  $tpl->setContent($confirmation->getHtml());
269  }
270 
274  protected function reset()
275  {
282  global $rbacsystem, $ilErr, $ilLog, $ilUser;
283 
284  if(!$rbacsystem->checkAccess('write', $this->object->getRefId()))
285  {
286  $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
287  }
288 
289  $this->object->resetAll();
290  $ilLog->write(__METHOD__ . ': Terms of service reset by ' . $ilUser->getId() . ' [' . $ilUser->getLogin() . ']');
291  ilUtil::sendSuccess($this->lng->txt('tos_reset_successfull'));
292 
293  $this->showAgreementByLanguage();
294  }
295 
299  protected function showAgreementByLanguage()
300  {
307  global $rbacsystem, $ilErr, $tpl, $ilToolbar;
308 
309  if(!$rbacsystem->checkAccess('read', $this->object->getRefId()))
310  {
311  $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
312  }
313 
314  $this->lng->loadLanguageModule('meta');
315 
316  if($rbacsystem->checkAccess('write', $this->object->getRefId()))
317  {
318  $ilToolbar->setFormAction($this->ctrl->getFormAction($this, 'settings'));
319  $ilToolbar->addFormButton($this->lng->txt('tos_reset_tos_for_all_users'), 'confirmReset');
320  }
321 
322  $this->showLastResetDate();
323 
324  $table = new ilTermsOfServiceAgreementByLanguageTableGUI($this, 'showAgreementByLanguage');
325  $table->setProvider($this->factory->getByContext(ilTermsOfServiceTableDataProviderFactory::CONTEXT_AGRREMENT_BY_LANGUAGE));
326  $table->populate();
327 
328  $tpl->setContent($table->getHtml());
329  }
330 
334  protected function showLastResetDate()
335  {
339  global $ilToolbar;
340 
341  if($this->object->getLastResetDate() && $this->object->getLastResetDate()->get(IL_CAL_UNIX) != 0)
342  {
345  $ilToolbar->addText(sprintf($this->lng->txt('tos_last_reset_date'), ilDatePresentation::formatDate($this->object->getLastResetDate())));
347  }
348  }
349 
353  protected function showMissingDocuments()
354  {
355  if(!$this->object->getStatus())
356  {
357  return;
358  }
359 
360  $provider = $this->factory->getByContext(ilTermsOfServiceTableDataProviderFactory::CONTEXT_AGRREMENT_BY_LANGUAGE);
361  $list = $provider->getList(array(), array());
362 
363  $has_documents = false;
364  foreach($list['items'] as $item)
365  {
366  if($item['agreement_document'])
367  {
368  $has_documents = true;
369  break;
370  }
371  }
372 
373  if(!$has_documents)
374  {
375  ilUtil::sendInfo($this->lng->txt('tos_no_documents_exist'));
376  }
377  }
378 
382  protected function applyAgreementByLanguageFilter()
383  {
384  $table = new ilTermsOfServiceAgreementByLanguageTableGUI($this, 'showAgreementByLanguage');
385  $table->resetOffset();
386  $table->writeFilterToSession();
387 
388  $this->showAgreementByLanguage();
389  }
390 
394  protected function resetAgreementByLanguageFilter()
395  {
396  $table = new ilTermsOfServiceAgreementByLanguageTableGUI($this, 'showAgreementByLanguage');
397  $table->resetOffset();
398  $table->resetFilter();
399 
400  $this->showAgreementByLanguage();
401  }
402 
406  protected function getAgreementTextByFilenameAsynch()
407  {
411  global $rbacsystem;
412 
413  require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceJsonResponse.php';
414  $response = new ilTermsOfServiceJsonResponse();
415 
416  if(!isset($_GET['agreement_document']) || !strlen($_GET['agreement_document']) || !$rbacsystem->checkAccess('read', $this->object->getRefId()))
417  {
418  $response->setStatus(ilTermsOfServiceJsonResponse::STATUS_FAILURE);
419  echo $response;
420  }
421 
422  $file = realpath(strip_tags(rawurldecode(ilUtil::stripOnlySlashes($_GET['agreement_document']))));
423  if(preg_match('/Customizing[\/\\\](global[\/\\\]agreement|clients[\/\\\]' . CLIENT_ID . '[\/\\\]agreement)[\/\\\]agreement_([a-z]{2})\.html$/', $file))
424  {
425  $response->setBody(nl2br(trim(file_get_contents($file))));
426  }
427  else
428  {
429  $response->setStatus(ilTermsOfServiceJsonResponse::STATUS_FAILURE);
430  }
431 
432  echo $response;
433  }
434 
438  protected function showAcceptanceHistory()
439  {
445  global $rbacsystem, $ilErr, $tpl;
446 
447  if(!$rbacsystem->checkAccess('read', '', $this->object->getRefId()) ||
448  !$rbacsystem->checkAccess('read', '', USER_FOLDER_ID)
449  )
450  {
451  $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
452  }
453 
454  $this->lng->loadLanguageModule('meta');
455 
456  $table = new ilTermsOfServiceAcceptanceHistoryTableGUI($this, 'showAcceptanceHistory');
457  $table->setProvider($this->factory->getByContext(ilTermsOfServiceTableDataProviderFactory::CONTEXT_ACCEPTANCE_HISTORY));
458  $table->populate();
459 
460  $tpl->setContent($table->getHtml());
461  }
462 
466  protected function getAcceptedContentAsynch()
467  {
468  require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceJsonResponse.php';
469  $response = new ilTermsOfServiceJsonResponse();
470 
471  if(!isset($_GET['tosv_id']))
472  {
473  $response->setStatus(ilTermsOfServiceJsonResponse::STATUS_FAILURE);
474  echo $response;
475  }
476 
478  $response->setBody($entity->getText());
479 
480  echo $response;
481  }
482 
486  protected function addUserAutoComplete()
487  {
491  global $rbacsystem;
492 
493  if(!$rbacsystem->checkAccess('read', '', $this->object->getRefId()) ||
494  !$rbacsystem->checkAccess('read', '', USER_FOLDER_ID)
495  )
496  {
497  echo json_encode(array());
498  exit();
499  }
500 
501  include_once 'Services/User/classes/class.ilUserAutoComplete.php';
502  $auto = new ilUserAutoComplete();
503  $auto->setSearchFields(array('login', 'firstname', 'lastname', 'email'));
504  $auto->enableFieldSearchableCheck(false);
505  $auto->setMoreLinkAvailable(true);
506 
507  if(($_REQUEST['fetchall']))
508  {
509  $auto->setLimit(ilUserAutoComplete::MAX_ENTRIES);
510  }
511 
512  echo $auto->getList($_REQUEST['term']);
513  exit();
514  }
515 
519  protected function applyAcceptanceHistoryFilter()
520  {
521  $table = new ilTermsOfServiceAcceptanceHistoryTableGUI($this, 'showAcceptanceHistory');
522  $table->resetOffset();
523  $table->writeFilterToSession();
524 
525  $this->showAcceptanceHistory();
526  }
527 
531  protected function resetAcceptanceHistoryFilter()
532  {
533  $table = new ilTermsOfServiceAcceptanceHistoryTableGUI($this, 'showAcceptanceHistory');
534  $table->resetOffset();
535  $table->resetFilter();
536 
537  $this->showAcceptanceHistory();
538  }
539 }