harishdurga / laravel-quiz

With this package, you can easily get quiz functionality into your Laravel project.
MIT License
162 stars 19 forks source link

QuizAttempt check if question is answered correctly #28

Closed alex1897 closed 2 years ago

alex1897 commented 2 years ago

Is it currently not possible to check after a question is answered if it's correct, right?

The leering effect is a lot better if the user knows on which question they made a mistake, instead of just telling them the points they made at the end.

I built myself a validation which seems to work (for single/multiple choice) but I guess there would a better way. Since the model already has the validation, could it provide a function to validate it?

Something like: $quizAttempt->validate($quiz_question->id)

harishdurga commented 2 years ago

Yeah, it's possible. Something like the following?

{
  "result": [
    {
      "question_id": 1,
      "is_correct": false,
      "score": -1,
      "given_answer": 20,
      "correct_answer": 10.5
    },
    {
      "question_id": 2,
      "is_correct": true,
      "score": 5,
      "given_answer": "123456",
      "correct_answer": "123456"
    }
  ]
}
alex1897 commented 2 years ago

Yes, even better with more information. I just thought about true/false. However, the given/correct will be difficult for the multiple choice.

harishdurga commented 2 years ago

correct_answer may also contain other types of data. The above JSON snippet is just for ref.

alex1897 commented 2 years ago

@harishdurga: Sorry for checking so late, I was busy on other projects.

I think this works in general, but noticed two things:

  1. If you allow multiple attempts, this does not work. In the user_answer is a list of all given answers of all attempts and the is_correct is also wrong
  2. Why is there an array around when calling quiz_attempt->validate($quiz_question_id). To get the value of is_correct I would need to call it like quiz_attempt->validate($quiz_question_id)[$quiz_question_id][is_correct] and the [$quiz_question_id] is redundant
Bildschirm­foto 2022-11-16 um 17 04 05 Bildschirm­foto 2022-11-16 um 16 54 23
harishdurga commented 1 year ago

Hey @alex1897 , If you have some spare time can you please raise a PR fixing the mentioned problem?

alex1897 commented 1 year ago

Hi @harishdurga, I already looked at the code and to be honest I can't solve the problem.

harishdurga commented 1 year ago

Understood. No problem, I will try to solve this in sometime.