Closed bernardhanna closed 6 months ago
The task requires two main functionalities: saving the confidence score to the database when a quiz attempt is submitted, and displaying the previous confidence score when the user attempts the same question again. The reasoning behind this implementation is to provide a seamless experience for the user to track their confidence progression over time.
In the submitAnswer
method, add the confidence score to the validation rules and save it to the database:
public function submitAnswer(Request $request)
{
$validated = $request->validate([
// ... other validation rules
'confidence_score' => 'required|numeric',
]);
// ... existing code to save the attempt
// Save the confidence score
$quizAttempt = new QuizAttempt([
// ... other attempt data
'confidence_score' => $validated['confidence_score'],
]);
$quizAttempt->save();
// ... existing code to return response
}
To retrieve and return the previous confidence score, modify the method that fetches the question data:
public function getQuestionData($questionId)
{
// ... existing code to get question data
// Retrieve the previous confidence score for the question
$previousConfidenceScore = QuizAttempt::where('question_id', $questionId)
->where('user_id', auth()->id())
->latest()
->value('confidence_score');
// ... existing code to return question data
return response()->json([
// ... other data
'previousConfidenceScore' => $previousConfidenceScore,
]);
}
const [confidenceScore, setConfidenceScore] = useState(0);
// ... inside the render method, within the form <input type="number" name="confidence_score" value={confidenceScore} onChange={(e) => setConfidenceScore(e.target.value)} // ... other attributes />
2. Update the `handleAnswerSubmit` function to include the confidence score in the submission:
```jsx
const handleAnswerSubmit = (event) => {
event.preventDefault();
// ... existing code
axios.post('/submit-answer', {
// ... other data
confidence_score: confidenceScore,
})
// ... existing code
};
Display the previous confidence score when fetching the question data:
useEffect(() => {
// ... existing code to fetch question data
// Set the previous confidence score from the response
setPreviousConfidenceScore(response.data.previousConfidenceScore);
}, [currentQuestionId]);
Render the previous confidence score on the UI:
{previousConfidenceScore && (
<div>
<p>Your Previous Confidence Score: {previousConfidenceScore}</p>
</div>
)}
With these changes, the system will save the confidence score to the database upon submission and display the previous confidence score when the user attempts the question again.
Click here to create a Pull Request with the proposed solution
Files used for this task:
What - description of what you me to do Hey @autopilot save confidence score to database table on submit and show previous confidence score for the question on users next attempt
Why - explain why this is important I want to allow users to see their previous attempts confidence score to track progression