java.lang.Object
fr.paris.lutece.plugins.wiki.modules.quiz.service.QuizService

public final class QuizService extends Object
Service class for quiz management operations.
  • Method Details

    • findById

      public static Quiz findById(int nQuizId)
      Finds a quiz by its ID.
      Parameters:
      nQuizId - the quiz ID
      Returns:
      the quiz, or null if not found
    • getQuizzesByBook

      public static List<Quiz> getQuizzesByBook(int nIdBook)
      Retrieves all quizzes associated with a specific book.
      Parameters:
      nIdBook - the book ID
      Returns:
      the list of quizzes
    • getPublishedQuizzesByBook

      public static List<Quiz> getPublishedQuizzesByBook(int nIdBook)
      Retrieves all published quizzes associated with a specific book.
      Parameters:
      nIdBook - the book ID
      Returns:
      the list of published quizzes
    • create

      public static Quiz create(Quiz quiz, Locale locale) throws fr.paris.lutece.plugins.wiki.exception.WikiValidationException
      Creates a new quiz.
      Parameters:
      quiz - the quiz to create
      locale - the locale for error messages
      Returns:
      the created quiz
      Throws:
      fr.paris.lutece.plugins.wiki.exception.WikiValidationException - if validation fails
    • update

      public static Quiz update(Quiz quiz, Locale locale) throws fr.paris.lutece.plugins.wiki.exception.WikiValidationException
      Updates an existing quiz.
      Parameters:
      quiz - the quiz to update
      locale - the locale for error messages
      Returns:
      the updated quiz
      Throws:
      fr.paris.lutece.plugins.wiki.exception.WikiValidationException - if validation fails
    • delete

      public static void delete(int nQuizId)
      Deletes a quiz.
      Parameters:
      nQuizId - the quiz ID
    • getQuizWithQuestions

      public static Quiz getQuizWithQuestions(int nQuizId)
      Retrieves a quiz along with its questions and answers.
      Parameters:
      nQuizId - the quiz ID
      Returns:
      the quiz with questions and answers, or null if not found
    • createQuestion

      public static QuizQuestion createQuestion(QuizQuestion question)
      Creates a new question.
      Parameters:
      question - the question to create
      Returns:
      the created question
    • updateQuestion

      public static QuizQuestion updateQuestion(QuizQuestion question)
      Updates an existing question.
      Parameters:
      question - the question to update
      Returns:
      the updated question
    • deleteQuestion

      public static void deleteQuestion(int nQuestionId)
      Deletes a question and its associated answers.
      Parameters:
      nQuestionId - the question ID
    • reorderQuestions

      public static void reorderQuestions(List<Integer> questionIds)
      Reorders questions based on the provided list of question IDs.
      Parameters:
      questionIds - the list of question IDs in the desired order
    • replaceAnswers

      public static void replaceAnswers(int nQuestionId, List<QuizAnswer> listAnswers)
      Replaces the answers for a question.
      Parameters:
      nQuestionId - the question ID
      listAnswers - the list of new answers
    • setQuestionSourcePages

      public static void setQuestionSourcePages(int nQuestionId, List<Integer> listPageIds)
      Sets the source pages for a question.
      Parameters:
      nQuestionId - the question ID
      listPageIds - the list of page IDs
    • canStartAttempt

      public static boolean canStartAttempt(Quiz quiz, String strUserGuid, boolean bCanEdit)
      Checks if a user can start a new attempt for a quiz.
      Parameters:
      quiz - The quiz to check
      strUserGuid - The unique identifier of the user
      bCanEdit - Whether the user has edit permissions on the quiz
      Returns:
      true if the user can start an attempt, false otherwise
    • startAttempt

      public static QuizAttempt startAttempt(int nQuizId, fr.paris.lutece.portal.service.security.LuteceUser user)
      Starts a new quiz attempt for a user.
      Parameters:
      nQuizId - The identifier of the quiz
      user - The Lutece user starting the attempt
      Returns:
      The created QuizAttempt object
    • getQuestionsForAttempt

      public static List<QuizQuestion> getQuestionsForAttempt(Quiz quiz)
      Retrieves all questions for a quiz attempt with answers, applying shuffle if configured.
      Parameters:
      quiz - The quiz to get questions for
      Returns:
      A list of QuizQuestion objects with their answers
    • submitAnswer

      public static QuizResponse submitAnswer(int nAttemptId, int nQuestionId, String strUserAnswer)
      Submits an answer for a question in an attempt and calculates the score.
      Parameters:
      nAttemptId - The identifier of the attempt
      nQuestionId - The identifier of the question
      strUserAnswer - The user's answer
      Returns:
      The QuizResponse object, or null if the question was not found
    • completeAttempt

      public static QuizAttempt completeAttempt(int nAttemptId)
      Completes a quiz attempt by calculating the final score and determining pass/fail status.
      Parameters:
      nAttemptId - The identifier of the attempt to complete
      Returns:
      The completed QuizAttempt object, or null if the attempt or quiz was not found
    • getAttemptWithResponses

      public static QuizAttempt getAttemptWithResponses(int nAttemptId)
      Retrieves a quiz attempt along with all its responses.
      Parameters:
      nAttemptId - The identifier of the attempt
      Returns:
      The QuizAttempt object with responses populated, or null if not found
    • getUserAttempts

      public static List<QuizAttempt> getUserAttempts(int nQuizId, String strUserGuid)
      Retrieves all attempts made by a user for a specific quiz.
      Parameters:
      nQuizId - The identifier of the quiz
      strUserGuid - The unique identifier of the user
      Returns:
      A list of QuizAttempt objects
    • getInProgressAttempt

      public static QuizAttempt getInProgressAttempt(int nQuizId, String strUserGuid)
      Retrieves an in-progress (incomplete) attempt for a user on a specific quiz.
      Parameters:
      nQuizId - The identifier of the quiz
      strUserGuid - The unique identifier of the user
      Returns:
      The in-progress QuizAttempt, or null if none exists
    • calculateTimeRemainingSeconds

      public static long calculateTimeRemainingSeconds(QuizAttempt attempt, Quiz quiz)
      Calculates the remaining time in seconds for a timed quiz attempt.
      Parameters:
      attempt - The quiz attempt
      quiz - The quiz with time limit configuration
      Returns:
      The remaining time in seconds, or -1 if no time limit is set or attempt is completed
    • isTimeExpired

      public static boolean isTimeExpired(QuizAttempt attempt, Quiz quiz)
      Checks if the time has expired for a timed quiz attempt.
      Parameters:
      attempt - The quiz attempt
      quiz - The quiz with time limit configuration
      Returns:
      true if time has expired, false otherwise