xoStatsRegen()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 73
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 48
c 0
b 0
f 0
nc 64
nop 0
dl 0
loc 73
rs 8.2012

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Xmf\Module\Helper\Cache;
4
use Xmf\IPAddress;
5
6
/**
7
 * Smarty plugin
8
 * -------------------------------------------------------------
9
 * File:     function.xoStats.php
10
 * Type:     function
11
 * Name:     xoStats
12
 * Purpose:  XOOPS Members Statistics
13
 * Author:   Lio MJ <[email protected]>
14
 * Examples:
15
 *  <{xoStats}>
16
 *  Latest Member : <a href="<{$xoops_url}>/userinfo.php?uid=<{$latestUid}>"><{$latestMemberUname}></a><br>
17
 *  Total Posts   : <{$totalPosts}><br>
18
 *  Total Users   : <{$totalUsers}><br>
19
 *  Total Online  : <{$totalOnline}><br>
20
 *  New Users Today     : <{$newUsersToday}><br>
21
 *  New Users Yesterday : <{$newUsersYesterday}><br>
22
 *
23
 *  These are the names of all the values that will be assigned for Smarty
24
 *      $latestMemberName - name of newest member
25
 *      $latestMemberUname - uname of newest member
26
 *      $latestUid - uid of newest member
27
 *      $totalOnline - total members online
28
 *      $totalPosts - total posts by all members
29
 *      $newUsersToday - number of members registered today
30
 *      $newUsersYesterday  - number of members registered yesterday
31
 *      $totalUsers - total number of members
32
 */
33
34
/**
35
 * @param array   $params
36
 * @param \Smarty $smarty Smarty instance
37
 *
38
 * @return void
39
 */
40
function smarty_function_xoStats($params, &$smarty)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

40
function smarty_function_xoStats(/** @scrutinizer ignore-unused */ $params, &$smarty)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
{
42
    $cache = new Cache('system');
43
    $stats = $cache->cacheRead('xostats', 'xoStatsRegen', 30);
44
45
    foreach ($stats as $k => $v) {
46
        $smarty->assign($k, $v);
47
    }
48
}
49
50
function xoStatsRegen()
51
{
52
    global $xoopsUser, $xoopsModule;
53
54
    $stats = array();
55
56
    /** @var \XoopsMemberHandler $memberHandler */
57
    $memberHandler = xoops_getHandler('member');
58
59
    // Getting Total Online Users
60
    /** @var \XoopsOnlineHandler $onlineHandler */
61
    $onlineHandler = xoops_getHandler('online');
62
    // set gc probability to 10% for now..
63
    if (mt_rand(1, 100) < 11) {
64
        $onlineHandler->gc(300);
65
    }
66
    if (is_object($xoopsUser)) {
67
        $uid   = $xoopsUser->getVar('uid');
68
        $uname = $xoopsUser->getVar('uname');
69
    } else {
70
        $uid   = 0;
71
        $uname = '';
72
    }
73
74
    $requestIp = IPAddress::fromRequest()->asReadable();
75
    $requestIp = (false === $requestIp) ? '0.0.0.0' : $requestIp;
0 ignored issues
show
introduced by
The condition false === $requestIp is always false.
Loading history...
76
    if (is_object($xoopsModule)) {
77
        $onlineHandler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), $requestIp);
78
    } else {
79
        $onlineHandler->write($uid, $uname, time(), 0, $requestIp);
80
    }
81
    $onlines = $onlineHandler->getAll();
82
    if (empty($onlines)) {
83
        $stats['totalOnline'] = 0;
84
    } else {
85
        $stats['totalOnline'] = count($onlines);
86
    }
87
88
    //Getting Total Registered Users
89
    $levelCriteria = new \Criteria('level', 0, '>');
90
    $criteria = new \CriteriaCompo($levelCriteria);
91
    $criteria24 = new \CriteriaCompo($levelCriteria);
92
    $criteria24->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (24 * 3600)), '>='), 'AND');
93
    $criteria48 = new \CriteriaCompo($levelCriteria);
94
    $criteria48->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (48 * 3600)), '>='), 'AND');
95
    $criteria48->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (24 * 3600)), '<'), 'AND');
96
    $stats['totalUsers'] = $memberHandler->getUserCount($levelCriteria);
97
98
    //Getting User Registration Statistics
99
    $stats['newUsersToday'] = $memberHandler->getUserCount($criteria24);
100
    $stats['newUsersYesterday'] = $memberHandler->getUserCount($criteria48);
101
102
    // Getting Last Registered Member
103
    $criteria->setOrder('DESC');
104
    $criteria->setSort('user_regdate');
105
    $criteria->setLimit(1);
106
    $lastMembers = $memberHandler->getUsers($criteria);
107
    $stats['latestMemberName'] = $lastMembers[0]->getVar('name');
108
    $stats['latestMemberUname'] = $lastMembers[0]->getVar('uname');
109
    $stats['latestUid'] = $lastMembers[0]->getVar('uid');
110
111
    //Total Post Count
112
    $sql = 'SELECT SUM(posts) AS totalposts FROM ' . $GLOBALS['xoopsDB']->prefix('users') . ' WHERE level > 0';
113
    $result = $GLOBALS['xoopsDB']->query($sql);
114
    if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
115
        throw new \RuntimeException(
116
            \sprintf(_DB_QUERY_ERROR, $sql) . $GLOBALS['xoopsDB']->error(), E_USER_ERROR
117
        );
118
    }
119
    $myrow = $GLOBALS['xoopsDB']->fetchArray($result);
120
    $stats['totalPosts'] = $myrow['totalposts'];
121
122
    return $stats;
123
}
124