Passed
Push — master ( 4f2b44...e97b8d )
by Michael
01:58
created

membersstats_edit()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 41
c 1
b 0
f 0
nc 1024
nop 1
dl 0
loc 59
rs 3.15

How to fix   Long Method    Complexity   

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
//  Author: SMD & Trabis
3
//  URL: http://www.xoopsmalaysia.org  & http://www.xuups.com
4
//  E-Mail: [email protected]  & [email protected]
5
6
if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;
7
8
function show_membersstats_block($options) {
9
    global $xoopsConfig, $xoopsUser, $xoopsModule, $xoopsDB, $_SERVER;
10
    /* @var XoopsOnlineHandler $online_handler */
11
    $online_handler = xoops_getHandler('online');
12
    // set gc probabillity to 10% for now..
13
    if (mt_rand(1, 100) < 11) {
14
        $online_handler->gc(300);
15
    }
16
    if (is_object($xoopsUser)) {
17
        $uid   = $xoopsUser->getVar('uid');
18
        $uname = $xoopsUser->getVar('uname');
19
    } else {
20
        $uid   = 0;
21
        $uname = '';
22
    }
23
    $requestIp = \Xmf\IPAddress::fromRequest()->asReadable();
24
    $requestIp = (false === $requestIp) ? '0.0.0.0' : $requestIp;
0 ignored issues
show
introduced by
The condition false === $requestIp is always false.
Loading history...
25
    if (is_object($xoopsModule)) {
26
        $online_handler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), $requestIp);
27
    } else {
28
        $online_handler->write($uid, $uname, time(), 0, $requestIp);
29
    }
30
    $onlines = $online_handler->getAll();
31
    if (!empty($onlines)) {
32
        $total   = count($onlines);
33
        $block   = array();
34
        $guests  = 0;
35
        $members = '';
36
        for ($i = 0; $i < $total; ++$i) {
37
            if ($onlines[$i]['online_uid'] > 0) {
38
                $members .= ' <a href="' . XOOPS_URL . '/userinfo.php?uid=' . $onlines[$i]['online_uid'] . '" title="' . $onlines[$i]['online_uname'] . '">' . $onlines[$i]['online_uname'] . '</a>,';
39
            } else {
40
                ++$guests;
41
            }
42
        }
43
        $block['online_total'] = sprintf(_ONLINEPHRASE, $total);
44
        if (is_object($xoopsModule)) {
45
            $mytotal = $online_handler->getCount(new Criteria('online_module', $xoopsModule->getVar('mid')));
46
            $block['online_total'] .= ' (' . sprintf(_ONLINEPHRASEX, $mytotal, $xoopsModule->getVar('name')) . ')';
47
        }
48
        // Membership Statistic
49
        $member_handler =xoops_gethandler('member');
50
        $today = formatTimestamp(time());
0 ignored issues
show
Unused Code introduced by
The assignment to $today is dead and can be removed.
Loading history...
51
        $level_criteria = new Criteria('level', 0, '>');
52
        $criteria = new CriteriaCompo($level_criteria);
53
        $criteria24 = new CriteriaCompo($level_criteria);
54
        $criteria48 = new CriteriaCompo($level_criteria);
55
        $total_active_users = $member_handler->getUserCount($level_criteria);
0 ignored issues
show
Bug introduced by
The method getUserCount() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

55
        /** @scrutinizer ignore-call */ 
56
        $total_active_users = $member_handler->getUserCount($level_criteria);
Loading history...
56
        //Fixing stats for last 24 and 48 hours
57
        $users_reg_24 = $member_handler->getUserCount($criteria24->add(new Criteria('user_regdate', (mktime(0,0,0)-(24*3600)), '>=')),'AND');
58
        $users_reg_48 = $member_handler->getUserCount($criteria48->add(new Criteria('user_regdate', (mktime(0,0,0)-(48*3600)), '>=')),'AND');
59
        $limit = 1;
60
        $criteria->setOrder('DESC');
61
        $criteria->setSort('user_regdate');
62
        $criteria->setLimit($limit);
63
        $lastmembers =$member_handler->getUsers($criteria);
0 ignored issues
show
Bug introduced by
The method getUsers() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

63
        /** @scrutinizer ignore-call */ 
64
        $lastmembers =$member_handler->getUsers($criteria);
Loading history...
64
        $lastusername = $lastmembers[0]->getVar('uname');
65
        $lastrealname = $lastmembers[0]->getVar('name');
66
        $lastid = $lastmembers[0]->getVar('uid');
67
        
68
	    //Total Post Count
69
		$sql = "SELECT SUM(posts) AS totalposts FROM ".$GLOBALS['xoopsDB']->prefix("users")." WHERE level > 0";
70
        $result = $GLOBALS['xoopsDB']->query($sql);
71
	    $myrow = $GLOBALS['xoopsDB']->fetchArray($result); 	
72
		$block['totalposts']=$myrow['totalposts'];
73
		
74
		// data
75
        $block['activeusers'] = $total_active_users;
76
        $block['todayreg'] = $users_reg_24;
77
        $block['yesterdayreg'] = $users_reg_48 - $users_reg_24;
78
        $block['online_names']   = $members;
79
        $block['online_members'] = $total - $guests;
80
        $block['online_guests']  = $guests; 
81
		$block['lang_more']      = _MB_XOOPSMEMBERS_MORE;
82
        
83
		$block['total_online'] = $total;
84
        
85
		if ($options[4]=='1' && $lastrealname!='')
86
		  {
87
			$block['latestmember'] = $lastrealname; 
88
		  }
89
		else {	
90
		    $block['latestmember'] = $lastusername;
91
		     }
92
		$block['latest_id'] = $lastid;
93
        
94
95
        // Language Definition
96
        $block['membership_lang'] = _MB_XOOPSMEMBERS_MEMBERSHIP;
97
        $block['today_lang'] = _MB_XOOPSMEMBERS_TODAY;
98
        $block['yesterday_lang'] = _MB_XOOPSMEMBERS_YESTERDAY;
99
        $block['overall_lang'] = _MB_XOOPSMEMBERS_OVERALL;
100
        $block['online_lang'] = _MB_XOOPSMEMBERS_ONLINE;
101
        $block['guests_lang'] = _MB_XOOPSMEMBERS_GUESTS;
102
        $block['members_lang'] = _MB_XOOPSMEMBERS_MEMBERS;
103
        $block['total_lang'] = _MB_XOOPSMEMBERS_TOTAL;
104
        $block['list_lang'] = _MB_XOOPSMEMBERS_LIST;
105
        $block['popup_lang'] = _MB_XOOPSMEMBERS_POPUP;
106
        $block['latest_lang'] = _MB_XOOPSMEMBERS_LATEST;
107
		$block['totalpostmade_lang'] =_MB_XOOPSMEMBERS_TOTALPOSTMADE;
108
		$block['totalposts_lang'] =_MB_XOOPSMEMBERS_TOTALPOSTS;
109
		$block['totalpost_lang']  =_MB_XOOPSMEMBERS_TOTALPOST;
110
		$block['wehave_lang'] =_MB_XOOPSMEMBERS_WEHAVE;
111
		$block['currentonline_lang'] =_MB_XOOPSMEMBERS_CURRENTONLINE;
112
		$block['registeredmembers_lang'] =_MB_XOOPSMEMBERS_REGISTEREDMEMBERS;
113
		$block['registeredtoday_lang'] =_MB_XOOPSMEMBERS_REGISTEREDTODAY;
114
		$block['registeredyesterday_lang'] =_MB_XOOPSMEMBERS_REGISTEREDYESTERDAY;
115
		$block['newestmember_lang'] =_MB_XOOPSMEMBERS_NEWESTMEMBER;
116
        $block['and_lang'] =_MB_XOOPSMEMBERS_AND;
117
        $block['showtotalpost'] = $options[0];
118
		$block['showtotalonline'] = $options[1];
119
	    $block['showreghistory'] = $options[2];
120
		$block['shownewmember'] = $options[3];
121
		$block['userealname'] = $options[4];
122
		
123
        return $block;
124
	} else {
125
		return false;
126
	}	
127
}
128
129
function membersstats_edit($options) {
130
131
	$form = _MB_XOOPSMEMBERS_SHOWTOTALPOST."&nbsp;";
132
	if ( $options[0] == 1 ) {
133
		$chk = " checked='checked'";
134
	}
135
	$form .= "<input type='radio' name='options[0]' value='1'".$chk." />&nbsp;"._YES."";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $chk does not seem to be defined for all execution paths leading up to this point.
Loading history...
136
	$chk = "";
137
	if ( $options[0] == 0 ) {
138
		$chk = " checked='checked'";
139
	}
140
	$form .= "&nbsp;<input type='radio' name='options[0]' value='0'".$chk." />"._NO."<br />";
141
142
143
	$form .= _MB_XOOPSMEMBERS_SHOWTOTALONLINE."&nbsp;";
144
	if ( $options[1] == 1 ) {
145
		$chk = " checked='checked'";
146
	}
147
	$form .= "<input type='radio' name='options[1]' value='1'".$chk." />&nbsp;"._YES."";
148
	$chk = "";
149
	if ( $options[1] == 0 ) {
150
		$chk = " checked='checked'";
151
	}
152
	$form .= "&nbsp;<input type='radio' name='options[1]' value='0'".$chk." />"._NO."<br />";
153
154
	$form .= _MB_XOOPSMEMBERS_SHOWREGHISTORY."&nbsp;";
155
	if ( $options[2] == 1 ) {
156
		$chk = " checked='checked'";
157
	}
158
	$form .= "<input type='radio' name='options[2]' value='1'".$chk." />&nbsp;"._YES."";
159
	$chk = "";
160
	if ( $options[2] == 0 ) {
161
		$chk = " checked='checked'";
162
	}
163
	$form .= "&nbsp;<input type='radio' name='options[2]' value='0'".$chk." />"._NO."<br />";
164
165
	$form .= _MB_XOOPSMEMBERS_SHOWNEWMEMBER."&nbsp;";
166
	if ( $options[3] == 1 ) {
167
		$chk = " checked='checked'";
168
	}
169
	$form .= "<input type='radio' name='options[3]' value='1'".$chk." />&nbsp;"._YES."";
170
	$chk = "";
171
	if ( $options[3] == 0 ) {
172
		$chk = " checked='checked'";
173
	}
174
	$form .= "&nbsp;<input type='radio' name='options[3]' value='0'".$chk." />"._NO."<br />";
175
176
	$form .= _MB_XOOPSMEMBERS_USEREALNAME."&nbsp;";
177
	if ( $options[4] == 1 ) {
178
		$chk = " checked='checked'";
179
	}
180
	$form .= "<input type='radio' name='options[4]' value='1'".$chk." />&nbsp;"._YES."";
181
	$chk = "";
182
	if ( $options[4] == 0 ) {
183
		$chk = " checked='checked'";
184
	}
185
	$form .= "&nbsp;<input type='radio' name='options[4]' value='0'".$chk." />"._NO."<br />";
186
187
	return $form;
188
}
189
190
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
191