Added code to update database from survey
This commit is contained in:
parent
dc68e98d92
commit
2cffceb5f5
139
db.php
Normal file
139
db.php
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// Settings
|
||||||
|
$config = require 'config.php';
|
||||||
|
|
||||||
|
|
||||||
|
$host = $config['db_host'];
|
||||||
|
$db = $config['db_name'];
|
||||||
|
$user = $config['db_user'];
|
||||||
|
$pass = $config['db_pass'];
|
||||||
|
echo $user.":".$pass.":".$host.":".$db;
|
||||||
|
$charset = 'utf8mb4';
|
||||||
|
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
|
||||||
|
|
||||||
|
$options = [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // throw exceptions on error
|
||||||
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // fetch results as associative arrays
|
||||||
|
PDO::ATTR_EMULATE_PREPARES => false, // use native prepared statements
|
||||||
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = new PDO($dsn, $user, $pass, $options);
|
||||||
|
} catch (\PDOException $e) {
|
||||||
|
die("Database connection failed: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ...and now the functions
|
||||||
|
|
||||||
|
function getOrCreateSurvey($surveyId, $description = '') {
|
||||||
|
global $pdo;
|
||||||
|
|
||||||
|
// Step 1: Check if survey exists
|
||||||
|
$check = $pdo->prepare("SELECT id FROM Surveys WHERE surveyId = :surveyId");
|
||||||
|
$check->execute(['surveyId' => $surveyId]);
|
||||||
|
$existing = $check->fetch();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
return $existing['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Insert if not found
|
||||||
|
$insert = $pdo->prepare("
|
||||||
|
INSERT INTO Surveys (surveyId, description)
|
||||||
|
VALUES (:surveyId, :description)
|
||||||
|
");
|
||||||
|
$insert->execute([
|
||||||
|
'surveyId' => $surveyId,
|
||||||
|
'description' => $description
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
// Step 3: Get the id
|
||||||
|
$check = $pdo->prepare("SELECT id FROM Surveys WHERE surveyId = :surveyId");
|
||||||
|
$check->execute(['surveyId' => $surveyId]);
|
||||||
|
$existing = $check->fetch();
|
||||||
|
return $existing['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
function upsertResponse($response) {
|
||||||
|
global $pdo;
|
||||||
|
|
||||||
|
$sql = "
|
||||||
|
INSERT INTO Responses (
|
||||||
|
responseId, surveyId, startDate, endDate, status, ipAddress,
|
||||||
|
progress, duration, finished, recordedDate, locationLatitude,
|
||||||
|
locationLongitude, distributionChannel, userLanguage
|
||||||
|
) VALUES (
|
||||||
|
:responseId, :surveyId, :startDate, :endDate, :status, :ipAddress,
|
||||||
|
:progress, :duration, :finished, :recordedDate, :locationLatitude,
|
||||||
|
:locationLongitude, :distributionChannel, :userLanguage
|
||||||
|
)
|
||||||
|
ON DUPLICATE KEY UPDATE
|
||||||
|
surveyId = VALUES(surveyId),
|
||||||
|
startDate = VALUES(startDate),
|
||||||
|
endDate = VALUES(endDate),
|
||||||
|
status = VALUES(status),
|
||||||
|
ipAddress = VALUES(ipAddress),
|
||||||
|
progress = VALUES(progress),
|
||||||
|
duration = VALUES(duration),
|
||||||
|
finished = VALUES(finished),
|
||||||
|
recordedDate = VALUES(recordedDate),
|
||||||
|
locationLatitude = VALUES(locationLatitude),
|
||||||
|
locationLongitude = VALUES(locationLongitude),
|
||||||
|
distributionChannel = VALUES(distributionChannel),
|
||||||
|
userLanguage = VALUES(userLanguage)
|
||||||
|
";
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare($sql);
|
||||||
|
$stmt->execute($response);
|
||||||
|
|
||||||
|
$selectStmt = $pdo->prepare("SELECT id FROM Responses WHERE responseId = :responseId");
|
||||||
|
$selectStmt->execute(['responseId' => $response['responseId']]);
|
||||||
|
$existing = $selectStmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
if ($existing) {
|
||||||
|
return $existing['id'];
|
||||||
|
} else {
|
||||||
|
return null; // not found, something unexpected happened
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function insertAnswers($pdo, $surveyId, $responseId, $answers) {
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare("
|
||||||
|
INSERT INTO Answers (surveyId, responseId, QID, text, value)
|
||||||
|
VALUES (:surveyId, :responseId, :QID, :text, :value)
|
||||||
|
ON DUPLICATE KEY UPDATE
|
||||||
|
text = VALUES(text),
|
||||||
|
value = VALUES(value)
|
||||||
|
");
|
||||||
|
|
||||||
|
foreach ($answers as $key => $val) {
|
||||||
|
if (strpos($key, 'QID') === 0) {
|
||||||
|
$qid = $key;
|
||||||
|
$text = null;
|
||||||
|
$value = null;
|
||||||
|
|
||||||
|
// Handle numeric between -3 and +3
|
||||||
|
if (is_numeric($val) && $val >= -3 && $val <= 3) {
|
||||||
|
$value = (int) $val;
|
||||||
|
} else {
|
||||||
|
$text = substr($val, 0, 255); // enforce VARCHAR(255) limit
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt->execute([
|
||||||
|
'surveyId' => $surveyId,
|
||||||
|
'responseId' => $responseId,
|
||||||
|
'QID' => $qid,
|
||||||
|
'text' => $text,
|
||||||
|
'value' => $value
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
36
drawbar.js
36
drawbar.js
@ -314,39 +314,3 @@ function doBarData(id,qid, survey, Q1=0, Q2=0, Q3=0 ) {
|
|||||||
console.error('Error:', error);
|
console.error('Error:', error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function Backup() {
|
|
||||||
return fetch('get_qid_counts.php', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
||||||
body: new URLSearchParams({ qid })
|
|
||||||
})
|
|
||||||
.then(response => {
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Network response was not OK');
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
})
|
|
||||||
.then(data => {
|
|
||||||
if (data.error) {
|
|
||||||
throw new Error(data.error);
|
|
||||||
}
|
|
||||||
// console.log(data);
|
|
||||||
// console.log(data[0][Object.keys(data[0])[0]]);
|
|
||||||
/* */
|
|
||||||
bardata = {
|
|
||||||
"-3": data[0][Object.keys(data[0])[0]],
|
|
||||||
"-2": data[1][Object.keys(data[1])[0]],
|
|
||||||
"-1": data[2][Object.keys(data[2])[0]],
|
|
||||||
"0": data[3][Object.keys(data[3])[0]],
|
|
||||||
"1": data[4][Object.keys(data[4])[0]],
|
|
||||||
"2": data[5][Object.keys(data[5])[0]],
|
|
||||||
"3": data[6][Object.keys(data[6])[0]],
|
|
||||||
};
|
|
||||||
|
|
||||||
//console.log(bardata);
|
|
||||||
drawBar(id,bardata);
|
|
||||||
// return data; // Should be an array like [{ value: -3, count: 2 }, ..., { value: 3, count: 5 }]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user