SpreadQuiz/testmgr.php
Epagris 89ac41a6e6 - report generation implemented
- recursive filtering fixed
- grouping added
2024-09-09 18:36:03 +02:00

421 lines
13 KiB
PHP

<?php
require_once "globals.php";
require_once "common_func.php";
require_once "gamemgr.php";
require_once "usermgr.php";
$testdb = new \SleekDB\Store(TESTDB, DATADIR, ["timeout" => false]);
const TEST_ONGOING = "ongoing";
const TEST_CONCLUDED = "concluded";
function create_or_continue_test(string $gameid, string $nickname): string
{
global $testdb;
// get game and user data
$game_data = get_game($gameid);
$user_data = get_user($nickname);
if ((count($game_data) === 0) || (count($user_data) === 0)) {
return "";
}
// check if this user has permission to take this test
// if the intersection of user's groups and game's assigned groups is zero, then the user has no access to this game
if (count(array_intersect($game_data["groups"], $user_data["groups"])) === 0) {
return "";
}
// check if the user had taken this test before
$fetch_criteria = [["gameid", "=", (int)$gameid], "AND", ["nickname", "=", $nickname]];
$previous_tests = $testdb->findBy($fetch_criteria);
if (count($previous_tests) > 0) { // if there are previous attempts, then...
// update timed tests to see if they had expired
update_timed_tests($previous_tests);
// re-fetch tests, look only for ongoing
$ongoing_tests = $testdb->findBy([$fetch_criteria, "AND", ["state", "=", TEST_ONGOING]]);
if (count($ongoing_tests) !== 0) { // if there's an ongoing test
return $ongoing_tests[0]["_id"];
} else { // there's no ongoing test
if ($game_data["properties"]["repeatable"]) { // test is repeatable...
return create_test($game_data, $user_data);
} else { // test is non-repeatable, cannot be attempted more times
return "";
}
}
} else { // there were no previous attempts
return create_test($game_data, $user_data);
}
}
function create_test(array $game_data, array $user_data): string
{
global $testdb;
$gameid = $game_data["_id"];
$game_properties = $game_data["properties"];
// fill basic data
$test_data = [
"gameid" => $gameid,
"nickname" => $user_data["nickname"],
"gamename" => $game_data["name"]
];
// fill challenges
$challenges = load_challenges($gameid);
// shuffle answers
foreach ($challenges as &$ch) {
shuffle($ch["answers"]);
$ch["correct_answer"] = "";
$ch["player_answer"] = "";
}
// involve properties
$now = time();
$properties = [
"state" => TEST_ONGOING,
"time_limited" => (($game_properties["time_limit"] ?: -1) > -1),
"start_time" => $now,
"repeatable" => $game_properties["repeatable"] ?: false
];
if ($properties["time_limited"]) {
$properties["end_limit_time"] = $now + $game_properties["time_limit"];
}
// merge properties and test data
$test_data = array_merge($test_data, $properties);
// add challenges
$test_data["challenges"] = $challenges;
// store game
$test_data = $testdb->insert($test_data);
$testid = $test_data["_id"];
return $testid;
}
function update_timed_tests(array $test_data_array)
{
$now = time();
foreach ($test_data_array as $test_data) {
// look for unprocessed expired tests
if (($test_data["state"] === TEST_ONGOING) && ($test_data["time_limited"]) && ($test_data["end_limit_time"] < $now)) {
conclude_test($test_data["_id"]);
}
}
}
function update_test(array $test_data)
{
global $testdb;
$testdb->update($test_data);
}
function get_test(string $testid): array
{
global $testdb;
return $testdb->findById($testid);
}
function save_answer(string $testid, $chidx, $ansidx)
{
$test_data = get_test($testid);
$chidx = (int)$chidx;
if ((count($test_data) > 0) && ($test_data["state"] === TEST_ONGOING)) {
if ($chidx < count($test_data["challenges"])) {
$test_data["challenges"][$chidx]["player_answer"] = $ansidx;
update_test($test_data);
}
}
}
function get_concluded_tests(string $gameid, string $nickname)
{
global $testdb;
$fetch_criteria = [["gameid", "=", (int)$gameid], "AND", ["nickname", "=", $nickname], "AND", ["state", "=", TEST_CONCLUDED]];
$test_data_array = $testdb->findBy($fetch_criteria);
return $test_data_array;
}
function conclude_test(string $testid)
{
$test_data = get_test($testid);
if (count($test_data) === 0) {
return;
}
// load game data
//$game_data = get_game($test_data["gameid"]);
$game_challenges = load_challenges($test_data["gameid"]);
// check the answers
$challenge_n = count($test_data["challenges"]); // number of challenges
$cans_n = 0; // number of correct answers
for ($chidx = 0; $chidx < $challenge_n; $chidx++) {
// get challenge
$tch = &$test_data["challenges"][$chidx];
$gch = $game_challenges[$chidx];
// translate correct answer into an index by the shuffled answer order
$cans_idx = array_search($gch["correct_answer"], $tch["answers"]);
$tch["correct_answer"] = $cans_idx;
// check the player's answer
$player_answer = trim($tch["player_answer"]);
if (($player_answer !== "") && ($cans_idx === (int)$player_answer)) {
$cans_n++;
}
}
// set state and fill summary
$test_data["state"] = TEST_CONCLUDED;
$test_data["end_time"] = time();
$test_data["summary"] = [
"challenge_n" => $challenge_n,
"correct_answer_n" => $cans_n
];
update_test($test_data);
}
function automatic_typecast(string $rval)
{
if (is_numeric($rval)) { // is it a numeric value?
if (((int)$rval) == ((double)$rval)) { // is it an integer?
return (int)$rval;
} else { // is it a float?
return (double)$rval;
}
} elseif (str_starts_with($rval, 'T')) { // is it a date/time value?
return strtotime(substr($rval, 1)); // convert to UNIX timestamp
} else { // it's a string
return substr($rval, 1, strlen($rval) - 2); // strip leading and trailing quotes
}
}
function split_criterion(string $crstr): array
{
preg_match("/([<>=!]+|LIKE|NOT LIKE|IN|NOT IN|CONTAINS|NOT CONTAINS|BETWEEN|NOT BETWEEN|EXISTS)/", $crstr, $matches, PREG_OFFSET_CAPTURE);
// extract operator
$op = $matches[0][0];
$op_pos = $matches[0][1];
// extract operands
$left = trim(substr($crstr, 0, $op_pos));
$right = trim(substr($crstr, $op_pos + strlen($op), strlen($crstr)));
// automatic type conversion
if (str_starts_with($right, "[") && str_ends_with($right, "]")) { // is it an array?
$right = substr($right, 1, -1); // strip leading and trailing brackets
$elements = explode(",", $right); // extract array elements
$right = []; // re-init right value, since it's an array
foreach ($elements as $element) { // insert array elements
$element = trim($element);
if ($element !== "") {
$right[] = automatic_typecast($element);
}
}
} else { // it must be a single value
$right = automatic_typecast($right);
}
return [$left, $op, $right];
}
function build_query(string $filter): array
{
// skip empty filter processing
if (trim($filter) === "") {
return [];
}
// subfilters and operations
$subfilts = [];
$operations = [];
// buffer and scoring
$k = 0;
$k_prev = 0;
$buffer = "";
for ($i = 0; $i < strlen($filter); $i++) {
$c = $filter[$i];
// extract groups surrounded by parantheses
if ($c === "(") {
$k++;
} elseif ($c === ")") {
$k--;
}
// only omit parentheses at the top-level expression
if (!((($c === "(") && ($k === 1)) || (($c === ")") && ($k === 0)))) {
$buffer .= $c;
}
// if k = 0, then we found a subfilter
if (($k === 0) && ($k_prev === 1)) {
$subfilts[] = trim($buffer);
$buffer = "";
} elseif (($k === 1) && ($k_prev === 0)) {
$op = trim($buffer);
if ($op !== "") {
$operations[] = $op;
}
$buffer = "";
}
// save k to be used next iteration
$k_prev = $k;
}
// decide, whether further expansion of condition is needed
$criteria = [];
for ($i = 0; $i < count($subfilts); $i++) {
$subfilt = $subfilts[$i];
// add subcriterion
if ($subfilt[0] === "(") {
$criteria[] = build_query($subfilt);
} else {
$criteria[] = split_criterion($subfilt);
}
// add operator
if (($i + 1) < count($subfilts)) {
$criteria[] = $operations[$i];
}
}
return $criteria;
}
function build_ordering(string $orderby): array
{
// don't process empty order instructions
if ($orderby === "") {
return [];
}
// explode string at tokens delimiting separate order criteria
$ordering = [];
$subcriteria = explode(";", $orderby);
foreach ($subcriteria as $subcriterion) {
$parts = explode(":", $subcriterion); // fetch parts
$field_name = trim($parts[0], "\ \n\r\t\v\0\"'"); // strip leading and trailing quotes if exists
$direction = strtolower(trim($parts[1])); // fetch ordering direction
$ordering[$field_name] = $direction; // build ordering instruction
}
return $ordering;
}
function get_results_by_gameid(string $gameid, string $filter, string $orderby, bool $exclude_challenge_data): array
{
global $testdb;
$qb = $testdb->createQueryBuilder();
$qb = $qb->where(["gameid", "=", (int)$gameid]);
// filtering
if (trim($filter) !== "") {
// auto complete starting and ending parenthesis
if (!str_starts_with($filter, "(")) {
$filter = "(" . $filter;
}
if (!str_ends_with($filter, ")")) {
$filter = $filter . ")";
}
$criteria = build_query($filter);
$qb->where($criteria);
}
// ordering
if (trim($orderby) !== "") {
$ordering = build_ordering($orderby);
$qb->orderBy($ordering);
}
// excluding challenge data
if ($exclude_challenge_data) {
$qb->except(["challenges"]);
}
$test_data_array = $qb->getQuery()->fetch();
return $test_data_array;
}
function generate_detailed_stats(string $gameid, array $testids): array
{
if ((count($testids) === 0) || ($gameid === "")) {
return [];
}
global $testdb;
// fetch relevant entries
$qb = $testdb->createQueryBuilder();
$criteria = [["gameid", "=", (int)$gameid], "AND", ["state", "=", "concluded"], "AND", ["_id", "IN", $testids]];
$qb->where($criteria);
$qb->select(["challenges"]);
$entries = $qb->getQuery()->fetch();
$challenge_indices = [];
// count answers
$aggregated = [];
foreach ($entries as $entry) {
foreach ($entry["challenges"] as $challenge) {
$correct_answer = $challenge["answers"][$challenge["correct_answer"]];
$compound = $challenge["question"] . $correct_answer . count($challenge["answers"]) . $challenge["image_url"];
$idhash = md5($compound);
// if this is a new challenge to the list...
if (!isset($challenge_indices[$idhash])) {
$challenge_indices[$idhash] = count($challenge_indices);
$challenge_info = [ // copy challenge info
"hash" => $idhash,
"image_url" => $challenge["image_url"],
"question" => $challenge["question"],
"answers" => $challenge["answers"],
"correct_answer" => $correct_answer,
"player_answers" => array_fill(0, count($challenge["answers"]), 0),
"answer_count" => count($challenge["answers"]),
];
$aggregated[$challenge_indices[$idhash]] = $challenge_info; // insert challenge info
}
// fetch challenge index
$challenge_idx = $challenge_indices[$idhash];
// add up player answer
$answer_idx = array_search($challenge["answers"][$challenge["player_answer"]], $aggregated[$challenge_idx]["answers"]); // transform player answer index to report answer index
$aggregated[$challenge_idx]["player_answers"][(int)$answer_idx]++;
}
}
// produce derived info
foreach ($aggregated as &$entry) {
$entry["answer_ratio"] = $entry["player_answers"];
$answer_n = count($entry["answer_ratio"]);
$sum = array_sum($entry["player_answers"]);
if ($sum === 0) {
continue;
}
for ($i = 0; $i < $answer_n; $i++) {
$entry["answer_ratio"][$i] = $entry["player_answers"][$i] / $sum;
}
}
// match challenges
return $aggregated;
}