#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: display
# category: database
# title: Survey summarize
# description: Only collects numeric entries for approximate stats
# version: 0.2
# state: alpha
# config: -
# access: public
#
# 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 🞓


include("./fossil_common.php");


#-- 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");
}

