Passed
Push — master ( e60e65...6408ae )
by Yannick
09:50
created

ZombieManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A last_year() 0 8 1
C listZombies() 0 85 10
A deactivate_zombies() 0 8 2
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Framework\Container;
5
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
6
7
/**
8
 * ZombieQuery.
9
 *
10
 * @copyright (c) 2012 University of Geneva
11
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
12
 * @author Laurent Opprecht <[email protected]>
13
 */
14
class ZombieManager
15
{
16
    public static function last_year()
17
    {
18
        $today = time();
19
        $day = date('j', $today);
20
        $month = date('n', $today);
21
        $year = date('Y', $today) - 1;
22
23
        return mktime(0, 0, 0, $month, $day, $year);
24
    }
25
26
    /**
27
     * Returns users whose last login is prior from $ceiling.
28
     *
29
     * @param int|string $ceiling     last login date
30
     * @param bool       $active_only if true returns only active users. Otherwise returns all users.
31
     *
32
     * @return array
33
     */
34
    public static function listZombies(
35
        $ceiling,
36
        $active_only = true,
37
        $from = 0,
38
        $count = 10,
39
        $column = 'user.firstname',
40
        $direction = 'desc'
41
    ) {
42
        $column = str_replace('user.', '', $column);
43
        if (empty($column)) {
44
            $column = 'firstname';
45
        }
46
47
        $validColumns = ['id', 'official_code', 'firstname', 'lastname', 'username', 'email', 'status', 'created_at', 'active', 'login_date'];
48
        if (!in_array($column, $validColumns)) {
49
            $column = 'firstname';
50
        }
51
        $ceiling = is_numeric($ceiling) ? (int) $ceiling : strtotime($ceiling);
52
        $ceiling = date('Y-m-d H:i:s', $ceiling);
53
54
        $user_table = Database::get_main_table(TABLE_MAIN_USER);
55
        $login_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
56
57
        /** @var AccessUrlHelper $accessUrlHelper */
58
        $accessUrlHelper = Container::$container->get(AccessUrlHelper::class);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        /** @scrutinizer ignore-call */ 
59
        $accessUrlHelper = Container::$container->get(AccessUrlHelper::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        $accessUrl = $accessUrlHelper->getCurrent();
60
61
        $sql = 'SELECT
62
                    user.id,
63
                    user.official_code,
64
                    user.firstname,
65
                    user.lastname,
66
                    user.username,
67
                    user.email,
68
                    user.status,
69
                    user.created_at,
70
                    user.active,
71
                    access.login_date';
72
73
        if ($accessUrlHelper->isMultiple()) {
74
            $access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
75
            $current_url_id = $accessUrl->getId();
76
77
            $sql .= " FROM $user_table as user, $login_table as access, $access_url_rel_user_table as url
78
                      WHERE
79
                        access.login_date = (SELECT MAX(a.login_date)
80
                                             FROM $login_table as a
81
                                             WHERE a.login_user_id = user.id
82
                                             ) AND
83
                        access.login_date <= '$ceiling' AND
84
                        user.id = access.login_user_id AND
85
                        url.user_id = user.id AND url.access_url_id=$current_url_id";
86
        } else {
87
            $sql .= " FROM $user_table as user, $login_table as access
88
                      WHERE
89
                        access.login_date = (SELECT MAX(a.login_date)
90
                                             FROM $login_table as a
91
                                             WHERE a.login_user_id = user.id
92
                                             ) AND
93
                        access.login_date <= '$ceiling' AND
94
                        user.id = access.login_user_id";
95
        }
96
97
        if ($active_only) {
98
            $sql .= ' AND user.active = 1';
99
        }
100
101
        $sql .= !str_contains($sql, 'WHERE') ? ' WHERE user.active <> '.USER_SOFT_DELETED : ' AND user.active <> '.USER_SOFT_DELETED;
102
        $column = str_replace('user.', '', $column);
103
        $sql .= " ORDER BY `$column` $direction";
104
        if (!is_null($from) && !is_null($count)) {
105
            $count = (int) $count;
106
            $from = (int) $from;
107
            $sql .= " LIMIT $from, $count ";
108
        }
109
110
        $result = Database::query($sql);
111
112
        if (Database::num_rows($result) === 0) {
113
            return [];
114
        }
115
        $userInfo = Database::store_result($result, 'ASSOC');
116
        $userInfo['auth_sources'] = api_get_user_entity($userInfo['id'])->getAuthSourcesAuthentications($accessUrl);
117
118
        return $userInfo;
119
    }
120
121
    /**
122
     * @param $ceiling
123
     */
124
    public static function deactivate_zombies($ceiling)
125
    {
126
        $zombies = self::listZombies($ceiling);
127
        $ids = [];
128
        foreach ($zombies as $zombie) {
129
            $ids[] = $zombie['id'];
130
        }
131
        UserManager::deactivate_users($ids);
132
    }
133
}
134