diff --git a/db.php b/db.php new file mode 100644 index 0000000..49b83c3 --- /dev/null +++ b/db.php @@ -0,0 +1,139 @@ + 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 + ]); + } + } +} + + + +?> diff --git a/drawbar.js b/drawbar.js index ee9ad7e..2d584ac 100644 --- a/drawbar.js +++ b/drawbar.js @@ -314,39 +314,3 @@ function doBarData(id,qid, survey, Q1=0, Q2=0, Q3=0 ) { 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 }] - }); -}