1: <?php
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 <richard@geekwright.com>
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: $result = $xoopsDB->query($sql);
48: if (!$xoopsDB->isResultSet($result)) {
49: throw new \RuntimeException(
50: \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR
51: );
52: }
53:
54: $rawStats = array();
55: $rawStats['']['month'] = 0;
56: $rawStats['']['week'] = 0;
57: $rawStats['']['day'] = 0;
58: $rawStats['']['hour'] = 0;
59: while (false !== ($row = $xoopsDB->fetchArray($result))) {
60: $rawStats[$row['type']][$row['age']] = $row['count'];
61: }
62: $ages = array('month', 'week', 'day', 'hour');
63: $stats = array();
64: foreach ($rawStats as $type => $hits) {
65: $stats[$type] = array();
66: }
67: ksort($stats);
68: $keys = array_keys($stats);
69: foreach ($keys as $type) {
70: $count = array();
71: foreach ($ages as $age) {
72: $count[] = isset($rawStats[$type][$age]) ? (int)$rawStats[$type][$age] : 0;
73: }
74: $stats[$type] = $count;
75: }
76:
77: $height = (count($keys) + 1) * 24;
78:
79: //
80: // http://gionkunz.github.io/chartist-js/examples.html#example-bar-horizontal
81: $script = "new Chartist.Bar('.ct-chart', {\n";
82: $script .= ' labels: ' . dumpArray(array_keys($stats)) . ",\n";
83: $script .= ' series: ';
84: $allSets = array();
85: for ($i=0; $i<4; ++$i) {
86: $newSet = array();
87: foreach ($stats as $set) {
88: $newSet[] = $set[$i] - (($i<3) ? $set[$i+1] : 0);
89: }
90: $allSets[] = dumpArray($newSet);
91: }
92: $series = dumpArray(array_reverse($allSets), '') . "\n";
93: $script .= $series;
94: //Xmf\Debug::dump($stats, $series);
95:
96: $script .= <<<EOS
97: }, {
98: seriesBarDistance: 10,
99: reverseData: true,
100: horizontalBars: true,
101: stackBars: true,
102: height: $height,
103: axisY: {
104: offset: 120
105: },
106: axisX: {
107: position: 'start',
108: labelInterpolationFnc: function(value, index) {
109: return Math.round(value);
110: }
111: }
112: });
113: EOS;
114:
115: $GLOBALS['xoTheme']->addStylesheet('modules/protector/assets/css/chartist.min.css');
116: $GLOBALS['xoTheme']->addScript('modules/protector/assets/js/chartist.min.js');
117: $GLOBALS['xoTheme']->addScript('', array(), $script);
118: $styles =<<<EOSS
119: .ct-series-a .ct-bar { stroke: grey; }
120: .ct-series-b .ct-bar { stroke: orange; }
121: .ct-series-c .ct-bar { stroke: yellow; }
122: .ct-series-d .ct-bar { stroke: red; }
123: .colorkeys {
124: display: inline;
125: width: 20px;
126: height: 20px;
127: margin: 5px;
128: border: 1px solid rgba(0, 0, 0, .2);
129: }
130:
131: .color-series-a { background: grey; }
132: .color-series-b { background: orange; }
133: .color-series-c { background: yellow; }
134: .color-series-d { background: red; }
135: EOSS;
136: $GLOBALS['xoTheme']->addStylesheet('', array(), $styles);
137:
138:
139: $moduleAdmin = \Xmf\Module\Admin::getInstance();
140:
141: $moduleAdmin->displayNavigation(basename(__FILE__));
142:
143: echo '<h3>' . _AM_ADMINSTATS_TITLE . '</h3>';
144: echo '<div class="ct-chart xct-minor-seventh"></div>';
145: echo '<script>'. $script .'</script>';
146:
147: echo '<div class="right">'
148: . '<div class="colorkeys color-series-a">&nbsp;&nbsp;&nbsp;</div><span>' . _AM_ADMINSTATS_LAST_MONTH . ' </span>'
149: . '<div class="colorkeys color-series-b">&nbsp;&nbsp;&nbsp;</div><span>' . _AM_ADMINSTATS_LAST_WEEK . ' </span>'
150: . '<div class="colorkeys color-series-c">&nbsp;&nbsp;&nbsp;</div><span>' . _AM_ADMINSTATS_LAST_DAY . ' </span>'
151: . '<div class="colorkeys color-series-d">&nbsp;&nbsp;&nbsp;</div><span>' . _AM_ADMINSTATS_LAST_HOUR . '</span>'
152: . '</div>';
153:
154: /*
155:
156: new Chartist.Bar('.ct-chart', {
157: labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
158: series: [
159: [5, 4, 3, 7, 5, 15, 8],
160: [5, 4, 3, 7, 5, 10, 3],
161: [3, 2, 9, 5, 4, 6, 4]
162: ]
163: }, {
164: seriesBarDistance: 10,
165: reverseData: true,
166: horizontalBars: true,
167: axisY: {
168: offset: 70
169: }
170: });
171: */
172: xoops_cp_footer();
173:
174: /*
175: SELECT `type`, 'month', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 30*24*60*60 GROUP BY `type`, 2
176: UNION ALL
177: SELECT `type`, 'week', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 7*24*60*60 GROUP BY `type`, 2
178: UNION ALL
179: SELECT `type`, 'day', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 24*60*60 GROUP BY `type`, 2
180: UNION ALL
181: SELECT `type`, 'hour', COUNT(*) FROM `wvgw_protector_log` WHERE `timestamp` < NOW() - 60*60 GROUP BY `type`, 2
182: */
183: