More rewrites, and output-mode fixed (we should documentate this)
[mailer.git] / inc / modules / guest / what-stats.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 12/14/2003 *
4  * ================                             Last change: 08/22/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-stats.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Statistics                                       *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Statistiken                                      *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // Add description as navigation point
45 addMenuDescription('guest', __FILE__);
46
47 // Derterminate which stats we want and set mode and title for the link below stats block
48 if (!isGetRequestElementSet('mode')) setRequestGetElement('mode', strtolower(getConfig('guest_stats')));
49
50 // Set config temporarily
51 setConfigEntry('guest_stats', strtoupper(getRequestElement('mode')));
52
53 switch (getRequestElement('mode')) {
54         case 'members' :
55                 $lmode = 'modules';
56                 break;
57
58         case 'modules' :
59                 $lmode = 'members';
60                 break;
61
62         case 'inactive':
63                 $lmode = 'inactive';
64                 break;
65
66         default:
67                 // Unsupported mode
68                 debug_report_bug(sprintf("Unsupported mode <strong>%s</strong> detected.", secureString(getRequestElement('mode'))));
69                 break;
70 }
71
72 // Set link title
73 $ltitle = getMessage('GUEST_STATS_' . strtoupper($lmode));
74
75 // @TODO This can be rewritten in a dynamic include
76 switch (getConfig('guest_stats')) {
77         case 'MEMBERS': // Statistics about your members
78                 // Members yesterday / today online
79                 $ymem = SQL_NUMROWS(SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `last_online` >= ".getConfig('START_YDAY')." AND `last_online` < ".getConfig('START_TDAY')." AND `status`='CONFIRMED'", __FILE__, __LINE__));
80                 $tmem = SQL_NUMROWS(SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `last_online` >= ".getConfig('START_TDAY')." AND `status`='CONFIRMED'", __FILE__, __LINE__));
81
82                 // Yesterday / today registered
83                 $yreg = SQL_NUMROWS(SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `joined` >= ".getConfig('START_YDAY')." AND `joined` < ".getConfig('START_TDAY'), __FILE__, __LINE__));
84                 $treg = SQL_NUMROWS(SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `joined` >= ".getConfig('START_TDAY'), __FILE__, __LINE__));
85
86                 // Only males / females
87                 $male   = countSumTotalData('M', 'user_data', 'userid', 'gender', true, " AND `status`='CONFIRMED'");
88                 $female = countSumTotalData('F', 'user_data', 'userid', 'gender', true, " AND `status`='CONFIRMED'");
89
90                 // Unconfirmed accounts
91                 $unconfirmed = SQL_NUMROWS(SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `status` != 'CONFIRMED'", __FILE__, __LINE__));
92
93                 // Total members
94                 $total = $male + $female;
95
96                 // List every month
97                 $months = array();
98                 for ($idx = 1; $idx < 13; $idx++) {
99                         // Copy it so we don't touch the for() loop index
100                         $month = $idx;
101
102                         // Append leading zero
103                         if ($idx < 10) $month = 0 . $idx;
104
105                         // Count months
106                         $months[$month] = countSumTotalData(bigintval($month), 'user_data', 'userid', 'birth_month', true, " AND `status`='CONFIRMED'");
107                 } // END - for
108
109                 // Members in categories
110                 $result = SQL_QUERY("SELECT `id`, `cat` FROM `{?_MYSQL_PREFIX?}_cats` WHERE `visible`='Y' ORDER BY `id` ASC", __FILE__, __LINE__);
111
112                 // Load categories first
113                 $cats = array(); $cat_cnt = array();
114
115                 // @TODO This can be somehow rewritten
116                 while ($content = SQL_FETCHARRAY($result)) {
117                         // Simple...
118                         $cats[$content['id']] = $content['cat'];
119                 } // END - while
120
121                 // Now we have all categories loaded, count members
122                 foreach ($cats as $id => $dummy) {
123                         // We only need id and nothing more to count...
124                         $cat_cnt[$id] = countSumTotalData(bigintval($id), 'user_cats', 'id', 'cat_id', true);
125                 } // END - foreach
126
127                 // Prepare data for the template
128                 $content['total_users']   = $total;
129                 $content['unconfirmed']   = $unconfirmed;
130                 $content['total_males']   = $male;
131                 $content['total_females'] = $female;
132                 $content['tmem_count']    = $tmem;
133                 $content['ymem_count']    = $ymem;
134                 $content['treg_count']    = $treg;
135                 $content['yreg_count']    = $yreg;
136                 $content['lmode']         = $lmode;
137                 $content['ltitle']        = $ltitle;
138
139                 // Generate monthly stats
140                 $SW = 2; $r2 = ' right2'; $l = 'll'; $r = 'lr'; $OUT = '';
141                 foreach ($months as $month => $cnt) {
142                         if ($SW == 2) $OUT .= "<tr>\n";
143
144                         // Prepare data for template
145                         $data = array(
146                                 'l_class'  => $l,
147                                 'm_descr'  => $GLOBALS['month_descr'][$month],
148                                 'r_class'  => $r,
149                                 'r2_class' => $r2,
150                                 'cnt'      => $cnt
151                         );
152
153                         // Load row template
154                         $OUT .= loadTemplate('guest_stats_month_row', true, $data);
155
156                         if ($SW == 2) {
157                                 $r2 = '';
158                                 $l = 'rl'; $r = 'rr';
159                         } else {
160                                 $OUT .= "</tr>\n";
161                                 $r2 = ' right2';
162                                 $l = 'll'; $r = 'lr';
163                         }
164                         $SW = 3 - $SW;
165                 }
166                 $content['month_rows'] = $OUT;
167
168                 // Generate category stats
169                 $OUT = ''; $SW = 2;
170                 foreach ($cat_cnt as $id => $cnt) {
171                         // Prepare data for the template
172                         $data = array(
173                                 'sw'  => $SW,
174                                 'cat' => $cats[$id],
175                                 'cnt' => $cnt,
176                         );
177
178                         // Load row template and switch colors
179                         $OUT .= loadTemplate('guest_stats_cats_row', true, $data);
180                         $SW = 3 - $SW;
181                 }
182                 $content['cats_rows'] = $OUT;
183
184                 // Load final template
185                 loadTemplate('guest_stats_member', false, $content);
186                 break;
187
188         case 'MODULES': // TOP10 module clicks
189                 // Admins may see all menus
190                 $AND = " AND `locked`='N' AND `visible`='Y'";
191                 if (isAdmin()) $AND = '';
192
193                 // Query for guest and member menus
194                 $guest_t10 = SQL_QUERY("SELECT `counter`, `title` FROM `{?_MYSQL_PREFIX?}_guest_menu` WHERE `counter` > 0".$AND." ORDER BY `counter` DESC LIMIT 0,10", __FILE__, __LINE__);
195                 $mem_t10   = SQL_QUERY("SELECT `counter`, `title` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE `counter` > 0".$AND." ORDER BY `counter` DESC LIMIT 0,10", __FILE__, __LINE__);
196                 $OUT = ''; 
197
198                 if (SQL_NUMROWS($guest_t10) > 0) {
199                         // Guest clicks
200                         $OUT .= loadTemplate('guest_stats_header', true, getMessage('GUEST_TOP_GUEST_STATS'));
201                         $SW = 2;
202                         while ($content = SQL_FETCHARRAY($guest_t10)) {
203                                 $content['sw'] = $SW;
204
205                                 // Load row template
206                                 $OUT .= loadTemplate('guest_stats_row', true, $content);
207                                 $SW = 3 - $SW;
208                         } // END - while
209                 } // END - if
210
211                 if (SQL_NUMROWS($mem_t10) > 0) {
212                         // Member clicks
213                         $OUT .= loadTemplate('guest_stats_header', true, getMessage('GUEST_TOP_MEMBER_STATS'));
214                         $SW = 2;
215                         while ($content = SQL_FETCHARRAY($mem_t10)) {
216                                 $content['sw'] = $SW;
217
218                                 // Load row template
219                                 $OUT .= loadTemplate('guest_stats_row', true, $content);
220                                 $SW = 3 - $SW;
221                         } // END - while
222                 } // END - if
223
224                 if ((SQL_NUMROWS($guest_t10) > 0) || (SQL_NUMROWS($mem_t10) > 0)) {
225                         // Prepare content
226                         $content = array(
227                                 'rows'   => $OUT,
228                                 'lmode'  => $lmode,
229                                 'ltitle' => $ltitle
230                         );
231
232                         // Load final template
233                         loadTemplate('guest_stats_table', false, $content);
234                 } else {
235                         // No clicks detected
236                         loadTemplate('admin_settings_saved', false, getMessage('GUEST_STATS_NO_CLICKS'));
237                 }
238                 break;
239
240         case 'INACTIVE': // Deactivated stats
241                 loadTemplate('admin_settings_saved', false, getMessage('GUEST_STATS_DEACTIVATED'));
242                 break;
243 }
244
245 // [EOF]
246 ?>