feat: implementation des dossiers candidats PDF, gestion des entretiens et optimisation de l'analyse IA
This commit is contained in:
131
app/Http/Controllers/Admin/CandidateExportController.php
Normal file
131
app/Http/Controllers/Admin/CandidateExportController.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Candidate;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CandidateExportController extends Controller
|
||||
{
|
||||
public function exportDossier(Candidate $candidate)
|
||||
{
|
||||
$candidate->load([
|
||||
'user',
|
||||
'jobPosition',
|
||||
'tenant',
|
||||
'attempts.quiz.questions',
|
||||
'attempts.answers.option',
|
||||
'attempts.answers.question',
|
||||
'documents'
|
||||
]);
|
||||
|
||||
$filename = 'Dossier_' . str_replace(' ', '_', $candidate->user->name) . '_' . date('Ymd') . '.pdf';
|
||||
|
||||
// 1. Generate Main Report with DomPDF
|
||||
$pdfReport = Pdf::loadView('pdfs.candidate-dossier', [
|
||||
'candidate' => $candidate
|
||||
]);
|
||||
$reportBinary = $pdfReport->output();
|
||||
|
||||
// 2. Setup FPDI for merging
|
||||
$mergedPdf = new \setasign\Fpdi\Fpdi();
|
||||
|
||||
// Add Main Report Pages
|
||||
$reportTmp = tempnam(sys_get_temp_dir(), 'pdf_report_');
|
||||
file_put_contents($reportTmp, $reportBinary);
|
||||
|
||||
try {
|
||||
$pageCount = $mergedPdf->setSourceFile($reportTmp);
|
||||
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
|
||||
$templateId = $mergedPdf->importPage($pageNo);
|
||||
$size = $mergedPdf->getTemplateSize($templateId);
|
||||
$mergedPdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
||||
$mergedPdf->useTemplate($templateId);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('FPDI Error on report: ' . $e->getMessage());
|
||||
}
|
||||
@unlink($reportTmp);
|
||||
|
||||
// 3. Append Candidate Documents (CV, Letter)
|
||||
foreach ($candidate->documents as $doc) {
|
||||
if (\Storage::disk('local')->exists($doc->file_path)) {
|
||||
$filePath = \Storage::disk('local')->path($doc->file_path);
|
||||
|
||||
try {
|
||||
$pageCount = $mergedPdf->setSourceFile($filePath);
|
||||
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
|
||||
$templateId = $mergedPdf->importPage($pageNo);
|
||||
$size = $mergedPdf->getTemplateSize($templateId);
|
||||
$mergedPdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
||||
$mergedPdf->useTemplate($templateId);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Log::warning('Could not merge document ID ' . $doc->id . ': ' . $e->getMessage());
|
||||
|
||||
// Add a professional placeholder page for unmergable documents
|
||||
$mergedPdf->AddPage('P', 'A4');
|
||||
$mergedPdf->SetFont('Arial', 'B', 16);
|
||||
$mergedPdf->SetTextColor(0, 79, 130); // Primary color
|
||||
$mergedPdf->Ln(50);
|
||||
$mergedPdf->Cell(0, 20, utf8_decode("DOCUMENT JOINT : " . strtoupper($doc->type)), 0, 1, 'C');
|
||||
$mergedPdf->SetFont('Arial', 'I', 12);
|
||||
$mergedPdf->Cell(0, 10, utf8_decode($doc->original_name), 0, 1, 'C');
|
||||
|
||||
$mergedPdf->Ln(30);
|
||||
$mergedPdf->SetFont('Arial', '', 11);
|
||||
$mergedPdf->SetTextColor(100, 100, 100);
|
||||
$mergedPdf->MultiCell(0, 8, utf8_decode("Ce document n'a pas pu être fusionné automatiquement au dossier car son format est trop récent (PDF 1.5+).\n\nPour garantir l'intégrité de la mise en page, veuillez consulter ce document séparément via l'interface du tableau de bord candidat."), 0, 'C');
|
||||
|
||||
$mergedPdf->Ln(20);
|
||||
$mergedPdf->SetDrawColor(224, 176, 76); // Highlight color
|
||||
$mergedPdf->Line(60, $mergedPdf->GetY(), 150, $mergedPdf->GetY());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return response($mergedPdf->Output('S'), 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
|
||||
'Cache-Control' => 'no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'no-cache',
|
||||
'Expires' => '0',
|
||||
]);
|
||||
}
|
||||
|
||||
public function exportZip(Candidate $candidate)
|
||||
{
|
||||
$candidate->load(['user', 'jobPosition', 'tenant', 'attempts.quiz.questions', 'attempts.answers.option', 'attempts.answers.question', 'documents']);
|
||||
|
||||
$baseName = 'Dossier_' . str_replace(' ', '_', $candidate->user->name) . '_' . date('Ymd');
|
||||
$zipPath = tempnam(sys_get_temp_dir(), 'candidate_zip_');
|
||||
$zip = new \ZipArchive();
|
||||
|
||||
if ($zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
|
||||
return back()->with('error', 'Impossible de créer le fichier ZIP.');
|
||||
}
|
||||
|
||||
// 1. Add the main report (Rapport CABM)
|
||||
$pdfReport = Pdf::loadView('pdfs.candidate-dossier', [
|
||||
'candidate' => $candidate
|
||||
]);
|
||||
$zip->addFromString($baseName . '/Rapport_Synthese_CABM.pdf', $pdfReport->output());
|
||||
|
||||
// 2. Add original documents
|
||||
foreach ($candidate->documents as $doc) {
|
||||
if (\Storage::disk('local')->exists($doc->file_path)) {
|
||||
$content = \Storage::disk('local')->get($doc->file_path);
|
||||
// Sanitize original name or use type
|
||||
$ext = pathinfo($doc->original_name, PATHINFO_EXTENSION);
|
||||
$fileName = strtoupper($doc->type) . '_' . $doc->original_name;
|
||||
$zip->addFromString($baseName . '/Documents_Originaux/' . $fileName, $content);
|
||||
}
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
|
||||
return response()->download($zipPath, $baseName . '.zip')->deleteFileAfterSend(true);
|
||||
}
|
||||
}
|
||||
@@ -90,12 +90,23 @@ class AttemptController extends Controller
|
||||
|
||||
public function saveAnswer(Request $request, Attempt $attempt)
|
||||
{
|
||||
// Security: Verify the authenticated user owns this attempt
|
||||
$candidate = auth()->user()->candidate;
|
||||
if (!$candidate || $attempt->candidate_id !== $candidate->id) {
|
||||
abort(403, 'You are not authorized to submit answers for this attempt.');
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'question_id' => 'required|exists:questions,id',
|
||||
'option_id' => 'nullable|exists:options,id',
|
||||
'text_content' => 'nullable|string',
|
||||
]);
|
||||
|
||||
// Extra guard: prevent answering a finished attempt
|
||||
if ($attempt->finished_at) {
|
||||
return response()->json(['error' => 'This attempt is already finished.'], 403);
|
||||
}
|
||||
|
||||
Answer::updateOrCreate(
|
||||
[
|
||||
'attempt_id' => $attempt->id,
|
||||
@@ -112,6 +123,12 @@ class AttemptController extends Controller
|
||||
|
||||
public function finish(Attempt $attempt)
|
||||
{
|
||||
// Security: Verify the authenticated user owns this attempt
|
||||
$candidate = auth()->user()->candidate;
|
||||
if (!$candidate || $attempt->candidate_id !== $candidate->id) {
|
||||
abort(403, 'You are not authorized to finish this attempt.');
|
||||
}
|
||||
|
||||
if ($attempt->finished_at) {
|
||||
return redirect()->route('dashboard');
|
||||
}
|
||||
|
||||
@@ -11,6 +11,11 @@ class BackupController extends Controller
|
||||
{
|
||||
public function download()
|
||||
{
|
||||
// Security: Only super admins can download backups containing all tenant data
|
||||
if (!auth()->user()->isSuperAdmin()) {
|
||||
abort(403, 'Seuls les super administrateurs peuvent télécharger des sauvegardes.');
|
||||
}
|
||||
|
||||
$databaseName = env('DB_DATABASE');
|
||||
$userName = env('DB_USERNAME');
|
||||
$password = env('DB_PASSWORD');
|
||||
|
||||
@@ -139,8 +139,20 @@ class CandidateController extends Controller
|
||||
$request->validate([
|
||||
'cv' => 'nullable|file|mimes:pdf|max:5120',
|
||||
'cover_letter' => 'nullable|file|mimes:pdf|max:5120',
|
||||
'name' => 'nullable|string|max:255',
|
||||
'email' => 'nullable|string|email|max:255|unique:users,email,' . $candidate->user_id,
|
||||
'phone' => 'nullable|string|max:255',
|
||||
'linkedin_url' => 'nullable|url|max:255',
|
||||
]);
|
||||
|
||||
// Update User info if name or email present
|
||||
if ($request->has('name') || $request->has('email')) {
|
||||
$candidate->user->update($request->only(['name', 'email']));
|
||||
}
|
||||
|
||||
// Update Candidate info
|
||||
$candidate->update($request->only(['phone', 'linkedin_url']));
|
||||
|
||||
if ($request->hasFile('cv')) {
|
||||
$this->replaceDocument($candidate, $request->file('cv'), 'cv');
|
||||
}
|
||||
@@ -149,20 +161,24 @@ class CandidateController extends Controller
|
||||
$this->replaceDocument($candidate, $request->file('cover_letter'), 'cover_letter');
|
||||
}
|
||||
|
||||
return back()->with('success', 'Documents mis à jour avec succès.');
|
||||
return back()->with('success', 'Profil mis à jour avec succès.');
|
||||
}
|
||||
|
||||
public function updateNotes(Request $request, Candidate $candidate)
|
||||
{
|
||||
$request->validate([
|
||||
'notes' => 'nullable|string',
|
||||
'interview_details' => 'nullable|array',
|
||||
'interview_score' => 'nullable|numeric|min:0|max:30',
|
||||
]);
|
||||
|
||||
$candidate->update([
|
||||
'notes' => $request->notes,
|
||||
'interview_details' => $request->interview_details,
|
||||
'interview_score' => $request->has('interview_score') ? $request->interview_score : $candidate->interview_score,
|
||||
]);
|
||||
|
||||
return back()->with('success', 'Notes mises à jour avec succès.');
|
||||
return back()->with('success', 'Entretien mis à jour avec succès.');
|
||||
}
|
||||
|
||||
public function updateScores(Request $request, Candidate $candidate)
|
||||
|
||||
Reference in New Issue
Block a user