111 lines
4.3 KiB
PHP
111 lines
4.3 KiB
PHP
<?php
|
||
session_start();
|
||
|
||
require_once __DIR__ . '/../ai/LocalAIApi.php';
|
||
require_once __DIR__ . '/../db/config.php';
|
||
|
||
header('Content-Type: application/json');
|
||
|
||
$questions = [
|
||
"I wake up feeling tired, even after a full night’s sleep.",
|
||
"By midday, I already feel mentally drained and out of energy.",
|
||
"I feel emotionally numb, detached, or ‘on autopilot’ most of the time.",
|
||
"Things that used to excite me now feel pointless or like a chore.",
|
||
"I feel irritated or cynical about people I work or study with.",
|
||
"I struggle to focus and constantly procrastinate, even on important tasks.",
|
||
"I feel guilty for not doing ‘enough’, no matter how much I actually do.",
|
||
"I often think about quitting everything for a while or disappearing from social media and work.",
|
||
"I use caffeine, sugar, nicotine, alcohol, or scrolling to ‘numb out’ instead of resting.",
|
||
"I feel like my life is just surviving, not really living"
|
||
];
|
||
|
||
if (!isset($_SESSION['question_index'])) {
|
||
$_SESSION['question_index'] = 0;
|
||
$_SESSION['answers'] = [];
|
||
}
|
||
|
||
$response = [];
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$data = json_decode(file_get_contents('php://input'), true);
|
||
$answer = $data['answer'] ?? null;
|
||
|
||
if ($answer !== null && $_SESSION['question_index'] < count($questions)) {
|
||
$_SESSION['answers'][$_SESSION['question_index']] = $answer;
|
||
$_SESSION['question_index']++;
|
||
}
|
||
}
|
||
|
||
if ($_SESSION['question_index'] < count($questions)) {
|
||
$response['question'] = $questions[$_SESSION['question_index']];
|
||
} else {
|
||
// Survey is complete, analyze the answers
|
||
$formatted_answers = [];
|
||
foreach ($_SESSION['answers'] as $index => $answer) {
|
||
$formatted_answers[] = "Q: " . $questions[$index] . "\nA: " . $answer;
|
||
}
|
||
$prompt = "You are a burnout analysis expert. A user has completed a burnout survey. The user answered on a scale of 1-5 where 1 is 'Never' and 5 is 'Always'. Analyze the following survey answers and provide a detailed analysis. Return the response as a JSON object with the following structure:
|
||
{
|
||
\"scores\": {
|
||
\"Exhaustion\": <score_0_to_5>,
|
||
\"Cynicism\": <score_0_to_5>,
|
||
\"Inefficacy\": <score_0_to_5>
|
||
},
|
||
\"analysis\": {
|
||
\"overallSummary\": \"<A paragraph summarizing the user's burnout level and key areas of concern.>\",
|
||
\"exhaustionSummary\": \"<A sentence or two explaining the exhaustion score.>\",
|
||
\"cynicismSummary\": \"<A sentence or two explaining the cynicism score.>\",
|
||
\"inefficacySummary\": \"<A sentence or two explaining the inefficacy score.>\"
|
||
},
|
||
\"recommendations\": [
|
||
{
|
||
\"title\": \"<Short recommendation title>\",
|
||
\"description\": \"<Longer description of the recommendation, explaining why it's important and how to implement it.>\"
|
||
},
|
||
... (provide 3 to 5 detailed recommendations)
|
||
],
|
||
\"nextSteps\": [
|
||
\"<A suggestion for a next step, e.g., 'Consider talking to a mental health professional.'>\",
|
||
\"<Another suggestion, e.g., 'Explore mindfulness exercises.'>\"
|
||
]
|
||
}
|
||
|
||
Answers:
|
||
" . implode("\n\n", $formatted_answers);
|
||
|
||
$ai_response = LocalAIApi::createResponse([
|
||
'input' => [
|
||
['role' => 'system', 'content' => 'You are a burnout analysis expert.'],
|
||
['role' => 'user', 'content' => $prompt],
|
||
],
|
||
]);
|
||
|
||
if (!empty($ai_response['success'])) {
|
||
$decoded_response = LocalAIApi::decodeJsonFromResponse($ai_response);
|
||
$_SESSION['results'] = $decoded_response;
|
||
|
||
if (isset($_SESSION['user_id'])) {
|
||
try {
|
||
$pdo = db();
|
||
$stmt = $pdo->prepare('INSERT INTO survey_results (user_id, results_json) VALUES (:user_id, :results_json)');
|
||
$stmt->execute([
|
||
':user_id' => $_SESSION['user_id'],
|
||
':results_json' => json_encode($decoded_response)
|
||
]);
|
||
} catch (PDOException $e) {
|
||
// Optionally handle or log the database error
|
||
}
|
||
}
|
||
} else {
|
||
// Handle AI API error, maybe provide default results
|
||
$_SESSION['results'] = [
|
||
'scores' => ['Exhaustion' => 0, 'Cynicism' => 0, 'Inefficacy' => 0],
|
||
'recommendations' => ['Could not analyze results at this time. Please try again later.']
|
||
];
|
||
}
|
||
|
||
$response['redirect'] = 'results.php';
|
||
}
|
||
|
||
echo json_encode($response);
|