| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- /**
- * TimeKeeper sidebar menu: the logged-in student's score per subject, today's
- * question count and play minutes remaining.
- *
- * Changes from the current repo version:
- * - "You have N minutes remaining" read the score row with $this->user,
- * which doesn't exist in a menu file, so the minutes were always blank;
- * it uses the logged-in user name now
- * - one GROUP BY query replaces the hand-built UNION: a student with no
- * answers yet produced "SELECT * FROM ;" (invalid SQL) and undefined
- * variable notices
- * - a student with exactly one answer today now also gets the "games
- * enabled" line (the singular branch skipped it), and the unclosed <p>
- * tags are closed
- * - a student with no settings row no longer errors (uses the default
- * minimum of 100 questions that timer.php creates new rows with)
- * - user name escaped for SQL, subjects/values escaped for HTML,
- * `e107.e107_tkReport` -> MPREFIX
- */
- if (!defined('e107_INIT')) {
- exit;
- }
- if (!e107::isInstalled('timekeeper')) {
- return '';
- }
- if (check_class("Main Admin") || check_class("Admin") || check_class("TIMEKEEPER_STUDENT")) {
- $sql = e107::getDb();
- $userName = e107::getUser()->getLoginName();
- $u = $sql->escape($userName);
- $reportTable = '`' . MPREFIX . 'tkReport`';
- $settingsHolder = $sql->retrieve('timekeeper', '*', 'username="' . $u . '"');
- $minQuestions = is_array($settingsHolder) ? (int) $settingsHolder['minQuestionCount'] : 100;
- // Score per subject, all time.
- $sql->gen("SELECT `subject`, COUNT(id) AS `count`,
- CAST((SUM(correct) / COUNT(id) * 100) AS DECIMAL(5,2)) AS percent
- FROM $reportTable WHERE `username` = '$u' GROUP BY `subject` ORDER BY `subject`");
- $subjectRows = [];
- while ($row = $sql->fetch()) {
- $subjectRows[] = $row;
- }
- $text = "<table style='width:100%;text-align:center;'>"
- . "<thead><tr><td style='text-align:left'>Subject</td><td>Score</td><td>No.Questions</td></tr></thead>"
- . "<tbody>";
- foreach ($subjectRows as $row) {
- $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>';
- }
- $text .= '</tbody></table>';
- // Today.
- $sql->gen("SELECT COUNT(id) AS cntr, CAST((SUM(correct)/COUNT(id)*100) AS DECIMAL(5,2)) AS percent
- FROM $reportTable WHERE username = '$u'
- AND answereddatestamp >= UNIX_TIMESTAMP(CURRENT_DATE())
- AND answereddatestamp <= UNIX_TIMESTAMP(CURRENT_DATE() + INTERVAL 1 DAY)");
- $today = $sql->fetch();
- $answeredToday = is_array($today) ? (int) $today['cntr'] : 0;
- if ($answeredToday > 0) {
- $percent = htmlspecialchars($today['percent']);
- if ($answeredToday === 1) {
- $text .= '<p>1 question has been answered<br/>today with ' . $percent . '% accuracy.</p><br/>';
- } else {
- $text .= '<p>' . $answeredToday . ' questions have been answered<br/>today with ' . $percent . '% accuracy.</p><br/>';
- }
- if ($minQuestions <= $answeredToday) {
- $text .= '<label style="background: Darkgreen;">Games enabled</label><br/>';
- $currentScore = $sql->retrieve('timekeeperScore', 'timekeeper_lifetimecorrect, timekeeper_lifetimequestions, timekeeper_minutes', 'timekeeper_username="' . $u . '"');
- $minutes = is_array($currentScore) ? (int) $currentScore['timekeeper_minutes'] : 0;
- $text .= '<label>You have ' . $minutes . ' minutes remaining.</label>';
- } else {
- $text .= '<label>Games disabled</label>';
- }
- }
- e107::getRender()->tablerender('Score', $text);
- }
|