19 declare(strict_types=1);
52 private \ILIAS\HTTP\GlobalHttpState
$http;
62 if (isset($DIC[
'ilDB'])) {
66 if (isset($DIC[
'tpl'])) {
67 $this->main_tpl = $DIC[
'tpl'];
69 if (isset($DIC[
'http'])) {
70 $this->
http = $DIC[
'http'];
73 if (isset($DIC[
'lng'])) {
74 $this->
lng = $DIC[
'lng'];
77 if (isset($DIC[
'ilLog'])) {
78 $this->log = $DIC[
'ilLog'];
81 if (isset($DIC[
'refinery'])) {
85 if (isset($DIC[
'ilObjDataCache'])) {
89 if (isset($DIC[
'rbacreview'])) {
90 $this->rbac_review = $DIC[
'rbacreview'];
93 if (isset($DIC[
'cron.repository'])) {
94 $this->cronRepository = $DIC[
'cron.repository'];
97 if (isset($DIC[
'ilSetting'])) {
105 $include_roles = $this->
settings->get(
106 'cron_inactive_user_delete_include_roles',
109 if ($include_roles ===
null) {
110 $this->include_roles = [];
112 $this->include_roles = array_filter(
array_map(
'intval', explode(
',', $include_roles)));
116 'cron_inactive_user_delete_period',
117 (
string) self::DEFAULT_INACTIVITY_PERIOD
119 $this->reminder_period = (
int) $this->
settings->get(
120 'cron_inactive_user_reminder_period',
121 (
string) self::DEFAULT_REMINDER_PERIOD
130 $number_as_string = (string) $number;
132 return strpos($number_as_string,
',') || strpos($number_as_string,
'.');
137 $time_difference = 0;
139 switch ($schedule_time) {
140 case JobScheduleType::DAILY:
141 $time_difference = 86400;
143 case JobScheduleType::IN_MINUTES:
144 $time_difference = 60 * $multiplier;
146 case JobScheduleType::IN_HOURS:
147 $time_difference = 3600 * $multiplier;
149 case JobScheduleType::IN_DAYS:
150 $time_difference = 86400 * $multiplier;
152 case JobScheduleType::WEEKLY:
153 $time_difference = 604800;
155 case JobScheduleType::MONTHLY:
156 $time_difference = 2629743;
158 case JobScheduleType::QUARTERLY:
159 $time_difference = 7889229;
162 $time_difference = 31556926;
166 return $time_difference;
171 return "user_inactive";
176 return $this->
lng->txt(
"delete_inactive_user_accounts");
181 return $this->
lng->txt(
"delete_inactive_user_accounts_desc");
186 return JobScheduleType::DAILY;
211 $status = JobResult::STATUS_NO_ACTION;
215 self::ACTION_USER_NONE => 0,
216 self::ACTION_USER_REMINDER_MAIL_SENT => 0,
217 self::ACTION_USER_DELETED => 0
219 foreach ($usr_ids as $usr_id) {
224 foreach ($this->include_roles as $role_id) {
225 if ($this->rbac_review->isAssigned($usr_id, $role_id)) {
227 $counters[$action_taken]++;
233 if ($counters[self::ACTION_USER_REMINDER_MAIL_SENT] > 0
234 || $counters[self::ACTION_USER_DELETED] > 0) {
235 $status = JobResult::STATUS_OK;
238 $this->cron_delete_reminder_mail->removeEntriesFromTableIfLastLoginIsNewer();
240 'CRON - ilCronDeleteInactiveUserAccounts::run(), deleted ' 241 .
"=> {$counters[self::ACTION_USER_DELETED]} User(s), sent reminder " 242 .
"mail to {$counters[self::ACTION_USER_REMINDER_MAIL_SENT]} User(s)" 246 $result->setStatus($status);
254 $timestamp_last_login = strtotime($user->getLastLogin());
255 $grace_period_over = time() - ($this->delete_period * 24 * 60 * 60);
257 if ($timestamp_last_login < $grace_period_over) {
259 return self::ACTION_USER_DELETED;
262 if ($this->reminder_period > 0) {
263 $timestamp_for_deletion = $timestamp_last_login - $grace_period_over;
266 $this->cron_delete_reminder_mail->sendReminderMailIfNeeded(
268 $this->reminder_period,
269 $account_will_be_deleted_on
272 return self::ACTION_USER_REMINDER_MAIL_SENT;
276 return self::ACTION_USER_NONE;
281 $cron_timing = $this->cronRepository->getCronJobData($this->
getId());
282 $time_difference = 0;
285 if (!is_array($cron_timing) || !isset($cron_timing[0]) || !is_array($cron_timing[0])) {
286 return time() + $date_for_deletion + $time_difference;
289 if (array_key_exists(
'schedule_type', $cron_timing[0])) {
290 if ($cron_timing[0][
'schedule_value'] !==
null) {
291 $multiplier = (
int) $cron_timing[0][
'schedule_value'];
298 return time() + $date_for_deletion + $time_difference;
303 $this->
lng->loadLanguageModule(
"user");
306 $schedule->setTitle($this->
lng->txt(
'delete_inactive_user_accounts_frequency'));
307 $schedule->setInfo($this->
lng->txt(
'delete_inactive_user_accounts_frequency_desc'));
310 $this->
lng->txt(
'delete_inactive_user_accounts_include_roles'),
311 'cron_inactive_user_delete_include_roles' 313 $sub_mlist->
setInfo($this->
lng->txt(
'delete_inactive_user_accounts_include_roles_desc'));
315 foreach ($this->rbac_review->getGlobalRoles() as $role_id) {
320 $sub_mlist->setOptions($roles);
321 $setting = $this->
settings->get(
'cron_inactive_user_delete_include_roles',
null);
322 if ($setting ===
null) {
325 $setting = explode(
',', $setting);
327 $sub_mlist->setValue($setting);
328 $sub_mlist->setWidth(300);
331 $default_setting = (string) self::DEFAULT_INACTIVITY_PERIOD;
334 $this->
lng->txt(
'delete_inactive_user_accounts_period'),
335 'cron_inactive_user_delete_period' 338 $sub_text->setInfo($this->
lng->txt(
'delete_inactive_user_accounts_period_desc'));
339 $sub_text->setValue($this->
settings->get(
"cron_inactive_user_delete_period", $default_setting));
340 $sub_text->setSize(4);
341 $sub_text->setMaxLength(4);
342 $sub_text->setRequired(
true);
346 $this->
lng->txt(
'send_mail_to_inactive_users'),
347 'cron_inactive_user_reminder_period' 350 $sub_period->setInfo($this->
lng->txt(
"send_mail_to_inactive_users_desc"));
351 $sub_period->setValue($this->
settings->get(
"cron_inactive_user_reminder_period", $default_setting));
352 $sub_period->setSuffix($this->
lng->txt(
"send_mail_to_inactive_users_suffix"));
353 $sub_period->setSize(4);
354 $sub_period->setMaxLength(4);
355 $sub_period->setRequired(
false);
356 $sub_period->setMinValue(0);
362 $this->
lng->loadLanguageModule(
"user");
371 $cron_period_custom = 0;
373 $reminder_period =
'';
375 $empty_string_trafo = $this->
refinery->custom()->transformation(
static function ($value):
string {
380 throw new Exception(
'The value to be transformed is not an empty string');
383 if ($this->
http->wrapper()->post()->has(
'sdyi')) {
384 $cron_period_custom = $this->
http->wrapper()->post()->retrieve(
387 $this->refinery->kindlyTo()->int(),
393 if ($this->
http->wrapper()->post()->has(
'cron_inactive_user_delete_period')) {
394 $delete_period = $this->
http->wrapper()->post()->retrieve(
395 'cron_inactive_user_delete_period',
397 $this->refinery->kindlyTo()->int(),
399 $this->
refinery->kindlyTo()->float(),
406 if ($this->
http->wrapper()->post()->has(
'cron_inactive_user_reminder_period')) {
407 $reminder_period = $this->
http->wrapper()->post()->retrieve(
408 'cron_inactive_user_reminder_period',
411 $this->refinery->byTrying([
412 $this->refinery->kindlyTo()->int(),
414 $this->
refinery->kindlyTo()->float(),
425 $this->
lng->txt(
'send_mail_to_inactive_users_numbers_only')
429 if ($this->
isDecimal($reminder_period)) {
432 $this->
lng->txt(
'send_mail_to_inactive_users_numbers_only')
436 if ($reminder_period >= $delete_period) {
439 $this->
lng->txt(
'send_mail_to_inactive_users_must_be_smaller_than')
443 if ($cron_period->value >= JobScheduleType::IN_DAYS->value &&
447 if ($cron_period === JobScheduleType::IN_DAYS) {
448 if ($check_window_logic < $cron_period_custom) {
451 } elseif ($cron_period === JobScheduleType::WEEKLY) {
452 if ($check_window_logic <= 7) {
455 } elseif ($cron_period === JobScheduleType::MONTHLY) {
456 if ($check_window_logic <= 31) {
459 } elseif ($cron_period === JobScheduleType::QUARTERLY) {
460 if ($check_window_logic <= 92) {
464 if ($check_window_logic <= 366) {
472 $this->
lng->txt(
'send_mail_reminder_window_too_small')
477 if ($delete_period > 0) {
478 $roles = implode(
',', $this->
http->wrapper()->post()->retrieve(
479 'cron_inactive_user_delete_include_roles',
481 $this->refinery->kindlyTo()->listOf($this->
refinery->kindlyTo()->int()),
486 $this->
settings->set(
'cron_inactive_user_delete_include_roles', $roles);
487 $this->
settings->set(
'cron_inactive_user_delete_period', (
string) $delete_period);
490 if ($this->reminder_period > $reminder_period) {
491 $this->cron_delete_reminder_mail->flushDataTable();
494 $this->
settings->set(
'cron_inactive_user_reminder_period', (
string) $reminder_period);
497 $this->main_tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"form_input_not_valid"));
const ACTION_USER_REMINDER_MAIL_SENT
ilGlobalTemplateInterface $main_tpl
static getUserIdsByInactivityPeriod(int $periodInDays)
Get ids of all users that have been inactive for at least the given period.
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
deleteUserOrSendReminderMail($usr_id)
Component logger with individual log levels by component id.
ilObjectDataCache $objectDataCache
ilCronDeleteInactiveUserReminderMail $cron_delete_reminder_mail
ILIAS Refinery Factory $refinery
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static http()
Fetches the global http state from ILIAS.
const DEFAULT_REMINDER_PERIOD
getDefaultScheduleValue()
ilRbacReview $rbac_review
saveCustomSettings(ilPropertyFormGUI $a_form)
addCustomSettingsToForm(ilPropertyFormGUI $a_form)
const ACTION_USER_DELETED
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
calculateDeletionData(int $date_for_deletion)
JobRepository $cronRepository
ILIAS HTTP GlobalHttpState $http
const DEFAULT_INACTIVITY_PERIOD
getTimeDifferenceBySchedule(JobScheduleType $schedule_time, int $multiplier)