Muhammad Fari Madyan
Hire me
Laravel · part 9 of 11

Laravel E-Learning Part 9: Resolving Community Issues, Exam Flow Hardening & CI/CD Pipeline

Part 9 of the Laravel E-Learning series: triaging a critical community bug report (Issue #16), fixing essay scoring calculations, bulletproofing attempt lifecycle guards, updating dynamic UI states, and establishing a GitHub Actions CI & Security workflow in PR #17.

Muhammad Fari Madyan

· 10 min read

Baca dalam Bahasa Indonesia →

The Beauty of Open Source: When Community Finds What You Missed

One of the most rewarding parts of open-sourcing our Laravel E-Learning & CBT Platform after the initial production release was seeing real developers and educators clone the repository, test the application, and push it beyond our assumptions.

Shortly after publishing the codebase, an open-source contributor and security researcher, @ze0expl01t, submitted GitHub Issue #16: "Bug Kritis di ExamStart.php".

The issue reported three critical flaws in the Computer-Based Testing (CBT) engine:

  1. Flawed Essay Scoring: Premature score calculation in ExamStart.php when exams contained essay questions.
  2. Missing Exam Start Guards: No validation of the student's classroom, the exam schedule, or retry rules.
  3. De-synchronized Student UI: Static buttons on the student dashboard and exam list that ignored active or completed attempt states.

In this 9th part of the series, we'll walk through the technical post-mortem: what each bug was, why it escaped our initial tests, how Pull Request #17 fixed it, and the automated GitHub Actions CI & Security workflow we added to keep regressions out of production.


Problem 1: The Premature Essay Scoring & False Failure Bug

The Flawed Logic

In Part 5: Building the Exam System, we designed the exam engine to support both multiple-choice and essay questions. Multiple-choice questions can be graded automatically on submission, while essay questions need a teacher to review and score them in the grading portal.

Here is how the original finishExam() method in app/Livewire/Student/ExamStart.php computed the final score:

PHP
// ❌ ORIGINAL VULNERABLE LOGIC
public function finishExam()
{
    $questions = $this->examination->questions;
    $totalPoints = 0;
    $earnedPoints = 0;
    $hasEssay = false;

    foreach ($questions as $q) {
        $totalPoints += $q->points;
        $answer = $this->answers[$q->id] ?? null;

        if ($q->question_type === 'essay') {
            $hasEssay = true;
            // Essay: don't auto-grade, leave points_earned = 0
        } elseif ($answer && $q->question_type === 'multiple_choice') {
            $isCorrect = strtoupper(trim($answer)) === strtoupper(trim($q->correct_answer ?? ''));
            ExamAnswer::where('exam_attempt_id', $this->attempt->id)
                ->where('question_id', $q->id)
                ->update(['is_correct' => $isCorrect, 'points_earned' => $isCorrect ? $q->points : 0]);
            if ($isCorrect) {
                $earnedPoints += $q->points;
            }
        }
    }

    // 💥 BUG: $totalPoints includes essay points, but $earnedPoints does not!
    $score = $totalPoints > 0 ? round(($earnedPoints / $totalPoints) * 100) : 0;
    $status = $hasEssay ? 'needs_grading' : 'completed';

    $this->attempt->update([
        'finished_at' => now(),
        'score' => $score,
        'is_passed' => $score >= $this->examination->passing_score,
        'status' => $status,
    ]);
}

Why This Was Catastrophic

Consider an exam with:

  • 5 multiple-choice questions (10 points each = 50 points)
  • 2 essay questions (25 points each = 50 points)
  • Total Possible Points: 100 points
  • Passing Threshold: 75 points

A diligent student answered all 5 multiple-choice questions correctly (earning 50 points) and wrote thorough essay answers. When they clicked "Selesaikan Ujian":

  1. $earnedPoints was 50.
  2. $totalPoints was 100.
  3. $score was calculated as round((50 / 100) * 100) = 50.
  4. $is_passed was evaluated as 50 >= 75 ➔ false (Failed!).
  5. Even worse, the database enum for status in the exam_attempts table only permitted ['in_progress', 'completed', 'force_finished'] in the original migration, so assigning 'needs_grading' triggered a database exception under strict SQL mode.

The Solution in PR #17

  1. Database Migration Update: We updated the exam_attempts migration enum to support needs_grading:

    PHP
    // database/migrations/2026_02_22_000011_create_exam_attempts_table.php
    $table->enum('status', ['in_progress', 'completed', 'needs_grading', 'force_finished'])
          ->default('in_progress');
    
  2. Decoupled Grading Lifecycle: When an exam contains essay questions, we defer the score calculation entirely:

    PHP
    // ✅ REFACTORED SECURE LOGIC in ExamStart.php
    if ($hasEssay) {
        $status = 'needs_grading';
        $score = null; // No premature numeric score!
        $isPassed = false;
        $message = "Ujian selesai! Soal essay akan dinilai oleh guru.";
    } else {
        $status = 'completed';
        $score = $totalPoints > 0 ? round(($earnedPoints / $totalPoints) * 100) : 0;
        $isPassed = $score >= $this->examination->passing_score;
        $message = "Ujian selesai! Nilai Anda: {$score}";
    }
    
    $this->attempt->update([
        'finished_at' => now(),
        'score' => $score,
        'is_passed' => $isPassed,
        'status' => $status,
    ]);
    
  3. Guaranteed Answer Persistence with updateOrCreate: Instead of relying on fragile earlier queries, we now upsert every answer (essay text and multiple-choice selections alike) with timestamp tracking:

    PHP
    ExamAnswer::updateOrCreate(
        ['exam_attempt_id' => $this->attempt->id, 'question_id' => $q->id],
        [
            'answer_text' => $answer,
            'answered_at' => ($answer !== null && $answer !== '') ? now() : null,
            'points_earned' => 0,
            'is_correct' => null, // Pending teacher evaluation
        ]
    );
    

Problem 2: Missing Exam Access & Attempt Validation Guards

The Gap

Originally, ExamStart.php assumed that if a user reached the URL /student/exams/{examination}, they were allowed to start the exam. There were no defensive checks in mount() or startExam().

In theory, a student could:

  • Access exams designated for other classes (classroom_id).
  • Access drafts or closed exams (status !== 'published').
  • Start an exam before start_at or after end_at.
  • Retake an exam indefinitely even when allow_retry === false.
  • Refresh the page and spawn duplicate attempt records in the database.

The Defensive Guard Clauses

In PR #17, we added comprehensive authorization and lifecycle guards to startExam(), plus an elapsed-time check in mount() (covered below):

PHP
public function startExam(): void
{
    $student = auth()->user()?->student;

    // 1. Guard Classroom Authorization
    if (!$student || $this->examination->classroom_id !== $student->classroom_id) {
        session()->flash('error', 'Anda tidak memiliki akses ke ujian ini.');
        $this->redirect(route('student.exams'), navigate: true);
        return;
    }

    // 2. Guard Publication Status
    if ($this->examination->status !== 'published') {
        session()->flash('error', 'Ujian belum dipublikasikan atau sudah ditutup.');
        $this->redirect(route('student.exams'), navigate: true);
        return;
    }

    // 3. Guard Schedule Window
    if (now()->lt($this->examination->start_at) || now()->gt($this->examination->end_at)) {
        session()->flash('error', 'Waktu pelaksanaan ujian tidak valid atau telah berakhir.');
        $this->redirect(route('student.exams'), navigate: true);
        return;
    }

    // 4. Resume Existing In-Progress Attempt (Prevents Duplication)
    $existing = ExamAttempt::where('examination_id', $this->examination->id)
        ->where('student_id', $student->id)
        ->where('status', 'in_progress')
        ->first();

    if ($existing) {
        $this->attempt = $existing;
        $this->examStarted = true;
        $this->loadAnswers();
        return;
    }

    // 5. Guard Retry Allowance
    $finishedAttemptsCount = ExamAttempt::where('examination_id', $this->examination->id)
        ->where('student_id', $student->id)
        ->whereIn('status', ['completed', 'needs_grading', 'force_finished'])
        ->count();

    if ($finishedAttemptsCount > 0 && !$this->examination->allow_retry) {
        session()->flash('error', 'Anda sudah menyelesaikan ujian ini dan tidak dapat mengulang.');
        $this->redirect(route('student.exams'), navigate: true);
        return;
    }

    // 6. Safe Attempt Creation
    $attemptNumber = ExamAttempt::where('examination_id', $this->examination->id)
        ->where('student_id', $student->id)
        ->count() + 1;

    $this->attempt = ExamAttempt::create([
        'examination_id' => $this->examination->id,
        'student_id' => $student->id,
        'started_at' => now(),
        'attempt_number' => $attemptNumber,
        'status' => 'in_progress',
    ]);

    $this->examStarted = true;
    $this->loadAnswers();
}

Handling Elapsed Time on Resume

What if a student leaves the tab, turns off their laptop, and returns after their 60-minute duration has expired?

In mount(), we now check the current time against whichever deadline comes first: the duration_minutes limit or the exam's overall end_at:

PHP
$durationDeadline = $this->attempt->started_at->copy()->addMinutes($this->examination->duration_minutes);
$deadline = $durationDeadline->lt($this->examination->end_at) ? $durationDeadline : $this->examination->end_at;

if (now()->gte($deadline)) {
    $this->finishExam();
    return;
}

If the deadline has passed, we auto-complete the attempt immediately and redirect the student, with their final submission preserved.


Problem 3: Attempt-Aware Dynamic UI States

On the student dashboard and exam list, cards used to show a static "Mulai Ujian" button as long as the exam was published and not yet expired. This confused students: "Did my exam submit? Can I continue? Why does it say Start Exam again?"

Solution in Dashboard.php & Exams.php

We updated the query's eager loading to fetch the student's latest attempt with each examination:

PHP
$upcomingExams = Examination::with([
    'subject',
    'attempts' => fn ($q) => $q->where('student_id', $studentId)->latest()
])
->where('classroom_id', $classroomId)
->where('status', 'published')
->where('end_at', '>', now())
->orderBy('start_at')
->get();

In the Blade views (resources/views/livewire/student/dashboard.blade.php), we replaced the static buttons with state-aware badges:

Attempt StateUI Action / BadgeButton Style
status === 'in_progress'Lanjutkan Ujian →Amber solid button
Finished & allow_retry == trueUlangi Ujian →Blue outline button
Finished & status === 'needs_grading'⏳ Menunggu PenilaianPurple pill badge
Finished & status === 'completed'✓ Selesai (Score shown)Green pill badge
Not started & Within scheduleMulai Ujian →Primary blue solid button
Not started & start_at > now()Mulai [time]Gray subtle badge
BLADE
@php
    $latestAttempt = $exam->attempts->first();
@endphp

@if ($latestAttempt && $latestAttempt->status === 'in_progress')
    <a href="{{ route('student.exams.start', $exam) }}" class="btn-amber">
        Lanjutkan Ujian &rarr;
    </a>
@elseif ($latestAttempt && $exam->allow_retry)
    <a href="{{ route('student.exams.start', $exam) }}" class="btn-outline-blue">
        Ulangi Ujian &rarr;
    </a>
@elseif ($latestAttempt && $latestAttempt->status === 'needs_grading')
    <span class="badge-purple">
        ⏳ Menunggu Penilaian
    </span>
@elseif ($latestAttempt)
    <span class="badge-green">
        &check; Selesai ({{ $latestAttempt->score ?? '-' }})
    </span>
@elseif ($exam->start_at <= now())
    <a href="{{ route('student.exams.start', $exam) }}" class="btn-primary">
        Mulai Ujian &rarr;
    </a>
@else
    <span class="badge-gray">
        Mulai {{ $exam->start_at->diffForHumans() }}
    </span>
@endif

Problem 4: Hardening with GitHub Actions CI & Security Pipeline

A key takeaway from Issue #16 was that manually testing dozens of UI states and role permutations is error-prone. To keep code quality high going forward, PR #17 added an end-to-end GitHub Actions workflow in .github/workflows/ci.yml.

The pipeline runs on every push to main, on every pull request, and automatically every Monday at 02:00 UTC.

Highlights of the Pipeline

  1. Deterministic Dependency Caching: We resolve the Composer cache path dynamically with composer config cache-files-dir, which cuts CI run times from ~3 minutes to under 40 seconds.

  2. Automated Vulnerability Audits: Both backend (composer audit --locked) and frontend (npm audit --audit-level=high) dependencies are scanned against known CVE databases.

  3. Committed Secret & Environment File Protection: A dedicated shell script scans Git-tracked files to make sure sensitive environment files and private keys are never committed:

    BASH
    SENSITIVE_FILES=$(git ls-files | grep -E "(^|/)(\.env(\.(local|production|staging|testing))?|.*\.pem|.*\.key|id_rsa|id_ed25519)$" | grep -v "\.example" || true)
    
    if [ -n "$SENSITIVE_FILES" ]; then
        echo "❌ Security issue: Sensitive file(s) found in repository!"
        exit 1
    fi
    
  4. Automated Code Style Enforcement: Laravel Pint runs with the --test flag to enforce PSR-12 and Laravel coding standards on every contribution.


Open Source Maintainer Lessons

Handling Issue #16 reinforced several best practices for managing open-source software:

  1. Acknowledge and Validate Promptly: When someone files an issue with clear reproduction steps, acknowledge it early. Let them know you understand how serious the bug is, and outline how you plan to resolve it.
  2. Treat Bug Reports as Systemic Clues: Issue #16 initially focused on ExamStart.php, but tracing it revealed that the student dashboard, exams list, database schema, and teacher monitor all touched the same attempt state machine. Fixing only ExamStart.php would have left broken UI states elsewhere.
  3. Automate Quality Gates (CI): Every bug you fix should inspire a test or a CI workflow step. If an issue was caused by an untested permutation, add test coverage so it can't quietly regress in a future PR.

Summary of Changes

ComponentBefore PR #17After PR #17
Essay GradingEssay points counted in the denominator; score calculated prematurelyStatus set to needs_grading; score left as null pending teacher review
Database SchemaStatus enum: ['in_progress', 'completed', 'force_finished']Added 'needs_grading' to status enum
Exam Access GuardNo classroom, schedule, or retry checksStrict validation in startExam(); auto-finish on expired resume in mount()
Student UIStatic "Mulai Ujian" buttonDynamic badges: Lanjutkan, Ulangi, ⏳ Menunggu Penilaian, ✓ Selesai
Teacher MonitorCrashed with Undefined array key "perlu_dinilai" on sortAdded sorting key with fallback ?? 99
CI / CD PipelineNoneFull GitHub Actions pipeline: Tests, Vite build, Pint, Composer/NPM audit, and secret scanner

Special thanks to @ze0expl01t for filing Issue #16 and helping make the platform safer and more resilient for schools and students!

Continue to Part 10: IDOR Security Hardening →


🔗 Resources:

ShareLinkedInXWhatsApp

Written by Muhammad Fari Madyan

Senior Backend Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

Work with me
← part 8Production Deploymentpart 10 →Eliminating IDOR Vulnerabilities & Security Hardening (PR #18)

Muhammad Fari Madyan

Senior Backend Engineer building real-time, high-concurrency systems that hold up in production. Co-founder of LampungDev. Based in Bandar Lampung, Indonesia (UTC+7), working remote.

Work

Case studiesCahTixExam platform, 4 stacksOpen source

© 2026 Muhammad Fari Madyan

Designed and built by Muhammad Fari Madyan