Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Feature: Session Reminder and Configuration as a User Default Settings #7841

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 58 additions & 22 deletions components/ILIAS/Authentication/classes/class.ilSessionReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,35 @@

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 LEAD_TIME_DISABLED = 0;
public const MIN_LEAD_TIME = 1;
public const SUGGESTED_LEAD_TIME = 5;

private ClockInterface $clock;
private ilObjUser $user;
private ilSetting $settings;
private int $lead_time = self::SUGGESTED_LEAD_TIME;
private int $expiration_time = 0;
private int $current_time = 0;
private int $seconds_until_expiration = 0;
private int $seconds_until_reminder = 0;

public function __construct(
ilObjUser $user,
ClockInterface $clock,
ilSetting $settings
) {
$this->user = $user;
$this->clock = $clock;
$this->settings = $settings;

$this->init();
}

public static function byLoggedInUser(): self
{
global $DIC;
Expand All @@ -47,37 +60,62 @@ public static function byLoggedInUser(): self

$reminder = new self(
$user,
(new DataFactory())->clock()->utc()
(new DataFactory())->clock()->utc(),
$DIC->settings()
);

return $reminder;
}

public static function isGloballyActivated(): bool
public function getGlobalSessionReminderLeadTime(): int
{
/** @var ilSetting $ilSetting */
global $DIC;
return $this->buildValidLeadTime(
(int) $this->settings->get('session_reminder_lead_time')
);
}

$ilSetting = $DIC['ilSetting'];
private function buildValidLeadTime(int $lead_time): int
{
$min_value = self::MIN_LEAD_TIME;
$max_value = $this->getMaxPossibleLeadTime();

if (
$lead_time !== self::LEAD_TIME_DISABLED &&
($lead_time < $min_value || $lead_time > $max_value)
) {
$lead_time = self::SUGGESTED_LEAD_TIME;
}

return (bool) $ilSetting->get('session_reminder_enabled');
return $lead_time !== self::LEAD_TIME_DISABLED ? min(
max(
$min_value,
$lead_time
),
$max_value
) : self::LEAD_TIME_DISABLED;
}

public function __construct(ilObjUser $user, ClockInterface $clock)
public function getEffectiveLeadTime(): int
{
$this->user = $user;
$this->clock = $clock;
return $this->buildValidLeadTime(
(int) ilObjUser::_lookupPref(
$this->getUser()->getId(),
'session_reminder_lead_time'
) ?: $this->getGlobalSessionReminderLeadTime()
);
}

$this->init();
public function getMaxPossibleLeadTime(): int
{
$expires = ilSession::getSessionExpireValue();

return max(self::MIN_LEAD_TIME, ($expires / 60) - 1);
}

private function init(): void
{
$this->setLeadTime(
((int) max(
self::MIN_LEAD_TIME,
(float) $this->getUser()->getPref('session_reminder_lead_time')
)) * 60
$this->getEffectiveLeadTime() * 60
);

$this->setExpirationTime(ilSession::getIdleValue() + $this->clock->now()->getTimestamp());
Expand All @@ -104,13 +142,11 @@ 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') &&
$this->isEnoughTimeLeftForReminder()
);
$this->getEffectiveLeadTime() !== self::LEAD_TIME_DISABLED &&
$this->isEnoughTimeLeftForReminder();
}

public function setUser(ilObjUser $user): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
ilDBInterface $db,
ilIniFile $clientIni,
ilLogger $logger,
ClockInterface $utcClock
ClockInterface $utcClock,
) {
$this->http = $http;
$this->refinery = $refinery;
Expand Down Expand Up @@ -93,13 +93,13 @@ public function handle(): ResponseInterface
return $this->toJsonResponse($response);
}

$expirationTime = (int) $data['expires'];
if (null === $expirationTime) {
$expiration_time = (int) $data['expires'];
if (null === $expiration_time) {
$response['message'] = 'ILIAS could not determine the expiration time from the session data.';
return $this->toJsonResponse($response);
}

if ($this->isSessionAlreadyExpired($expirationTime)) {
if ($this->isSessionAlreadyExpired($expiration_time)) {
$response['message'] = 'The session is already expired. The client should have received a remind command before.';
return $this->toJsonResponse($response);
}
Expand All @@ -111,18 +111,16 @@ public function handle(): ResponseInterface
return $this->toJsonResponse($response);
}

$reminderTime = $expirationTime - ((int) max(
ilSessionReminder::MIN_LEAD_TIME,
(float) $ilUser->getPref('session_reminder_lead_time')
)) * 60;
if ($reminderTime > $this->clock->now()->getTimestamp()) {
$session_reminder = ilSessionReminder::byLoggedInUser();
$reminder_time = $expiration_time - ($session_reminder->getEffectiveLeadTime() * 60);
if ($reminder_time > $this->clock->now()->getTimestamp()) {
// session will expire in <lead_time> minutes
$response['message'] = 'Lead time not reached, yet. Current time: ' .
date('Y-m-d H:i:s') . ', Reminder time: ' . date('Y-m-d H:i:s', $reminderTime);
date('Y-m-d H:i:s') . ', Reminder time: ' . date('Y-m-d H:i:s', $reminder_time);
return $this->toJsonResponse($response);
}

$dateTime = new ilDateTime($expirationTime, IL_CAL_UNIX);
$dateTime = new ilDateTime($expiration_time, IL_CAL_UNIX);
switch ($ilUser->getTimeFormat()) {
case ilCalendarSettings::TIME_FORMAT_12:
$formatted_expiration_time = $dateTime->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
Expand All @@ -141,7 +139,7 @@ public function handle(): ResponseInterface
'%0A',
sprintf(
$this->lng->txt('session_reminder_alert'),
ilDatePresentation::secondsToString($expirationTime - $this->clock->now()->getTimestamp()),
ilDatePresentation::secondsToString($expiration_time - $this->clock->now()->getTimestamp()),
$formatted_expiration_time,
$this->clientIni->readVariable('client', 'name') . ' | ' . ilUtil::_getHttpPath()
)
Expand Down
2 changes: 1 addition & 1 deletion components/ILIAS/Authentication/resources/sessioncheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
$DIC->database(),
$DIC['ilClientIniFile'],
$DIC->logger()->auth(),
(new DataFactory())->clock()->utc()
(new DataFactory())->clock()->utc(),
)
)->handle()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,46 +276,29 @@ public function initGeneralSettingsForm(): void
$lv->setValue($last_visited);
$this->form->addItem($lv);

if (ilSessionReminder::isGloballyActivated()) {
$cb = new ilCheckboxInputGUI($this->lng->txt('session_reminder'), 'session_reminder_enabled');
$cb->setInfo($this->lng->txt('session_reminder_info'));
$cb->setValue('1');
$cb->setChecked((bool) $this->user->getPref('session_reminder_enabled'));

$expires = ilSession::getSessionExpireValue();
$lead_time_gui = new ilNumberInputGUI(
$this->lng->txt('session_reminder_lead_time'),
if ($this->userSettingVisible('session_reminder')) {
$session_reminder = new ilNumberInputGUI(
$this->lng->txt('session_reminder_input'),
'session_reminder_lead_time'
);
$lead_time_gui->setInfo(
$session_reminder_object = ilSessionReminder::byLoggedInUser();
$expires = ilSession::getSessionExpireValue();
$session_reminder->setInfo(
sprintf(
$this->lng->txt('session_reminder_lead_time_info'),
ilSessionReminder::LEAD_TIME_DISABLED,
ilSessionReminder::SUGGESTED_LEAD_TIME,
ilDatePresentation::secondsToString($expires, true)
)
);

$min_value = ilSessionReminder::MIN_LEAD_TIME;
$max_value = max($min_value, ($expires / 60) - 1);

$current_user_value = $this->user->getPref('session_reminder_lead_time');
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->setDisabled(!$this->workWithUserSetting('session_reminder'));
$session_reminder->setValue(
(string) $session_reminder_object->getEffectiveLeadTime()
);

$lead_time_gui->setValue((string) $value);
$lead_time_gui->setSize(3);
$lead_time_gui->setMinValue($min_value);
$lead_time_gui->setMaxValue($max_value);
$cb->addSubItem($lead_time_gui);

$this->form->addItem($cb);
$session_reminder->setSize(3);
$session_reminder->setMinValue(ilSessionReminder::LEAD_TIME_DISABLED);
$session_reminder->setMaxValue($session_reminder_object->getMaxPossibleLeadTime());
$this->form->addItem($session_reminder);
}

// calendar settings (copied here to be reachable when calendar is inactive)
Expand Down Expand Up @@ -434,8 +417,7 @@ public function saveGeneralSettings(): void
}
}

if (ilSessionReminder::isGloballyActivated()) {
$this->user->setPref('session_reminder_enabled', $this->form->getInput('session_reminder_enabled'));
if ($this->workWithUserSetting('session_reminder')) {
$this->user->setPref(
'session_reminder_lead_time',
(string) $this->form->getInput('session_reminder_lead_time')
Expand Down
55 changes: 5 additions & 50 deletions components/ILIAS/User/classes/class.ilObjUserFolderGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use ILIAS\User\Profile\ChangeListeners\UserFieldAttributesChangeListener;
use ILIAS\User\Profile\ChangeListeners\InterestedUserFieldChangeListener;
use ILIAS\User\Profile\ChangeListeners\ChangedUserFieldAttribute;

use ILIAS\DI\Container as DIContainer;
use ILIAS\Filesystem\Filesystem;
use ILIAS\FileUpload\FileUpload;
Expand Down Expand Up @@ -1746,8 +1745,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'),
Expand Down Expand Up @@ -1896,14 +1893,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')
Expand Down Expand Up @@ -2035,45 +2024,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') . '<br />' .
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();
Expand Down Expand Up @@ -2447,6 +2397,11 @@ public function saveGlobalUserSettingsObject(string $action = ''): void
}
}

$this->ilias->setSetting(
'session_reminder_lead_time',
$this->user_request->getDefaultSessionReminder()
);

if (isset($checked['export_preferences']) && $checked['export_preferences'] === 1) {
$this->ilias->setSetting(
'usr_settings_export_preferences',
Expand Down
Loading
Loading