- TeX report generation initials

This commit is contained in:
Wiesner András 2024-09-26 14:18:58 +02:00
parent eff038a7ea
commit ada4c5c496
4 changed files with 164 additions and 5 deletions

View File

@ -416,4 +416,16 @@ class GroupMgr
return null; 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;
}
} }

141
class/ReportBuilder.php Normal file
View File

@ -0,0 +1,141 @@
<?php
require_once "GroupMgr.php";
require_once "TestMgr.php";
require_once "common_func.php";
class ReportBuilder
{
static private function getStatsByFilters(int $gameid, string $filter, string $groups, string $ordering)
{
$groupMgr = new GroupMgr();
$testMgr = new TestMgr();
// get nicknames
$groups = explode_list($groups);
$nicknames = $groupMgr->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!!!
}
}

View File

@ -16,6 +16,7 @@ const MAIN_URL = "main.php";
const SESSION_NAME = "spreadquiz_sid"; const SESSION_NAME = "spreadquiz_sid";
const MAINTENANCE_FLAG_FILE = "MAINTENANCE"; const MAINTENANCE_FLAG_FILE = "MAINTENANCE";
const MISSING_IMAGE_PLACEHOLDER = "media/image-missing_120px.png"; const MISSING_IMAGE_PLACEHOLDER = "media/image-missing_120px.png";
const REPORT_DIR = DATADIR . DIRECTORY_SEPARATOR . "reports";
session_name(SESSION_NAME); session_name(SESSION_NAME);
@ -32,6 +33,7 @@ function init_datadir() {
if (!file_exists(DATADIR)) { if (!file_exists(DATADIR)) {
mkdir(DATADIR); mkdir(DATADIR);
mkdir(GAMEMEDIA_DIR); mkdir(GAMEMEDIA_DIR);
mkdir(REPORT_DIR);
} }
} }

View File

@ -28,6 +28,8 @@ require_once "class/GameMgr.php";
require_once "class/TestMgr.php"; require_once "class/TestMgr.php";
require_once "class/ReportBuilder.php";
// ------------------------ // ------------------------
$userMgr = new UserMgr(); $userMgr = new UserMgr();
@ -487,6 +489,8 @@ function get_results_by_gameid(ReqHandler &$rh, array $params): array
$result = []; $result = [];
if (($game !== null) && ($game->isUserContributorOrOwner($user->getNickname()) || $user->hasQuizmasterPrivilege())) { if (($game !== null) && ($game->isUserContributorOrOwner($user->getNickname()) || $user->hasQuizmasterPrivilege())) {
// creating filter criteria on groups
$group_filter = []; $group_filter = [];
if ($groups !== []) { if ($groups !== []) {
global $groupMgr; global $groupMgr;
@ -502,15 +506,11 @@ function get_results_by_gameid(ReqHandler &$rh, array $params): array
} }
$n++; $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 } else { // a group not found means a faulty query
return []; return [];
} }
// execute filtering
$game_results = $testMgr->getResultsByGameId($gameid, $filter, $ordering, true, $group_filter); $game_results = $testMgr->getResultsByGameId($gameid, $filter, $ordering, true, $group_filter);
$result = $game_results; $result = $game_results;
} }
@ -518,6 +518,10 @@ function get_results_by_gameid(ReqHandler &$rh, array $params): array
return $result; return $result;
} }
function generate_report(ReqHandler &$rh, array $params): string {
}
function generate_detailed_game_stats(ReqHandler &$rh, array $params): array function generate_detailed_game_stats(ReqHandler &$rh, array $params): array
{ {
global $testMgr; global $testMgr;