From 44b26e4bfdbf42302a1b0c66a7f2cad9c9e7a3e2 Mon Sep 17 00:00:00 2001 From: Fabian Helfer Date: Tue, 20 Aug 2024 10:18:45 +0200 Subject: [PATCH] Session Reminder Lead Time Changeable in LUA --- .../classes/class.ilSessionReminder.php | 51 +++++++++++++---- .../Profile/ilUserProfileDefaultFields.php | 2 - .../Settings/class.ilPersonalSettingsGUI.php | 25 ++------- .../Migration/class.ilUserDB10UpdateSteps.php | 46 +++++++++++++++ .../classes/Setup/class.ilUserSetupAgent.php | 3 + .../User/classes/class.ilObjUserFolderGUI.php | 56 ------------------- .../ILIAS/User/classes/class.ilObjUserGUI.php | 47 ++++++++++------ .../User/classes/class.ilUserDataSet.php | 2 +- .../class.ilUserFieldSettingsTableGUI.php | 18 +----- 9 files changed, 127 insertions(+), 123 deletions(-) create mode 100644 components/ILIAS/User/classes/Setup/Migration/class.ilUserDB10UpdateSteps.php diff --git a/components/ILIAS/Authentication/classes/class.ilSessionReminder.php b/components/ILIAS/Authentication/classes/class.ilSessionReminder.php index 32c94a7e7ae1..06a378541749 100755 --- a/components/ILIAS/Authentication/classes/class.ilSessionReminder.php +++ b/components/ILIAS/Authentication/classes/class.ilSessionReminder.php @@ -18,14 +18,13 @@ declare(strict_types=1); -use ILIAS\Data\Factory as DataFactory; use ILIAS\Data\Clock\ClockInterface; +use ILIAS\Data\Factory as DataFactory; class ilSessionReminder { public const MIN_LEAD_TIME = 2; public const SUGGESTED_LEAD_TIME = 5; - private ClockInterface $clock; private ilObjUser $user; private int $lead_time = self::SUGGESTED_LEAD_TIME; @@ -53,14 +52,47 @@ public static function byLoggedInUser(): self return $reminder; } - public static function isGloballyActivated(): bool + public static function getGlobalSessionReminderLeadTime(): int { - /** @var ilSetting $ilSetting */ global $DIC; - $ilSetting = $DIC['ilSetting']; - return (bool) $ilSetting->get('session_reminder_enabled'); + return self::getAcceptableLeadTime( + (int) $ilSetting->get('session_reminder_lead_time') + ); + } + + private static function getAcceptableLeadTime(int $lead_time): int + { + $min_value = 0; + $max_value = self::getMaxLeadTime(); + + if ($lead_time < $min_value || $lead_time > $max_value) { + $lead_time = self::SUGGESTED_LEAD_TIME; + } + + return min( + max( + $min_value, + $lead_time + ), + $max_value + ); + } + + public static function getLocalSessionLeadTime(int $user_id): int + { + return self::getAcceptableLeadTime( + (int) ilObjUser::_lookupPref( + $user_id, + 'session_reminder_lead_time' + ) ?: self::getGlobalSessionReminderLeadTime() + ); + } + + public static function isLocallyActivated(int $user_id): bool + { + return self::getLocalSessionLeadTime($user_id) > 0; } public static function getMaxLeadTime(): int @@ -114,13 +146,12 @@ private function isEnoughTimeLeftForReminder(): bool public function isActive(): bool { - return ( - self::isGloballyActivated() && + return !$this->getUser()->isAnonymous() && $this->getUser()->getId() > 0 && - (int) $this->getUser()->getPref('session_reminder_enabled') && + self::isLocallyActivated($this->getUser()->getId()) && $this->isEnoughTimeLeftForReminder() - ); + ; } public function setUser(ilObjUser $user): self diff --git a/components/ILIAS/User/classes/Profile/ilUserProfileDefaultFields.php b/components/ILIAS/User/classes/Profile/ilUserProfileDefaultFields.php index aa07565e4365..f19c08890594 100755 --- a/components/ILIAS/User/classes/Profile/ilUserProfileDefaultFields.php +++ b/components/ILIAS/User/classes/Profile/ilUserProfileDefaultFields.php @@ -408,8 +408,6 @@ class ilUserProfileDefaultFields 'session_reminder' => [ 'input' => 'session_reminder_lead_time', 'default' => 'y', - 'visible_hide' => true, - 'visib_lua_hide' => true, 'required_hide' => true, 'visib_reg_hide' => true, 'course_export_hide' => true, diff --git a/components/ILIAS/User/classes/Settings/class.ilPersonalSettingsGUI.php b/components/ILIAS/User/classes/Settings/class.ilPersonalSettingsGUI.php index b94d9dfad96f..cc0ed511c124 100755 --- a/components/ILIAS/User/classes/Settings/class.ilPersonalSettingsGUI.php +++ b/components/ILIAS/User/classes/Settings/class.ilPersonalSettingsGUI.php @@ -296,7 +296,7 @@ public function initGeneralSettingsForm(): void $lv->setValue($last_visited); $this->form->addItem($lv); - if (ilSessionReminder::isGloballyActivated()) { + if ($this->userSettingVisible('session_reminder')) { $session_reminder = new ilNumberInputGUI( $this->lng->txt('session_reminder'), 'session_reminder_lead_time' @@ -309,27 +309,12 @@ public function initGeneralSettingsForm(): void ) ); $session_reminder->setDisabled(!$this->workWithUserSetting('session_reminder')); - - $min_value = ilSessionReminder::MIN_LEAD_TIME; - $max_value = ilSessionReminder::getMaxLeadTime(); - - $current_user_value = $this->user->getPref('session_reminder_lead_time') ?: $this->settings->get('session_reminder'); - if ($current_user_value < $min_value || $current_user_value > $max_value) { - $current_user_value = ilSessionReminder::SUGGESTED_LEAD_TIME; - } - $value = min( - max( - $min_value, - $current_user_value - ), - $max_value - ); $session_reminder->setValue( - (string) $value + (string) ilSessionReminder::getLocalSessionLeadTime($this->user->getId()) ); $session_reminder->setSize(3); - $session_reminder->setMinValue($min_value); - $session_reminder->setMaxValue($max_value); + $session_reminder->setMinValue(0); + $session_reminder->setMaxValue(ilSessionReminder::getMaxLeadTime()); $this->form->addItem($session_reminder); } @@ -456,7 +441,7 @@ public function saveGeneralSettings(): void } } - if (ilSessionReminder::isGloballyActivated() && $this->workWithUserSetting('session_reminder')) { + if ($this->workWithUserSetting('session_reminder')) { $this->user->setPref( 'session_reminder_lead_time', (string) $this->form->getInput('session_reminder_lead_time') diff --git a/components/ILIAS/User/classes/Setup/Migration/class.ilUserDB10UpdateSteps.php b/components/ILIAS/User/classes/Setup/Migration/class.ilUserDB10UpdateSteps.php new file mode 100644 index 000000000000..9aff07895224 --- /dev/null +++ b/components/ILIAS/User/classes/Setup/Migration/class.ilUserDB10UpdateSteps.php @@ -0,0 +1,46 @@ + + */ +class ilUserDB10UpdateSteps implements ilDatabaseUpdateSteps +{ + private const USER_DATA_TABLE_NAME = 'usr_data'; + + protected ilDBInterface $db; + + public function prepare(ilDBInterface $db): void + { + $this->db = $db; + } + + + + public function step_1(): void + { + $query = 'DELETE FROM settings WHERE module="common" AND keyword="session_reminder";'; + $this->db->manipulate($query); + } +} diff --git a/components/ILIAS/User/classes/Setup/class.ilUserSetupAgent.php b/components/ILIAS/User/classes/Setup/class.ilUserSetupAgent.php index b74abccc97a8..a6a2c8b8ae3d 100755 --- a/components/ILIAS/User/classes/Setup/class.ilUserSetupAgent.php +++ b/components/ILIAS/User/classes/Setup/class.ilUserSetupAgent.php @@ -61,6 +61,9 @@ public function getUpdateObjective(Setup\Config $config = null): Setup\Objective ), new ilDatabaseUpdateStepsExecutedObjective( new ilUserDB90() + ), + new ilDatabaseUpdateStepsExecutedObjective( + new ilUserDB10UpdateSteps() ) ); } diff --git a/components/ILIAS/User/classes/class.ilObjUserFolderGUI.php b/components/ILIAS/User/classes/class.ilObjUserFolderGUI.php index be72d9c78fdc..e868176fe6d6 100755 --- a/components/ILIAS/User/classes/class.ilObjUserFolderGUI.php +++ b/components/ILIAS/User/classes/class.ilObjUserFolderGUI.php @@ -1730,8 +1730,6 @@ protected function generalSettingsObject(): void 'dpro_withdrawal_usr_deletion' => (bool) $this->settings->get('dpro_withdrawal_usr_deletion'), 'tos_withdrawal_usr_deletion' => (bool) $this->settings->get('tos_withdrawal_usr_deletion'), - 'session_reminder_enabled' => $this->settings->get('session_reminder_enabled'), - 'login_max_attempts' => $security->getLoginMaxAttempts(), 'ps_prevent_simultaneous_logins' => (int) $security->isPreventionOfSimultaneousLoginsEnabled(), 'password_assistance' => (bool) $this->settings->get('password_assistance'), @@ -1880,14 +1878,6 @@ public function saveGeneralSettingsObject(): void $this->form->getInput('password_assistance') ); - // BEGIN SESSION SETTINGS - - $this->settings->set( - 'session_reminder_enabled', - $this->form->getInput('session_reminder_enabled') - ); - - // END SESSION SETTINGS $this->settings->set( 'letter_avatars', $this->form->getInput('letter_avatars') @@ -2019,45 +2009,6 @@ protected function initFormGeneralSettings(): void (string) ilSessionControl::DEFAULT_ALLOW_CLIENT_MAINTENANCE ); - - // create session reminder subform - $session_reminder = new ilCheckboxInputGUI( - $this->lng->txt('session_reminder'), - 'session_reminder_enabled' - ); - $expires = ilSession::getSessionExpireValue(); - $time = ilDatePresentation::secondsToString( - $expires, - true - ); - $session_reminder->setInfo( - $this->lng->txt('session_reminder_info') . '
' . - sprintf( - $this->lng->txt('session_reminder_session_duration'), - $time - ) - ); - - // add radio group to form - if ($allow_client_maintenance) { - // just shows the status wether the session - //setting maintenance is allowed by setup - $this->form->addItem($session_reminder); - } else { - // just shows the status wether the session - //setting maintenance is allowed by setup - $session_config = new ilNonEditableValueGUI( - $this->lng->txt('session_config'), - 'session_config' - ); - $session_config->setValue($this->lng->txt('session_config_maintenance_disabled')); - $session_reminder->setDisabled(true); - $session_config->addSubItem($session_reminder); - $this->form->addItem($session_config); - } - - // END SESSION SETTINGS - $this->lng->loadLanguageModule('ps'); $pass = new ilFormSectionHeaderGUI(); @@ -2478,13 +2429,6 @@ public function saveGlobalUserSettingsObject(string $action = ''): void ); } - if (isset($input['default_session_reminder'])) { - $this->ilias->setSetting( - 'session_reminder', - $input['default_session_reminder'] - ); - } - $this->tpl->setOnScreenMessage('success', $this->lng->txt('usr_settings_saved')); $this->settingsObject(); } diff --git a/components/ILIAS/User/classes/class.ilObjUserGUI.php b/components/ILIAS/User/classes/class.ilObjUserGUI.php index 419cc977c234..1dee295360de 100755 --- a/components/ILIAS/User/classes/class.ilObjUserGUI.php +++ b/components/ILIAS/User/classes/class.ilObjUserGUI.php @@ -430,6 +430,9 @@ public function saveObject(): void if ($this->isSettingChangeable('hits_per_page')) { $user_object->setPref('hits_per_page', $this->form_gui->getInput('hits_per_page')); } + if ($this->isSettingChangeable('session_reminder')) { + $user_object->setPref('session_reminder_lead_time', (string) $this->form_gui->getInput('session_reminder_lead_time')); + } if ($this->isSettingChangeable('hide_own_online_status')) { $user_object->setPref( 'hide_own_online_status', @@ -454,12 +457,6 @@ public function saveObject(): void $this->form_gui->getInput('chat_broadcast_typing') ? 'y' : 'n' ); } - if ($this->settings->get('session_reminder_enabled') === '1') { - $user_object->setPref( - 'session_reminder_enabled', - $this->form_gui->getInput('session_reminder_enabled') - ); - } $user_object->writePrefs(); //set role entries @@ -747,6 +744,11 @@ public function updateObject(): void if ($this->isSettingChangeable('hits_per_page')) { $this->object->setPref('hits_per_page', $this->form_gui->getInput('hits_per_page')); } + + if ($this->isSettingChangeable('session_reminder')) { + $this->object->setPref('session_reminder_lead_time', (string) $this->form_gui->getInput('session_reminder_lead_time')); + } + if ($this->isSettingChangeable('hide_own_online_status')) { $this->object->setPref( 'hide_own_online_status', @@ -776,13 +778,6 @@ public function updateObject(): void // this ts is needed by ilSecuritySettings $this->object->setLastPasswordChangeTS(time()); - if ($this->settings->get('session_reminder_enabled') === '1') { - $this->object->setPref( - 'session_reminder_enabled', - $this->form_gui->getInput('session_reminder_enabled') - ); - } - // #10054 - profile may have been completed, check below is only for incomplete $this->object->setProfileIncomplete(false); @@ -900,11 +895,11 @@ public function getValues(): void $data['language'] = $this->object->getLanguage(); $data['skin_style'] = $this->object->skin . ':' . $this->object->prefs['style']; $data['hits_per_page'] = $this->object->prefs['hits_per_page'] ?? ''; + $data['session_reminder_lead_time'] = $this->object->prefs['session_reminder_lead_time'] ?? ilSessionReminder::getGlobalSessionReminderLeadTime(); $data['hide_own_online_status'] = $this->object->prefs['hide_own_online_status'] ?? ''; $data['bs_allow_to_contact_me'] = ($this->object->prefs['bs_allow_to_contact_me'] ?? '') == 'y'; $data['chat_osc_accept_msg'] = ($this->object->prefs['chat_osc_accept_msg'] ?? '') == 'y'; $data['chat_broadcast_typing'] = ($this->object->prefs['chat_broadcast_typing'] ?? '') == 'y'; - $data['session_reminder_enabled'] = (int) ($this->object->prefs['session_reminder_enabled'] ?? 0); $data['send_mail'] = (($this->object->prefs['send_info_mails'] ?? '') == 'y'); @@ -1258,6 +1253,7 @@ public function initForm(string $a_mode): void || $this->isSettingChangeable('bs_allow_to_contact_me') || $this->isSettingChangeable('chat_osc_accept_msg') || $this->isSettingChangeable('chat_broadcast_typing') + || ($this->isSettingChangeable('session_reminder')) ) { $sec_st = new ilFormSectionHeaderGUI(); $sec_st->setTitle($this->lng->txt('settings')); @@ -1379,10 +1375,25 @@ public function initForm(string $a_mode): void $this->form_gui->addItem($chat_osc_acm); } - if ((int) $this->settings->get('session_reminder_enabled')) { - $cb = new ilCheckboxInputGUI($this->lng->txt('session_reminder'), 'session_reminder_enabled'); - $cb->setValue('1'); - $this->form_gui->addItem($cb); + if ($this->isSettingChangeable('session_reminder')) { + $session_reminder = new ilNumberInputGUI( + $this->lng->txt('session_reminder'), + 'session_reminder_lead_time' + ); + $expires = ilSession::getSessionExpireValue(); + $session_reminder->setInfo( + sprintf( + $this->lng->txt('session_reminder_lead_time_info'), + ilDatePresentation::secondsToString($expires, true) + ) + ); + $session_reminder->setValue( + (string) ilSessionReminder::getGlobalSessionReminderLeadTime() + ); + $session_reminder->setSize(3); + $session_reminder->setMinValue(0); + $session_reminder->setMaxValue(ilSessionReminder::getMaxLeadTime()); + $this->form_gui->addItem($session_reminder); } if ($this->isSettingChangeable('send_mail')) { diff --git a/components/ILIAS/User/classes/class.ilUserDataSet.php b/components/ILIAS/User/classes/class.ilUserDataSet.php index 51137c08f583..4a969307714d 100755 --- a/components/ILIAS/User/classes/class.ilUserDataSet.php +++ b/components/ILIAS/User/classes/class.ilUserDataSet.php @@ -243,7 +243,7 @@ public function readData(string $a_entity, string $a_version, array $a_ids): voi "public_profile", "public_sel_country", "public_street", "public_title", "public_upload", "public_zipcode", "screen_reader_optimization", "show_users_online", "store_last_visited", "time_format", "user_tz", "weekstart", - "session_reminder_enabled", "session_reminder_lead_time", "usr_starting_point", + "session_reminder_lead_time", "usr_starting_point", "chat_broadcast_typing"]; if (version_compare($a_version, '5.2.0', '>=')) { diff --git a/components/ILIAS/User/classes/class.ilUserFieldSettingsTableGUI.php b/components/ILIAS/User/classes/class.ilUserFieldSettingsTableGUI.php index bdcd84204ec7..c23de749ec3c 100755 --- a/components/ILIAS/User/classes/class.ilUserFieldSettingsTableGUI.php +++ b/components/ILIAS/User/classes/class.ilUserFieldSettingsTableGUI.php @@ -193,24 +193,10 @@ protected function fillRow(array $a_set): void break; case 'session_reminder_lead_time': $this->tpl->setCurrentBlock('def_input'); - $expires = ilSession::getSessionExpireValue(); - $min_value = ilSessionReminder::MIN_LEAD_TIME; - $max_value = ilSessionReminder::getMaxLeadTime(); - $current_user_value = $ilSetting->get('session_reminder'); - if ($current_user_value < $min_value || $current_user_value > $max_value) { - $current_user_value = ilSessionReminder::SUGGESTED_LEAD_TIME; - } - $value = min( - max( - $min_value, - $current_user_value - ), - $max_value - ); $this->tpl->setVariable('PROFILE_OPTION_DEFAULT_VALUE', 'default_' . $field); - $this->tpl->setVariable('CURRENT_OPTION_VISIBLE', $value); - $this->tpl->setVariable('CURRENT_OPTION_MAXIMUM', $max_value); + $this->tpl->setVariable('CURRENT_OPTION_VISIBLE', ilSessionReminder::getGlobalSessionReminderLeadTime()); + $this->tpl->setVariable('CURRENT_OPTION_MAXIMUM', ilSessionReminder::getMaxLeadTime()); $this->tpl->parseCurrentBlock();