timekeeper_menu.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * TimeKeeper sidebar menu: the logged-in student's score per subject, today's
  4. * question count and play minutes remaining.
  5. *
  6. * Changes from the current repo version:
  7. * - "You have N minutes remaining" read the score row with $this->user,
  8. * which doesn't exist in a menu file, so the minutes were always blank;
  9. * it uses the logged-in user name now
  10. * - one GROUP BY query replaces the hand-built UNION: a student with no
  11. * answers yet produced "SELECT * FROM ;" (invalid SQL) and undefined
  12. * variable notices
  13. * - a student with exactly one answer today now also gets the "games
  14. * enabled" line (the singular branch skipped it), and the unclosed <p>
  15. * tags are closed
  16. * - a student with no settings row no longer errors (uses the default
  17. * minimum of 100 questions that timer.php creates new rows with)
  18. * - user name escaped for SQL, subjects/values escaped for HTML,
  19. * `e107.e107_tkReport` -> MPREFIX
  20. */
  21. if (!defined('e107_INIT')) {
  22. exit;
  23. }
  24. if (!e107::isInstalled('timekeeper')) {
  25. return '';
  26. }
  27. if (check_class("Main Admin") || check_class("Admin") || check_class("TIMEKEEPER_STUDENT")) {
  28. $sql = e107::getDb();
  29. $userName = e107::getUser()->getLoginName();
  30. $u = $sql->escape($userName);
  31. $reportTable = '`' . MPREFIX . 'tkReport`';
  32. $settingsHolder = $sql->retrieve('timekeeper', '*', 'username="' . $u . '"');
  33. $minQuestions = is_array($settingsHolder) ? (int) $settingsHolder['minQuestionCount'] : 100;
  34. // Score per subject, all time.
  35. $sql->gen("SELECT `subject`, COUNT(id) AS `count`,
  36. CAST((SUM(correct) / COUNT(id) * 100) AS DECIMAL(5,2)) AS percent
  37. FROM $reportTable WHERE `username` = '$u' GROUP BY `subject` ORDER BY `subject`");
  38. $subjectRows = [];
  39. while ($row = $sql->fetch()) {
  40. $subjectRows[] = $row;
  41. }
  42. $text = "<table style='width:100%;text-align:center;'>"
  43. . "<thead><tr><td style='text-align:left'>Subject</td><td>Score</td><td>No.Questions</td></tr></thead>"
  44. . "<tbody>";
  45. foreach ($subjectRows as $row) {
  46. $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>';
  47. }
  48. $text .= '</tbody></table>';
  49. // Today.
  50. $sql->gen("SELECT COUNT(id) AS cntr, CAST((SUM(correct)/COUNT(id)*100) AS DECIMAL(5,2)) AS percent
  51. FROM $reportTable WHERE username = '$u'
  52. AND answereddatestamp >= UNIX_TIMESTAMP(CURRENT_DATE())
  53. AND answereddatestamp <= UNIX_TIMESTAMP(CURRENT_DATE() + INTERVAL 1 DAY)");
  54. $today = $sql->fetch();
  55. $answeredToday = is_array($today) ? (int) $today['cntr'] : 0;
  56. if ($answeredToday > 0) {
  57. $percent = htmlspecialchars($today['percent']);
  58. if ($answeredToday === 1) {
  59. $text .= '<p>1 question has been answered<br/>today with ' . $percent . '% accuracy.</p><br/>';
  60. } else {
  61. $text .= '<p>' . $answeredToday . ' questions have been answered<br/>today with ' . $percent . '% accuracy.</p><br/>';
  62. }
  63. if ($minQuestions <= $answeredToday) {
  64. $text .= '<label style="background: Darkgreen;">Games enabled</label><br/>';
  65. $currentScore = $sql->retrieve('timekeeperScore', 'timekeeper_lifetimecorrect, timekeeper_lifetimequestions, timekeeper_minutes', 'timekeeper_username="' . $u . '"');
  66. $minutes = is_array($currentScore) ? (int) $currentScore['timekeeper_minutes'] : 0;
  67. $text .= '<label>You have ' . $minutes . ' minutes remaining.</label>';
  68. } else {
  69. $text .= '<label>Games disabled</label>';
  70. }
  71. }
  72. e107::getRender()->tablerender('Score', $text);
  73. }