From ada4c5c496d545e801e736f2e0f9cc55ceb92530 Mon Sep 17 00:00:00 2001 From: Epagris Date: Thu, 26 Sep 2024 14:18:58 +0200 Subject: [PATCH] - TeX report generation initials --- class/GroupMgr.php | 12 ++++ class/ReportBuilder.php | 141 ++++++++++++++++++++++++++++++++++++++++ globals.php | 2 + interface.php | 14 ++-- 4 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 class/ReportBuilder.php diff --git a/class/GroupMgr.php b/class/GroupMgr.php index eca14e3..89b25ac 100644 --- a/class/GroupMgr.php +++ b/class/GroupMgr.php @@ -416,4 +416,16 @@ class GroupMgr return null; } } + + // Get the union of all members in the groups specified + function getJoinedMembersByUniqueNames(array $uniqueNames) : array { + $nicknames = []; + foreach ($uniqueNames as $uniqueName) { + $group = $this->getGroupByUniqueName($uniqueName); + if ($group !== null) { + $nicknames = array_merge($nicknames, $group->getMembers()); + } + } + return $nicknames; + } } \ No newline at end of file diff --git a/class/ReportBuilder.php b/class/ReportBuilder.php new file mode 100644 index 0000000..43d50b7 --- /dev/null +++ b/class/ReportBuilder.php @@ -0,0 +1,141 @@ +getJoinedMembersByUniqueNames($groups); + $groupFilter = ["nickname", "IN", $nicknames]; + + // get IDs + $tests = $testMgr->getResultsByGameId($gameid, $filter, $ordering, true, $groupFilter); + $ids = array_map(fn($test) => $test->getId(), $tests); + + // generate stats + return $testMgr->generateDetailedStats($gameid, $ids); + } +} + +class Answer { + const CORRECT = "Correct"; + const INCORRECT = "Incorrect"; + + private string $text; // Answer's text + private string $type; // Answer's type + private float $ratio; // Answer's ratio + + public function __construct(string $type, string $text, float $ratio) { + $this->type = $type; + $this->text = $text; + $this->ratio = $ratio; + } + + // Convert answer into TeX format. + public function genTeX(): string { + return "\\answer" . $this->type . "{" . $this->ratio . "}{" . $this->text . "}\n"; + } +} + +class Challenge { + private string $question; + private array $answers; + + function __construct(array $data) + { + $this->question = $data["question"]; // store question + for ($i = 0; $i < count($data["answers"]); $i++) { + $answer = $data["answers"][$i]; + $ratio = $data["answer_ratio"][$i]; + $type = $answer === $data["correctAnswer"] ? Answer::CORRECT : Answer::INCORRECT; + $this->answers[] = new Answer($type, $answer, $ratio); + } + } + + public function getQuestion(): string { + return $this->question; + } + + public function getAnswers(): array { + return $this->answers; + } + + // Generate TeX representation. + public function genTeX(): string { + $tex = "\\begin{question}{" . $this->question . "}\n"; + foreach ($this->answers as $answer) { + $tex .= $answer->genTeX(); + } + $tex .= "\\end{question}\n"; + return $tex; + } +} + +class ReportSection { + private string $title; + private array $challenges; + function __construct(string $title, array $stats) { + $this->stats = $stats; + } + + function getChallenges() : array { + return $this->challenges; + } + + function getTitle() : string { + return $this->title; + } + + // Generate TeX representation of this report. + function genTeX() : string { + $tex = "\\begin{quiz}{" . $this->title . "}\n"; + foreach ($this->challenges as $challenge) { + $tex .= $challenge->genTeX(); + } + $tex .= "\\end{quiz}\n"; + return $tex; + } +} + +class Report +{ + private string $title; + private array $sections; + function __construct(string $title) + { + $this->title = $title; + $this->sections = []; + } + + + // Add a new section to the report. + function addSection(ReportSection &$section) : void + { + $this->sections[] = $section; + } + + // Generate TeX representation. + function genTeX() :string + { + $tex = ""; + foreach ($this->sections as $section) { + $tex .= $section->genTeX() . "\n\n"; + } + return $tex; + } + + // Save TeX representation + function saveTeX(string $fileName) : void { + file_put_contents($fileName, $this->genTeX()); // TODO: cím!!! + } +} + diff --git a/globals.php b/globals.php index 1604d41..47893d3 100644 --- a/globals.php +++ b/globals.php @@ -16,6 +16,7 @@ const MAIN_URL = "main.php"; const SESSION_NAME = "spreadquiz_sid"; const MAINTENANCE_FLAG_FILE = "MAINTENANCE"; const MISSING_IMAGE_PLACEHOLDER = "media/image-missing_120px.png"; +const REPORT_DIR = DATADIR . DIRECTORY_SEPARATOR . "reports"; session_name(SESSION_NAME); @@ -32,6 +33,7 @@ function init_datadir() { if (!file_exists(DATADIR)) { mkdir(DATADIR); mkdir(GAMEMEDIA_DIR); + mkdir(REPORT_DIR); } } diff --git a/interface.php b/interface.php index 2513625..2f3736a 100644 --- a/interface.php +++ b/interface.php @@ -28,6 +28,8 @@ require_once "class/GameMgr.php"; require_once "class/TestMgr.php"; +require_once "class/ReportBuilder.php"; + // ------------------------ $userMgr = new UserMgr(); @@ -487,6 +489,8 @@ function get_results_by_gameid(ReqHandler &$rh, array $params): array $result = []; if (($game !== null) && ($game->isUserContributorOrOwner($user->getNickname()) || $user->hasQuizmasterPrivilege())) { + + // creating filter criteria on groups $group_filter = []; if ($groups !== []) { global $groupMgr; @@ -502,15 +506,11 @@ function get_results_by_gameid(ReqHandler &$rh, array $params): array } $n++; } - - // prepend filter criteria with AND -// if (count($group_filter) > 0) { -// $group_filter = ["AND", $group_filter]; -// } } else { // a group not found means a faulty query return []; } + // execute filtering $game_results = $testMgr->getResultsByGameId($gameid, $filter, $ordering, true, $group_filter); $result = $game_results; } @@ -518,6 +518,10 @@ function get_results_by_gameid(ReqHandler &$rh, array $params): array return $result; } +function generate_report(ReqHandler &$rh, array $params): string { + +} + function generate_detailed_game_stats(ReqHandler &$rh, array $params): array { global $testMgr;