Class QuizController
Handles the display of quiz questions, collection of user answers, submission of quiz
results, and navigation back to the chat screen. Interacts with the database via
QuizDAO
, QuizQuestionDAO
, AnswerOptionDAO
, and
UserAnswerDAO
to retrieve quiz details and store user answers. Uses JavaFX for
rendering the quiz UI, including question lists, answer buttons, and submission logic.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionQuizController
(SQLiteConnection db, Quiz chosenQuiz, User currentUser) Constructs a new QuizController with the specified database connection, quiz, and user. -
Method Summary
Modifier and TypeMethodDescriptioncreateNewUserAnswer
(int questionNumber, String option) Creates a new user answer record based on user input.getQuestionAnswerOption
(int questionNumber, String option) Retrieves a specific answer option record by question number and option value.getQuestionAnswerOptions
(int questionNumber) Retrieves all answer option records for a specific question.getQuestionUserAnswer
(int attempt, int questionNumber) Retrieves a specific user answer record by attempt and question number.getQuiz()
Retrieves the current quiz record from the database.getQuizQuestion
(int questionNumber) Retrieves a specific quiz question record by number.Retrieves all quiz question records for the current quiz.getQuizUserAnswers
(int attempt) Retrieves all user answer records for a specific quiz attempt.void
Initializes the quiz interface components.void
saveAnswers
(int messageId, int attempt, Map<Integer, String> answers, UserAnswerDAO dao) Saves user answers to the database.
-
Constructor Details
-
QuizController
public QuizController(SQLiteConnection db, Quiz chosenQuiz, User currentUser) throws IllegalStateException, RuntimeException, SQLException Constructs a new QuizController with the specified database connection, quiz, and user.Initializes the controller with the provided database connection, the chosen quiz, and the authenticated user. Throws exceptions if the user or quiz is null or if database operations fail.
- Parameters:
db
- The SQLite database connectionchosenQuiz
- The quiz to be takencurrentUser
- The authenticated user taking the quiz- Throws:
IllegalStateException
- If the user or quiz is nullRuntimeException
- If initialization failsSQLException
- If database operations fail during initialization
-
-
Method Details
-
initialize
public void initialize()Initializes the quiz interface components.Sets up the quiz name, loads questions, configures the question list view, return button, submit button, and attempts dropdown. Displays the first question by default.
-
saveAnswers
Saves user answers to the database.Creates and persists a
UserAnswer
record for each user-selected answer.- Parameters:
messageId
- The message ID of the quizattempt
- The attempt numberanswers
- The map of question numbers to answersdao
- The UserAnswerDAO for database operations
-
getQuiz
Retrieves the current quiz record from the database.Fetches the quiz details using the message ID from
QuizDAO
.- Returns:
- The current quiz, or null if a database error occurs
-
getQuizQuestions
Retrieves all quiz question records for the current quiz.Fetches all questions associated with the quiz using
QuizQuestionDAO
.- Returns:
- A list of quiz questions, or null if a database error occurs
-
getQuizQuestion
Retrieves a specific quiz question record by number.Fetches a single question for the current quiz using
QuizQuestionDAO
.- Parameters:
questionNumber
- The number of the question to retrieve- Returns:
- The quiz question, or null if a database error occurs
-
getQuestionAnswerOptions
Retrieves all answer option records for a specific question.Fetches answer options for the specified question number using
AnswerOptionDAO
.- Parameters:
questionNumber
- The number of the question to retrieve options for- Returns:
- A list of answer options, or null if a database error occurs
-
getQuestionAnswerOption
Retrieves a specific answer option record by question number and option value.Fetches a single answer option using
AnswerOptionDAO
.- Parameters:
questionNumber
- The number of the questionoption
- The option value to retrieve- Returns:
- The answer option, or null if a database error occurs
-
createNewUserAnswer
Creates a new user answer record based on user input.Validates the question number and answer option before creating and persisting a
UserAnswer
record usingUserAnswerDAO
.- Parameters:
questionNumber
- The number of the questionoption
- The user’s selected answer option- Returns:
- The created user answer, or null if a database error occurs
- Throws:
IllegalArgumentException
- If the question number or option is invalid
-
getQuestionUserAnswer
Retrieves a specific user answer record by attempt and question number.Fetches a user answer using
UserAnswerDAO
.- Parameters:
attempt
- The attempt numberquestionNumber
- The number of the question- Returns:
- The user answer, or null if a database error occurs
-
getQuizUserAnswers
Retrieves all user answer records for a specific quiz attempt.Fetches all answers for the specified attempt using
UserAnswerDAO
.- Parameters:
attempt
- The attempt number- Returns:
- A list of user answers, or null if a database error occurs
-