|
|
import json |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
|
|
|
|
|
DB_PATH = os.path.join(BASE_DIR, 'data', 'questions.json') |
|
|
|
|
|
class QuizHandler: |
|
|
@staticmethod |
|
|
def get_questions(): |
|
|
"""Mengambil semua soal dari database JSON""" |
|
|
try: |
|
|
if not os.path.exists(DB_PATH): |
|
|
return [] |
|
|
with open(DB_PATH, 'r') as f: |
|
|
return json.load(f) |
|
|
except Exception as e: |
|
|
print(f"Error reading quiz db: {e}") |
|
|
return [] |
|
|
|
|
|
@staticmethod |
|
|
def calculate_mbti(answers): |
|
|
""" |
|
|
Hitung MBTI berdasarkan jawaban user. |
|
|
Format answers: { "1": 2, "2": -1, ... } (Key=ID Soal, Value=Skala -3 s/d 3) |
|
|
""" |
|
|
questions = QuizHandler.get_questions() |
|
|
if not questions: |
|
|
return "UNKNOWN" |
|
|
|
|
|
|
|
|
scores = {'EI': 0, 'SN': 0, 'TF': 0, 'JP': 0} |
|
|
|
|
|
for q in questions: |
|
|
q_id = str(q['id']) |
|
|
if q_id in answers: |
|
|
|
|
|
|
|
|
|
|
|
val = int(answers[q_id]) |
|
|
scores[q['dimension']] += (val * q['direction']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = "" |
|
|
result += "E" if scores['EI'] >= 0 else "I" |
|
|
result += "S" if scores['SN'] >= 0 else "N" |
|
|
result += "T" if scores['TF'] >= 0 else "F" |
|
|
result += "J" if scores['JP'] >= 0 else "P" |
|
|
|
|
|
return result |