| Conditions | 16 |
| Paths | 6 |
| Total Lines | 83 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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; |
||
|
|
|||
| 23 | case 'hours': |
||
| 24 | $hours = floor($difference/3600); |
||
| 25 | $difference = $difference % 3600; |
||
| 26 | $time['hours'] = $hours; |
||
| 27 | case 'minutes': |
||
| 28 | $minutes = floor($difference/60); |
||
| 29 | $difference = $difference % 60; |
||
| 30 | $time['minutes'] = $minutes; |
||
| 31 | case 'seconds': |
||
| 32 | $seconds = $difference; |
||
| 33 | $time['seconds'] = $seconds; |
||
| 34 | } |
||
| 35 | |||
| 36 | $output = array(); |
||
| 37 | if(is_array($time)) { |
||
| 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 '; |
||
| 65 | $x++; |
||
| 66 | |||
| 67 | |||
| 68 | $block['recentlogin'][] = $recentlogin; |
||
| 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 | } |
||
| 136 |