extends Control # Extending Control
Define API URLs
var base_url = “https://lms.jinhyung.kim/api/quiz/”
var chapter_id = 1 # Assuming chapter 1 for example
var submit_url = base_url + str(chapter_id) + “/submit”
Define HTTPRequest and RichTextLabel nodes
@onready var http_request = $HTTPRequest
@onready var rich_text_label = $RichTextLabel # Get RichTextLabel node
When the scene is ready, fetch quiz questions for the specified chapter
func _ready():
# Get jwt_token from globals.gd
var token = Globals.jwt_token
get_quiz_questions(token)
Function to fetch quiz questions for a chapter
func get_quiz_questions(token):
var url = base_url + str(chapter_id) # Construct the request URL
var headers = ["Authorization: Bearer " + token] # Set Authorization header
http_request.request(url, headers, true, HTTPClient.METHOD_GET) # Send GET request without fourth parameter
http_request.connect(“request_completed”, Callable(self, “_on_get_quiz_request_completed”)) # Connect signal
Handle signal when quiz questions request is completed
func _on_get_quiz_request_completed(result, response_code, headers, body):
if response_code == 200:
var json = JSON.new() # Instantiate JSON class
var json_result = json.parse(body.get_string_from_utf8())
if json_result.error == OK:
var quiz_questions = json_result.result
print("Quiz questions: ", quiz_questions)
# Display the questions in the RichTextLabel
var display_text = ""
for i in range(quiz_questions.size()):
var question = quiz_questions[i].content # Get the question content
display_text += str(i + 1) + ". " + question + "\n" # Format question number and content
for j in range(quiz_questions[i].options.size()):
var option = quiz_questions[i].options[j].content # Get option content
# Generate option letters (a, b, c, d...)
var option_letter = String(char(97 + j)) # 97 is the ASCII value for 'a'
display_text += " " + option_letter + ") " + option + "\n"
# Set the quiz content to the RichTextLabel
rich_text_label.text = display_text
# Simulate user answering and submit answers
submit_quiz_answers(quiz_questions)
else:
print("JSON parse error")
else:
print("Error fetching quiz questions: ", response_code)
Function to submit answers
func submit_quiz_answers(quiz_questions):
var token = Globals.jwt_token # Get token from Globals
# Construct the answer dictionary (Simulating user selections)
var answers = {
"answers": [
{ "quizId": quiz_questions[0].id, "optionId": quiz_questions[0].options[1].id }, # Assume the user selects the second option for the first question
{ "quizId": quiz_questions[1].id, "optionId": quiz_questions[1].options[3].id } # Assume the user selects the fourth option for the second question
]
}
# Convert the answer dictionary to a JSON string
var json_data = JSON.stringify(answers)
# Set request headers
var headers = ["Authorization: Bearer " + token, "Content-Type: application/json"]
# Send POST request, ensuring the fourth parameter is a JSON string
http_request.request(submit_url, headers, true, HTTPClient.METHOD_POST, json_data)
# Connect the signal for request completion
http_request.connect("request_completed", Callable(self, "_on_submit_quiz_request_completed"))
Handle signal when quiz submission request is completed
func _on_submit_quiz_request_completed(result, response_code, headers, body):
if response_code == 200:
var json = JSON.new() # Instantiate JSON class
var json_result = json.parse(body.get_string_from_utf8())
if json_result.error == OK:
var submission_result = json_result.result
print("Submission result: ", submission_result)
# Display submission results
rich_text_label.text += “\n\nQuiz submission results:\n”
rich_text_label.text += "Correct Answers: " + str(submission_result.correctAnswers) + “\n”
rich_text_label.text += "Total Score: " + str(submission_result.totalScore)
else:
print(“JSON parse error”)
else:
print("Error submitting answers: ", response_code)