Quiz game - Create questions, answers and historic

2020-03-07 08:15发布

I'm working on a game which has two types of user; admin and user, so admin can generate as many questions as he wants, so those questions can be NORMAL, MULTICHOICE, BINARI, SMALL_DESCRIPTION, so every time admin wants to create one he has to decide which type of answer wants to this question, also the topic of this Question ( it can be a subtopic ).

He can generate a Quiz, the way to generate a Quiz is that he has to select questions that he created before.

Also he can check the historical of the user, means that with a call to an endpoint he should be able to check the questions that that user did (with the score, which question has failed, what he answered).

I have from now the classes Question and Answer but I'm kinda stuck with the Question generate with Answer assigned to a topic and then creating a Quiz, because I missing also User in both parts, to know which user has created the question / quiz and which user has answered the question / quiz and store some data to do the historical.

My Question class has :

@Entity(name="question")
public class Question extends DateAudit {
@Id
@Column(name = "question_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_seq")
@SequenceGenerator(name = "question_seq", allocationSize = 1)
private Long id;

@Column(name = "text_question")
private String textQuestion; //The question itself "What's the name of ..."

@Column(name = "answerDescription")
@NotBlank(message = "Answer description")
private String answerDescription; //The answer to the question as an explanation

@Column(name = "isExamQuestion", nullable = false) 
private Boolean isExamQuestion; //A flag for the user to filter when he wants to do a question he only will see those that are not isExamQuestion, isExamQuestion are the questions that are going to appear when he wants to create a Quiz

@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Set<Answer> answers; //List of answers...

@Column(name = "answer_type", nullable = false) 
private String answerType; //I don't know if it goes here, but the answerType mentioned before NORMAL,MULTICHOICE,.... is to render on the user app

And the Answer

@Entity(name = "answer")
public class Answer extends DateAudit {

    @Id
    @Column(name = "answer_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answer_seq")
    @SequenceGenerator(name = "answer_seq", allocationSize = 1)
    private Long id;

    @Column(name = "answerToQuestion") 
    @NotBlank(message = "Answer to question name can not be blank") 
    private String answerToQuestion; //If the questions is what's 2*2 this could be 3

    @ManyToOne 
    private Question question; //In what question appear that answer

    //Here I'm wondering if I have to add a flag saying isCorrect to detect if it's the correct answer or not

As you can see I'm missing the Topic stuff, the historical, and the quiz, and i'm not referencing which user did the question, or quiz, or solved the quiz / question would you please guide me how to do this?

EDIT

Questions that I've received :

is the question only targeted to one user or a group of user ?

The same question or quiz can be answered for difference users, meaning, that Q1 can be done by 40 users.

are the quiz related to topic ?

When you create a quiz you select a topic to select the questions that has that topic. Example : Creating a Quiz of Learn to Sum I'll have to filter out by topic : Math and then a SubTopic Sum so I'll can choose the questions to put inside the Quiz.

how do you plan to create a topic or subtopic ?

Admin should have an endpoint to create topic or subtopic, from now there's only a Subtopic, there's not Subtopic of a subtopic, from now is Topic : Math Subtopic : Square root. So, before creating a question, or a quiz, admin should first create a Topic and if he wants to add a subtopic then create one, so then when he tries to create a Question he can say that that question is from X topic / subtopic and that question can be assigned to that.

What do you mean with "historical"?

Well, that's something for Admin side, Admin should have an endpoint that with an Id or name of the user returns all of the Quiz (first endpoint) or all of the Questions(second endpoint) that user has done, with the score, number of fails / number of correct ones, but I'm wondering that this should be front end side do the calculation, I mean, endpoint returns all of this info, Total of Questions/Quiz has done, Score, what question has failed, etc.. and then in frontend do more calculations.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-03-07 08:38

Sorry I do not put annotations for my answer. You figure them out. All are Entities. There is no need for Answer entity, or it depens about your question type. Do them own classes, and user forms to fill them. There is example for multichoice.

You can manage who can create Quiz from Controller. You can add Quiz to your User entity: List<Quiz> myQuizesAsAdmin; List<Quiz> myQuizesAsAnswerer;

You can see history from User entity Query; filter and sort them using List<Quiz> myQuizesAsAnswerer; You can also add Classes for like a school class and add Quizes what they have to do.

How to code: Admin create a Quiz myQuiz = new Quiz(name, QuizId, Questions, StartDate, EndDate, etc...)

Now you have an original Quiz, so you have do a copy or clone of it whit unique id. I added a constructor to Quiz.class to do it. Quiz iDoThisQuiz = new Quiz (this.QuizRepository.findByQuizIdAndByOriginal(thisQuisId, true)); Now answerer can do the quiz and answer questions. Save it to database after filling. Now in your database you have an "original quiz" and copies of it what are answered quizes.

    public class Quiz {
               private String name;
               private boolean original;
               private long id; // This is unique for every entity
               private String quizId; // this is for this quiz general id. Like a name. Unique for this quiz.
               private List<Question> questions;
               private User answerer;
               private User admin;
               private Date endDateToDoDate; // and another dates like when it has been done.
               private double totalPoints; // If you want to on

        }
public Quiz(Quiz originalQuiz) {
     this.id= this has to be unique, autogenerated or what you use to generate ids. 
     this.quizId = originalQuiz.quizId;
     this.name = originalQuiz.name;
     this.question = new Question(originalQuiz.question);
     this.original = false;
     ... 

--

@Entity(name = "question")
public class Question extends DateAudit {
    @Id
    @Column(name = "question_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_seq")
    @SequenceGenerator(name = "question_seq", allocationSize = 1)
    private Long id;
    private boolean isAnswered;

    /* TOPIC AND SUBTOPIC, create class Topic. */
    private Topic topic;
    private Topic subtopic; //Or you have a subtopic in Topic class

    @Column(name = "text_question")
    private String textQuestion; // The question itself "What's the name of ..."

    /* question types. All have to have their own class. When admin create quetion he will add one question type --  setMultiChoice()
     * Another ones are NULL so when you create answer form it selects that what is not NULL */
    private MultiChoise multiChoise;
    private NormalQuestion normalQuestion;
    /* etc question types */

    @Column(name = "answerDescription")
    @NotBlank(message = "Answer description")
    private String answerDescription; // The answer to the question as an explanation

    @Column(name = "isExamQuestion", nullable = false)
    private Boolean isExamQuestion; // A flag for the user to filter when he wants to do a question he only will see
                                    // those that are not isExamQuestion, isExamQuestion are the questions that are
                                    // going to appear when he wants to create a Quiz


}



@Entity
public class MultiChoice extends Points{
    private List<TextAndBoolean> choices;
    private boolean answered;

}



 public class Points {
    private double points;

}


public class TextAndBoolean {
    private String text;
    private boolean selected;

}
查看更多
登录 后发表回答