- automatic typecast to bool added

- searching by group's unique name added
This commit is contained in:
Wiesner András 2024-09-26 13:17:19 +02:00
parent 5b2cc0b8f4
commit 0c44163bdf
6 changed files with 55 additions and 4 deletions

View File

@ -20,6 +20,8 @@ class ExpressionBuilder
} else { // is it a float?
return (double)$rval;
}
} else if (in_array(strtolower($rval), ["true", "false"]) ) { // it's a boolean
return $rval === "true";
} else if (str_starts_with($rval, '"') && str_ends_with($rval, '"')){ // it's a string
return substr($rval, 1, strlen($rval) - 2); // strip leading and trailing quotes
} else { // it must be a column name

View File

@ -403,4 +403,17 @@ class GroupMgr
$a = $qb->where(["games", "CONTAINS", (int)$gameid])->select(["_id"])->getQuery()->fetch();
return array_map(fn($r) => $r["_id"], $a);
}
// Get group by group compound (name#id).
function getGroupByUniqueName(string $uniqueName): Group|null {
$a = $this->db->findOneBy([ function($g) use ($uniqueName) : bool {
$group = Group::fromArray($this, $g);
return $group->getUniqueName() === $uniqueName;
}]);
if ($a !== null) {
return Group::fromArray($this, $a);
} else {
return null;
}
}
}

View File

@ -400,7 +400,7 @@ class TestMgr
}
// Get test results by game ID.
function getResultsByGameId(string $gameid, string $filter, string $orderby, bool $exclude_challenge_data): array
function getResultsByGameId(string $gameid, string $filter, string $orderby, bool $exclude_challenge_data, array ...$furtherFilters): array
{
$qb = $this->db->createQueryBuilder();
$qb = $qb->where(["gameid", "=", (int)$gameid]);
@ -417,9 +417,19 @@ class TestMgr
}
$criteria = ExpressionBuilder::buildQuery($filter);
$qb->where($criteria);
}
// add further filters
if (count($furtherFilters) > 0) {
foreach ($furtherFilters as $ff) {
if ($ff !== []) {
$qb->where($ff);
}
}
}
// ordering
if (trim($orderby) !== "") {
$ordering = ExpressionBuilder::buildOrdering($orderby);

View File

@ -481,12 +481,35 @@ function get_results_by_gameid(ReqHandler &$rh, array $params): array
$gameid = trim($params["gameid"]);
$filter = trim($params["filter"] ?? "");
$ordering = trim($params["orderby"] ?? "");
$groups = explode_list(trim($params["groups"] ?? ""));
$game = $gameMgr->getGame($gameid);
$result = [];
if (($game !== null) && ($game->isUserContributorOrOwner($user->getNickname()) || $user->hasQuizmasterPrivilege())) {
$game_results = $testMgr->getResultsByGameId($gameid, $filter, $ordering, true);
$group_filter = [];
if ($groups !== []) {
global $groupMgr;
$n = 0;
foreach ($groups as $groupname) {
if ($n > 0) { // place OR between each group criterion
$group_filter[] = "OR";
}
$group = $groupMgr->getGroupByUniqueName($groupname);
if ($group !== null) {
$nicknames = $group->getMembers();
$group_filter[] = ["nickname", "IN", $nicknames];
}
$n++;
}
// prepend filter criteria with AND
// if (count($group_filter) > 0) {
// $group_filter = ["AND", $group_filter];
// }
}
$game_results = $testMgr->getResultsByGameId($gameid, $filter, $ordering, true, $group_filter);
$result = $game_results;
}

View File

@ -35,6 +35,7 @@ function fetch_results() {
let filterF = document.getElementById("filter");
let orderbyF = document.getElementById("orderby");
let groupsF = document.getElementById("groups");
let filter = autoconvert_datetime(filterF.value.trim());
@ -44,7 +45,8 @@ function fetch_results() {
action: "get_results_by_gameid",
gameid: GAMEID,
filter: filter.trim(),
orderby: orderbyF.value.trim()
orderby: orderbyF.value.trim(),
groups: groupsF.value.trim()
};
request(req).then(resp => {

View File

@ -48,7 +48,8 @@ if (!$gameMgr->getGame($game_id)->isUserContributorOrOwner($user_data["nickname"
<section style="margin-bottom: 0.3em">
<input type="text" placeholder="Szűrőfeltétel" id="filter" style="font-family: 'Monaco', monospace; width: 50em;">
<input type="text" placeholder="Rendezés" id="orderby" style="font-family: 'Monaco', monospace; width: 50em;">
<input type="text" placeholder="Csoportok" id="groups" style="font-family: 'Monaco', monospace; width: 30em;">
<input type="text" placeholder="Rendezés" id="orderby" style="font-family: 'Monaco', monospace; width: 30em;">
<input type="button" value="Szűrés" onclick="fetch_results()">
<input type="button" value="Jelentés előállítása" onclick="generate_report()">
<input type="button" value="Kijelöltek törlése" onclick="delete_tests()">