Passed
Push — master ( b6c62e...3fdbae )
by Michael
07:59
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
require_once dirname(__DIR__, 2) . '/mainfile.php';
39
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
40
require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
41
require_once XOOPS_ROOT_PATH . '/modules/chess/include/constants.inc.php';
42
require_once XOOPS_ROOT_PATH . '/modules/chess/include/functions.php';
43
require_once XOOPS_ROOT_PATH . '/modules/chess/include/ratings.php';
44
45
// check whether the rating feature is enabled
46
if ('none' == chess_moduleConfig('rating_system')) {
47
    redirect_header(XOOPS_URL . '/modules/chess/index.php', _CHESS_REDIRECT_DELAY_FAILURE, _MD_CHESS_RATINGS_OFF);
48
}
49
50
$GLOBALS['xoopsOption']['template_main']                  = 'chess_ratings.tpl';
51
$xoopsConfig['module_cache'][$xoopsModule->getVar('mid')] = 0; // disable caching
52
require_once XOOPS_ROOT_PATH . '/header.php';
53
54
// user input
55
$submit_recalc_ratings  = isset($_POST['submit_recalc_ratings']);
56
$confirm_recalc_ratings = (int)@$_POST['confirm_recalc_ratings'];
57
$start                  = (int)@$_GET['start']; // for page nav: offset of first row of results to display (default to 0)
58
59
#var_dump($_REQUEST);#*#DEBUG#
60
61
// If form-submit, check security token.
62
if ($submit_recalc_ratings && is_object($GLOBALS['xoopsSecurity']) && !$GLOBALS['xoopsSecurity']->check()) {
63
    redirect_header(
64
        XOOPS_URL . '/modules/chess/ratings.php',
65
        _CHESS_REDIRECT_DELAY_FAILURE,
66
        _MD_CHESS_TOKEN_ERROR . '<br>' . implode('<br>', $GLOBALS['xoopsSecurity']->getErrors())
67
    );
68
}
69
70
$msg       = '';
71
$msg_class = '';
72
73
// If arbiter requested recalculation of ratings, do it.
74
if ($submit_recalc_ratings && is_object($xoopsUser) && $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
75
    if ($confirm_recalc_ratings) {
76
        chess_recalc_ratings();
77
78
        $msg = _MD_CHESS_RECALC_DONE;
79
80
        $msg_class = 'resultMsg';
81
    } else {
82
        $msg = _MD_CHESS_RECALC_NOT_DONE;
83
84
        $msg_class = 'errorMsg';
85
    }
86
}
87
88
// Display the ratings.
89
chess_ratings($start, $msg, $msg_class);
90
91
require_once XOOPS_ROOT_PATH . '/footer.php';
92
/**#@-*/
93
94
/**
95
 * Display all the players' ratings.
96
 *
97
 * @param int    $start     Starting offset for page navigator
98
 * @param string $msg       Message to display (if non-empty string)
99
 * @param string $msg_class CSS class for displaying message text
100
 */
101
function chess_ratings($start = 0, $msg = '', $msg_class = 'errorMsg')
102
{
103
    global $xoopsDB, $xoopsUser, $xoopsModule, $xoopsTpl;
104
105
    // Display ratings.
106
107
    // Get maximum number of items to display on a page, and constrain it to a reasonable value.
108
109
    $max_items_to_display = chess_moduleConfig('max_items');
110
111
    $max_items_to_display = min(max($max_items_to_display, 1), 1000);
112
113
    $games_table = $xoopsDB->prefix('chess_games');
0 ignored issues
show
The assignment to $games_table is dead and can be removed.
Loading history...
114
115
    $ratings_table = $xoopsDB->prefix('chess_ratings');
116
117
    // Two queries are performed, one without a limit clause to count the total number of rows for the page navigator,
118
119
    // and one with a limit clause to get the data for display on the current page.
120
121
    // SQL_CALC_FOUND_ROWS and FOUND_ROWS(), available in MySQL 4.0.0, provide a more efficient way of doing this.
122
123
    $result = $xoopsDB->query(
124
        "
125
        SELECT    COUNT(*)
126
        FROM      $ratings_table AS p
127
    "
128
    );
129
130
    [$num_items] = $xoopsDB->fetchRow($result);
131
132
    $xoopsDB->freeRecordSet($result);
133
134
    $pagenav = new XoopsPageNav($num_items, $max_items_to_display, $start, 'start');
135
136
    $result = $xoopsDB->query(
137
        "
138
        SELECT    player_uid, rating, (games_won+games_lost+games_drawn) AS games_played
139
        FROM      $ratings_table
140
        ORDER BY  rating DESC, player_uid ASC
141
        LIMIT     $start, $max_items_to_display
142
    "
143
    );
144
145
    // user IDs that will require mapping to usernames
146
147
    $userids = [];
148
149
    $players = [];
150
151
    while (false !== ($row = $xoopsDB->fetchArray($result))) {
152
        // save user IDs that will require mapping to usernames
153
154
        $userids[] = $row['player_uid'];
155
156
        $players[] = [
157
            'player_uid'   => $row['player_uid'],
158
            'rating'       => $row['rating'],
159
            'games_played' => $row['games_played'],
160
        ];
161
    }
162
163
    $xoopsDB->freeRecordSet($result);
164
165
    // get mapping of user IDs to usernames
166
167
    $memberHandler = xoops_getHandler('member');
168
169
    $criteria = new \Criteria('uid', '(' . implode(',', $userids) . ')', 'IN');
170
171
    $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

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