fixed more language settings in email; marked more strings for translation
[e-DoKo.git] / include / stats.php
1 <?php
2 /* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 Arun Persaud <arun@nubati.net>
3  *
4  *   This file is part of e-DoKo.
5  *
6  *   e-DoKo is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   e-DoKo is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 /* make sure that we are not called from outside the scripts,
22  * use a variable defined in config.php to check this
23  */
24 if(!isset($HOST))
25   exit;
26
27 /* turn warnings off (problem with two warnings in function.php */
28 error_reporting(!E_ALL & ~E_NOTICE);
29
30 $name  = $_SESSION["name"];
31 $email = DB_get_email('name',$name);
32
33 $myid = DB_get_userid('email',$email);
34 if(!$myid)
35   return;
36
37 $PREF = DB_get_PREF($myid);
38 /* set language chosen in preferences*/
39 $_SESSION['language'] = $PREF['language'];
40 set_language($PREF['language']);
41
42 DB_update_user_timestamp($myid);
43
44 display_user_menu($myid);
45
46 /* check if cached version exist */
47 if( !$content = getCache("cache/stats.html",60*60*24) )
48 {
49   /* start caching */
50   ob_start();
51
52   /* start statistics*/
53   echo "<div class=\"user wide\">\n";
54
55   echo "<p>Generated ".date("Y-m-d H:i:s")." (server time) </p>";
56
57   /* total number of games */
58   echo "<p>The number of finished games on this server is: ";
59   $r = DB_query_array("SELECT COUNT(*) from Game".
60                       " WHERE status='gameover'");
61   $GameN =  $r[0];
62   echo " $GameN </p>\n";
63
64   echo "<p>The contra party wins in ";
65   $result = DB_query("SELECT COUNT(*) from Score".
66                      " WHERE score='againstqueens'");
67   while( $r = DB_fetch_array($result))
68     echo $r[0];
69   echo " games.</p>\n";
70
71   /* longest and shortest game */
72   $r=DB_query("SELECT timediff(mod_date,create_date) ,session,id".
73             " FROM Game WHERE status='gameover'".
74               " ORDER BY time_to_sec(timediff(mod_date,create_date)) ASC LIMIT 1");
75
76   if($r)
77     {
78       $short= DB_fetch_array($r);
79       $names = DB_get_all_names_by_gameid($short[2]);
80       echo "<p> The shortest game took only ".$short[0]." hours and was played by  ".join(", ",$names).".<br />\n";
81     }
82
83   $r=DB_query("SELECT datediff(mod_date,create_date) ,session,id".
84               " FROM Game WHERE status='gameover'".
85               " ORDER BY time_to_sec(timediff(mod_date,create_date)) DESC LIMIT 1");
86   if($r)
87     {
88       $long= DB_fetch_array($r);
89       echo "The longest game took ".$long[0]." days.</p>\n";
90     }
91
92   $r=DB_query("SELECT COUNT(*) as c, session, id FROM Game ".
93               " GROUP BY session ORDER BY c DESC LIMIT 1");
94   if($r)
95     {
96       $long  = DB_fetch_array($r);
97       $names = DB_get_all_names_by_gameid($long[2]);
98       echo "<p>The longest session is session ".$long[1]." with ".$long[0].
99         " games played by ".join(", ",$names).".</p>\n";
100     }
101
102
103   /* number of solos */
104   $result = DB_query_array_all("SELECT type,COUNT(*) as c from Game".
105                                " WHERE status='gameover'".
106                                " GROUP BY type".
107                                " ORDER BY c DESC");
108   array_unshift($result,array("Type","Frequency"));
109   echo output_table($result,"Game types","stats");
110
111   /* break up solos in types */
112   $result = DB_query_array_all("SELECT solo,COUNT(*) as c from Game".
113                                " WHERE status='gameover'".
114                                " AND type='solo'".
115                                " GROUP BY solo".
116                                " ORDER BY c DESC");
117   array_unshift($result,array("Type","Frequency"));
118   echo output_table($result,"Kind of solos","stats");
119
120   /*
121  2 top user mit maximaler quote an solo (min 10 games)
122
123  top scoring game: winning players
124
125  game with the same cards: show 3 at random:
126  player who won, points, what kind of game
127  select g1.id, g2.id from game g1 left join game g2 on g1.randomnumbers=g2.randomnumbers where g1.id<g2.id order by g1.id
128  select id from game where randomnumbers like "blablabl%"; the % is like .* in regexp
129  select id,type,solo,status from game where id in (select id from game where randomnumbers in (select randomnumbers from game where id=27));
130   */
131
132   /* number of calls*/
133   $result = DB_query_array_all("SELECT CONCAT_WS(' ',party,IFNULL(point_call,'no call')),COUNT(*) from Hand".
134                                " LEFT JOIN Game on Game.id=Hand.game_id ".
135                                " WHERE Game.status='gameover'".
136                                " GROUP BY party,point_call");
137   array_unshift($result,array("call","Frequency"));
138   echo output_table($result,"Kind of call","stats");
139
140
141   /* most reminders */
142   $result = DB_query_array_all("SELECT fullname, COUNT(*)  /" .
143                                "      (SELECT COUNT(*) FROM Hand".
144                                "       WHERE user_id=User.id) as c".
145                                " FROM Reminder".
146                                " LEFT JOIN User ON User.id=user_id".
147                                " GROUP BY user_id".
148                                " ORDER BY c DESC LIMIT 5" );
149   array_unshift($result,array("Name","Reminders"));
150   echo output_table($result,"Most reminders per game","stats");
151
152   /* fox */
153   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
154                                "      (SELECT COUNT(*) FROM Hand".
155                                "       WHERE user_id=User.id) as c".
156                                " FROM Score".
157                                " LEFT JOIN User ON User.id=winner_id".
158                                " WHERE score='fox'".
159                                " GROUP BY winner_id".
160                                " ORDER BY c DESC LIMIT 5" );
161   array_unshift($result,array("Name","Number of foxes caught"));
162   echo output_table($result,"Most caught foxes","stats");
163
164   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
165                                "      (SELECT COUNT(*) FROM Hand".
166                                "       WHERE user_id=User.id) as c".
167                                " FROM Score".
168                                " LEFT JOIN User ON User.id=looser_id".
169                                " WHERE score='fox'".
170                                " GROUP BY looser_id".
171                                " ORDER BY c DESC LIMIT 5" );
172   array_unshift($result,array("Name","Number of foxes lost"));
173   echo output_table($result,"Lost foxes (most)","stats");
174
175   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
176                                "      (SELECT COUNT(*) FROM Hand".
177                                "       WHERE user_id=User.id) as c".
178                                " FROM Score".
179                                " LEFT JOIN User ON User.id=looser_id".
180                                " WHERE score='fox'".
181                                " GROUP BY looser_id".
182                                " ORDER BY c ASC LIMIT 5" );
183   array_unshift($result,array("Name","Number of foxes lost"));
184   echo output_table($result,"Lost foxes (least)","stats");
185
186   /* which position wins the most tricks  */
187   $result = DB_query_array_all("SELECT CASE winner ".
188                                "   WHEN 1 THEN 'left' ".
189                                "   WHEN 2 THEN 'top' ".
190                                "   WHEN 3 THEN 'right' ".
191                                "   WHEN 4 THEN 'bottom' END,".
192                                " COUNT(*) AS c FROM Trick".
193                                " GROUP BY winner ".
194                                " HAVING LENGTH(winner)>0  ".
195                                " ORDER BY winner ASC " );
196   array_unshift($result,array("Position","Number of tricks"));
197   echo output_table($result,"Tricks at the table","stats");
198
199   /*
200  does the party win more often if they start
201
202  echo "<p>The party playing first wins in";
203  $result = mysql_query("SELECT COUNT(*) from Score".
204  " LEFT JOIN Game ON Game.id=game_id".
205  " WHERE score='againstqueens'".
206  " AND Game.status='gameover'".
207  " AND Game.type<>'solo'");
208  while( $r = mysql_fetch_array($result,MYSQL_NUM))
209  echo $r[1]." (".$r[0].") <br />\n";
210  echo " games</p>\n";
211   */
212   $result = generate_global_score_table();
213   array_unshift($result,array('Name','Average score per game','Total Points','Number of games', 'Active games',
214                               'Response Time [min]','Number of solos','Solos/game'));
215   echo output_table($result,'Players (need more than 10 games)','stats','ScoreTable');
216
217   /*
218    * how often is the last trick a non-trump trick
219    */
220
221   /* needs this so that all tables are within the div and don't float around */
222   echo "<p style=\"clear:both;\">&nbsp;</p>\n";
223
224   echo "</div>\n"; /* end output */
225
226   /* write file to cache */
227   $content = ob_get_contents();
228   ob_end_clean();
229   createCache($content,"cache/stats.html");
230 }
231
232 echo $content;
233
234 ?>
235