Completed
Pull Request — master (#547)
by Richard
09:44
created

stats.php ➔ dumpArray()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 9
nop 2
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * admin/about.php
5
 *
6
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
7
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
8
 * @since      1.0
9
 * @author     Richard Griffith <[email protected]>
10
 * @package    gwiki
11
 */
12
13
include __DIR__ . '/../../../mainfile.php';
14
$mydirname = basename( dirname(__DIR__) ) ;
15
$mydirpath = dirname(__DIR__) ;
16
require $mydirpath.'/mytrustdirname.php' ; // set $mytrustdirname
17
18
require XOOPS_TRUST_PATH.'/modules/'.$mytrustdirname.'/admin/admin_header.php';
19
20
xoops_cp_header();
21
22
function dumpArray($array, $wrap = null)
23
{
24
    $firstTime = true;
25
    $string = '[';
26
    foreach ($array as $value) {
27
        $string .= (!$firstTime) ? ', ' : '';
28
        $firstTime = false;
29
        $wrap = ($wrap === null) ? ((is_int($value)) ? '' : '\'') : $wrap;
30
        $string .= $wrap . $value . $wrap;
31
    }
32
    $string .= ']';
33
    return $string;
34
}
35
36
$queryFormat = "SELECT `type`, '%s' as age, COUNT(*) as count FROM `" . $xoopsDB->prefix($mydirname . "_log")
37
    . "` WHERE `timestamp` > NOW() - INTERVAL %d SECOND GROUP BY `type`, 2 ";
38
39
$sql = '';
40
$sql .= sprintf($queryFormat, 'month', 30*24*60*60);
41
$sql .= 'UNION ALL ';
42
$sql .= sprintf($queryFormat, 'week', 7*24*60*60);
43
$sql .= 'UNION ALL ';
44
$sql .= sprintf($queryFormat, 'day', 24*60*60);
45
$sql .= 'UNION ALL ';
46
$sql .= sprintf($queryFormat, 'hour', 60*60);
47
48
$rawStats = array();
49
$rawStats['']['month'] = 0;
50
$rawStats['']['week'] = 0;
51
$rawStats['']['day'] = 0;
52
$rawStats['']['hour'] = 0;
53
$result = $xoopsDB->query($sql);
54
while ($row = $xoopsDB->fetchArray($result)) {
55
    $rawStats[$row['type']][$row['age']] = $row['count'];
56
}
57
$ages = array('month', 'week', 'day', 'hour');
58
$stats = [];
59
foreach ($rawStats as $type => $hits) {
60
    $stats[$type] = [];
61
}
62
ksort($stats);
63
$keys = array_keys($stats);
64
foreach ($keys as $type) {
65
    $count = [];
66
    foreach ($ages as $age) {
67
        $count[] = isset($rawStats[$type][$age]) ? (int)$rawStats[$type][$age] : 0;
68
    }
69
    $stats[$type] = $count;
70
}
71
72
//
73
// http://gionkunz.github.io/chartist-js/examples.html#example-bar-horizontal
74
$script = "new Chartist.Bar('.ct-chart', {\n";
75
$script .= '  labels: ' . dumpArray(array_keys($stats)) . ",\n";
76
$script .= '  series: ';
77
$allSets = [];
78
for ($i=0; $i<4; ++$i) {
79
    $newSet = [];
80
    foreach ($stats as $set) {
81
        $newSet[] = $set[$i] - (($i<3) ? $set[$i+1] : 0);
82
    }
83
    $allSets[] = dumpArray($newSet);
84
}
85
$series = dumpArray(array_reverse($allSets), '') . "\n";
86
$script .= $series;
87
//Xmf\Debug::dump($stats, $series);
88
89
$script .= <<<EOS
90
}, {
91
  seriesBarDistance: 10,
92
  reverseData: true,
93
  horizontalBars: true,
94
  stackBars: true,
95
  axisY: {
96
        offset: 80
97
  },
98
  axisX: {
99
    position: 'start',
100
    labelInterpolationFnc: function(value, index) {
101
      return Math.round(value);
102
    }
103
  }
104
});
105
EOS;
106
107
$GLOBALS['xoTheme']->addStylesheet('modules/protector/assets/css/chartist.min.css');
108
$GLOBALS['xoTheme']->addScript('modules/protector/assets/js/chartist.min.js');
109
$GLOBALS['xoTheme']->addScript('', [], $script);
110
$styles =<<<EOSS
111
.ct-series-a .ct-bar { stroke: grey; }
112
.ct-series-b .ct-bar { stroke: orange; }
113
.ct-series-c .ct-bar { stroke: yellow; }
114
.ct-series-d .ct-bar { stroke: red; }
115
.colorkeys {
116
  display: inline;
117
  width: 20px;
118
  height: 20px;
119
  margin: 5px;
120
  border: 1px solid rgba(0, 0, 0, .2);
121
}
122
123
.color-series-a { background: grey; }
124
.color-series-b { background: orange; }
125
.color-series-c { background: yellow; }
126
.color-series-d { background: red; }
127
EOSS;
128
$GLOBALS['xoTheme']->addStylesheet('', [], $styles);
129
130
131
$moduleAdmin = \Xmf\Module\Admin::getInstance();
132
133
$moduleAdmin->displayNavigation(basename(__FILE__));
134
135
echo '<h3>' . _AM_ADMINSTATS_TITLE . '</h3>';
136
echo '<div class="ct-chart xct-minor-seventh"></div>';
137
echo '<script>'. $script .'</script>';
138
139
echo '<div class="right">'
140
    . '<div class="colorkeys color-series-a">&nbsp;&nbsp;&nbsp;</div><span>Last Month </span>'
141
    . '<div class="colorkeys color-series-b">&nbsp;&nbsp;&nbsp;</div><span>Last Week </span>'
142
    . '<div class="colorkeys color-series-c">&nbsp;&nbsp;&nbsp;</div><span>Last Day </span>'
143
    . '<div class="colorkeys color-series-d">&nbsp;&nbsp;&nbsp;</div><span>Last Hour</span>'
144
    . '</div>';
145
146
/*
147
148
new Chartist.Bar('.ct-chart', {
149
  labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
150
  series: [
151
    [5, 4, 3, 7, 5, 15, 8],
152
    [5, 4, 3, 7, 5, 10, 3],
153
    [3, 2, 9, 5, 4, 6, 4]
154
]
155
}, {
156
    seriesBarDistance: 10,
157
  reverseData: true,
158
  horizontalBars: true,
159
  axisY: {
160
        offset: 70
161
  }
162
});
163
*/
164
xoops_cp_footer();
165
166
/*
167
SELECT `type`, 'month', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 30*24*60*60 GROUP BY `type`, 2
168
UNION ALL
169
SELECT `type`, 'week', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 7*24*60*60 GROUP BY `type`, 2
170
UNION ALL
171
SELECT `type`, 'day', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 24*60*60 GROUP BY `type`, 2
172
UNION ALL
173
SELECT `type`, 'hour', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 60*60 GROUP BY `type`, 2
174
*/