From e313923ad4f374d583a18d1912793aaa7b664a00 Mon Sep 17 00:00:00 2001 From: Epagris Date: Thu, 26 Sep 2024 11:34:02 +0200 Subject: [PATCH] - checking maintenance in each file added - a few CLI commands added - ticking and concluding expired tests added --- check_maintenance.php | 8 +++++++ class/GameMgr.php | 2 ++ class/GroupMgr.php | 2 ++ class/TestMgr.php | 46 ++++++++++++++++++++++++++++++++++++++++- class/UserMgr.php | 2 ++ cli_actions.php | 39 ++++++++++++++++++++++++++++++++++ default_frame.php | 2 ++ game_manager_frame.php | 1 + globals.php | 6 ------ group_manager_frame.php | 2 ++ interface.php | 10 ++------- login.php | 2 ++ main.php | 2 ++ result_analyzer.php | 1 + testground.php | 1 + user_manager_frame.php | 1 + 16 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 check_maintenance.php create mode 100644 cli_actions.php 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 @@