first
This commit is contained in:
@@ -17,7 +17,7 @@ class StatisticsController extends Controller
|
||||
{
|
||||
public function show(Program $program): View
|
||||
{
|
||||
$program->load(['attendances.participant', 'questionnaire.questionnaireSet.questions']);
|
||||
$program->load(['questionnaire.questionnaireSet.questions']);
|
||||
|
||||
// Attendance by session
|
||||
$bySession = $program->attendances()
|
||||
@@ -40,23 +40,29 @@ class StatisticsController extends Controller
|
||||
->pluck('total', 'status')
|
||||
->toArray();
|
||||
|
||||
// Response rate
|
||||
// Response rate + question stats
|
||||
$pq = $program->questionnaire;
|
||||
$responseRate = null;
|
||||
$questionStats = [];
|
||||
$totalResponses = 0;
|
||||
|
||||
if ($pq && $pq->is_confirmed) {
|
||||
$totalAttended = $program->attendances()->count();
|
||||
$totalAttended = array_sum($bySession); // reuse already-fetched data
|
||||
$totalResponses = QuestionnaireResponse::where('program_id', $program->id)->count();
|
||||
$responseRate = $totalAttended > 0 ? round($totalResponses / $totalAttended * 100) : 0;
|
||||
|
||||
// Rating question averages
|
||||
$questions = $pq->questionnaireSet->questions ?? collect();
|
||||
|
||||
// Load ALL answers in one query, group by question — avoids N+1
|
||||
$allAnswers = QuestionnaireAnswer::whereIn('questionnaire_question_id', $questions->pluck('id'))
|
||||
->get()
|
||||
->groupBy('questionnaire_question_id');
|
||||
|
||||
foreach ($questions as $q) {
|
||||
$answers = $allAnswers->get($q->id, collect());
|
||||
|
||||
if ($q->question_type === 'rating') {
|
||||
$answers = QuestionnaireAnswer::where('questionnaire_question_id', $q->id)
|
||||
->pluck('answer_value');
|
||||
$values = $answers->map(fn($v) => is_array($v) ? (int)($v[0] ?? 0) : (int)$v);
|
||||
$values = $answers->map(fn($a) => is_array($a->answer_value) ? (int) ($a->answer_value[0] ?? 0) : (int) $a->answer_value);
|
||||
$questionStats[] = [
|
||||
'id' => $q->id,
|
||||
'text' => $q->question_text,
|
||||
@@ -65,11 +71,9 @@ class StatisticsController extends Controller
|
||||
'count' => $values->count(),
|
||||
];
|
||||
} elseif (in_array($q->question_type, ['single_choice', 'multiple_choice'])) {
|
||||
$answers = QuestionnaireAnswer::where('questionnaire_question_id', $q->id)
|
||||
->pluck('answer_value');
|
||||
$counts = [];
|
||||
foreach ($answers as $val) {
|
||||
$items = is_array($val) ? $val : [$val];
|
||||
foreach ($answers as $row) {
|
||||
$items = is_array($row->answer_value) ? $row->answer_value : [$row->answer_value];
|
||||
foreach ($items as $item) {
|
||||
$counts[$item] = ($counts[$item] ?? 0) + 1;
|
||||
}
|
||||
@@ -86,14 +90,15 @@ class StatisticsController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// Reuse data already computed above — no extra queries
|
||||
$summary = [
|
||||
'total_attendances' => $program->attendances()->count(),
|
||||
'pre_registered' => $program->attendances()->where('attendance_source', 'pre_registered_staff')->count(),
|
||||
'walk_in' => $program->attendances()->where('attendance_source', 'walk_in_external')->count(),
|
||||
'total_certificates' => $program->certificates()->count(),
|
||||
'generated_certs' => $program->certificates()->whereIn('status', ['generated', 'emailed', 'downloaded'])->count(),
|
||||
'downloaded_certs' => $program->certificates()->where('status', 'downloaded')->count(),
|
||||
'total_responses' => QuestionnaireResponse::where('program_id', $program->id)->count(),
|
||||
'total_attendances' => array_sum($bySession),
|
||||
'pre_registered' => $bySource['pre_registered_staff'] ?? 0,
|
||||
'walk_in' => $bySource['walk_in_external'] ?? 0,
|
||||
'total_certificates' => array_sum($certStats),
|
||||
'generated_certs' => ($certStats['generated'] ?? 0) + ($certStats['emailed'] ?? 0) + ($certStats['downloaded'] ?? 0),
|
||||
'downloaded_certs' => $certStats['downloaded'] ?? 0,
|
||||
'total_responses' => $totalResponses,
|
||||
];
|
||||
|
||||
return view('admin.programs.statistics.show', compact(
|
||||
|
||||
Reference in New Issue
Block a user