#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: display
# category: database
# title: Survey summarize
# description: Only collects
# version: 0.2
# state: alpha
# config: -
#
# Requires a link like "/ext/survey_sum/SAMPLE",
# and counts the numeric fields (checkboxes yes/no).
# It's just meant for a quick result list, and
# outputs approximate points 🞓
#-- database (== fossil repo)
function db($sql="", $params=[]) {
static $db;
if (empty($db)) {
$db = new PDO("sqlite:$_SERVER[FOSSIL_REPOSITORY]");
#$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
if ($params) {
$stmt = $db->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
else {
return $db->query($sql)->fetchAll();
}
}
#-- insert
function show($tbl) {
$r = [];
foreach (db("SELECT fields FROM `survey_$tbl`") as $j) {
if ($j = json_decode($j["fields"])) {
foreach ($j as $k=>$v) {
if (is_numeric($v)) {
@$r[$k] += $v;
}
}
}
}
ini_set("display_errors", 1);
if ($r) {
print("| question | score |\n|-------|-------|\n");
$max = max($r) + 1;
foreach ($r as $k=>$v) {
$k = htmlspecialchars($k, ENT_QUOTES); # though already MD
$n = ($v / $max) * 20.1;
$n = str_repeat("🞓", (int)$n);
print("| $k | $n |\n");
}
}
}
#-- run
if ($tbl = preg_replace("/\W+/", "", basename($_SERVER["PATH_INFO"]))) {
header("Content-Type: text/x-markdown");
print("\n##### survey $tbl\n##### Survey '*$tbl*' (approx. results)\n\n");
show($tbl);
}
else {
die("No table name given /survey/NAME");
}