ILIAS  release_4-3 Revision
 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 
14 {
15  const INTERVAL_DAILY = '1';
16  const INTERVAL_WEEKLY = '2';
17  const INTERVAL_MONTHLY = '3';
18  const INTERVAL_QUARTERLY = '4';
19 
21 
22  private $interval = null;
23 
24  private $include_roles = null;
25 
26  private $period = null;
27 
28  private $enabled = false;
29 
30  public function __construct()
31  {
32  global $ilSetting;
33 
34  $this->interval = $ilSetting->get(
35  'cron_inactive_user_delete_interval',
36  self::getDefaultIntervalKey()
37  );
38 
39  $this->include_roles = $ilSetting->get(
40  'cron_inactive_user_delete_include_roles', null
41  );
42  if($this->include_roles === null) $this->include_roles = array();
43  else $this->include_roles = explode(',', $this->include_roles);
44 
45  $this->period = $ilSetting->get(
46  'cron_inactive_user_delete_period',
47  self::DEFAULT_INACTIVITY_PERIOD
48  );
49 
50  $last_run = (int)$ilSetting->get('cron_inactive_user_delete_last_run', 0);
51 
52  if( $ilSetting->get('cron_inactive_user_delete', false) )
53  {
54  if( !$last_run || (time() - $last_run) > $this->getCurrentIntervalPeriod() )
55  {
56  $this->enabled = true;
57 
58  $ilSetting->set('cron_inactive_user_delete_last_run', time());
59  }
60  }
61  }
62 
63  public function run()
64  {
65  if( !$this->enabled ) return;
66 
67  global $rbacreview;
68 
69  $usr_ids = ilObjUser::_getUserIdsByInactivityPeriod($this->period);
70 
71  foreach($usr_ids as $usr_id)
72  {
73  if($usr_id == ANONYMOUS_USER_ID || $usr_id == SYSTEM_USER_ID) continue;
74 
75  $continue = true;
76  foreach($this->include_roles as $role_id)
77  {
78  if( $rbacreview->isAssigned($usr_id, $role_id) )
79  {
80  $continue = false;
81  break;
82  }
83  }
84  if($continue) continue;
85 
86  $user = ilObjectFactory::getInstanceByObjId($usr_id);
87 
88  $user->delete();
89  }
90  }
91 
92  private function getCurrentIntervalPeriod()
93  {
94  $period = 60 * 60;
95 
96  switch( $this->interval )
97  {
98  case self::INTERVAL_QUARTERLY: $period *= 3;
99  case self::INTERVAL_MONTHLY: $period *= 30;
100  case self::INTERVAL_WEEKLY: $period *= 7;
101  case self::INTERVAL_DAILY: $period *= 24;
102  }
103 
104  return $period;
105  }
106 
107  public static function getPossibleIntervalsArray()
108  {
109  global $lng;
110 
111  return array(
112  self::INTERVAL_DAILY => $lng->txt("daily"),
113  self::INTERVAL_WEEKLY => $lng->txt("weekly"),
114  self::INTERVAL_MONTHLY => $lng->txt("monthly"),
115  self::INTERVAL_QUARTERLY => $lng->txt("quarterly")
116  );
117  }
118 
119  public static function getDefaultIntervalKey()
120  {
121  return self::INTERVAL_DAILY;
122  }
123 }
124