Check-in [a08c7b5ce3]
Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add survey tbl display tool. |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
a08c7b5ce3512788d9f3da858005bb87 |
User & Date: | mario 2021-09-19 10:16:52 |
Context
2021-10-09
| ||
20:30 | Prepare meta tags for wiki pages check-in: e228e3b3dc user: mario tags: trunk | |
2021-09-19
| ||
10:16 | Add survey tbl display tool. check-in: a08c7b5ce3 user: mario tags: trunk | |
09:31 | crude survey tool (storage backend only) check-in: 43bb630b42 user: mario tags: trunk | |
Changes
Changes to extroot/survey.
1 2 3 4 5 6 7 8 | #!/usr/bin/php-cgi -dcgi.force_redirect=0 <?php # encoding: utf-8 # api: cgi # type: store # category: database # title: Survey submit # description: Store simple POST form data into database survey_* table | | | | | > | > | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #!/usr/bin/php-cgi -dcgi.force_redirect=0 <?php # encoding: utf-8 # api: cgi # type: store # category: database # title: Survey submit # description: Store simple POST form data into database survey_* table # version: 0.2 # state: alpha # config: - # # Requires a form like: # # <action target="/ext/survey/SAMPLE" method=POST> # <input type=checkbox name=q1 value=1> # <input type=radio name=q2 value=1> # <input type=text name=q3> # # And will then populate a `survey_SAMPLE` table with JSON entries (no # field expansion or anything). # #-- 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 store() { $tbl = preg_replace("/\W+/", "", basename($_SERVER["PATH_INFO"])); if (!$tbl) { die("No table name given /survey/NAME"); } else { $json = json_encode($_POST); db("CREATE TABLE IF NOT EXISTS `survey_$tbl` (submitted TEXT, remote_addr TEXT, fields TEXT)"); #error_reporting(E_ALL);ini_set("display_errors", 1); db("INSERT INTO `survey_$tbl` (submitted, remote_addr, fields) VALUES (DATETIME('now'), ?, ?)", [$_SERVER["REMOTE_ADDR"], $json]); header("Location: /#thanks"); } } #-- run if (count($_POST)) { store(); } else { die("Empty form"); } |
Added extroot/survey_sum.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #!/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"); } |