tk_stats_page.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Kiosk-style score page: tk_stats_page.php?user=<name>
  4. *
  5. * Changes from the current repo version:
  6. * - ?user= must be a plain username; it is escaped for SQL and for HTML
  7. * - optional shared secret: if the plugin pref `stats_key` is set, the page
  8. * needs ?key=<that value>. Until it is set the page stays open to anyone,
  9. * as before (it shows a student's scores and remaining minutes)
  10. * - an unknown user no longer crashes (count(false) is fatal on PHP 8)
  11. * - one GROUP BY query replaces the hand-built UNION; no subjects no longer
  12. * produces invalid SQL or undefined-variable notices
  13. * - a student with exactly 1 answer today now also gets the "games enabled"
  14. * line (the singular branch skipped it)
  15. * - `e107.e107_tkReport` -> MPREFIX; removed the leftover print_r('hi')
  16. */
  17. if (!defined('e107_INIT')) {
  18. require_once('../../class2.php');
  19. }
  20. if (!e107::isInstalled('timekeeper')) {
  21. return '';
  22. }
  23. // Optional shared secret (plugin pref `stats_key`).
  24. $requiredKey = (string) e107::getPlugPref('timekeeper', 'stats_key', '');
  25. if ($requiredKey !== '' && !hash_equals($requiredKey, (string) filter_input(INPUT_GET, 'key'))) {
  26. http_response_code(403);
  27. exit;
  28. }
  29. $userName = filter_input(INPUT_GET, 'user');
  30. if (!is_string($userName) || !preg_match('/^[A-Za-z0-9_.-]{1,50}$/', $userName)) {
  31. exit;
  32. }
  33. $sql = e107::getDb();
  34. $u = $sql->escape($userName);
  35. $reportTable = '`' . MPREFIX . 'tkReport`';
  36. // Settings row (unknown users show nothing).
  37. $settings = $sql->retrieve('timekeeper', '*', 'username="' . $u . '"');
  38. if (!is_array($settings)) {
  39. exit;
  40. }
  41. $minQuestions = (int) $settings['minQuestionCount'];
  42. // Score per subject, all time.
  43. $sql->gen("SELECT `subject`, COUNT(id) AS `count`,
  44. CAST((SUM(correct) / COUNT(id) * 100) AS DECIMAL(5,2)) AS percent
  45. FROM $reportTable WHERE `username` = '$u' GROUP BY `subject` ORDER BY `subject`");
  46. $subjectRows = [];
  47. while ($row = $sql->fetch()) {
  48. $subjectRows[] = $row;
  49. }
  50. $safeUser = htmlspecialchars($userName, ENT_QUOTES, 'UTF-8');
  51. $text = '<div><h1>Score for ' . $safeUser . '</h1>';
  52. $text .= "<table style='width:100%;text-align:center;'>"
  53. . "<thead><tr><td style='text-align:left'>Subject</td><td>Score</td><td>No.Questions</td></tr></thead>"
  54. . "<tbody>";
  55. foreach ($subjectRows as $row) {
  56. $text .= "<tr><td style='text-align:left'>" . htmlspecialchars(ucfirst($row['subject']), ENT_QUOTES, 'UTF-8') . '</td><td>' . htmlspecialchars($row['percent']) . '%</td><td>' . (int) $row['count'] . '</td></tr>';
  57. }
  58. $text .= '</tbody></table>';
  59. // Today.
  60. $sql->gen("SELECT COUNT(id) AS cntr, CAST((SUM(correct)/COUNT(id)*100) AS DECIMAL(5,2)) AS percent
  61. FROM $reportTable WHERE username = '$u'
  62. AND answereddatestamp >= UNIX_TIMESTAMP(CURRENT_DATE())
  63. AND answereddatestamp <= UNIX_TIMESTAMP(CURRENT_DATE() + INTERVAL 1 DAY)");
  64. $today = $sql->fetch();
  65. $answeredToday = is_array($today) ? (int) $today['cntr'] : 0;
  66. if ($answeredToday > 0) {
  67. $percent = htmlspecialchars($today['percent']);
  68. if ($answeredToday === 1) {
  69. $text .= '<p>1 question has been answered<br/>today with ' . $percent . '% accuracy.</p><br/>';
  70. } else {
  71. $text .= '<p>' . $answeredToday . ' questions have been answered<br/>today with ' . $percent . '% accuracy.</p><br/>';
  72. }
  73. if ($minQuestions <= $answeredToday) {
  74. $text .= '<label style="background: darkgreen;">Games enabled</label><br/>';
  75. $currentScore = $sql->retrieve('timekeeperScore', 'timekeeper_lifetimecorrect, timekeeper_lifetimequestions, timekeeper_minutes', 'timekeeper_username="' . $u . '"');
  76. $minutes = is_array($currentScore) ? (int) $currentScore['timekeeper_minutes'] : 0;
  77. $text .= '<label>You have ' . $minutes . ' minutes remaining.</label>';
  78. } else {
  79. $text .= '<label>Games disabled</label>';
  80. }
  81. }
  82. $text .= '</div>';
  83. echo '<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="60"></head>'
  84. . '<body style="background-color: #1a0000;color:white;">' . $text . '</body></html>';