java.lang.Object
ai.tutor.cab302exceptionalhandlers.controller.QuizController

public class QuizController extends Object
Controller for managing the quiz-taking interface in the AI tutor application.

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 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 connection
      chosenQuiz - The quiz to be taken
      currentUser - The authenticated user taking the quiz
      Throws:
      IllegalStateException - If the user or quiz is null
      RuntimeException - If initialization fails
      SQLException - 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

      public void saveAnswers(int messageId, int attempt, Map<Integer,String> answers, UserAnswerDAO dao)
      Saves user answers to the database.

      Creates and persists a UserAnswer record for each user-selected answer.

      Parameters:
      messageId - The message ID of the quiz
      attempt - The attempt number
      answers - The map of question numbers to answers
      dao - The UserAnswerDAO for database operations
    • getQuiz

      public Quiz 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

      public List<QuizQuestion> 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

      public QuizQuestion getQuizQuestion(int questionNumber)
      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

      public List<AnswerOption> getQuestionAnswerOptions(int questionNumber)
      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

      public AnswerOption getQuestionAnswerOption(int questionNumber, String option)
      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 question
      option - The option value to retrieve
      Returns:
      The answer option, or null if a database error occurs
    • createNewUserAnswer

      public UserAnswer createNewUserAnswer(int questionNumber, String option)
      Creates a new user answer record based on user input.

      Validates the question number and answer option before creating and persisting a UserAnswer record using UserAnswerDAO.

      Parameters:
      questionNumber - The number of the question
      option - 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

      public UserAnswer getQuestionUserAnswer(int attempt, int questionNumber)
      Retrieves a specific user answer record by attempt and question number.

      Fetches a user answer using UserAnswerDAO.

      Parameters:
      attempt - The attempt number
      questionNumber - The number of the question
      Returns:
      The user answer, or null if a database error occurs
    • getQuizUserAnswers

      public List<UserAnswer> getQuizUserAnswers(int attempt)
      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