Passed
Push — master ( 4eb8b5...c6e41d )
by Michael
07:19 queued 04:35
created

ratings.php (2 issues)

1
<?php
2
3
// $Id$
4
//  ------------------------------------------------------------------------ //
5
//                XOOPS - PHP Content Management System                      //
6
//                    Copyright (c) 2000 XOOPS.org                           //
7
//                       <https://xoops.org>                             //
8
// ------------------------------------------------------------------------- //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  You may not change or alter any portion of this comment or credits       //
15
//  of supporting developers from this source code or any supporting         //
16
//  source code which is considered copyrighted (c) material of the          //
17
//  original comment or credit authors.                                      //
18
//                                                                           //
19
//  This program is distributed in the hope that it will be useful,          //
20
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
21
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
22
//  GNU General Public License for more details.                             //
23
//                                                                           //
24
//  You should have received a copy of the GNU General Public License        //
25
//  along with this program; if not, write to the Free Software              //
26
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
27
//  ------------------------------------------------------------------------ //
28
29
/**
30
 * Display player ratings.
31
 *
32
 * @package    chess
33
 * @subpackage ratings
34
 */
35
36
/**#@+
37
 */
38
39
$GLOBALS['xoopsOption']['template_main'] = 'chess_ratings.tpl';
40
require __DIR__ . '/header.php';
41
42
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
43
require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
44
require_once XOOPS_ROOT_PATH . '/modules/chess/include/constants.inc.php';
45
require_once XOOPS_ROOT_PATH . '/modules/chess/include/functions.php';
46
require_once XOOPS_ROOT_PATH . '/modules/chess/include/ratings.php';
47
48
// check whether the rating feature is enabled
49
if ('none' == chess_moduleConfig('rating_system')) {
50
    redirect_header(XOOPS_URL . '/modules/chess/index.php', _CHESS_REDIRECT_DELAY_FAILURE, _MD_CHESS_RATINGS_OFF);
51
}
52
53
$xoopsConfig['module_cache'][$xoopsModule->getVar('mid')] = 0; // disable caching
54
55
// user input
56
$submit_recalc_ratings  = isset($_POST['submit_recalc_ratings']);
57
$confirm_recalc_ratings = (int)@$_POST['confirm_recalc_ratings'];
58
$start                  = (int)@$_GET['start']; // for page nav: offset of first row of results to display (default to 0)
59
60
#var_dump($_REQUEST);#*#DEBUG#
61
62
// If form-submit, check security token.
63
if ($submit_recalc_ratings && is_object($GLOBALS['xoopsSecurity']) && !$GLOBALS['xoopsSecurity']->check()) {
64
    redirect_header(
65
        XOOPS_URL . '/modules/chess/ratings.php',
66
        _CHESS_REDIRECT_DELAY_FAILURE,
67
        _MD_CHESS_TOKEN_ERROR . '<br>' . implode('<br>', $GLOBALS['xoopsSecurity']->getErrors())
68
    );
69
}
70
71
$msg       = '';
72
$msg_class = '';
73
74
// If arbiter requested recalculation of ratings, do it.
75
if ($submit_recalc_ratings && is_object($xoopsUser) && $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
76
    if ($confirm_recalc_ratings) {
77
        chess_recalc_ratings();
78
79
        $msg = _MD_CHESS_RECALC_DONE;
80
81
        $msg_class = 'resultMsg';
82
    } else {
83
        $msg = _MD_CHESS_RECALC_NOT_DONE;
84
85
        $msg_class = 'errorMsg';
86
    }
87
}
88
89
// Display the ratings.
90
chess_ratings($start, $msg, $msg_class);
91
92
require_once XOOPS_ROOT_PATH . '/footer.php';
93
/**#@-*/
94
95
/**
96
 * Display all the players' ratings.
97
 *
98
 * @param int    $start     Starting offset for page navigator
99
 * @param string $msg       Message to display (if non-empty string)
100
 * @param string $msg_class CSS class for displaying message text
101
 */
102
function chess_ratings($start = 0, $msg = '', $msg_class = 'errorMsg')
103
{
104
    global $xoopsDB, $xoopsUser, $xoopsModule, $xoopsTpl;
105
106
    // Display ratings.
107
108
    // Get maximum number of items to display on a page, and constrain it to a reasonable value.
109
110
    $max_items_to_display = chess_moduleConfig('max_items');
111
112
    $max_items_to_display = min(max($max_items_to_display, 1), 1000);
113
114
    $games_table = $xoopsDB->prefix('chess_games');
0 ignored issues
show
The assignment to $games_table is dead and can be removed.
Loading history...
115
116
    $ratings_table = $xoopsDB->prefix('chess_ratings');
117
118
    // Two queries are performed, one without a limit clause to count the total number of rows for the page navigator,
119
120
    // and one with a limit clause to get the data for display on the current page.
121
122
    // SQL_CALC_FOUND_ROWS and FOUND_ROWS(), available in MySQL 4.0.0, provide a more efficient way of doing this.
123
124
    $result = $xoopsDB->query(
125
        "
126
        SELECT    COUNT(*)
127
        FROM      $ratings_table AS p
128
    "
129
    );
130
131
    [$num_items] = $xoopsDB->fetchRow($result);
132
133
    $xoopsDB->freeRecordSet($result);
134
135
    $pagenav = new XoopsPageNav($num_items, $max_items_to_display, $start, 'start');
136
137
    $result = $xoopsDB->query(
138
        "
139
        SELECT    player_uid, rating, (games_won+games_lost+games_drawn) AS games_played
140
        FROM      $ratings_table
141
        ORDER BY  rating DESC, player_uid ASC
142
        LIMIT     $start, $max_items_to_display
143
    "
144
    );
145
146
    // user IDs that will require mapping to usernames
147
148
    $userids = [];
149
150
    $players = [];
151
152
    while (false !== ($row = $xoopsDB->fetchArray($result))) {
153
        // save user IDs that will require mapping to usernames
154
155
        $userids[] = $row['player_uid'];
156
157
        $players[] = [
158
            'player_uid'   => $row['player_uid'],
159
            'rating'       => $row['rating'],
160
            'games_played' => $row['games_played'],
161
        ];
162
    }
163
164
    $xoopsDB->freeRecordSet($result);
165
166
    // get mapping of user IDs to usernames
167
168
    $memberHandler = xoops_getHandler('member');
169
170
    $criteria = new \Criteria('uid', '(' . implode(',', $userids) . ')', 'IN');
171
172
    $usernames = $memberHandler->getUserList($criteria);
0 ignored issues
show
The method getUserList() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
    /** @scrutinizer ignore-call */ 
173
    $usernames = $memberHandler->getUserList($criteria);
Loading history...
173
174
    // add usernames to $players
175
176
    foreach ($players as $k => $player) {
177
        $players[$k]['player_uname'] = $usernames[$player['player_uid']] ?? '?';
178
    }
179
180
    // Display form for arbiter to recalculate ratings.
181
182
    if (is_object($xoopsUser) && $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
183
        // security token added below
184
185
        $form = new XoopsThemeForm(_MD_CHESS_RECALC_RATINGS, 'form1', 'ratings.php');
186
187
        // checkbox (initially unchecked)
188
189
        $checked_value = 1;
190
191
        $checkbox_confirm_recalc_ratings = new XoopsFormCheckBox('', 'confirm_recalc_ratings', !$checked_value);
192
193
        $checkbox_confirm_recalc_ratings->addOption($checked_value, _MD_CHESS_RECALC_CONFIRM);
194
195
        $form->addElement($checkbox_confirm_recalc_ratings);
196
197
        $form->addElement(new XoopsFormButton('', 'submit_recalc_ratings', _MD_CHESS_SUBMIT_BUTTON, 'submit'));
198
199
        $form->assign($xoopsTpl);
200
201
        // security token
202
203
        // This method is used because form is templated.
204
205
        $xoopsTpl->assign('chess_xoops_request_token', is_object($GLOBALS['xoopsSecurity']) ? $GLOBALS['xoopsSecurity']->getTokenHTML() : '');
206
    }
207
208
    // Template variables
209
210
    $xoopsTpl->assign('chess_provisional_games', chess_ratings_num_provisional_games());
211
212
    $xoopsTpl->assign('chess_msg', $msg);
213
214
    $xoopsTpl->assign('chess_msg_class', $msg_class);
215
216
    $xoopsTpl->assign('chess_players_pagenav', $pagenav->renderNav());
217
218
    $xoopsTpl->assign('chess_players', $players);
219
}
220