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

timeDifference()   C

Complexity

Conditions 12

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
dl 0
loc 44
rs 6.9666

How to fix   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: Lio MJ
3
// Website: https://www.github.com/liomj/
4
5
if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;
6
   
7
    function show_memberslastlogin_block($options) {
8
	
9
	function timeDifference($start,$end,$return='days') {
10
    //change times to Unix timestamp.
11
    //$start = strtotime($start);
12
    //$end = strtotime($end);
13
    //subtract dates
14
    $difference = max($end, $start) - min($end,$start);
15
    $time = NULL;
16
    //24 hours equal to 86400
17
    //calculate time difference.
18
    switch($return) {
19
        case 'days':
20
             $days = floor($difference/86400);
21
                $difference = $difference % 86400;
22
                    $time['days'] = $days;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
23
        case 'hours':
24
            $hours = floor($difference/3600);
25
                $difference = $difference % 3600;
26
                    $time['hours'] = $hours;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
27
        case 'minutes':
28
            $minutes = floor($difference/60);
29
                $difference = $difference % 60;
30
                    $time['minutes'] = $minutes;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
31
        case 'seconds':
32
            $seconds = $difference;
33
                $time['seconds'] = $seconds;
34
    }
35
    
36
    $output = array();
37
       if(is_array($time)) {
0 ignored issues
show
introduced by
The condition is_array($time) is always false.
Loading history...
38
        $showSec = true;
39
                    if(isset($time['hours']) && $time['hours'] > 0) {
40
                        $output[] = $time['hours'] . ' Hour';
41
                        $showSec = false;
42
                    }
43
                    
44
                    if(isset($time['minutes']) && $time['minutes'] > 0) {
45
                        $output[] = $time['minutes'] . ' minutes';
46
                        $showSec = false;
47
                    }
48
                    
49
                    if(isset($time['seconds']) && $showSec == true) {
50
                        return $time['seconds'] . ' seconds';
51
                    }    
52
            return implode(', ',$output);
53
        }
54
}  	
55
				
56
	$x=0;
57
	$now = time();
58
    $hours = 24;
59
    $time = ( intval( $hours ) > 0 ) ? time() - ( intval( $hours ) * 3600 ) : ( time() - 24*3600 );
60
    $block = array(); 
61
	$sql = "SELECT distinct uid, name, uname, user_avatar, last_login FROM ".$GLOBALS['xoopsDB']->prefix("users")." WHERE level > 0 AND last_login >= '" . $time . "' ORDER BY last_login DESC LIMIT " . $options[3] . "";
62
    $result = $GLOBALS['xoopsDB']->query($sql);
63
	while (list($uid, $name, $uname,$user_avatar,$last_login)= $GLOBALS['xoopsDB']->fetchRow($result) ) {	
64
	$sincelastlogin = ' ' . timeDifference($last_login,$now,'hours') . ' ago ';
0 ignored issues
show
Bug introduced by
Are you sure the usage of timeDifference($last_login, $now, 'hours') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
    $x++; 
66
	
67
	
68
	$block['recentlogin'][] = $recentlogin;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $recentlogin does not seem to be defined for all execution paths leading up to this point.
Loading history...
69
    $recentlogin['uid']=$uid; 
70
71
	if ($name!='' && $options[2]=='1')
72
		  {
73
	          $recentlogin['name']=htmlspecialchars($name, ENT_QUOTES); 
74
		  }
75
		  else
76
		  {
77
			  $recentlogin['name']=$uname; 
78
		  }
79
	$recentlogin['user_avatar']=$user_avatar; 
80
	$recentlogin['last_login']=$last_login; 
81
	$recentlogin['sincelastlogin']=$sincelastlogin; 
82
	
83
	} 
84
		
85
	    $block['showrecentloginname'] = $options[0];
86
		$block['showrecentloginavatar'] = $options[1];
87
		$block['userealname'] = $options[2];
88
		$block['memberdisplay'] = $options[3];
89
    return $block; 
90
}
91
92
93
94
function memberslastlogin_edit($options) {
95
	
96
	$form .= _MB_XOOPSMEMBERS_SHOWRECENTLOGINNAME."&nbsp;";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $form seems to be never defined.
Loading history...
97
	if ( $options[0] == 1 ) {
98
		$chk = " checked='checked'";
99
	}
100
	$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...
101
	$chk = "";
102
	if ( $options[0] == 0 ) {
103
		$chk = " checked='checked'";
104
	}
105
	$form .= "&nbsp;<input type='radio' name='options[0]' value='0'".$chk." />"._NO."<br />";
106
107
	$form .= _MB_XOOPSMEMBERS_SHOWRECENTLOGINAVATAR."&nbsp;";
108
	if ( $options[1] == 1 ) {
109
		$chk = " checked='checked'";
110
	}
111
	$form .= "<input type='radio' name='options[1]' value='1'".$chk." />&nbsp;"._YES."";
112
	$chk = "";
113
	if ( $options[1] == 0 ) {
114
		$chk = " checked='checked'";
115
	}
116
	$form .= "&nbsp;<input type='radio' name='options[1]' value='0'".$chk." />"._NO."<br />";
117
118
	$form .= _MB_XOOPSMEMBERS_USEREALNAME."&nbsp;";
119
	if ( $options[2] == 1 ) {
120
		$chk = " checked='checked'";
121
	}
122
	$form .= "<input type='radio' name='options[2]' value='1'".$chk." />&nbsp;"._YES."";
123
	$chk = "";
124
	if ( $options[2] == 0 ) {
125
		$chk = " checked='checked'";
126
	}
127
	$form .= "&nbsp;<input type='radio' name='options[2]' value='0'".$chk." />"._NO."<br />";
128
	
129
	$form .= _MB_XOOPSMEMBERS_MEMBERDISPLAY."&nbsp;";
130
	$form .= "<input type='text' name='options[3]' value='".$options[3]."'/>";
131
	return $form;
132
}
133
134
135
?>
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...
136