ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  $this->include_roles = $ilSetting->get(
32  'cron_inactive_user_delete_include_roles',
33  null
34  );
35  if ($this->include_roles === null) {
36  $this->include_roles = array();
37  } else {
38  $this->include_roles = explode(',', $this->include_roles);
39  }
40 
41  $this->period = $ilSetting->get(
42  'cron_inactive_user_delete_period',
43  self::DEFAULT_INACTIVITY_PERIOD
44  );
45  $this->reminderTimer = $ilSetting->get(
46  'cron_inactive_user_reminder_period',
47  self::DEFAULT_REMINDER_PERIOD
48  );
49  }
50  }
51 
57  protected function getTimeDifferenceBySchedule($schedule_time, $multiplier)
58  {
59  $time_difference = 0;
60  switch ($schedule_time) {
62  $time_difference = 86400;
63  break;
65  $time_difference = 60 * $multiplier;
66  break;
68  $time_difference = 3600 * $multiplier;
69  break;
71  $time_difference = 86400 * $multiplier;
72  break;
74  $time_difference = 604800;
75  break;
77  $time_difference = 2629743;
78  break;
80  $time_difference = 7889229;
81  break;
83  $time_difference = 31556926;
84  break;
85  }
86  return $time_difference;
87  }
88 
89  public function getId()
90  {
91  return "user_inactive";
92  }
93 
94  public function getTitle()
95  {
96  global $lng;
97 
98  return $lng->txt("delete_inactive_user_accounts");
99  }
100 
101  public function getDescription()
102  {
103  global $lng;
104 
105  return $lng->txt("delete_inactive_user_accounts_desc");
106  }
107 
108  public function getDefaultScheduleType()
109  {
110  return self::SCHEDULE_TYPE_DAILY;
111  }
112 
113  public function getDefaultScheduleValue()
114  {
115  return;
116  }
117 
118  public function hasAutoActivation()
119  {
120  return false;
121  }
122 
123  public function hasFlexibleSchedule()
124  {
125  return true;
126  }
127 
128  public function hasCustomSettings()
129  {
130  return true;
131  }
132 
133  public function run()
134  {
135  global $rbacreview, $ilLog;
136 
138  $reminder_time = (int) $this->reminderTimer;
139  $checkMail = (int) $this->period - $reminder_time;
140  $usr_ids = ilObjUser::_getUserIdsByInactivityPeriod($checkMail);
141  $counter = 0;
142  $userDeleted = 0;
143  $userMailsDelivered = 0;
144  foreach ($usr_ids as $usr_id) {
145  if ($usr_id == ANONYMOUS_USER_ID || $usr_id == SYSTEM_USER_ID) {
146  continue;
147  }
148 
149  $continue = true;
150  foreach ($this->include_roles as $role_id) {
151  if ($rbacreview->isAssigned($usr_id, $role_id)) {
152  $continue = false;
153  break;
154  }
155  }
156  if ($continue) {
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  $user->delete();
169  $userDeleted++;
170  } elseif ($reminder_time > 0) {
171  $timestamp_for_deletion = $timestamp_last_login - $grace_period_over;
172  $account_will_be_deleted_on = $this->calculateDeletionData($timestamp_for_deletion);
173  $mailSent = ilCronDeleteInactiveUserReminderMail::checkIfReminderMailShouldBeSend($user, $reminder_time, $account_will_be_deleted_on);
174  if ($mailSent) {
175  $userMailsDelivered++;
176  }
177  }
178  $counter++;
179  }
180 
181  if ($counter) {
182  $status = ilCronJobResult::STATUS_OK;
183  }
185  $ilLog->write("CRON - ilCronDeleteInactiveUserAccounts::run(), deleted => $userDeleted User(s), sent reminder mail to $userMailsDelivered User(s)");
186  $result = new ilCronJobResult();
187  $result->setStatus($status);
188  return $result;
189  }
190 
191  protected function calculateDeletionData($date_for_deletion)
192  {
193  $cron_timing = ilCronManager::getCronJobData($this->getId());
194  $time_difference = 0;
195  $multiplier = 1;
196 
197  if (!is_array($cron_timing) || !is_array($cron_timing[0])) {
198  return time() + $date_for_deletion + $time_difference;
199  }
200 
201  if (array_key_exists('schedule_type', $cron_timing[0])) {
202  if ((array_key_exists('schedule_type', $cron_timing[0]) && $cron_timing[0]['schedule_value'] != null)) {
203  $multiplier = $cron_timing[0]['schedule_value'];
204  }
205  $time_difference = $this->getTimeDifferenceBySchedule($cron_timing[0]['schedule_type'], $multiplier);
206  }
207  return time() + $date_for_deletion + $time_difference;
208  }
209 
211  {
212  global $lng, $rbacreview, $ilObjDataCache, $ilSetting;
213  $lng->loadLanguageModule("user");
214 
215  $schedule = $a_form->getItemByPostVar('type');
216  $schedule->setTitle(
217  $lng->txt('delete_inactive_user_accounts_frequency'),
218  'delete_inactive_user_accounts_frequency'
219  );
220  $schedule->setInfo(
221  $lng->txt('delete_inactive_user_accounts_frequency_desc'),
222  'delete_inactive_user_accounts_frequency_desc'
223  );
224 
225  include_once('Services/Form/classes/class.ilMultiSelectInputGUI.php');
226  $sub_mlist = new ilMultiSelectInputGUI(
227  $lng->txt('delete_inactive_user_accounts_include_roles'),
228  'cron_inactive_user_delete_include_roles'
229  );
230  $sub_mlist->setInfo($lng->txt('delete_inactive_user_accounts_include_roles_desc'));
231  $roles = array();
232  foreach ($rbacreview->getGlobalRoles() as $role_id) {
233  if ($role_id != ANONYMOUS_ROLE_ID) {
234  $roles[$role_id] = $ilObjDataCache->lookupTitle($role_id);
235  }
236  }
237  $sub_mlist->setOptions($roles);
238  $setting = $ilSetting->get('cron_inactive_user_delete_include_roles', null);
239  if ($setting === null) {
240  $setting = array();
241  } else {
242  $setting = explode(',', $setting);
243  }
244  $sub_mlist->setValue($setting);
245  $sub_mlist->setWidth(300);
246  #$sub_mlist->setHeight(100);
247  $a_form->addItem($sub_mlist);
248 
249  $default_setting = self::DEFAULT_INACTIVITY_PERIOD;
250  $sub_text = new ilNumberInputGUI(
251  $lng->txt('delete_inactive_user_accounts_period'),
252  'cron_inactive_user_delete_period'
253  );
254  $sub_text->setInfo($lng->txt('delete_inactive_user_accounts_period_desc'));
255  $sub_text->setValue($ilSetting->get("cron_inactive_user_delete_period", $default_setting));
256  $sub_text->setSize(4);
257  $sub_text->setMaxLength(4);
258  $sub_text->setRequired(true);
259  $a_form->addItem($sub_text);
260 
261  $sub_period = new ilNumberInputGUI(
262  $lng->txt('send_mail_to_inactive_users'),
263  'cron_inactive_user_reminder_period'
264  );
265  $sub_period->setInfo($lng->txt("send_mail_to_inactive_users_desc"));
266  $sub_period->setValue($ilSetting->get("cron_inactive_user_reminder_period", $default_setting));
267  $sub_period->setSuffix($lng->txt("send_mail_to_inactive_users_suffix"));
268  $sub_period->setSize(4);
269  $sub_period->setMaxLength(4);
270  $sub_period->setRequired(false);
271  $sub_period->setMinValue(0);
272  $a_form->addItem($sub_period);
273  /*
274  $default_setting = ilCronDeleteInactiveUserAccounts::DEFAULT_SETTING_INCLUDE_ADMINS;
275  $sub_cb = new ilCheckboxInputGUI($lng->txt('delete_inactive_user_accounts_include_admins'),'cron_inactive_user_delete_include_admins');
276  $sub_cb->setChecked($ilSetting->get("cron_inactive_user_delete_include_admins", $default_setting) ? 1 : 0 );
277  //$sub_cb->setOptionTitle($lng->txt('delete_inactive_user_accounts_include_admins'));
278  $sub_cb->setInfo($lng->txt('delete_inactive_user_accounts_include_admins_desc'));
279  $a_form->addItem($sub_cb);
280  */
281  }
282 
283  public function saveCustomSettings(ilPropertyFormGUI $a_form)
284  {
285  global $ilSetting, $lng;
286  $lng->loadLanguageModule("user");
287  $setting = implode(',', $_POST['cron_inactive_user_delete_include_roles']);
288  if (!strlen($setting)) {
289  $setting = null;
290  }
291 
292  $valid = true;
293  $delete_period = ilUtil::stripSlashes($_POST['cron_inactive_user_delete_period']);
294  $reminder_period = ilUtil::stripSlashes($_POST['cron_inactive_user_reminder_period']);
295  $cron_period = (int) ilUtil::stripSlashes($_POST['type']);
296  $cron_period_custom = (int) ilUtil::stripSlashes($_POST['sdyi']);
297 
298  if ($this->isDecimal($delete_period)) {
299  $valid = false;
300  $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');
301  }
302  if ($this->isDecimal($reminder_period)) {
303  $valid = false;
304  $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');
305  }
306  if ($reminder_period >= $delete_period) {
307  $valid = false;
308  $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');
309  }
310  if ($cron_period >= ilCronJob::SCHEDULE_TYPE_IN_DAYS && $cron_period <= ilCronJob::SCHEDULE_TYPE_YEARLY && $reminder_period > 0) {
311  $logic = true;
312  $check_window_logic = $delete_period - $reminder_period;
313  if ($cron_period == ilCronJob::SCHEDULE_TYPE_IN_DAYS) {
314  if ($check_window_logic < $cron_period_custom) {
315  $logic = false;
316  }
317  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_WEEKLY) {
318  if ($check_window_logic <= 7) {
319  $logic = false;
320  }
321  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_MONTHLY) {
322  if ($check_window_logic <= 31) {
323  $logic = false;
324  }
325  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_QUARTERLY) {
326  if ($check_window_logic <= 92) {
327  $logic = false;
328  }
329  } elseif ($cron_period == ilCronJob::SCHEDULE_TYPE_YEARLY) {
330  if ($check_window_logic <= 366) {
331  $logic = false;
332  }
333  }
334  if (!$logic) {
335  $valid = false;
336  $a_form->getItemByPostVar('cron_inactive_user_reminder_period')->setAlert($lng->txt('send_mail_reminder_window_too_small'), 'send_mail_reminder_window_too_small');
337  }
338  }
339  if ($_POST['cron_inactive_user_delete_period']) {
340  $ilSetting->set('cron_inactive_user_delete_include_roles', $setting);
341  $ilSetting->set('cron_inactive_user_delete_period', $_POST['cron_inactive_user_delete_period']);
342  }
343  if ($this->reminderTimer > $reminder_period) {
345  }
346  $ilSetting->set('cron_inactive_user_reminder_period', $reminder_period);
347 
348  if (!$valid) {
349  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
350  return false;
351  }
352 
353  return true;
354  }
355 
356  protected function isDecimal($number)
357  {
358  if (strpos($number, ',') || strpos($number, '.')) {
359  return true;
360  }
361  return false;
362  }
363 }
run()
Run job.
getItemByPostVar($a_post_var)
Get Item by POST variable.
$result
This class represents a property form user interface.
Cron job application base class.
$valid
const SCHEDULE_TYPE_IN_MINUTES
static _getUserIdsByInactivityPeriod($period)
get ids of all users that have been inactive for at least the given period
addItem($a_item)
Add Item (Property, SectionHeader).
const SCHEDULE_TYPE_MONTHLY
const SCHEDULE_TYPE_WEEKLY
setInfo($a_info)
Set Information Text.
$counter
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)
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
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
const SCHEDULE_TYPE_YEARLY
global $ilSetting
Definition: privfeed.php:17
global $lng
Definition: privfeed.php:17
const SCHEDULE_TYPE_DAILY
const SCHEDULE_TYPE_QUARTERLY
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
Cron job result data container.
const SCHEDULE_TYPE_IN_HOURS
$_POST["username"]