ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCronDeleteInactiveUserAccounts.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/Cron/classes/class.ilCronJob.php";
5 include_once 'Services/Mail/classes/class.ilMimeMail.php';
6 include_once 'Services/User/classes/class.ilCronDeleteInactiveUserReminderMail.php';
7 
18 {
21 
22  private $period = null;
23  private $reminderTimer = null;
24  private $include_roles = null;
25 
26  public function __construct()
27  {
28  global $DIC;
29 
30  $ilSetting = $DIC['ilSetting'];
31 
32  if (is_object($ilSetting)) {
33  $this->include_roles = $ilSetting->get(
34  'cron_inactive_user_delete_include_roles',
35  null
36  );
37  if ($this->include_roles === null) {
38  $this->include_roles = array();
39  } else {
40  $this->include_roles = explode(',', $this->include_roles);
41  }
42 
43  $this->period = $ilSetting->get(
44  'cron_inactive_user_delete_period',
45  self::DEFAULT_INACTIVITY_PERIOD
46  );
47  $this->reminderTimer = $ilSetting->get(
48  'cron_inactive_user_reminder_period',
49  self::DEFAULT_REMINDER_PERIOD
50  );
51  }
52  }
53 
59  protected function getTimeDifferenceBySchedule($schedule_time, $multiplier)
60  {
61  $time_difference = 0;
62  switch ($schedule_time) {
64  $time_difference = 86400;
65  break;
67  $time_difference = 60 * $multiplier;
68  break;
70  $time_difference = 3600 * $multiplier;
71  break;
73  $time_difference = 86400 * $multiplier;
74  break;
76  $time_difference = 604800;
77  break;
79  $time_difference = 2629743;
80  break;
82  $time_difference = 7889229;
83  break;
85  $time_difference = 31556926;
86  break;
87  }
88  return $time_difference;
89  }
90 
91  public function getId()
92  {
93  return "user_inactive";
94  }
95 
96  public function getTitle()
97  {
98  global $DIC;
99 
100  $lng = $DIC['lng'];
101 
102  return $lng->txt("delete_inactive_user_accounts");
103  }
104 
105  public function getDescription()
106  {
107  global $DIC;
108 
109  $lng = $DIC['lng'];
110 
111  return $lng->txt("delete_inactive_user_accounts_desc");
112  }
113 
114  public function getDefaultScheduleType()
115  {
116  return self::SCHEDULE_TYPE_DAILY;
117  }
118 
119  public function getDefaultScheduleValue()
120  {
121  return;
122  }
123 
124  public function hasAutoActivation()
125  {
126  return false;
127  }
128 
129  public function hasFlexibleSchedule()
130  {
131  return true;
132  }
133 
134  public function hasCustomSettings()
135  {
136  return true;
137  }
138 
139  public function run()
140  {
141  global $DIC;
142 
143  $rbacreview = $DIC['rbacreview'];
144  $ilLog = $DIC['ilLog'];
145 
147  $reminder_time = (int) $this->reminderTimer;
148  $checkMail = (int) $this->period - $reminder_time;
149  $usr_ids = ilObjUser::getUserIdsByInactivityPeriod($checkMail);
150  $counter = 0;
151  $userDeleted = 0;
152  $userMailsDelivered = 0;
153  foreach ($usr_ids as $usr_id) {
154  if ($usr_id == ANONYMOUS_USER_ID || $usr_id == SYSTEM_USER_ID) {
155  continue;
156  }
157 
158  $continue = true;
159  foreach ($this->include_roles as $role_id) {
160  if ($rbacreview->isAssigned($usr_id, $role_id)) {
161  $continue = false;
162  break;
163  }
164  }
165  if ($continue) {
166  continue;
167  }
168 
173  $user = ilObjectFactory::getInstanceByObjId($usr_id);
174  $timestamp_last_login = strtotime($user->getLastLogin());
175  $grace_period_over = time() - ((int) $this->period * 24 * 60 * 60);
176  if ($timestamp_last_login < $grace_period_over) {
177  $user->delete();
178  $userDeleted++;
179  } elseif ($reminder_time > 0) {
180  $timestamp_for_deletion = $timestamp_last_login - $grace_period_over;
181  $account_will_be_deleted_on = $this->calculateDeletionData($timestamp_for_deletion);
182  $mailSent = ilCronDeleteInactiveUserReminderMail::checkIfReminderMailShouldBeSend($user, $reminder_time, $account_will_be_deleted_on);
183  if ($mailSent) {
184  $userMailsDelivered++;
185  }
186  }
187  $counter++;
188  }
189 
190  if ($counter) {
191  $status = ilCronJobResult::STATUS_OK;
192  }
194  $ilLog->write("CRON - ilCronDeleteInactiveUserAccounts::run(), deleted => $userDeleted User(s), sent reminder mail to $userMailsDelivered User(s)");
195  $result = new ilCronJobResult();
196  $result->setStatus($status);
197  return $result;
198  }
199 
200  protected function calculateDeletionData($date_for_deletion)
201  {
202  $cron_timing = ilCronManager::getCronJobData($this->getId());
203  $time_difference = 0;
204  $multiplier = 1;
205 
206  if (!is_array($cron_timing) || !is_array($cron_timing[0])) {
207  return time() + $date_for_deletion + $time_difference;
208  }
209 
210  if (array_key_exists('schedule_type', $cron_timing[0])) {
211  if ($cron_timing[0]['schedule_value'] != null) {
212  $multiplier = $cron_timing[0]['schedule_value'];
213  }
214  $time_difference = $this->getTimeDifferenceBySchedule($cron_timing[0]['schedule_type'], $multiplier);
215  }
216  return time() + $date_for_deletion + $time_difference;
217  }
218 
220  {
221  global $DIC;
222 
223  $lng = $DIC['lng'];
224  $rbacreview = $DIC['rbacreview'];
225  $ilObjDataCache = $DIC['ilObjDataCache'];
226  $ilSetting = $DIC['ilSetting'];
227  $lng->loadLanguageModule("user");
228 
229  $schedule = $a_form->getItemByPostVar('type');
230  $schedule->setTitle($lng->txt('delete_inactive_user_accounts_frequency'));
231  $schedule->setInfo($lng->txt('delete_inactive_user_accounts_frequency_desc'));
232 
233  include_once('Services/Form/classes/class.ilMultiSelectInputGUI.php');
234  $sub_mlist = new ilMultiSelectInputGUI(
235  $lng->txt('delete_inactive_user_accounts_include_roles'),
236  'cron_inactive_user_delete_include_roles'
237  );
238  $sub_mlist->setInfo($lng->txt('delete_inactive_user_accounts_include_roles_desc'));
239  $roles = array();
240  foreach ($rbacreview->getGlobalRoles() as $role_id) {
241  if ($role_id != ANONYMOUS_ROLE_ID) {
242  $roles[$role_id] = $ilObjDataCache->lookupTitle($role_id);
243  }
244  }
245  $sub_mlist->setOptions($roles);
246  $setting = $ilSetting->get('cron_inactive_user_delete_include_roles', null);
247  if ($setting === null) {
248  $setting = array();
249  } else {
250  $setting = explode(',', $setting);
251  }
252  $sub_mlist->setValue($setting);
253  $sub_mlist->setWidth(300);
254  $a_form->addItem($sub_mlist);
255 
256  $default_setting = self::DEFAULT_INACTIVITY_PERIOD;
257  $sub_text = new ilNumberInputGUI(
258  $lng->txt('delete_inactive_user_accounts_period'),
259  'cron_inactive_user_delete_period'
260  );
261  $sub_text->setInfo($lng->txt('delete_inactive_user_accounts_period_desc'));
262  $sub_text->setValue($ilSetting->get("cron_inactive_user_delete_period", $default_setting));
263  $sub_text->setSize(4);
264  $sub_text->setMaxLength(4);
265  $sub_text->setRequired(true);
266  $a_form->addItem($sub_text);
267 
268  $sub_period = new ilNumberInputGUI(
269  $lng->txt('send_mail_to_inactive_users'),
270  'cron_inactive_user_reminder_period'
271  );
272  $sub_period->setInfo($lng->txt("send_mail_to_inactive_users_desc"));
273  $sub_period->setValue($ilSetting->get("cron_inactive_user_reminder_period", $default_setting));
274  $sub_period->setSuffix($lng->txt("send_mail_to_inactive_users_suffix"));
275  $sub_period->setSize(4);
276  $sub_period->setMaxLength(4);
277  $sub_period->setRequired(false);
278  $sub_period->setMinValue(0);
279  $a_form->addItem($sub_period);
280  }
281 
282  public function saveCustomSettings(ilPropertyFormGUI $a_form)
283  {
284  global $DIC;
285 
286  $ilSetting = $DIC['ilSetting'];
287  $lng = $DIC['lng'];
288  $lng->loadLanguageModule("user");
289  $setting = implode(',', $_POST['cron_inactive_user_delete_include_roles']);
290  if (!strlen($setting)) {
291  $setting = null;
292  }
293 
294  $valid = true;
295  $delete_period = ilUtil::stripSlashes($_POST['cron_inactive_user_delete_period']);
296  $reminder_period = ilUtil::stripSlashes($_POST['cron_inactive_user_reminder_period']);
297  $cron_period = (int) ilUtil::stripSlashes($_POST['type']);
298  $cron_period_custom = (int) ilUtil::stripSlashes($_POST['sdyi']);
299 
300  if ($this->isDecimal($delete_period)) {
301  $valid = false;
302  $a_form->getItemByPostVar('cron_inactive_user_delete_period')->setAlert($lng->txt('send_mail_to_inactive_users_numbers_only'));
303  }
304  if ($this->isDecimal($reminder_period)) {
305  $valid = false;
306  $a_form->getItemByPostVar('cron_inactive_user_reminder_period')->setAlert($lng->txt('send_mail_to_inactive_users_numbers_only'));
307  }
308  if ($reminder_period >= $delete_period) {
309  $valid = false;
310  $a_form->getItemByPostVar('cron_inactive_user_reminder_period')->setAlert($lng->txt('send_mail_to_inactive_users_must_be_smaller_than'));
311  }
312  if ($cron_period >= ilCronJob::SCHEDULE_TYPE_IN_DAYS && $cron_period <= ilCronJob::SCHEDULE_TYPE_YEARLY && $reminder_period > 0) {
313  $logic = true;
314  $check_window_logic = $delete_period - $reminder_period;
315  if ($cron_period == ilCronJob::SCHEDULE_TYPE_IN_DAYS) {
316  if ($check_window_logic < $cron_period_custom) {
317  $logic = false;
318  }
319  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_WEEKLY) {
320  if ($check_window_logic <= 7) {
321  $logic = false;
322  }
323  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_MONTHLY) {
324  if ($check_window_logic <= 31) {
325  $logic = false;
326  }
327  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_QUARTERLY) {
328  if ($check_window_logic <= 92) {
329  $logic = false;
330  }
331  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_YEARLY) {
332  if ($check_window_logic <= 366) {
333  $logic = false;
334  }
335  }
336  if (!$logic) {
337  $valid = false;
338  $a_form->getItemByPostVar('cron_inactive_user_reminder_period')->setAlert($lng->txt('send_mail_reminder_window_too_small'));
339  }
340  }
341  if ($_POST['cron_inactive_user_delete_period']) {
342  $ilSetting->set('cron_inactive_user_delete_include_roles', $setting);
343  $ilSetting->set('cron_inactive_user_delete_period', $_POST['cron_inactive_user_delete_period']);
344  }
345  if ($this->reminderTimer > $reminder_period) {
347  }
348  $ilSetting->set('cron_inactive_user_reminder_period', $reminder_period);
349 
350  if (!$valid) {
351  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
352  return false;
353  }
354 
355  return true;
356  }
357 
358  protected function isDecimal($number)
359  {
360  if (strpos($number, ',') || strpos($number, '.')) {
361  return true;
362  }
363  return false;
364  }
365 }
run()
Run job.
getItemByPostVar($a_post_var)
Get Item by POST variable.
$result
static getUserIdsByInactivityPeriod(int $periodInDays)
Get ids of all users that have been inactive for at least the given period.
This class represents a property form user interface.
Cron job application base class.
$valid
const SCHEDULE_TYPE_IN_MINUTES
addItem($a_item)
Add Item (Property, SectionHeader).
const SCHEDULE_TYPE_MONTHLY
const SCHEDULE_TYPE_WEEKLY
setInfo($a_info)
Set Information Text.
This class represents a multi selection list property in a property form.
static getCronJobData($a_id=null, $a_include_inactive=true)
Get cron job configuration/execution data.
static checkIfReminderMailShouldBeSend(ilObjUser $user, $reminderTime, $time_frame_for_deletion)
$lng
This class represents a number property in a property form.
const SCHEDULE_TYPE_IN_DAYS
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
const SCHEDULE_TYPE_YEARLY
global $ilSetting
Definition: privfeed.php:17
const SCHEDULE_TYPE_DAILY
$DIC
Definition: xapitoken.php:46
const SCHEDULE_TYPE_QUARTERLY
Cron job result data container.
const SCHEDULE_TYPE_IN_HOURS
$_POST["username"]