Initial commit
This commit is contained in:
140
app/Http/Controllers/AttemptController.php
Normal file
140
app/Http/Controllers/AttemptController.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\Attempt;
|
||||
use App\Models\Answer;
|
||||
use App\Models\Quiz;
|
||||
use App\Models\Option;
|
||||
use App\Models\AdminLog;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class AttemptController extends Controller
|
||||
{
|
||||
public function destroy(Attempt $attempt)
|
||||
{
|
||||
$this->authorizeAdmin();
|
||||
|
||||
$candidateName = $attempt->candidate->user->name;
|
||||
$quizTitle = $attempt->quiz->title;
|
||||
|
||||
DB::transaction(function () use ($attempt, $candidateName, $quizTitle) {
|
||||
// Log the action
|
||||
AdminLog::create([
|
||||
'user_id' => auth()->id(),
|
||||
'action' => 'DELETE_ATTEMPT',
|
||||
'description' => "Suppression du test '{$quizTitle}' pour le candidat '{$candidateName}'."
|
||||
]);
|
||||
|
||||
// Delete attempt (cascades to answers)
|
||||
$attempt->delete();
|
||||
|
||||
// Re-evaluate candidate status if needed
|
||||
$candidate = $attempt->candidate;
|
||||
if ($candidate->attempts()->count() === 0) {
|
||||
$candidate->update(['status' => 'en_attente']);
|
||||
}
|
||||
});
|
||||
|
||||
return back()->with('success', 'Tentative de test supprimée et action journalisée.');
|
||||
}
|
||||
|
||||
private function authorizeAdmin()
|
||||
{
|
||||
if (!auth()->user()->isAdmin()) {
|
||||
abort(403);
|
||||
}
|
||||
}
|
||||
public function show(Quiz $quiz)
|
||||
{
|
||||
$candidate = auth()->user()->candidate;
|
||||
|
||||
if (!$candidate) {
|
||||
abort(403, 'Seuls les candidats peuvent passer des quiz.');
|
||||
}
|
||||
|
||||
$attempt = Attempt::where('candidate_id', $candidate->id)
|
||||
->where('quiz_id', $quiz->id)
|
||||
->first();
|
||||
|
||||
if ($attempt && $attempt->finished_at) {
|
||||
return Inertia::render('Candidate/Thanks');
|
||||
}
|
||||
|
||||
if (!$attempt) {
|
||||
$attempt = Attempt::create([
|
||||
'candidate_id' => $candidate->id,
|
||||
'quiz_id' => $quiz->id,
|
||||
'started_at' => now(),
|
||||
]);
|
||||
|
||||
$candidate->update(['status' => 'en_cours']);
|
||||
}
|
||||
|
||||
$quiz->load(['questions.options']);
|
||||
|
||||
return Inertia::render('Candidate/QuizInterface', [
|
||||
'quiz' => $quiz,
|
||||
'attempt' => $attempt->load('answers')
|
||||
]);
|
||||
}
|
||||
|
||||
public function saveAnswer(Request $request, Attempt $attempt)
|
||||
{
|
||||
$request->validate([
|
||||
'question_id' => 'required|exists:questions,id',
|
||||
'option_id' => 'nullable|exists:options,id',
|
||||
'text_content' => 'nullable|string',
|
||||
]);
|
||||
|
||||
Answer::updateOrCreate(
|
||||
[
|
||||
'attempt_id' => $attempt->id,
|
||||
'question_id' => $request->question_id,
|
||||
],
|
||||
[
|
||||
'option_id' => $request->option_id,
|
||||
'text_content' => $request->text_content,
|
||||
]
|
||||
);
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
public function finish(Attempt $attempt)
|
||||
{
|
||||
if ($attempt->finished_at) {
|
||||
return redirect()->route('dashboard');
|
||||
}
|
||||
|
||||
$attempt->load(['quiz.questions.options', 'answers']);
|
||||
|
||||
$score = 0;
|
||||
$maxScore = $attempt->quiz->questions->sum('points');
|
||||
|
||||
foreach ($attempt->quiz->questions as $question) {
|
||||
if ($question->type === 'qcm') {
|
||||
$userAnswer = $attempt->answers->where('question_id', $question->id)->first();
|
||||
if ($userAnswer && $userAnswer->option_id) {
|
||||
$option = $question->options->where('id', $userAnswer->option_id)->first();
|
||||
if ($option && $option->is_correct) {
|
||||
$score += $question->points;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$attempt->update([
|
||||
'score' => $score,
|
||||
'max_score' => $maxScore,
|
||||
'finished_at' => now(),
|
||||
]);
|
||||
|
||||
$attempt->candidate->update(['status' => 'termine']);
|
||||
|
||||
return redirect()->route('dashboard');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user