ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups 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 $ilSetting;
29 
30  if(is_object($ilSetting))
31  {
32  $this->include_roles = $ilSetting->get(
33  'cron_inactive_user_delete_include_roles', null
34  );
35  if($this->include_roles === null) $this->include_roles = array();
36  else $this->include_roles = explode(',', $this->include_roles);
37 
38  $this->period = $ilSetting->get(
39  'cron_inactive_user_delete_period',
40  self::DEFAULT_INACTIVITY_PERIOD
41  );
42  $this->reminderTimer = $ilSetting->get(
43  'cron_inactive_user_reminder_period',
44  self::DEFAULT_REMINDER_PERIOD
45  );
46  }
47  }
48 
54  protected function getTimeDifferenceBySchedule($schedule_time, $multiplier)
55  {
56  $time_difference = 0;
57  switch($schedule_time)
58  {
60  $time_difference = 86400;
61  break;
63  $time_difference = 60 * $multiplier;
64  break;
66  $time_difference = 3600 * $multiplier;
67  break;
69  $time_difference = 86400 * $multiplier;
70  break;
72  $time_difference = 604800;
73  break;
75  $time_difference = 2629743;
76  break;
78  $time_difference = 7889229;
79  break;
81  $time_difference = 31556926;
82  break;
83  }
84  return $time_difference;
85  }
86 
87  public function getId()
88  {
89  return "user_inactive";
90  }
91 
92  public function getTitle()
93  {
94  global $lng;
95 
96  return $lng->txt("delete_inactive_user_accounts");
97  }
98 
99  public function getDescription()
100  {
101  global $lng;
102 
103  return $lng->txt("delete_inactive_user_accounts_desc");
104  }
105 
106  public function getDefaultScheduleType()
107  {
109  }
110 
111  public function getDefaultScheduleValue()
112  {
113  return;
114  }
115 
116  public function hasAutoActivation()
117  {
118  return false;
119  }
120 
121  public function hasFlexibleSchedule()
122  {
123  return true;
124  }
125 
126  public function hasCustomSettings()
127  {
128  return true;
129  }
130 
131  public function run()
132  {
133  global $rbacreview, $ilLog;
134 
136  $reminder_time = (int)$this->reminderTimer;
137  $checkMail = (int)$this->period - $reminder_time;
138  $usr_ids = ilObjUser::_getUserIdsByInactivityPeriod($checkMail);
139  $counter = 0;
140  $userDeleted = 0;
141  $userMailsDelivered = 0;
142  foreach($usr_ids as $usr_id)
143  {
144  if($usr_id == ANONYMOUS_USER_ID || $usr_id == SYSTEM_USER_ID) continue;
145 
146  $continue = true;
147  foreach($this->include_roles as $role_id)
148  {
149  if( $rbacreview->isAssigned($usr_id, $role_id) )
150  {
151  $continue = false;
152  break;
153  }
154  }
155  if($continue)
156  {
157  continue;
158  }
159 
164  $user = ilObjectFactory::getInstanceByObjId($usr_id);
165  $timestamp_last_login = strtotime($user->getLastLogin());
166  $grace_period_over = time() - ((int)$this->period * 24 * 60 * 60);
167  if($timestamp_last_login < $grace_period_over)
168  {
169  $user->delete();
170  $userDeleted++;
171  }
172  else if($reminder_time > 0)
173  {
174  $timestamp_for_deletion = $timestamp_last_login - $grace_period_over;
175  $account_will_be_deleted_on = $this->calculateDeletionData($timestamp_for_deletion);
176  $mailSent = ilCronDeleteInactiveUserReminderMail::checkIfReminderMailShouldBeSend($user, $reminder_time, $account_will_be_deleted_on);
177  if($mailSent)
178  {
179  $userMailsDelivered++;
180  }
181  }
182  $counter++;
183  }
184 
185  if($counter)
186  {
187  $status = ilCronJobResult::STATUS_OK;
188  }
190  $ilLog->write("CRON - ilCronDeleteInactiveUserAccounts::run(), deleted => $userDeleted User(s), sent reminder mail to $userMailsDelivered User(s)");
191  $result = new ilCronJobResult();
192  $result->setStatus($status);
193  return $result;
194  }
195 
196  protected function calculateDeletionData($date_for_deletion)
197  {
198  $cron_timing = ilCronManager::getCronJobData($this->getId());
199  $time_difference = 0;
200  $multiplier = 1;
201 
202  if(!is_array($cron_timing) || !is_array($cron_timing[0]))
203  {
204  return time() + $date_for_deletion + $time_difference;
205  }
206 
207  if(array_key_exists('schedule_type', $cron_timing[0]))
208  {
209  if((array_key_exists('schedule_type', $cron_timing[0]) && $cron_timing[0]['schedule_value'] != null))
210  {
211  $multiplier = $cron_timing[0]['schedule_value'];
212  }
213  $time_difference = $this->getTimeDifferenceBySchedule($cron_timing[0]['schedule_type'], $multiplier);
214  }
215  return time() + $date_for_deletion + $time_difference;
216  }
217 
219  {
220  global $lng, $rbacreview, $ilObjDataCache, $ilSetting;
221  $lng->loadLanguageModule("user");
222 
223  $schedule = $a_form->getItemByPostVar('type');
224  $schedule->setTitle($lng->txt('delete_inactive_user_accounts_frequency'),
225  'delete_inactive_user_accounts_frequency');
226  $schedule->setInfo($lng->txt('delete_inactive_user_accounts_frequency_desc'),
227  'delete_inactive_user_accounts_frequency_desc');
228 
229  include_once('Services/Form/classes/class.ilMultiSelectInputGUI.php');
230  $sub_mlist = new ilMultiSelectInputGUI(
231  $lng->txt('delete_inactive_user_accounts_include_roles'),
232  'cron_inactive_user_delete_include_roles'
233  );
234  $sub_mlist->setInfo($lng->txt('delete_inactive_user_accounts_include_roles_desc'));
235  $roles = array();
236  foreach($rbacreview->getGlobalRoles() as $role_id)
237  {
238  if( $role_id != ANONYMOUS_ROLE_ID )
239  $roles[$role_id] = $ilObjDataCache->lookupTitle($role_id);
240  }
241  $sub_mlist->setOptions($roles);
242  $setting = $ilSetting->get('cron_inactive_user_delete_include_roles', null);
243  if($setting === null) $setting = array();
244  else $setting = explode(',', $setting);
245  $sub_mlist->setValue($setting);
246  $sub_mlist->setWidth(300);
247  #$sub_mlist->setHeight(100);
248  $a_form->addItem($sub_mlist);
249 
250  $default_setting = self::DEFAULT_INACTIVITY_PERIOD;
251  $sub_text = new ilNumberInputGUI(
252  $lng->txt('delete_inactive_user_accounts_period'),
253  'cron_inactive_user_delete_period'
254  );
255  $sub_text->setInfo($lng->txt('delete_inactive_user_accounts_period_desc'));
256  $sub_text->setValue($ilSetting->get("cron_inactive_user_delete_period", $default_setting));
257  $sub_text->setSize(4);
258  $sub_text->setMaxLength(4);
259  $sub_text->setRequired(true);
260  $a_form->addItem($sub_text);
261 
262  $sub_period = new ilNumberInputGUI(
263  $lng->txt('send_mail_to_inactive_users'),
264  'cron_inactive_user_reminder_period'
265  );
266  $sub_period->setInfo($lng->txt("send_mail_to_inactive_users_desc"));
267  $sub_period->setValue($ilSetting->get("cron_inactive_user_reminder_period", $default_setting));
268  $sub_period->setSuffix($lng->txt("send_mail_to_inactive_users_suffix"));
269  $sub_period->setSize(4);
270  $sub_period->setMaxLength(4);
271  $sub_period->setRequired(false);
272  $sub_period->setMinValue(0);
273  $a_form->addItem($sub_period);
274  /*
275  $default_setting = ilCronDeleteInactiveUserAccounts::DEFAULT_SETTING_INCLUDE_ADMINS;
276  $sub_cb = new ilCheckboxInputGUI($lng->txt('delete_inactive_user_accounts_include_admins'),'cron_inactive_user_delete_include_admins');
277  $sub_cb->setChecked($ilSetting->get("cron_inactive_user_delete_include_admins", $default_setting) ? 1 : 0 );
278  //$sub_cb->setOptionTitle($lng->txt('delete_inactive_user_accounts_include_admins'));
279  $sub_cb->setInfo($lng->txt('delete_inactive_user_accounts_include_admins_desc'));
280  $a_form->addItem($sub_cb);
281  */
282  }
283 
284  public function saveCustomSettings(ilPropertyFormGUI $a_form)
285  {
286  global $ilSetting, $lng;
287  $lng->loadLanguageModule("user");
288  $setting = implode(',', $_POST['cron_inactive_user_delete_include_roles']);
289  if(!strlen($setting))
290  {
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  {
302  $valid = false;
303  $a_form->getItemByPostVar('cron_inactive_user_delete_period')->setAlert($lng->txt('send_mail_to_inactive_users_numbers_only'),'send_mail_to_inactive_users_numbers_only');
304  }
305  if($this->isDecimal($reminder_period))
306  {
307  $valid = false;
308  $a_form->getItemByPostVar('cron_inactive_user_reminder_period')->setAlert($lng->txt('send_mail_to_inactive_users_numbers_only'),'send_mail_to_inactive_users_numbers_only');
309  }
310  if($reminder_period >= $delete_period)
311  {
312  $valid = false;
313  $a_form->getItemByPostVar('cron_inactive_user_reminder_period')->setAlert($lng->txt('send_mail_to_inactive_users_must_be_smaller_than'),'send_mail_to_inactive_users_must_be_smaller_than');
314  }
315  if($cron_period >= ilCronJob::SCHEDULE_TYPE_IN_DAYS && $cron_period <= ilCronJob::SCHEDULE_TYPE_YEARLY && $reminder_period > 0)
316  {
317  $logic = true;
318  $check_window_logic = $delete_period - $reminder_period;
319  if($cron_period == ilCronJob::SCHEDULE_TYPE_IN_DAYS)
320  {
321  if($check_window_logic < $cron_period_custom)
322  {
323  $logic = false;
324  }
325  }
326  else if($cron_period == ilCronJob::SCHEDULE_TYPE_WEEKLY)
327  {
328  if($check_window_logic <= 7)
329  {
330  $logic = false;
331  }
332  }
333  else if($cron_period == ilCronJob::SCHEDULE_TYPE_MONTHLY)
334  {
335  if($check_window_logic <= 31)
336  {
337  $logic = false;
338  }
339  }
340  else if($cron_period == ilCronJob::SCHEDULE_TYPE_QUARTERLY)
341  {
342  if($check_window_logic <= 92)
343  {
344  $logic = false;
345  }
346  }
347  else if($cron_period == ilCronJob::SCHEDULE_TYPE_YEARLY)
348  {
349  if($check_window_logic <= 366)
350  {
351  $logic = false;
352  }
353  }
354  if(!$logic)
355  {
356  $valid = false;
357  $a_form->getItemByPostVar('cron_inactive_user_reminder_period')->setAlert($lng->txt('send_mail_reminder_window_too_small'),'send_mail_reminder_window_too_small');
358  }
359  }
360  if($_POST['cron_inactive_user_delete_period'])
361  {
362  $ilSetting->set('cron_inactive_user_delete_include_roles', $setting);
363  $ilSetting->set('cron_inactive_user_delete_period', $_POST['cron_inactive_user_delete_period']);
364  }
365  if($this->reminderTimer > $reminder_period)
366  {
368  }
369  $ilSetting->set('cron_inactive_user_reminder_period', $reminder_period);
370 
371  if(!$valid)
372  {
373  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
374  return false;
375  }
376 
377  return true;
378  }
379 
380  protected function isDecimal($number)
381  {
382  if(strpos($number,',') || strpos($number,'.'))
383  {
384  return true;
385  }
386  return false;
387  }
388 }
389