-
Notifications
You must be signed in to change notification settings - Fork 2
/
rankingclass.php
909 lines (784 loc) · 38 KB
/
rankingclass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<?php
/**
* Class which calculates (and caches) the rankings for different views,
* e.g., weekend rankings, yearly rankings, alpine ranking, and so far
* and so on.
*
* @file rankingclass.php
* @author Reto Stauffer
* @date November 3, 2018
* @brief New more efficient ranking table calculation.
*/
/** Class which calculates (and caches) the rankings for different views,
* e.g., weekend rankings, yearly rankings, alpine ranking, and so far
* and so on.
*
* @param deadman (str)
* login name of the user which
* provides the points for players not having participated. On
* wetterturnier this user is known as "Sleepy" (default).
* @param max_points (int):
* maximum number of points per weekend.
* Used to compute the 'relative points' gained of the players
* given the ranking settings. Default is `200` as on wetterturnier.de.
* @param cache (bool)
* whether file cache is active or not. If `true` (default) the rankings
* will be stored (serialized) in the `cache` folder and will be re-used
* if another user makes the same request. Files will be kept for 10 minutes.
* Can be disabled if set to `false`. Note: requires write access to the
* `cache` folder within the wp-wetterturnier plugin directory!
*/
class wetterturnier_rankingObject {
/// Will contain a copy of the global $wpdb instance. Used as
/// class-internal reference for database requests.
private $wpdb;
/// Attribute to store the cityObject.
private $cityObj = NULL;
/// Maximum number of points per weekend.
private $max_points = 200;
# Whether or not the trend (increase/decrease in rank)
# should be calculated. Is controlled by set_tdates().
private $calc_trend;
# Used to generate the name of the cache files if $cache is enabled.
private $cachehash = "UNNAMED";
# WTuser object
private $WTuser;
# Used to store the ranking from prepare_ranking
private $ranking = NULL;
# Whether file caching should be used or not
private $cache = true;
# Name of the deadman.
private $deadman;
# Used to store some words to be able to provide multilingual
# output. Using php language files to store some words into the
# resulting JSON array to display the tables via jQuery.
private $dict;
function __construct($deadman = "Sleepy", $max_points = 200, $cache = true) {
global $wpdb; $this->wpdb = $wpdb;
global $WTuser;
# Check if access is granted
$this->WTuser = $WTuser;
$this->deadman = $deadman;
$this->max_points = $max_points;
$this->cache = $cache;
//default: one city
$this->cities = 1;
$this->dict = new stdClass();
$this->dict->older = __("Older", "wpwt");
$this->dict->newer = __("Newer", "wpwt");
//for leaderboard
$this->dict->place = __("place", "wpwt");
$this->dict->th = array(__("st","wpwt"),__("nd","wpwt"),__("rd","wpwt"),__("th","wpwt"));
$this->dict->trophy = array( 'first', 'second', 'third', 'none' );
$this->dict->p = __("points", "wpwt");
$this->dict->points = __("Points", "wpwt");
// only if d1d2
$this->dict->points_d1 = __("Sat","wpwt");
$this->dict->points_d2 = __("Sun","wpwt");
//normal rankings
$this->dict->trend = "+/-";
$this->dict->played = __("Part", "wpwt");
$this->dict->difference = __("Diff", "wpwt");
$this->dict->rank = __("Rank", "wpwt");
$this->dict->user = __("User", "wpwt");
$this->dict->max_points = __("The maximum score (total) for the ranking is", "wpwt");
$this->dict->statusbar = __("Statusbar","wpwt");
//only for eternal ranking
$this->dict->points_max = __("Max","wpwt");
$this->dict->points_mean = __("Mean","wpwt");
$this->dict->points_med = __("> Median","wpwt");
$this->dict->sd_ind = __("Ind. SD","wpwt");
$this->dict->won_weekends = __("Wins","wpwt");
$this->dict->top3 = __("Top3 (%)","wpwt");
$this->dict->won_seasons = __("Seasons (x/N)","wpwt");
$this->dict->total_tournaments = __("The total number of tournaments is","wpwt");
}
/* Set city/cities. Rankings can be computed for one specific city or
* for multiple cities (overall ranking for users participating in
* all cities, e.g., the 5-city-ranking).
*
* The input argument `$cityObj` can either be a single object of the
* class :php:class:`wetterturnier_cityObject` or an array containing
* multiple :php:class:`wetterturnier_cityObject` objects.
* This method has no return value, it simply stores the object internally
* which is then used later on when preparing the data/ranks.
*
* @param $cityObj (:php:class:`wetterturnier_cityObject` or an :obj:`array` of :php:class:`wetterturnier_cityObject`)
* defines for which city/cities the ranking should be calculated.
*
* See also :php:meth:`set_tdates`.
*/
public function set_cities( $cityObj ) {
$this->cityObj = $cityObj;
# for PHP < 7.3
if ( ! function_exists('is_countable') ) {
function is_countable($var) {
return ( is_array($var) || $var instanceof Countable );
}
}
$this->cities = (is_countable($this->cityObj)) ? count($this->cityObj) : 1;
}
/* Simply setting a type or name. Only used to define the cache file name.
* Default is "UNNAMED".
*
* @param type (str)
* name or hash used to create the cache files.
*/
public function set_cachehash( $type ) { $this->cachehash = str_replace(" ", "_", (string)$type); }
/* Store the date ranges for which the request should be made.
*
* @param from(int or stdClass)
* either a single integer
* for the first date (days since 1970-01-01) for the current rank,
* or an object. If it is an object we assume it contains four elements
* specifying 'from', 'to', 'from_prev', and 'to_prev'. Else all four
* iputs have to be given!
* @param to (Null or int)
* if input $from is an object this argument
* is simply ignored. Else should contain an integer with the last day
* (days since 1970-01-01) of the period for the current rank.
* @param from_prev (Null or int)
* if input $from is an object this argument
* is simply ignored. Else should contain an integer with the first day
* (days since 1970-01-01) of the period for the previouse rank. Used to
* compute the trend.
* @param to_prev (Null or int)
* if input $from is an object this argument
* is simply ignored. Else should contain an integer with the last day
* (days since 1970-01-01) of the period for the previouse rank. Used to
* compute the trend.
*
* See also :php:meth:`set_tdates`.
*/
public function set_tdates($from, $to=Null, $from_prev=Null, $to_prev=Null, $calc_trend=false) {
if ( ! is_object($from) ) {
$this->tdates = (object) array("from" => $from, "to" => $to,
"from_prev" => $from_prev, "to_prev" => $to_prev);
} else { $this->tdates = $from; }
if ( is_null($this->tdates->from_prev) | is_null($this->tdates->to_prev) ) {
$this->tdates->min = min($this->tdates->from, $this->tdates->to);
$this->tdates->max = max($this->tdates->from, $this->tdates->to);
$this->calc_trend = false;
} else {
$this->tdates->min = min($this->tdates->from, $this->tdates->to,
$this->tdates->from_prev, $this->tdates->to_prev);
$this->tdates->max = max($this->tdates->from, $this->tdates->to,
$this->tdates->from_prev, $this->tdates->to_prev);
$this->calc_trend = $calc_trend;
}
$this->tdates->latest = $this->WTuser->latest_tournament(floor(time() / 86400))->tdate;
$this->tdates->current = $this->WTuser->current_tournament(0, $check_access=false)->tdate;
// If 'max' > 'latest':
if ( property_exists($this->tdates, "latest") ) {
if ( $this->tdates->max > $this->tdates->latest ) {
$this->tdates->max = $this->tdates->latest;
}
}
$this->tdates->today = (int)floor(gmdate('U')/86400.);
# if today is a tournament day and === max: max -= 1 (not included)
if ($this->tdates->today == $this->tdates->current
AND $this->tdates->current == $this->tdates->max) {
$this->tdates->max -= 1;
}
}
/* Returns the data in a structured way. The 'data' are the points for each
* specific city/tournament_date/user where the 'user' nesting level is not
* requred (and therefore not returned) if input `$deadman = true`.
*
* @pararm deadman (bool)
* if `false` (default) all points will
* be returned. Else (if `true`) only the deadman points will
* be returned. Uses the 'deadman' argument from this object.
* If the deadman user cannot be found: return null such that players
* which have not participated simply get 0 points.
* Returns:
* A stdClass object of the following form, here one example
* with for only one tournament date (tdate).
* If input argument $deadman is `true`, only the deadman user will be loaded.
* >>> stdClass Object
* >>> (
* >>> [data] => stdClass Object
* >>> (
* >>> [ambot_lang] => stdClass Object
* >>> (
* >>> [tdate_17830] => 158.60000610351562
* >>> )
* >>> [DWD-MOS-Mix] => stdClass Object
* >>> (
* >>> [tdate_17830] => 152.8000030517578
* >>> )
* >>> [..next user..] ...
* >>>
* >>> )
* >>> [users] => Array
* >>> (
* >>> [0] => ambot_lang
* >>> [1] => DWD-MOS-Mix
* >>> [2] => ..next user..
* >>> )
* >>> [tdates] => Array
* >>> (
* >>> [0] => 17830
* >>> )
* >>> )
*/
private function _get_data_object( $deadman = false, $d1d2 = false, $type = "ranking" ) {
$prefix = $this->wpdb->prefix;
if ($type === "eternal") {
//if more than one city was given we look for the eternal all-city ranking (cityID = 0)
$cityID = ( $this->cities > 1 ) ? 0 : $this->cityObj->get("ID");
// get ranking from userstats
$sql = "SELECT u.ID, u.user_login, s.points_adj AS points, s.ranks_weekend AS ranks_weekend,\n" .
"s.sd_ind AS sd_ind, s.points_med AS points_med, s.max AS points_max, s.mean AS points_mean, s.part AS played\n" .
"FROM " . $prefix . "users AS u RIGHT OUTER JOIN " . $prefix .
"wetterturnier_userstats AS s ON u.ID = s.userID\n" .
"WHERE cityID = " .$cityID. " AND ID IS NOT NULL AND s.points_adj != 0 GROUP BY u.ID";
} else {
# If $deadman is set to true: only fetch deadman data
if ($deadman) {
$deadman = get_user_by( "login", $this->deadman );
}
$where_user = ($deadman) ? sprintf(" AND userID = %d \n", $deadman->ID) : " ";
# Where city
if ( is_array($this->cityObj) ) {
$tmp = array();
foreach ( $this->cityObj as $rec ) { array_push($tmp, (int)$rec->get("ID")); }
$where_city = sprintf("b.cityID IN (%s)", join(",", $tmp));
unset($tmp);
} else {
$where_city = sprintf("b.cityID = %d", (int)$this->cityObj->get("ID"));
}
# Where tdate
if ( $this->tdates->min == $this->tdates->max ) {
$where_tdate = "b.tdate = " . $this->tdates->max;
} else {
$where_tdate = "b.tdate BETWEEN ".$this->tdates->min . " AND " . $this->tdates->max;
}
# Just no need to load user_login for a known user!
$usercol = ($deadman) ? "" : "u.ID, u.user_login, ";
# Create SQL command
$sql = "SELECT b.tdate, " . $usercol . "SUM(b.points) AS points, ";
if ( $d1d2 ) {
$sql .= "SUM(b.points_d1) AS points_d1,\n" .
"SUM(b.points_d2) AS points_d2,\n";
}
$sql .= "COUNT(*) AS played\n" .
"FROM " . $prefix . "users AS u RIGHT OUTER JOIN\n" .
$prefix . "wetterturnier_betstat AS b\n" .
"ON u.ID=b.userID WHERE\n" .
$where_city . " AND " . $where_tdate . $where_user . "GROUP BY u.ID, b.tdate\n";
# If calculating the ranking for multiple
# cities we have to capsule the statement above:
if ( $this->cities > 1 ) {
$sql = sprintf("SELECT * FROM (\n%s\n) AS X WHERE X.played = %d",
$sql, $this->cities );
}
}
$dbres = $this->wpdb->get_results($sql);
# If deadman is requested: create one stdClass object containing
# the points for each tournament date, no need to add an extra
# nesting level containing the username.
if ( $deadman ) {
$res = new stdClass();
foreach ( $dbres as $rec ) {
# Append tournament date to city
$thash = sprintf("tdate_%d", $rec->tdate);
$res->$thash = $rec->points;
}
} else {
$res = (object)array("data"=>new stdClass(), "users"=>array(), "tdates"=>array());
foreach ( $dbres as $rec ) {
# Append user names and tournament dates
if ( ! in_array($rec->user_login, $res->users) ) {
array_push($res->users, $rec->user_login);
}
# User hash
$uhash = $rec->user_login;
if ( ! property_exists($res->data, $uhash) ) { $res->data->$uhash = new stdClass(); }
$res->data->$uhash->userID = $rec->ID;
if ( $type !== "eternal" ) {
if ( ! in_array($rec->tdate, $res->tdates ) ) {
array_push($res->tdates, $rec->tdate );
}
# Append tournament date to city
$thash = sprintf("tdate_%d", $rec->tdate);
if ( ! isset($res->data->$uhash->$thash) ) {
$res->data->$uhash->$thash = new stdClass();
}
$res->data->$uhash->$thash->points = $rec->points;
if ($d1d2) {
$res->data->$uhash->$thash->points_d1 = $rec->points_d1;
$res->data->$uhash->$thash->points_d2 = $rec->points_d2;
}
} else {
$ranks = $rec->ranks_weekend;
$ranks = explode( ",", $ranks );
$res->data->$uhash->won_weekends = $ranks[0];
$res->data->$uhash->top3 = array_sum( $ranks );
$res->data->$uhash->points = $rec->points;
$res->data->$uhash->played = $rec->played;
$res->data->$uhash->sd_ind = $rec->sd_ind;
$res->data->$uhash->points_max = $rec->points_max;
$res->data->$uhash->points_mean = $rec->points_mean;
$res->data->$uhash->points_med = $rec->points_med;
//$res->data->$uhash->won_seasons = $rec->won_seasons;
//$res->data->$uhash->played_seasons = $rec->played_seasons;
}
}
}
# for PHP < 7.3
if ( ! function_exists('is_countable') ) {
function is_countable($var) {
return ( is_array($var) || $var instanceof Countable );
}
}
if (! is_countable($this->cityObj)) {
$cityIDs = $this->cityObj->get("ID");
} else {
$cityIDs = array();
foreach ( $this->cityObj as $rec ) {
array_push($cityIDs, $rec->get("ID"));
}
$cityIDs = implode(",", $cityIDs);
}
$sql = "SELECT COUNT(DISTINCT(tdate)) AS c FROM " . $prefix .
"wetterturnier_betstat WHERE tdate BETWEEN " .
$this->tdates->from . " AND " . $this->tdates->max .
" AND cityID IN(" . $cityIDs . ")";
$res->ntournaments = $this->wpdb->get_row($sql)->c;
$sql = sprintf("SELECT COUNT(*) AS c FROM %swetterturnier_dates WHERE tdate BETWEEN %d AND %d AND status = 1",
$prefix, $this->tdates->from, $this->tdates->to);
$res->total_tournaments = $this->wpdb->get_row($sql)->c;
return $res;
}
/* Helper method, returns the tournament date before the first one requested
* for the ranking. If the first one for the ranking is the first, so that
* there is no previous, a :obj:`null` will be returned.
*/
private function _get_previous_tournament_date( $sort = "DESC" ) {
# Find tdate before $tdate->min for ranking
$sql = sprintf("SELECT DISTINCT(tdate) FROM %swetterturnier_betstat "
." WHERE tdate < %d ORDER BY tdate DESC LIMIT 1;",
$this->wpdb->prefix, $this->tdates->min);
$res = $this->wpdb->get_row($sql);
if ( $this->wpdb->num_rows == 0 ) {
return null;
}
return $res->tdate;
}
/* Helper method, returns the tournament date after the last tournament date
* requested. This is used to provide the link on the front page to navigate
* trough the ranking pages.
* If the last tournament is the current one (newest) a :obj:`null` will be returned.
*/
private function _get_later_tournament_date( ) {
# Find tdate before $tdate->min for ranking
$sql = sprintf("SELECT DISTINCT(tdate) FROM %swetterturnier_betstat " .
" WHERE tdate > %d ORDER BY tdate ASC LIMIT 1;", $this->wpdb->prefix,$this->tdates->max);
$res = $this->wpdb->get_row($sql);
if ( $this->wpdb->num_rows == 0 ) {
return null;
}
return $res->tdate;
}
/* Creates the admin link to modify the current user/station.
*
* @param type (str)
* user type (e.g., user or mitteltip)
* Obj (...): Either a wordpress user Object or a wetterturnier
* :class:`wetterturnier_stationObject` for the observations.
*
* @return Returns a string with a html element to place the edit button or
* an empty string if no editbuttion is needed (or not allowed as the user
* is no admin).
*/
private function _get_edit_button( $type, $Obj ) {
// If no admin: return
if ( ! current_user_can('manage_options') ) { return(""); }
// If mitteltip or sleepy: return
if ( $type === "mitteltip" || $type === "sleepy" ) { return(""); }
// If this is an observation entry (use $type = "obs")
if ( $type === "obs" ) {
return( sprintf("<span class='button small edit edit-obs' url='%s' "
."station='%d' cityID='%s' tdate='%d'></span>",
admin_url(), (int)$identifier,
$this->cityObj->get('ID'), $this->tdates->max));
} else {
return( sprintf("<span class='button small edit edit-bet' url='%s' "
."userID='%d' cityID='%s' tdate='%d'></span>",
admin_url(), $Obj->ID, $this->cityObj->get('ID'), $this->tdates->max));
}
}
/* Creates a button to view the bets of a user or group in detail
* (featherlight window)
*/
private function _get_detail_button( $userObj ) {
if ($userObj->display_name === "Sleepy") {
$span_class = "button sleepy";
} else {
$span_class = "button small detail";
}
return sprintf("<span class=\"" . $span_class . "\" userid=\"%d\" "
."cityid=\"%d\" tdate=\"%d\"></span>",
$userObj->ID, $this->cityObj->get("ID"), $this->tdates->max);
}
/** Returns the file name for the cache file (only used if cache is set
* to ``true``, see initialization arguments of this class).
*
* @return Returns the absolute path to the cache file.
*/
private function _get_cache_file_name() {
# Where city
if ( is_array($this->cityObj) ) {
$tmp = array();
foreach ( $this->cityObj as $rec ) { array_push($tmp, (string)$rec->get("ID") ); }
$city_hash = join(":", $tmp);
} else {
$city_hash = (string)$this->cityObj->get("ID");
}
# Where tdate
$tdate_hash = sprintf("%s-%s_%d-%d",
(is_null($this->tdates->from_prev)) ? "NULL" : sprintf("%d", $this->tdates->from_prev),
(is_null($this->tdates->from_prev)) ? "NULL" : sprintf("%d", $this->tdates->to_prev),
$this->tdates->from, $this->tdates->to);
# Return cache file
return(sprintf("%scache/wt-ranking_%s_%s_%s.json", plugin_dir_path(__FILE__),
$this->cachehash, $tdate_hash, $city_hash));
}
/* Assign rank to each value of the array $in.
* Pretty cool function I wrote, I think :).
*
* Args:
* in (array): Array containing as set of numeric values.
*
* Returns:
* Returns an array of the same length with ranks. Highest
* values of $in get rank 1, lower values get higher ranks.
* The same values are attributed to the same ranks.
* Ranks are re-used. Some ranks may not appear if some
* elements in $in do have the same value!
*/
private function _array_rank( $in ) {
# Keep input array "x" and replace values with rank.
# This preserves the order. Working on a copy called $x
# to set the ranks.
$x = $in; arsort($x);
# Initival values
$rank = 0; $hiddenrank = 0;
$hold = null;
foreach ( $x as $key=>$val ) {
# Always increade hidden rank
$hiddenrank += 1;
# If current value is lower than previous:
# set new hold, and set rank to hiddenrank.
if ( is_null($hold) || $val < $hold ) {
$rank = $hiddenrank;
$hold = $val;
}
# Set rank $rank for $in[$key]
$in[$key] = $rank;
}
return $in;
}
/* Prepares a ranking object based on the class attributes 'tdate' and
* 'cityObj' (also allowed for time periods and multiple cities at the
* same time). This function loads the data from the database and creates
* a structured object containing the user points and ranks.
* Given a valid deadman username the points will be filled with the
* points of deadman (known as Sleepy on wetterturnier.de), if not
* 0 points will be given for not participating at all.
*
* Note: if cache is set to true (see __construct method) the result
* will be stored in a file as a serialized object. If the file exists
* and is not older than 600 seconds the data will be loaded from the
* file rather than re-calculating the rankings based on the database.
* This can be used to reduce the server load, especially as most
* users do check the very same rankings over and over again.
* Requires a folder "wp-wetterturnier/cache" with rw permissions for
* the webserver user. Files are _not_ deleted via php, if you use
* the caching take care of removing the files in the cache folder
* every now and then to avoid filling the disc for nothing.
*
* @return No return! Stores the ranking object on the parent object itself.
* There are different ouptut methods to display/return the data.
*
* .. todo:: Explain caching.
*/
public function prepare_ranking( $type = "ranking", $d1d2 = true ) {
$calc_trend = $this->calc_trend;
if ( is_null($this->tdates) || is_null($this->cityObj) ) {
//echo "Sorry, cannot prepare ranking, tdate or cityObject not set!";
return null;
}
if ($type === "weekend") { $this->cache = false; }
# If caching is enabled: check if we can load the
# data from disc to dont re-calculate the ranking again.
if ( $this->cache ) {
$cache_file = $this->_get_cache_file_name();
if ( file_exists($cache_file) ) {
//TODO different caching times for each ranking type
//switch ($type)
# If newer than 15 minutes: load file
if ( time() - filemtime($cache_file) <= 900 ) {
$this->ranking = unserialize(file_get_contents($cache_file));
return(false);
}
}
}
# Loading deadman points. Whenever a player did not participate he/she
# will get these points. May return "NULL" if the deadman is not defined or eternal ranking.
$deadman = ($type === "eternal") ? NULL : $this->_get_data_object(true, false);
# Loading user data
$userdata = $this->_get_data_object(false, $d1d2=$d1d2, $type=$type);
$ranking = (object)array("pre"=>new stdClass(), "now"=>new stdClass());
# Prepare data
foreach ( $userdata->data as $user=>$data ) {
# Append user to $ranking object if not yet existing
if ( ! property_exists($ranking->pre, $user) ) {
if ( $this->calc_trend ) {
$ranking->pre->$user = (object)array( "played"=>0, "points"=>0 );
}
$ranking->now->$user = (object)array( "played"=>0, "points"=>0 );
if ($d1d2) { $ranking->now->$user->points_d1 = NULL; $ranking->now->$user->points_d2 = NULL; }
}
# Looping over the tournament dates
foreach ( $userdata->tdates as $tdate ) {
# Skip if in the future
if ( $tdate > $this->tdates->latest ) { continue; }
# Create hash for the object names
$thash = sprintf("tdate_%d", $tdate);
# Default: 0 points and not participated
$points = 0; $played = 0;
if ($d1d2) { $points_d1 = NULL; $points_d2 = NULL; }
# If user got points: use user points
if ( property_exists($data, $thash) ) {
$played = 1;
$points = $data->$thash->points;
if ( $d1d2 ) {
$points_d1 = $data->$thash->points_d1;
$points_d2 = $data->$thash->points_d2;
}
# Else check if deadman exists and has points for this
# specific tournament date ($thash).
} else if ( $deadman and property_exists($deadman, $thash) ) {
$points = $deadman->$thash;
}
# Adding points
# We have to check whether the points fall in the
# previous time period (from_prev, to_prev) or/and
# into the current time period (from, to).
# These points are used later on to calculate the
# trends (+/- ranks gained).
#
# Visual help
#
# from_prev to_prev
# +-----------------+
# +-----------------+
# from to
# -------------------------------> tdate axis
#
if ( $this->calc_trend ) {
if ( $tdate >= $this->tdates->from_prev and $tdate <= $this->tdates->to_prev ) {
$ranking->pre->$user->points += $points;
$ranking->pre->$user->played += $played;
}
}
if ( $tdate >= $this->tdates->from and $tdate <= $this->tdates->to ) {
$ranking->now->$user->points += $points;
$ranking->now->$user->played += $played;
if ($d1d2 and ! is_null($points_d1) ) {
$ranking->now->$user->points_d1 += $points_d1;
$ranking->now->$user->points_d2 += $points_d2;
}
}
}
// for eternal ranking we already have participation count and so on in db
if ( $type === "eternal" ) {
$ranking->now->$user->points = $data->points;
$ranking->now->$user->played = $data->played;
$ranking->now->$user->won_weekends = (int)$data->won_weekends;
$ranking->now->$user->top3 = $data->top3;
$ranking->now->$user->sd_ind = $data->sd_ind;
$ranking->now->$user->points_max = $data->points_max;
$ranking->now->$user->points_mean = $data->points_mean;
$ranking->now->$user->points_med = $data->points_med;
//$ranking->now->$user->won_seasons = $data->won_seasons;
}
#drop players who not participated at all in the ranking, workaround for strange bug
#TODO: investigate on this quirk, if clause should not be neccesary (actually)!
#could be useful for eternal list, maybe leave it?
if ( $ranking->now->$user->played == 0 ) { unset($ranking->now->$user); }
}
# If tdates->from == tdates-> to (only one weekend)
# we drop the players which have _not_ participated
# on this specific weekend. Else they would show up
# getting the sleepy-points.
if ( $this->tdates->from == $this->tdates->to ) {
foreach ( $ranking->now as $username=>$info ) {
# If the user has not participated the current
# weekend: kill from $ranking object.
if ( $info->played == 0 ) {
unset($ranking->now->$username);
unset($ranking->pre->$username);
}
}
}
# Extracting points to get rank
$rank = (object)array("now"=>array());
// Current rank
foreach ( $ranking->now as $rec ) {
array_push( $rank->now, round($rec->points,2) );
}
$rank->now = $this->_array_rank( $rank->now );
# Previous rank (to calculate the trend), if requested
if ( $this->calc_trend ) {
$rank->pre = array();
foreach ( $ranking->pre as $rec ) {
array_push( $rank->pre, round($rec->points,2) );
}
$rank->pre = $this->_array_rank( $rank->pre );
}
# Array of the same order as $rank containing usernames
$users = array();
foreach ( $ranking->now as $user=>$x ) { array_push($users, $user); }
# Looping in rank order
$order = $rank->now; asort($order);
$final = new stdClass();
$points_winner = NULL;
$total_tournaments = $userdata->total_tournaments;
$ntournaments = $userdata->ntournaments;
if ($type !== "eternal") {
$max_points = $this->max_points * $total_tournaments * $this->cities;
}
foreach ( $order as $idx=>$trash ) {
# Current user in loop (winner first)
$user = $users[$idx];
# Setting winner points, used to compute differences.
if ( is_null($points_winner) ) { $points_winner = $ranking->now->$user->points; }
if ($type === "eternal") { $max_points = $points_winner; }
# Appending data
$final->$user = new stdClass();
$final->$user->rank_now = $rank->now[$idx];
$final->$user->points_now = $this->WTuser->number_format($ranking->now->$user->points,1);
$final->$user->played_now = (int)$ranking->now->$user->played;
$final->$user->points_relative = ($max_points != 0) ? $ranking->now->$user->points
/ $max_points : 0;
$final->$user->points_diff = $this->WTuser->number_format($points_winner
- $ranking->now->$user->points, 1);
if ( $d1d2 ) {
if (is_null($ranking->now->$user->points_d1)) {
$final->$user->points_d1 = $final->$user->points_d2 = "-";
} else {
$final->$user->points_d1 = $this->WTuser->number_format($ranking->now->$user->points_d1,1);
$final->$user->points_d2 = $this->WTuser->number_format($ranking->now->$user->points_d2,1);
}
} else if ( $type === "eternal" ) {
$final->$user->sd_ind = $this->WTuser->number_format(
$ranking->now->$user->sd_ind, 1);
$final->$user->points_max = $this->WTuser->number_format(
$ranking->now->$user->points_max, 1);
$final->$user->points_mean = $this->WTuser->number_format(
$ranking->now->$user->points_mean, 1);
$final->$user->points_med = $this->WTuser->number_format(
$ranking->now->$user->points_med, 1);
$final->$user->won_weekends = $ranking->now->$user->won_weekends;
$final->$user->top3 = $this->WTuser->number_format(100 *
($ranking->now->$user->top3 / $ranking->now->$user->played), 1);
//$final->$user->won_seasons = $this->WTuser->number_format($ranking->now->$user->won_seasons);
}
if ( $this->calc_trend ) {
$final->$user->rank_pre = $rank->pre[$idx];
$final->$user->trend = $rank->pre[$idx] - $rank->now[$idx];
}
# Replace username with "user display name"
# and add userclass (for display) using the
# method :meth:`generalclass.get_user_display_class_and_name`.
$userObj = get_user_by( "login", $user );
$final->$user->userID = $userObj->ID;
$tmp = $this->WTuser->get_user_display_class_and_name($userObj->ID, $userObj);
$final->$user->display_name = $tmp->display_name;
$final->$user->userclass = $tmp->userclass;
// Create edit button for administrators, on in weekend rankings
if ( ! is_array($this->cityObj) and $type === "weekend" ) {
$final->$user->edit_button = $this->_get_edit_button( $tmp->userclass, $userObj );
$final->$user->detail_button = $this->_get_detail_button( $userObj );
}
# Loading additional information only for leaderboard
if ($type === "leading") {
$final->$user->avatar = get_wp_user_avatar($userObj->ID, 96);
$final->$user->avatar_link = sprintf(bbp_get_user_profile_url($userObj->ID));
}
// Getting profile link
$final->$user->profile_link = $this->WTuser->get_user_profile_link( $tmp );
}
unset($ranking);
unset($rank);
$this->ranking = new stdClass();
$this->ranking->meta = new stdClass();
$this->ranking->meta->has_trends = $this->calc_trend;
$this->ranking->meta->ntournaments = $ntournaments;
$this->ranking->meta->total_tournaments = $total_tournaments;
$this->ranking->meta->max_points = $max_points;
$this->ranking->meta->older = $this->tdates->older;
$this->ranking->meta->newer = $this->tdates->newer;
$this->ranking->meta->from = $this->WTuser->date_format($this->tdates->from);
$this->ranking->meta->to = $this->WTuser->date_format($this->tdates->to);
// If only one city:
if ( ! is_array($this->cityObj) ) {
$this->ranking->meta->city = $this->cityObj->get("name");
} else {
$names = array();
foreach ( $this->cityObj as $rec ) { array_push($names, $rec->get("name")); }
$this->ranking->meta->city = join(" ", $names);
}
// The data
$this->ranking->data = $final;
if ( is_plugin_active("polylang") ) {
$this->ranking->meta->lang = pll_get_current_language();
}
# Write data to cache file
if ( $this->cache ) {
if ( is_writable(dirname($cache_file)) ) {
file_put_contents($cache_file, serialize($this->ranking), LOCK_EX);
}
}
}
/**
* Prints the content of $this->ranking (or $this->ranking->data),
* just a development helper function!
*
* @param $data (bool)
* if ``false`` the whole object will be printed, if set to ``true``
* only the ``data`` is printed.
*/
public function print_r($data = false) {
print_r(($data) ? $this->ranking->data : $this->ranking );
die("Exit in development method \"print_r\"");
}
/**
* Public function which simply returns the internal object.
*
* @return Returns the structured object containing the data.
*/
public function return_obj() {
return($this->ranking);
}
/**
* Returns the ranking prepared by prepare_ranking as JSON string.
* This is what will be returned for the ajax requests.
*
* @return Returns JSON string containing data and meta information
* which is used by the :file:`js/wetterturnier.ranking.js` function
* ``show_ranking(..)`` to display the ranking table on the frontend.
*/
public function return_json( ) {
if ( is_null($this->ranking) ) {
return json_encode(array("error"=>"Data not prepared, prepare_ranking not called?"));
} else {
$this->ranking->dict = $this->dict;
return json_encode($this->ranking);
}
}
}
?>