More SQL rewrites, TODO: Put all table and column names in backticks (`)
[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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Add description as navigation point
41 ADD_DESCR("guest", __FILE__);
42
43 // Derterminate which stats we want and set mode and title for the link below stats block
44 if (!isset($_GET['mode'])) $_GET['mode'] = strtolower($_CONFIG['guest_stats']);
45 switch ($_GET['mode'])
46 {
47         case "members": $_CONFIG['guest_stats'] = "MEMBERS"; $lmode = "modules"; $ltitle = GUEST_STATS_MODULES; break;
48         case "modules": $_CONFIG['guest_stats'] = "MODULES"; $lmode = "members"; $ltitle = GUEST_STATS_MEMBERS; break;
49 }
50
51 switch ($_CONFIG['guest_stats'])
52 {
53 case "MEMBERS": // Statistics about your members
54         // Members yesterday / today online
55         $ymem = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE last_online >= ".START_YDAY." AND last_online < ".START_TDAY." AND status='CONFIRMED'", __FILE__, __LINE__));
56         $tmem = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE last_online >= ".START_TDAY." AND status='CONFIRMED'", __FILE__, __LINE__));
57
58         // Yesterday / today registered
59         $yreg = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE joined >= ".START_YDAY." AND joined < ".START_TDAY, __FILE__, __LINE__));
60         $treg = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE joined >= ".START_TDAY, __FILE__, __LINE__));
61
62         // Only males / females
63         $male   = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE gender='M' AND status='CONFIRMED'", __FILE__, __LINE__));
64         $female = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE gender='F' AND status='CONFIRMED'", __FILE__, __LINE__));
65
66         // Unconfirmed accounts
67         $unconfirmed = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE status != 'CONFIRMED'", __FILE__, __LINE__));
68
69         // Total members
70         $total = $male + $female;
71
72         // List every month
73         $months = array();
74         for ($idx = 1; $idx < 13; $idx++)
75         {
76                 $month = $idx; if ($idx < 10) $month = "0".$idx;
77                 $months[$month] = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM `"._MYSQL_PREFIX."_user_data` WHERE birth_month=%s AND status='CONFIRMED'",
78                  array(bigintval($month)), __FILE__, __LINE__));
79         }
80
81         // Members in categories
82         $result = SQL_QUERY("SELECT id, cat FROM "._MYSQL_PREFIX."_cats WHERE visible='Y' ORDER BY id", __FILE__, __LINE__);
83
84         // Load categories first
85         $cats = array(); $cat_cnt = array();
86         while (list($id, $cat) = SQL_FETCHROW($result))
87         {
88                 // Simple...
89                 $cats[$id] = $cat;
90         }
91
92         // Now we have all categories loaded, count members
93         foreach ($cats as $id => $dummy)
94         {
95                 // We only need id and nothing more to count...
96                 $cat_cnt[$id] = SQL_NUMROWS(SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_cats WHERE cat_id=%s",
97                  array(bigintval($id)), __FILE__, __LINE__));
98         }
99
100         // Prepare data for the template
101         define('__TOTAL_USERS' , $total);
102         define('__UNCONFIRMED' , $unconfirmed);
103         define('__MALE_COUNT'  , $male);
104         define('__FEMALE_COUNT', $female);
105         define('__TMEM_COUNT'  , $tmem);
106         define('__YMEM_COUNT'  , $ymem);
107         define('__TREG_COUNT'  , $treg);
108         define('__YREG_COUNT'  , $yreg);
109         define('__LMODE_VALUE' , $lmode);
110         define('__LINK_TITLE'  , $ltitle);
111
112         // Generate monthly stats
113         $SW = 2; $r2 = " right2"; $l = "ll"; $r = "lr"; $OUT = "";
114         foreach ($months as $month => $cnt)
115         {
116                 if ($SW == 2) $OUT .= "<TR>\n";
117
118                 // Prepare data for template
119                 $content = array(
120                         'l_class'  => $l,
121                         'm_descr'  => $MONTH_DESCR[$month],
122                         'r_class'  => $r,
123                         'r2_class' => $r2,
124                         'cnt'      => $cnt
125                 );
126
127                 // Load row template
128                 $OUT .= LOAD_TEMPLATE("guest_stats_month_row", true, $content);
129
130                 if ($SW == 2)
131                 {
132                         $r2 = "";
133                         $l = "rl"; $r = "rr";
134                 }
135                  else
136                 {
137                         $OUT .= "</TR>\n";
138                         $r2 = " right2";
139                         $l = "ll"; $r = "lr";
140                 }
141                 $SW = 3 - $SW;
142         }
143         define('__MONTH_STATS_ROWS', $OUT);
144
145         // Generate category stats
146         $SW = 2; $OUT = "";
147         foreach ($cat_cnt as $id => $cnt)
148         {
149                 // Prepare data for the template
150                 $content = array(
151                         'sw'  => $SW,
152                         'cat' => $cats[$id],
153                         'cnt' => $cnt,
154                 );
155
156                 // Load row template and switch colors
157                 $OUT .= LOAD_TEMPLATE("guest_stats_cats_row", true, $content);
158                 $SW = 3 - $SW;
159         }
160         define('__CATS_STATS_ROWS', $OUT);
161
162         // Load final template
163         LOAD_TEMPLATE("guest_stats_member");
164         break;
165
166 case "MODULES": // TOP10 module clicks
167         $AND = "";
168         if (!IS_ADMIN()) $AND = " AND locked='N' AND visible='Y'";
169         $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__);
170         $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__);
171         if ((SQL_NUMROWS($guest_t10) > 0) || (SQL_NUMROWS($mem_t10) > 0))
172         {
173                 // Output header
174                 OUTPUT_HTML("<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"guest_table dashed\" width=\"310\">
175 <TR>
176   <TD align=\"center\" class=\"guest_stats_title bottom2\" colspan=\"2\"><STRONG>".GUEST_TOPTEN_STATS."</STRONG></TD>
177 </TR>");
178         }
179
180         if (SQL_NUMROWS($guest_t10) > 0)
181         {
182                 // Guest clicks
183                 OUTPUT_HTML("<TR>
184   <TD align=\"center\" class=\"guest_title2 bottom2\" colspan=\"2\">".GUEST_TOP_GUEST_STATS."</TD>
185 </TR>");
186                 $SW = 2;
187                 while (list($clicks, $title) = SQL_FETCHROW($guest_t10))
188                 {
189                         OUTPUT_HTML("<TR>
190   <TD class=\"switch_sw".$SW." bottom2 right2\" align=\"right\" width=\"250\">".$title."&nbsp;</TD>
191   <TD class=\"switch_sw".$SW." bottom2\" width=\"50\">&nbsp;".$clicks."</TD>
192 </TR>");
193                         $SW = 3 - $SW;
194                 }
195         }
196         if (SQL_NUMROWS($guest_t10) > 0)
197         {
198                 // Guest clicks
199                 OUTPUT_HTML("<TR>
200   <TD align=\"center\" class=\"guest_title2 bottom2\" colspan=\"2\">".GUEST_TOP_MEMBER_STATS."</TD>
201 </TR>");
202                 $SW = 2;
203                 while (list($clicks, $title) = SQL_FETCHROW($mem_t10))
204                 {
205                         OUTPUT_HTML("<TR>
206   <TD class=\"switch_sw".$SW." bottom2 right2\" align=\"right\" width=\"250\">".$title."&nbsp;</TD>
207   <TD class=\"switch_sw".$SW." bottom2\" width=\"50\">&nbsp;".$clicks."</TD>
208 </TR>");
209                         $SW = 3 - $SW;
210                 }
211         }
212         if ((SQL_NUMROWS($guest_t10) > 0) || (SQL_NUMROWS($mem_t10) > 0))
213         {
214                 // Output footer
215                 OUTPUT_HTML("<TR>
216   <TD align=\"center\" class=\"guest_stats_footer\" colspan=\"2\"><A href=\"".URL."/modules.php?module=index&amp;what=stats&amp;mode=".$lmode."\">".$ltitle."</A></TD>
217 </TR>
218 </TABLE>");
219         }
220         break;
221
222 case "INACTIVE": // Deactivated stats
223         LOAD_TEMPLATE("admin_settings_saved", false, "<STRONG>".GUEST_STATS_DEACTIVATED."</STRONG>");
224         break;
225 }
226
227 //
228 ?>