diff --git a/check_maintenance.php b/check_maintenance.php new file mode 100644 index 0000000..e8a27b4 --- /dev/null +++ b/check_maintenance.php @@ -0,0 +1,8 @@ +challengeN > 0) { + $this->percentage = $this->correctAnswerN / (double)$this->challengeN * 100.0; + } else { // avoid division by zero + $this->percentage = 0.0; + } + } function __construct(int $challengeN, int $correctAnswerN) { $this->challengeN = $challengeN; $this->correctAnswerN = $correctAnswerN; + $this->calculatePercentage(); } // Get challenge count. @@ -33,6 +48,7 @@ class TestSummary function setCorrectAnswerN(int $correctAnswerN): void { $this->correctAnswerN = $correctAnswerN; + $this->calculatePercentage(); } // Get ratio of correct results. @@ -50,7 +66,7 @@ class TestSummary // Convert to array. function toArray(): array { - return ["challenge_n" => $this->challengeN, "correct_answer_n" => $this->correctAnswerN]; + return ["challenge_n" => $this->challengeN, "correct_answer_n" => $this->correctAnswerN, "percentage" => $this->percentage]; } } @@ -158,6 +174,7 @@ class Test extends AutoStoring // auto-conclude time-constrained test if expired if ($this->timeLimited && $this->isOngoing() && ($this->endLimitTime <= time())) { $this->concludeTest(); + $this->endTime = $this->endLimitTime; // date back end time to the limiting value } } @@ -485,4 +502,31 @@ class TestMgr // match challenges return $aggregated; } + + // Upgrade test. Just load tests and save them. + function upgradeTests(array $ids = []) : void { + $a = []; + if ($ids === []) { + $a = $this->db->findAll(); + } else { + $a = $this->db->findBy(["_id", "IN", $ids]); + } + + foreach ($a as $t) { + $test = new Test($this, $t); + $test->storeMods(); + } + } + + // Extract timed test IDs. Scan the database for tests with time limit ON. + function extractExpiredTimedTestIds(bool $ongoingOnly = true) : array { + $query = [["time_limited", "=", true], "AND", ["end_limit_time", "<", time()]]; + if ($ongoingOnly) { + $query = [ ...$query, "AND", ["state", "=", TEST_ONGOING]]; + } + + $qb = $this->db->createQueryBuilder(); + $a = $qb->where($query)->select(["_id"])->orderBy(["_id" => "ASC"])->getQuery()->fetch(); + return array_map(fn($a) => $a["_id"], $a); + } } \ No newline at end of file diff --git a/class/UserMgr.php b/class/UserMgr.php index 5decdf2..725dc42 100644 --- a/class/UserMgr.php +++ b/class/UserMgr.php @@ -1,5 +1,7 @@ upgradeTests(); + printf("OK!\n"); + } + break; + case "get_timed_tests": + { + $testMgr = new TestMgr(); + printf("Expired timed tests: %s\n", join(", ", $testMgr->extractExpiredTimedTestIds())); + } + break; + } +} + +// Tick tests +if (isset($options["tick"])) { + $testMgr = new TestMgr(); + $timedTestIDs = $testMgr->extractExpiredTimedTestIds(); + $testMgr->upgradeTests($timedTestIDs); +} diff --git a/default_frame.php b/default_frame.php index 260c40a..3f9647b 100644 --- a/default_frame.php +++ b/default_frame.php @@ -1,5 +1,7 @@