zombie_manager.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 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. * @return ResultSet
  26. */
  27. static function listZombies($ceiling, $active_only = true, $count = 0, $from = 10, $column = 'user.firstname', $direction = 'desc')
  28. {
  29. if (empty($column)) {
  30. $column = 'user.firstname';
  31. }
  32. $ceiling = is_numeric($ceiling) ? (int) $ceiling : strtotime($ceiling);
  33. $ceiling = date('Y-m-d H:i:s', $ceiling);
  34. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  35. $login_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  36. $sql = 'SELECT
  37. user.user_id,
  38. user.firstname,
  39. user.lastname,
  40. user.username,
  41. user.auth_source,
  42. user.email,
  43. user.status,
  44. user.registration_date,
  45. user.active,
  46. access.login_date';
  47. if (api_is_multiple_url_enabled()) {
  48. $access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  49. $current_url_id = api_get_current_access_url_id();
  50. $sql .= " FROM $user_table as user, $login_table as access, $access_url_rel_user_table as url
  51. WHERE
  52. access.login_date = (SELECT MAX(a.login_date)
  53. FROM $login_table as a
  54. WHERE a.login_user_id = user.user_id
  55. ) AND
  56. access.login_date <= '$ceiling' AND
  57. user.user_id = access.login_user_id AND
  58. url.user_id = user.user_id AND url.access_url_id=$current_url_id";
  59. } else {
  60. $sql .= " FROM $user_table as user, $login_table as access
  61. WHERE
  62. access.login_date = (SELECT MAX(a.login_date)
  63. FROM $login_table as a
  64. WHERE a.login_user_id = user.user_id
  65. ) AND
  66. access.login_date <= '$ceiling' AND
  67. user.user_id = access.login_user_id";
  68. }
  69. if ($active_only) {
  70. $sql .= ' AND user.active = 1';
  71. }
  72. $count = intval($count);
  73. $from = intval($from);
  74. $sql .= " ORDER BY $column $direction";
  75. $sql .= " LIMIT $count, $from ";
  76. $result = Database::query($sql);
  77. return Database::store_result($result, 'ASSOC');
  78. }
  79. /**
  80. * @param $ceiling
  81. */
  82. static function deactivate_zombies($ceiling)
  83. {
  84. $zombies = self::list_zombies($ceiling);
  85. $ids = array();
  86. foreach ($zombies as $zombie) {
  87. $ids[] = $zombie['user_id'];
  88. }
  89. UserManager::deactivate_users($ids);
  90. }
  91. }