zombie_manager.class.php 3.8 KB

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