A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[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 (!REQUEST_ISSET_GET(('mode'))) REQUEST_SET_GET('mode', strtolower(getConfig('guest_stats')));
45
46 switch (REQUEST_GET('mode')) {
47         case "members" :
48                 setConfigEntry('guest_stats', "MEMBERS");
49                 $lmode = "modules";
50                 $ltitle = getMessage('GUEST_STATS_MODULES');
51                 break;
52
53         case "modules" :
54                 setConfigEntry('guest_stats', "MODULES");
55                 $lmode = "members";
56                 $ltitle = getMessage('GUEST_STATS_MEMBERS');
57                 break;
58
59         case "inactive":
60                 setConfigEntry('guest_stats', "INACTIVE");
61                 $lmode = "inactive";
62                 $ltitle = getMessage('GUEST_STATS_INACTIVE');
63                 break;
64 }
65
66 switch (getConfig('guest_stats'))
67 {
68 case "MEMBERS": // Statistics about your members
69         // Members yesterday / today online
70         $ymem = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE last_online >= ".constant('START_YDAY')." AND last_online < ".constant('START_TDAY')." AND `status`='CONFIRMED'", __FILE__, __LINE__));
71         $tmem = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE last_online >= ".constant('START_TDAY')." AND `status`='CONFIRMED'", __FILE__, __LINE__));
72
73         // Yesterday / today registered
74         $yreg = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE joined >= ".constant('START_YDAY')." AND joined < ".constant('START_TDAY'), __FILE__, __LINE__));
75         $treg = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE joined >= ".constant('START_TDAY'), __FILE__, __LINE__));
76
77         // Only males / females
78         $male   = GET_TOTAL_DATA("M", "user_data", "userid", "gender", true, " AND `status`='CONFIRMED'");
79         $female = GET_TOTAL_DATA("F", "user_data", "userid", "gender", true, " AND `status`='CONFIRMED'");
80
81         // Unconfirmed accounts
82         $unconfirmed = SQL_NUMROWS(SQL_QUERY("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE status != 'CONFIRMED'", __FILE__, __LINE__));
83
84         // Total members
85         $total = $male + $female;
86
87         // List every month
88         $months = array();
89         for ($idx = 1; $idx < 13; $idx++) {
90                 // Copy it so we don't touch the for() loop index
91                 $month = $idx;
92
93                 // Append leading zero
94                 if ($idx < 10) $month = "0".$idx;
95
96                 // Count months
97                 $months[$month] = GET_TOTAL_DATA(bigintval($month), "user_data", "userid", "birth_month", true, " AND `status`='CONFIRMED'");
98         }
99
100         // Members in categories
101         $result = SQL_QUERY("SELECT id, cat FROM `{!_MYSQL_PREFIX!}_cats` WHERE `visible`='Y' ORDER BY `id`", __FILE__, __LINE__);
102
103         // Load categories first
104         $cats = array(); $cat_cnt = array();
105         // @TODO This can be somehow rewritten
106         while ($content = SQL_FETCHARRAY($result)) {
107                 // Simple...
108                 $cats[$content['id']] = $content['cat'];
109         }
110
111         // Now we have all categories loaded, count members
112         foreach ($cats as $id => $dummy) {
113                 // We only need id and nothing more to count...
114                 $cat_cnt[$id] = GET_TOTAL_DATA(bigintval($id), "user_cats", "id", "cat_id", true);
115         }
116
117         // Prepare data for the template
118         // @TODO Rewrite all these constants
119         define('__TOTAL_USERS' , $total);
120         define('__UNCONFIRMED' , $unconfirmed);
121         define('__MALE_COUNT'  , $male);
122         define('__FEMALE_COUNT', $female);
123         define('__TMEM_COUNT'  , $tmem);
124         define('__YMEM_COUNT'  , $ymem);
125         define('__TREG_COUNT'  , $treg);
126         define('__YREG_COUNT'  , $yreg);
127         define('__LMODE_VALUE' , $lmode);
128         define('__LINK_TITLE'  , $ltitle);
129
130         // Generate monthly stats
131         $SW = 2; $r2 = " right2"; $l = "ll"; $r = "lr"; $OUT = "";
132         foreach ($months as $month => $cnt) {
133                 if ($SW == 2) $OUT .= "<tr>\n";
134
135                 // Prepare data for template
136                 $content = array(
137                         'l_class'  => $l,
138                         'm_descr'  => $GLOBALS['month_descr'][$month],
139                         'r_class'  => $r,
140                         'r2_class' => $r2,
141                         'cnt'      => $cnt
142                 );
143
144                 // Load row template
145                 $OUT .= LOAD_TEMPLATE("guest_stats_month_row", true, $content);
146
147                 if ($SW == 2) {
148                         $r2 = "";
149                         $l = "rl"; $r = "rr";
150                 } else {
151                         $OUT .= "</tr>\n";
152                         $r2 = " right2";
153                         $l = "ll"; $r = "lr";
154                 }
155                 $SW = 3 - $SW;
156         }
157         define('__MONTH_STATS_ROWS', $OUT);
158
159         // Generate category stats
160         $OUT = ""; $SW = 2;
161         foreach ($cat_cnt as $id => $cnt) {
162                 // Prepare data for the template
163                 $content = array(
164                         'sw'  => $SW,
165                         'cat' => $cats[$id],
166                         'cnt' => $cnt,
167                 );
168
169                 // Load row template and switch colors
170                 $OUT .= LOAD_TEMPLATE("guest_stats_cats_row", true, $content);
171                 $SW = 3 - $SW;
172         }
173         define('__CATS_STATS_ROWS', $OUT);
174
175         // Load final template
176         LOAD_TEMPLATE("guest_stats_member");
177         break;
178
179 case "MODULES": // TOP10 module clicks
180         $AND = "";
181         if (!IS_ADMIN()) $AND = " AND `locked`='N' AND `visible`='Y'";
182         $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__);
183         $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__);
184         $OUT = "";
185         if ((SQL_NUMROWS($guest_t10) > 0) || (SQL_NUMROWS($mem_t10) > 0)) {
186                 // Output header
187                 // @TODO Rewrite this to one template and $OUT .= ....
188                 OUTPUT_HTML("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"guest_table dashed\" width=\"310\">
189 <tr>
190   <td align=\"center\" class=\"guest_stats_title bottom2\" colspan=\"2\"><strong>{--GUEST_TOPTEN_STATS--}</strong></td>
191 </tr>");
192         }
193
194         if (SQL_NUMROWS($guest_t10) > 0) {
195                 // Guest clicks
196                 OUTPUT_HTML("<tr>
197   <td align=\"center\" class=\"guest_title2 bottom2\" colspan=\"2\">{--GUEST_TOP_GUEST_STATS--}</td>
198 </tr>");
199                 $SW = 2;
200                 while ($content = SQL_FETCHARRAY($guest_t10)) {
201                         OUTPUT_HTML("<tr>
202   <td class=\"switch_sw".$SW." bottom2 right2\" align=\"right\" width=\"250\">".$content['title']."&nbsp;</td>
203   <td class=\"switch_sw".$SW." bottom2\" width=\"50\">&nbsp;".$content['counter']."</td>
204 </tr>");
205                         $SW = 3 - $SW;
206                 }
207         }
208
209         if (SQL_NUMROWS($guest_t10) > 0) {
210                 // Guest clicks
211                 OUTPUT_HTML("<tr>
212   <td align=\"center\" class=\"guest_title2 bottom2\" colspan=\"2\">{--GUEST_TOP_MEMBER_STATS--}</td>
213 </tr>");
214                 $SW = 2;
215                 while ($content = SQL_FETCHARRAY($mem_t10)) {
216                         OUTPUT_HTML("<tr>
217   <td class=\"switch_sw".$SW." bottom2 right2\" align=\"right\" width=\"250\">".$content['title']."&nbsp;</td>
218   <td class=\"switch_sw".$SW." bottom2\" width=\"50\">&nbsp;".$content['counter']."</td>
219 </tr>");
220                         $SW = 3 - $SW;
221                 }
222         }
223
224         if ((SQL_NUMROWS($guest_t10) > 0) || (SQL_NUMROWS($mem_t10) > 0)) {
225                 // Output footer
226                 OUTPUT_HTML("<tr>
227   <td align=\"center\" class=\"guest_stats_footer\" colspan=\"2\">
228     <a href=\"{!URL!}/modules.php?module=index&amp;what=stats&amp;mode=".$lmode."\">".$ltitle."</a>
229   </td>
230 </tr>
231 </table>");
232         }
233         break;
234
235 case "INACTIVE": // Deactivated stats
236         LOAD_TEMPLATE("admin_settings_saved", false, "<strong>{--GUEST_STATS_DEACTIVATED--}</strong>");
237         break;
238 }
239
240 //
241 ?>