| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- <?php
- /**
- * Checks submitted answers, updates the score and writes the report row.
- *
- * Changes from the current repo version:
- * - one correctness decision ($correct, from isCorrect()) now drives the
- * on-screen message, the score/minutes AND the report row. Before, the
- * message and score used a strict === comparison while the report used
- * isCorrect(), so "1.1" vs "1.10" showed as wrong but was logged correct
- * - anagrams: the old retrieve('tk_wordList','word') returned a single value
- * so findInList() looped over a string and never matched. Now an answer is
- * accepted if it uses exactly the same letters AND is in the word list
- * - 'rounding' had no case in the switch, so every rounding question hit
- * "An error has been detected"; added with its messages
- * - user-typed text is escaped before being shown
- * - updateScore(): a brand-new user (retrieve() returns false) no longer
- * increments keys on false or decrements null; user escaped in SQL
- * - getDifficulty(): MPREFIX instead of `e107.`, user escaped, no null result
- * - removed the broken, unused shortCode() and findInList()
- * - a wrong answer takes a minute off but never below zero
- * - fractions: the answer may be typed as 3/4, a mixed number (1 1/2), a whole number
- * or an exact decimal (0.75); it must be in its simplest form, and a right value that
- * isn't simplified (or isn't a mixed number when one was asked for) is marked wrong
- * with a hint
- * - a correct answer gets a friendlier, randomised message: a cheer, the answer
- * worked out, the play minutes earned, and a streak of correct answers (the
- * old "Congratulations" strings at messages[...][1] are no longer used)
- */
- class answerChecker2 {
- private $user;
- private $messages = [
- 'numbers' => ['Find the missing number. {QUESTION}', "Congratulations '{USER_ANSWER}' was correct for Find the missing number '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Find the missing number in {QUESTION}'"],
- 'addition' => ['Solve the following equation. {QUESTION}.', "Congratulations '{USER_ANSWER}' was correct for Solve the following equation. '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Solve the following equation in {QUESTION}'"],
- 'subtraction' => ['Solve the following equation. {QUESTION}.', "Congratulations '{USER_ANSWER}' was correct for Solve the following equation. '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Solve the following equation in {QUESTION}'"],
- 'multiplication' => ['Solve the following equation. {QUESTION}.', "Congratulations '{USER_ANSWER}' was correct for Solve the following equation. '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Solve the following equation in {QUESTION}'"],
- 'division' => ['Solve the following equation. {QUESTION}.', "Congratulations '{USER_ANSWER}' was correct for Solve the following equation. '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Solve the following equation in {QUESTION}'"],
- 'rounding' => ['Round to the nearest 5 cents. {QUESTION}', "Congratulations '{USER_ANSWER}' was correct for rounding to the nearest 5 cents '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Round {QUESTION} to the nearest 5 cents'"],
- 'fractions' => ['Fractions: {QUESTION}', "Congratulations '{USER_ANSWER}' was correct for '{QUESTION}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for '{QUESTION}'"],
- 'word_problems' => ['{QUESTION}', "Congratulations '{USER_ANSWER}' was correct for '{QUESTION}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for '{QUESTION}'"],
- 'anagrams' => ['Unjumble the letters to find the word. {QUESTION}.', "Congratulations '{USER_ANSWER}' was correct for Unjumble the letters '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Unjumble the letters in {QUESTION}'"],
- 'generalKnowledge' => [],
- 'elements' => ['Fill in the missing element information. {QUESTION}', "Congratulations '{USER_ANSWER}' was correct for Fill in the missing element information '{ANSWER}'", "Sorry but '{USER_ANSWER}' was incorrect it should have been '{ANSWER}' for 'Fill in the missing element information in {QUESTION}'"],
- 'continents' => [],
- 'countries' => [],
- 'flags' => [],
- 'capitalCities' => [],
- 'artists' => [],
- 'malaySpelling' => [],
- 'nameBodyParts' => [],
- 'nameBodyOrgans' => [],
- 'basicFirstAid' => []
- ];
- /**
- * Get the right answer checker for the subject
- */
- public function checkQuestionSubject($subject, $user) {
- $this->user = $user;
- switch ($subject) {
- case 'numbers':
- case 'addition':
- case 'subtraction':
- case 'multiplication':
- case 'division':
- case 'rounding':
- case 'word_problems':
- $this->checkGeneric($subject);
- break;
- case 'spelling':
- $this->checkSpelling(); //Custom checker
- break;
- case 'anagrams':
- $this->checkAnagrams();
- break;
- case 'fractions':
- $this->checkFractions();
- break;
- case 'generalKnowledge':
- case 'elements':
- $this->checkGeneric('elements');
- break;
- case 'continents':
- case 'countries':
- case 'flags':
- case 'capitalCities':
- case 'artists':
- case 'malaySpelling':
- case 'nameBodyParts':
- case 'nameBodyOrgans':
- case 'basicFirstAid':
- default:
- print_a('An error has been detected in the answer checker. Please report this and refresh the page.');
- break;
- }
- }
- /**
- * Check the answer for anything basic
- */
- private function checkGeneric($subject) {
- $usersAnswer = trim((string) filter_input(INPUT_POST, 'a1'));
- $answer = $this->loadPending();
- if ($answer === null) {
- return;
- }
- $correct = $this->isCorrect($answer['answer1'], $usersAnswer);
- $this->finishCheck($subject, $correct, $answer['answer1'], $usersAnswer, $this->questionText($answer, $subject));
- }
- /**
- * Check an anagram answer: the exact word, or any other real word made
- * from the same letters.
- */
- private function checkAnagrams() {
- $usersAnswer = trim((string) filter_input(INPUT_POST, 'a1'));
- $answer = $this->loadPending();
- if ($answer === null) {
- return;
- }
- $correct = $this->isCorrect($answer['answer1'], $usersAnswer)
- || $this->isValidAnagram($usersAnswer, $answer['answer1']);
- $this->finishCheck('anagrams', $correct, $answer['answer1'], $usersAnswer, $this->questionText($answer, 'anagrams'));
- }
- /**
- * Check a fractions answer. Any correct form is accepted (3/4, a mixed
- * number like 1 1/2, a whole number, an exact decimal like 0.75) but it has
- * to be in its simplest form, and a mixed number if one was asked for.
- */
- private function checkFractions() {
- $usersAnswer = trim((string) filter_input(INPUT_POST, 'a1'));
- $answer = $this->loadPending();
- if ($answer === null) {
- return;
- }
- $format = isset($answer['format']) ? $answer['format'] : 'simplest';
- $verdict = $this->fractionVerdict((string) $answer['answer1'], $format, $usersAnswer);
- $hint = '';
- if ($verdict === 'unsimplified') {
- $hint = "That's the right value, but it can be written more simply. ";
- } elseif ($verdict === 'wrongform') {
- $hint = "That's the right value, but write it as a mixed number, like 2 1/3. ";
- }
- $this->finishCheck('fractions', $verdict === 'correct', $answer['answer1'], $usersAnswer, $this->questionText($answer, 'fractions'), $hint);
- }
- /**
- * @param string $expectedText The right answer in its simplest form ("3/4", "1 1/2", "7").
- * @param string $format 'simplest', or 'mixed' when a mixed number was asked for.
- * @param string $given What the student typed.
- * @return string 'correct', 'unsimplified', 'wrongform' or 'wrong'
- */
- private function fractionVerdict($expectedText, $format, $given) {
- require_once __DIR__ . '/fraction/src/Fraction.php';
- $typed = $this->parseFractionAnswer($given);
- if ($typed === null) {
- return 'wrong';
- }
- try {
- $expected = Fraction::fromString($expectedText);
- } catch (InvalidArgumentException $e) {
- return 'wrong';
- }
- if (!$typed['value']->isSameValueAs($expected)) {
- return 'wrong';
- }
- if ($format === 'mixed' && $typed['form'] !== 'mixed') {
- return 'wrongform';
- }
- if (!$typed['lowest']) {
- return 'unsimplified';
- }
- return 'correct';
- }
- /**
- * Read what the student typed as a fraction.
- *
- * @return array|null ['value' => Fraction, 'form' => whole|fraction|mixed|decimal,
- * 'lowest' => bool (in simplest form)], or null if it isn't a number
- */
- private function parseFractionAnswer($text) {
- $text = trim(preg_replace('/\s+/', ' ', (string) $text));
- try {
- if (preg_match('#^(\d{1,9})$#', $text, $m)) {
- return ['value' => new Fraction((int) $m[1]), 'form' => 'whole', 'lowest' => true];
- }
- if (preg_match('#^(\d{1,9}) ?/ ?(\d{1,9})$#', $text, $m)) {
- $n = (int) $m[1];
- $d = (int) $m[2];
- if ($d < 1) {
- return null;
- }
- return ['value' => new Fraction($n, $d), 'form' => 'fraction', 'lowest' => ($n > 0 && $this->gcdInt($n, $d) === 1)];
- }
- if (preg_match('#^(\d{1,9}) (\d{1,9}) ?/ ?(\d{1,9})$#', $text, $m)) {
- $w = (int) $m[1];
- $n = (int) $m[2];
- $d = (int) $m[3];
- if ($d < 1) {
- return null;
- }
- $value = (new Fraction($w))->add(new Fraction($n, $d));
- return ['value' => $value, 'form' => 'mixed', 'lowest' => ($n > 0 && $n < $d && $this->gcdInt($n, $d) === 1)];
- }
- if (preg_match('#^(\d{0,9})\.(\d{1,8})$#', $text, $m)) {
- // Exact: 0.29 is 29/100. (Fraction::fromFloat() goes through floating point.)
- $digits = ($m[1] === '' ? '0' : $m[1]) . $m[2];
- return ['value' => new Fraction((int) $digits, (int) pow(10, strlen($m[2]))), 'form' => 'decimal', 'lowest' => true];
- }
- } catch (InvalidArgumentException $e) {
- return null;
- }
- return null;
- }
- private function gcdInt($a, $b) {
- $a = abs($a);
- $b = abs($b);
- while ($b > 0) {
- $t = $a % $b;
- $a = $b;
- $b = $t;
- }
- return $a;
- }
- /**
- * Check the answer for spelling
- */
- private function checkSpelling() {
- $usersAnswer = trim((string) filter_input(INPUT_POST, 'a1'));
- $answer = $this->loadPending();
- if ($answer === null) {
- return;
- }
- $word = (string) $answer['word'];
- $correct = $this->isCorrect($word, $usersAnswer);
- $minutes = $this->updateScore($correct);
- $this->addToReport($word, $usersAnswer, 'spelling', "Spell the word " . $word, $correct);
- if ($correct) {
- $text = $this->correctText('spelling', $word, $usersAnswer, $word, $minutes);
- $title = 'Correct!';
- } else {
- $safeAnswer = htmlspecialchars($usersAnswer);
- $safeWord = htmlspecialchars($word);
- $text = "Sorry but '" . $safeAnswer . "' was incorrect it should have been '" . $safeWord . "' for 'spell the word " . $safeWord . "'<br/>";
- $title = 'Spelling answer check';
- }
- e107::getRender()->tablerender($title, $text);
- }
- /**
- * Shared tail of the generic and anagram checks: build the message, update
- * the score, write the report row, render the result. $correct is the one
- * decision used for all three.
- */
- private function finishCheck($subject, $correct, $expected, $usersAnswer, $question, $hint = '') {
- $expected = (string) $expected;
- $messages = $this->messages[$subject];
- $minutes = $this->updateScore($correct);
- $questionString = strtr($messages[0], ['{ANSWER}' => $expected, '{QUESTION}' => $question]);
- $this->addToReport($expected, $usersAnswer, $subject, $questionString, $correct);
- if ($correct) {
- $text = $this->correctText($subject, $expected, $usersAnswer, $question, $minutes);
- $title = 'Correct!';
- } else {
- $safe = [
- '{ANSWER}' => htmlspecialchars($expected),
- '{USER_ANSWER}' => htmlspecialchars($usersAnswer),
- '{QUESTION}' => htmlspecialchars($question),
- ];
- $text = $hint . strtr($messages[2], $safe) . "<br/>";
- $title = ucfirst($subject) . ' answer check';
- }
- e107::getRender()->tablerender($title, $text);
- }
- private $cheers = [
- "🎉 Woohoo! You got it!",
- "⭐ Brilliant!",
- "🚀 Boom! Nailed it!",
- "💪 Spot on!",
- "🙌 High five! That's right!",
- "🧠 Big brain move!",
- "🌟 You're a star!",
- "😎 Too easy for you!",
- "🏆 Champion answer!",
- "✨ Yes! Exactly right!",
- "🎯 Bullseye!",
- "🦄 Magical! Correct!",
- ];
- /**
- * The message for a correct answer: a cheer, the answer, minutes earned,
- * and a streak when there is one. Everything typed by the student is escaped.
- */
- private function correctText($subject, $expected, $usersAnswer, $question, $minutes) {
- $text = '<div style="font-size:160%;font-weight:bold;">' . $this->cheers[array_rand($this->cheers)] . '</div>';
- $text .= '<p>' . $this->correctDetail($subject, $expected, $usersAnswer, $question) . '</p>';
- if ($minutes > 0) {
- $text .= '<p>🎮 <b>+' . (int) $minutes . ((int) $minutes === 1 ? ' minute' : ' minutes') . '</b> of play time earned!</p>';
- }
- $streak = $this->correctStreak();
- if ($streak >= 25) {
- $text .= '<p>🚀 25 or more correct in a row. Unstoppable!</p>';
- } elseif ($streak >= 10) {
- $text .= '<p>🚀 ' . $streak . ' correct in a row. Unstoppable!</p>';
- } elseif ($streak >= 5) {
- $text .= '<p>🔥 ' . $streak . ' correct in a row. You\'re on fire!</p>';
- } elseif ($streak >= 3) {
- $text .= '<p>🔥 ' . $streak . ' correct in a row!</p>';
- }
- return $text;
- }
- /**
- * The line that says what the right answer was, for this kind of question.
- */
- private function correctDetail($subject, $expected, $usersAnswer, $question) {
- $e = htmlspecialchars($expected);
- switch ($subject) {
- case 'numbers':
- return 'The missing number was <b>' . $e . '</b>.';
- case 'addition':
- case 'subtraction':
- case 'multiplication':
- case 'division':
- $sum = strtr($question, ['x' => ' × ', '/' => ' ÷ ', '+' => ' + ', '-' => ' − ']);
- return '<b>' . htmlspecialchars($sum) . ' = ' . $e . '</b>';
- case 'rounding':
- return '<b>' . htmlspecialchars($question) . '</b> rounds to <b>' . $e . '</b> (the nearest 5 cents).';
- case 'anagrams':
- if (mb_strtolower(trim($usersAnswer)) !== mb_strtolower($expected)) {
- return 'You found <b>' . htmlspecialchars($usersAnswer) . '</b>. I was thinking of <b>' . $e . '</b>, but that is a real word too!';
- }
- return 'You unjumbled it: <b>' . $e . '</b>!';
- case 'fractions':
- return '<b>' . htmlspecialchars($question) . ' = ' . $e . '</b>';
- case 'word_problems':
- return 'The answer was <b>' . $e . '</b>.';
- case 'spelling':
- return 'You spelled <b>' . $e . '</b> perfectly!';
- default:
- return '<b>' . $e . '</b> was exactly right.';
- }
- }
- /**
- * How many of the student's latest answers in a row were correct (up to 25).
- * Called after the current answer has been written to the report.
- */
- private function correctStreak() {
- $db = e107::getDb();
- $db->gen("SELECT `correct` FROM `" . MPREFIX . "tkReport` WHERE `username` = '" . $db->escape($this->user) . "' ORDER BY `id` DESC LIMIT 25");
- $recent = [];
- while ($row = $db->fetch()) {
- $recent[] = (int) $row['correct'];
- }
- $streak = 0;
- foreach ($recent as $correct) {
- if ($correct !== 1) {
- break;
- }
- $streak++;
- }
- return $streak;
- }
- /**
- * Question text for messages. Array questions (equations, number
- * sequences, scrambled letters) are joined; a rounding question is just
- * the amount.
- */
- private function questionText($answer, $subject) {
- $question = isset($answer['question']) ? $answer['question'] : '';
- if (is_array($question)) {
- if ($subject === 'rounding') {
- $question = array_slice($question, 0, 1);
- }
- $question = implode('', $question);
- }
- return (string) $question;
- }
- /**
- * Numbers compare numerically ("1.1" == "1.10"), everything else
- * case-insensitively, after trimming.
- */
- private function isCorrect($expected, $given): bool {
- $expected = trim((string) $expected);
- $given = trim((string) $given);
- if (is_numeric($expected) && is_numeric($given)) {
- return abs((float) $expected - (float) $given) < 1e-9;
- }
- return mb_strtolower($expected) === mb_strtolower($given);
- }
- /**
- * An alternative anagram answer is valid when it uses exactly the same
- * letters as the original word and is itself in the word list.
- */
- private function isValidAnagram($given, $original) {
- $given = trim((string) $given);
- if ($given === '' || !$this->sameLetters($given, (string) $original)) {
- return false;
- }
- $db = e107::getDb();
- return $db->count('tk_wordList', '(*)', "word = '" . $db->escape($given) . "'") > 0;
- }
- private function sameLetters($a, $b) {
- $a = preg_split('//u', mb_strtolower($a), -1, PREG_SPLIT_NO_EMPTY);
- $b = preg_split('//u', mb_strtolower($b), -1, PREG_SPLIT_NO_EMPTY);
- sort($a);
- sort($b);
- return $a === $b;
- }
- /**
- * Add or minus the score.
- *
- * @return int The play minutes awarded for a correct answer (0 otherwise).
- */
- private function updateScore($scorePoint) {
- $db = e107::getDb();
- $user = $db->escape($this->user);
- $currentScore = $db->retrieve('timekeeperScore', 'timekeeper_lifetimecorrect, timekeeper_lifetimequestions, timekeeper_minutes', 'timekeeper_username="' . $user . '"');
- $isNew = !is_array($currentScore);
- if ($isNew) {
- $currentScore = ['timekeeper_lifetimecorrect' => 0, 'timekeeper_lifetimequestions' => 0, 'timekeeper_minutes' => 0];
- }
- $awarded = 0;
- if ($scorePoint) {
- $currentScore['timekeeper_lifetimecorrect']++;
- $awarded = (int) $this->scaleTimeAddition();
- $currentScore['timekeeper_minutes'] = $currentScore['timekeeper_minutes'] + $awarded;
- } else {
- // Never below zero: the column is signed now, and a negative balance would
- // otherwise have to be earned back before any play time counts.
- $currentScore['timekeeper_minutes'] = max(0, $currentScore['timekeeper_minutes'] - 1);
- }
- $currentScore['timekeeper_lifetimequestions']++;
- if ($isNew) {
- $currentScore['timekeeper_lastquestiondatestamp'] = time();
- $currentScore['timekeeper_yearlevel'] = 0;
- $currentScore['timekeeper_username'] = $this->user;
- $db->insert('timekeeperScore', $currentScore);
- } else {
- $currentScore['WHERE'] = 'timekeeper_username="' . $user . '"';
- $db->update('timekeeperScore', $currentScore);
- }
- return $awarded;
- }
- /**
- * Return how many minutes play time to award scaled by difficulty
- */
- private function scaleTimeAddition() {
- //TODO look at this and fix it.
- $difficulty = $this->getDifficulty($this->user); //replace with db call
- $returnMinutes = (int) $difficulty / 10;
- $returnMinutes = round($returnMinutes);
- if ($returnMinutes <= 1) {
- $returnMinutes = 1;
- }
- return round($returnMinutes);
- }
- /**
- * Return users overall accuracy (percent) for the last month.
- */
- private function getDifficulty($user) {
- $db = e107::getDb();
- $db->gen("SELECT CAST((SUM(correct)/COUNT(id)*100) AS DECIMAL(5,2)) AS percent
- FROM `" . MPREFIX . "tkReport` WHERE username = '" . $db->escape($user) . "'
- AND answereddatestamp >= UNIX_TIMESTAMP(CURRENT_DATE() - INTERVAL 1 MONTH)
- AND answereddatestamp <= UNIX_TIMESTAMP(CURRENT_DATE() + INTERVAL 1 DAY)");
- $row = $db->fetch();
- return is_array($row) ? (float) $row['percent'] : 0;
- }
- /**
- * Add the last answered question to the report DB table.
- */
- private function addToReport($answer, $usersAnswer, $subject, $questionString, $isCorrect) {
- $insert = [
- 'username' => $this->user,
- 'question' => $questionString,
- 'answereddatestamp' => time(),
- 'userAnswer' => trim($usersAnswer),
- 'answer' => trim($answer),
- 'subject' => $subject,
- 'correct' => $isCorrect ? 1 : 0
- ];
- e107::getDb()->insert('tkReport', $insert);
- }
- /**
- * Load the expected answer for this user's open question. Single use:
- * a stale tab, a tampered token or a double-submit gets the message below.
- */
- private function loadPending(): ?array {
- $token = filter_input(INPUT_POST, 'checkerData');
- $answer = TkAnswerStore::consume($this->user, is_string($token) ? $token : null);
- if ($answer === null) {
- e107::getRender()->tablerender('Answer check',
- 'That question has expired or was already answered. Please answer the new question.');
- }
- return $answer;
- }
- }
|