zombie_manager.class.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * ZombieQuery
  4. *
  5. * @copyright (c) 2012 University of Geneva
  6. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  7. * @author Laurent Opprecht <laurent@opprecht.info>
  8. */
  9. class ZombieManager
  10. {
  11. static function last_year()
  12. {
  13. $today = time();
  14. $day = date('j', $today);
  15. $month = date('n', $today);
  16. $year = date('Y', $today) - 1;
  17. return mktime(0, 0, 0, $month, $day, $year);
  18. }
  19. /**
  20. * Returns users whose last login is prior from $ceiling
  21. *
  22. * @param int|string $ceiling last login date
  23. * @param bool $active_only if true returns only active users. Otherwise returns all users.
  24. * @return ResultSet
  25. */
  26. static function list_zombies($ceiling, $active_only = true)
  27. {
  28. $ceiling = is_numeric($ceiling) ? (int) $ceiling : strtotime($ceiling);
  29. $ceiling = date('Y-m-d H:i:s', $ceiling);
  30. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  31. $login_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  32. $sql = 'SELECT
  33. user.user_id,
  34. user.firstname,
  35. user.lastname,
  36. user.username,
  37. user.auth_source,
  38. user.email,
  39. user.status,
  40. user.registration_date,
  41. user.active,
  42. access.login_date';
  43. global $_configuration;
  44. if ($_configuration['multiple_access_urls']) {
  45. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  46. $current_url_id = api_get_current_access_url_id();
  47. $sql .= " FROM $user_table as user, $login_table as access, $access_url_rel_user_table as url
  48. WHERE
  49. access.login_date = (SELECT MAX(a.login_date)
  50. FROM $login_table as a
  51. WHERE a.login_user_id = user.user_id
  52. ) AND
  53. access.login_date <= '$ceiling' AND
  54. user.user_id = access.login_user_id AND
  55. url.user_id = user.user_id AND url.access_url_id=$current_url_id";
  56. } else {
  57. $sql .= " FROM $user_table as user, $login_table as access
  58. WHERE
  59. access.login_date = (SELECT MAX(a.login_date)
  60. FROM $login_table as a
  61. WHERE a.login_user_id = user.user_id
  62. ) AND
  63. access.login_date <= '$ceiling' AND
  64. user.user_id = access.login_user_id";
  65. }
  66. if($active_only)
  67. {
  68. $sql .= ' AND user.active = 1';
  69. }
  70. return ResultSet::create($sql);
  71. }
  72. static function deactivate_zombies($ceiling)
  73. {
  74. $zombies = self::list_zombies($ceiling);
  75. $ids = array();
  76. foreach($zombies as $zombie)
  77. {
  78. $ids[] = $zombie['user_id'];
  79. }
  80. UserManager::deactivate_users($ids);
  81. }
  82. }