Passed
Pull Request — master (#1197)
by Richard
05:22
created

smarty_function_xoStats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
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 Post    : <{$totalpost}><br>
18
 *  Total User    : <{$totaluser}><br>
19
 *  Total Online  : <{$totalonline}><br>
20
 *  Registered Today     : <{$totalregisteredtoday}><br>
21
 *  Registered Yesterday : <{$totalregisteredyesterday}><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
 *      $totalpost - total posts by all members
29
 *      $totalregisteredtoday - number of members registered today
30
 *      $totalregisteredyesterday  - number of members registered yesterday
31
 *      $totaluser - total number of members
32
 *
33
 */
34
35
/**
36
 * @param array   $params
37
 * @param \Smarty $smarty Smarty instance
38
 *
39
 * @return void
40
 */
41
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

41
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...
42
{
43
    $cache = new Cache('system');
44
    $stats = $cache->cacheRead('xostats', 'regen_xoStats', 30);
45
46
    foreach ($stats as $k => $v) {
47
        $smarty->assign($k, $v);
48
    }
49
}
50
51
function regen_xoStats()
52
{
53
    global $xoopsUser, $xoopsModule;
54
55
    $stats = array();
56
57
    /** @var \XoopsMemberHandler $memberHandler */
58
    $memberHandler = xoops_getHandler('member');
59
60
    // Getting Total Online Users
61
    /** @var \XoopsOnlineHandler $online_handler */
62
    $online_handler = xoops_getHandler('online');
63
    // set gc probabillity to 10% for now..
64
    if (mt_rand(1, 100) < 11) {
65
        $online_handler->gc(300);
66
    }
67
    if (is_object($xoopsUser)) {
68
        $uid   = $xoopsUser->getVar('uid');
69
        $uname = $xoopsUser->getVar('uname');
70
    } else {
71
        $uid   = 0;
72
        $uname = '';
73
    }
74
75
    $requestIp = IPAddress::fromRequest()->asReadable();
76
    $requestIp = (false === $requestIp) ? '0.0.0.0' : $requestIp;
0 ignored issues
show
introduced by
The condition false === $requestIp is always false.
Loading history...
77
    if (is_object($xoopsModule)) {
78
        $online_handler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), $requestIp);
79
    } else {
80
        $online_handler->write($uid, $uname, time(), 0, $requestIp);
81
    }
82
    $onlines = $online_handler->getAll();
83
    if (empty($onlines)) {
84
        $stats['totalonline'] = 0;
85
    } else {
86
        $totalonline   = count($onlines);
87
        $stats['totalonline'] = $totalonline;
88
    }
89
90
    //Getting Total Registered Users
91
    $level_criteria = new \Criteria('level', 0, '>');
92
    $criteria = new \CriteriaCompo($level_criteria);
93
    $criteria24 = new \CriteriaCompo($level_criteria);
94
    $criteria48 = new \CriteriaCompo($level_criteria);
95
    $totaluser = $memberHandler->getUserCount($level_criteria);
96
    $stats['totaluser'] = $totaluser;
97
98
    //Getting User Registration Statistics
99
    $users_reg_24 = $memberHandler->getUserCount($criteria24->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (24 * 3600)), '>=')), 'AND');
0 ignored issues
show
Unused Code introduced by
The call to XoopsMemberHandler::getUserCount() has too many arguments starting with 'AND'. ( Ignorable by Annotation )

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

99
    /** @scrutinizer ignore-call */ 
100
    $users_reg_24 = $memberHandler->getUserCount($criteria24->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (24 * 3600)), '>=')), 'AND');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
    $users_reg_48 = $memberHandler->getUserCount($criteria48->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (48 * 3600)), '>=')), 'AND');
101
    $todayregister       = $users_reg_24;
102
    $yesterdayregister   = $users_reg_48;
103
    $stats['totalregisteredtoday'] = $todayregister;
104
    $stats['totalregisteredyesterday'] = $yesterdayregister;
105
106
    // Getting Last Registered Member
107
    $limit        = 1;
108
    $criteria->setOrder('DESC');
109
    $criteria->setSort('user_regdate');
110
    $criteria->setLimit($limit);
111
    $lastmembers = $memberHandler->getUsers($criteria);
112
    $lastusername = $lastmembers[0]->getVar('uname');
113
    $lastrealname = $lastmembers[0]->getVar('name');
114
    $latestuid = $lastmembers[0]->getVar('uid');
115
    $latestmembername = $lastrealname;
116
    $latestmemberuname = $lastusername;
117
    $stats['latestmembername'] = $latestmembername;
118
    $stats['latestmemberuname'] = $latestmemberuname;
119
    $stats['latestuid'] = $latestuid;
120
121
    //Total Post Count
122
    $sql = 'SELECT SUM(posts) AS totalpost FROM ' . $GLOBALS['xoopsDB']->prefix('users') . ' WHERE level > 0';
123
    $result = $GLOBALS['xoopsDB']->query($sql);
124
    $myrow = $GLOBALS['xoopsDB']->fetchArray($result);
125
    $totalpost = $myrow['totalpost'];
126
    $stats['totalpost'] = $totalpost;
127
128
    return $stats;
129
}
130