Added Filters table

This commit is contained in:
Peter Edmond 2025-05-30 21:27:29 +01:00
parent a6793ac03e
commit 437e1afede
5 changed files with 188 additions and 9 deletions

0
MITLicense.txt Normal file
View File

73
MakeDatabase.sql Normal file
View File

@ -0,0 +1,73 @@
USE demodb;
CREATE TABLE `Surveys` (
`id` int NOT NULL AUTO_INCREMENT,
`surveyId` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `unique_id` (`id`),
UNIQUE KEY `unique_surveyid` (`surveyId`)
);
CREATE TABLE `Responses` (
`id` int NOT NULL AUTO_INCREMENT,
`Q1` int DEFAULT NULL,
`Q2` int DEFAULT NULL,
`Q3` int DEFAULT NULL,
`surveyId` int NOT NULL,
`responseId` varchar(18) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`startDate` datetime DEFAULT NULL,
`endDate` datetime DEFAULT NULL,
`status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ipAddress` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`progress` int DEFAULT NULL,
`duration` int DEFAULT NULL,
`finished` tinyint(1) DEFAULT NULL,
`recordedDate` datetime DEFAULT NULL,
`locationLatitude` decimal(9,6) DEFAULT NULL,
`locationLongitude` decimal(9,6) DEFAULT NULL,
`distributionChannel` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`userLanguage` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_id` (`id`),
UNIQUE KEY `unique_response_id` (`responseId`),
KEY `Responses_ibfk_1` (`surveyId`),
CONSTRAINT `Responses_ibfk_1` FOREIGN KEY (`surveyId`) REFERENCES `Surveys` (`id`)
);
CREATE TABLE `Answers` (
`id` int NOT NULL AUTO_INCREMENT,
`surveyId` int NOT NULL,
`responseId` int NOT NULL,
`QID` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
`text` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`value` tinyint DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_survey_response_qid` (`surveyId`,`responseId`,`QID`),
KEY `surveyId` (`surveyId`),
KEY `responseId` (`responseId`),
CONSTRAINT `Answers_ibfk_1` FOREIGN KEY (`surveyId`) REFERENCES `Surveys` (`id`),
CONSTRAINT `Answers_ibfk_2` FOREIGN KEY (`responseId`) REFERENCES `Responses` (`id`),
CONSTRAINT `Answers_chk_1` CHECK ((`value` between -(3) and 3))
);
CREATE TABLE `Users` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`password_hash` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`totp_secret` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`role` enum('user','admin') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'user',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
);
CREATE TABLE Filters (
id INT NOT NULL AUTO_INCREMENT,
surveyId INT NOT NULL,
FilterQuestion INT NOT NULL,
FilterOption INT NOT NULL,
Text VARCHAR(128),
PRIMARY KEY (id),
FOREIGN KEY (surveyId) REFERENCES Surveys(id)
);

View File

@ -4,6 +4,9 @@ header('Content-Type: application/json');
if (php_sapi_name() === 'cli') { if (php_sapi_name() === 'cli') {
echo "Running from command line.\n"; echo "Running from command line.\n";
$qid="QID2_7"; $qid="QID2_7";
$q1 = 1;
$q2 = 2;
$q3 = 0;
} else { } else {
//echo "Running from browser.\n"; //echo "Running from browser.\n";
// Basic input validation and sanitization // Basic input validation and sanitization
@ -12,6 +15,22 @@ if (php_sapi_name() === 'cli') {
exit; exit;
} }
$qid = $_POST['qid']; $qid = $_POST['qid'];
$q1 = filter_input(INPUT_POST, 'q1', FILTER_VALIDATE_INT);
$q2 = filter_input(INPUT_POST, 'q2', FILTER_VALIDATE_INT);
$q3 = filter_input(INPUT_POST, 'q3', FILTER_VALIDATE_INT);
}
$qualifier = "";
if ($q1 > 0) {
$qualifier.=" AND r.Q1 = " . $q1;
}
if ($q2 > 0) {
$qualifier.=" AND r.Q2 = " . $q2;
}
if ($q3 > 0) {
$qualifier.=" AND r.Q3 = " . $q3;
} }
@ -34,14 +53,21 @@ try {
$pdo = new PDO($dsn, $user, $pass, $options); $pdo = new PDO($dsn, $user, $pass, $options);
$sql = " $sql = "
SELECT value, COUNT(*) AS count SELECT
FROM Answers a.value,
WHERE QID = :qid COUNT(*) AS count,
AND value BETWEEN -3 AND 3 r.Q1,
GROUP BY value r.Q2,
ORDER BY value r.Q3
FROM Answers a
INNER JOIN Responses r ON a.responseId = r.id
WHERE a.QID = :qid' . $quantifier . '
GROUP BY a.value, r.Q1, r.Q2, r.Q3
ORDER BY a.value;
"; ";
echo $sql;
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute(['qid' => $qid]); $stmt->execute(['qid' => $qid]);
$results = $stmt->fetchAll(); $results = $stmt->fetchAll();

View File

@ -97,21 +97,21 @@
<br> <br>
<div style="visibility: hidden" id="groupings"> <div style="visibility: hidden" id="groupings">
<form> <form>
<label for="location">Where do you work in the organisation:</label> <label for="location">Where do you work in the organisation?:</label>
<select id="location" name="location"> <select id="location" name="location">
<option value="0">Any</option> <option value="0">Any</option>
<option value="1">Site 1</option> <option value="1">Site 1</option>
<option value="2">Site 2</option> <option value="2">Site 2</option>
</select> </select>
<br> <br>
<label for="level">Where operational level do you work at in the organisation:</label> <label for="level">Where operational level do you work at in the organisation?:</label>
<select id="level" name="level"> <select id="level" name="level">
<option value="0">Any</option> <option value="0">Any</option>
<option value="1">Role 1</option> <option value="1">Role 1</option>
<option value="2">Role 2</option> <option value="2">Role 2</option>
</select> </select>
<br> <br>
<label for="gov">Where operational level do you work at in the organisation:</label> <label for="gov">Have you ever held one of the following roles in our business?:</label>
<select id="gov" name="gov"> <select id="gov" name="gov">
<option value="0">Any</option> <option value="0">Any</option>
<option value="1">Yes</option> <option value="1">Yes</option>

80
setupGroups.php Normal file
View File

@ -0,0 +1,80 @@
<?php
header('Content-Type: application/json');
if (php_sapi_name() === 'cli') {
echo "Running from command line.\n";
} else {
//echo "Running from browser.\n";
// Basic input validation and sanitization
if (!isset($_POST['action']) || empty($_POST['qid'])) {
echo json_encode(['error' => 'Needs an action to carry out']);
exit;
}
}
// Database connection (adjust credentials accordingly)
//
$config = require 'config.php';
$host = $config['db_host'];
$db = $config['db_name'];
$user = $config['db_user'];
$pass = $config['db_pass'];
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
// Map of QIDs to Response columns
$qidToColumn = [
'QID68' => 'Q1',
'QID69' => 'Q2',
'QID70' => 'Q3'
];
$totalUpdated = 0;
foreach ($qidToColumn as $qid => $column) {
$sql = "
UPDATE Responses r
JOIN Answers a ON r.id = a.responseId
SET r.{$column} = a.value
WHERE a.QID = :qid
AND r.{$column} IS NULL
";
$stmt = $pdo->prepare($sql);
$stmt->execute(['qid' => $qid]);
$rows = $stmt->rowCount();
echo "Updated {$rows} rows for {$qid}{$column}\n";
$totalUpdated += $rows;
}
echo "Total rows updated: {$totalUpdated}\n";
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
}