timeDifference()   C
last analyzed

Complexity

Conditions 12
Paths 144

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 12
eloc 30
c 2
b 1
f 0
nc 144
nop 3
dl 0
loc 45
rs 6.6

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')) {
6
    exit;
7
}
8
9
/**
10
 * @param $options
11
 * @return array
12
 */
13
function show_memberslastlogin_block($options)
14
{
15
    $x      = 0;
16
    $now    = time();
17
    $hours  = 24;
18
    $time   = ((int)$hours > 0) ? time() - ((int)$hours * 3600) : (time() - 24 * 3600);
19
    $block  = [];
20
    $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] . '';
21
    $result = $GLOBALS['xoopsDB']->query($sql);
22
    while (list($uid, $name, $uname, $user_avatar, $last_login) = $GLOBALS['xoopsDB']->fetchRow($result)) {
23
        $sincelastlogin = ' ' . timeDifference($last_login, $now, _MB_XOOPSMEMBERS_HOURS) . ' ' . _MB_XOOPSMEMBERS_AGO;
24
        $x++;
25
26
        $recentlogin = [];
27
        $recentlogin['uid']     = $uid;
28
        if ('' != $name && '1' == $options[2]) {
29
            $recentlogin['name'] = htmlspecialchars($name, ENT_QUOTES);
30
        } else {
31
            $recentlogin['name'] = $uname;
32
        }
33
        $recentlogin['user_avatar']    = $user_avatar;
34
        $recentlogin['last_login']     = $last_login;
35
        $recentlogin['sincelastlogin'] = $sincelastlogin;
36
37
        $block['recentlogin'][] = $recentlogin;
38
        unset($recentlogin);
39
    }
40
41
    $block['showrecentloginname']   = $options[0];
42
    $block['showrecentloginavatar'] = $options[1];
43
    $block['userealname']           = $options[2];
44
    $block['memberdisplay']         = $options[3];
45
    return $block;
46
}
47
48
/**
49
 * @param        $start
50
 * @param        $end
51
 * @param string $return
52
 * @return string
53
 */
54
function timeDifference($start, $end, $return = 'days')
55
{
56
    //change times to Unix timestamp.
57
    //$start = strtotime($start);
58
    //$end = strtotime($end);
59
    //subtract dates
60
    $difference = max($end, $start) - min($end,$start);
61
    $time       = null;
62
    //24 hours equal to 86400
63
    //calculate time difference.
64
    switch($return) {
65
        case 'days':
66
            $days = floor($difference/86400);
67
            $difference = $difference % 86400;
68
            $time['days'] = $days;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
69
        case 'hours':
70
            $hours = floor($difference/3600);
71
            $difference = $difference % 3600;
72
            $time['hours'] = $hours;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
73
        case 'minutes':
74
            $minutes = floor($difference/60);
75
            $difference = $difference % 60;
76
            $time['minutes'] = $minutes;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
77
        case 'seconds':
78
            $seconds = $difference;
79
            $time['seconds'] = $seconds;
80
    }
81
82
    $output = [];
83
    if(is_array($time)) {
0 ignored issues
show
introduced by
The condition is_array($time) is always false.
Loading history...
84
        $showSec = true;
85
        if(isset($time['hours']) && $time['hours'] > 0) {
86
            $output[] = $time['hours']. ' ' . _MB_XOOPSMEMBERS_HOUR;
87
            $showSec = false;
88
        }
89
90
        if(isset($time['minutes']) && $time['minutes'] > 0) {
91
            $output[] = $time['minutes']. ' ' . _MB_XOOPSMEMBERS_MINUTES;
92
            $showSec = false;
93
        }
94
95
        if (isset($time['seconds']) && true === $showSec) {
96
            return $time['seconds']. ' ' . _MB_XOOPSMEMBERS_SECONDS;
97
        }
98
        return implode(', ',$output);
99
    }
100
}
101
102
/**
103
 * @param $options
104
 * @return string
105
 */
106
function memberslastlogin_edit($options)
107
{
108
    $form = _MB_XOOPSMEMBERS_SHOWRECENTLOGINNAME . '&nbsp;';
109
    $chk = '';
110
    if (1 == $options[0]) {
111
        $chk = " checked";
112
    }
113
    $form .= "<input type='radio' name='options[0]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
114
    $chk  = '';
115
    if (0 == $options[0]) {
116
        $chk = " checked";
117
    }
118
    $form .= "&nbsp;<input type='radio' name='options[0]' value='0'" . $chk . ' >' . _NO . '<br>';
119
120
    $form .= _MB_XOOPSMEMBERS_SHOWRECENTLOGINAVATAR . '&nbsp;';
121
    if (1 == $options[1]) {
122
        $chk = " checked";
123
    }
124
    $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
125
    $chk  = '';
126
    if (0 == $options[1]) {
127
        $chk = " checked";
128
    }
129
    $form .= "&nbsp;<input type='radio' name='options[1]' value='0'" . $chk . ' >' . _NO . '<br>';
130
131
    $form .= _MB_XOOPSMEMBERS_USEREALNAME . '&nbsp;';
132
    if (1 == $options[2]) {
133
        $chk = " checked";
134
    }
135
    $form .= "<input type='radio' name='options[2]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
136
    $chk  = '';
137
    if (0 == $options[2]) {
138
        $chk = " checked";
139
    }
140
    $form .= "&nbsp;<input type='radio' name='options[2]' value='0'" . $chk . ' >' . _NO . '<br>';
141
142
    $form .= _MB_XOOPSMEMBERS_MEMBERDISPLAY . '&nbsp;';
143
    $form .= "<input type='text' name='options[3]' value='" . $options[3] . "'>";
144
    return $form;
145
}
146
147
148