diff --git a/MITLicense.txt b/MITLicense.txt new file mode 100644 index 0000000..e69de29 diff --git a/MakeDatabase.sql b/MakeDatabase.sql new file mode 100644 index 0000000..a436629 --- /dev/null +++ b/MakeDatabase.sql @@ -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) +); \ No newline at end of file diff --git a/get_qid_counts.php b/get_qid_counts.php index 8312e89..9008be2 100644 --- a/get_qid_counts.php +++ b/get_qid_counts.php @@ -4,6 +4,9 @@ header('Content-Type: application/json'); if (php_sapi_name() === 'cli') { echo "Running from command line.\n"; $qid="QID2_7"; + $q1 = 1; + $q2 = 2; + $q3 = 0; } else { //echo "Running from browser.\n"; // Basic input validation and sanitization @@ -12,6 +15,22 @@ if (php_sapi_name() === 'cli') { exit; } $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); $sql = " - SELECT value, COUNT(*) AS count - FROM Answers - WHERE QID = :qid - AND value BETWEEN -3 AND 3 - GROUP BY value - ORDER BY value + SELECT + a.value, + COUNT(*) AS count, + r.Q1, + r.Q2, + 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->execute(['qid' => $qid]); $results = $stmt->fetchAll(); diff --git a/reportTemplate.html b/reportTemplate.html index 72eb867..314f240 100644 --- a/reportTemplate.html +++ b/reportTemplate.html @@ -97,21 +97,21 @@