<?php
// Standalone test for new grading functions

// Custom function to calculate Bangla subject grade based on new criteria
// For Bangla: CQ average > 22 AND MCQ average > 9 to pass
function calculateBanglaGrade($cq_marks_array, $mcq_marks, $total_marks, $pass_marks) {
    // Calculate average of 2 CQ marks
    $cq_average = 0;
    if (count($cq_marks_array) >= 2) {
        $cq_average = ($cq_marks_array[0] + $cq_marks_array[1]) / 2;
    } else if (count($cq_marks_array) == 1) {
        $cq_average = $cq_marks_array[0];
    }
    
    // For Bangla: CQ average > 22 AND MCQ > 9 to pass
    if ($cq_average > 22 && $mcq_marks > 9) {
        // Calculate overall marks
        $overall_marks = $cq_average + $mcq_marks;
        
        // Calculate percentage based on total marks
        $percentage = ($overall_marks / $total_marks) * 100;
        
        if ($percentage >= 80) return 'A+';
        elseif ($percentage >= 70) return 'A';
        elseif ($percentage >= 60) return 'A-';
        elseif ($percentage >= 50) return 'B';
        elseif ($percentage >= 40) return 'C';
        elseif ($percentage >= 33) return 'D';
        else return 'F';
    } else {
        // If criteria not met, return F
        return 'F';
    }
}

// Custom function to calculate English subject grade based on new criteria
// For English: CQ average > 32 to pass
function calculateEnglishGrade($cq_marks_array, $total_marks, $pass_marks) {
    // Calculate average of 2 CQ marks
    $cq_average = 0;
    if (count($cq_marks_array) >= 2) {
        $cq_average = ($cq_marks_array[0] + $cq_marks_array[1]) / 2;
    } else if (count($cq_marks_array) == 1) {
        $cq_average = $cq_marks_array[0];
    }
    
    // For English: CQ average > 32 to pass
    if ($cq_average > 32) {
        // Calculate percentage based on total marks
        $percentage = ($cq_average / $total_marks) * 100;
        
        if ($percentage >= 80) return 'A+';
        elseif ($percentage >= 70) return 'A';
        elseif ($percentage >= 60) return 'A-';
        elseif ($percentage >= 50) return 'B';
        elseif ($percentage >= 40) return 'C';
        elseif ($percentage >= 33) return 'D';
        else return 'F';
    } else {
        // If criteria not met, return F
        return 'F';
    }
}

echo "<h2>Testing New Grading Functions</h2>\n";

// Test Bangla grading function
echo "<h3>Bangla Grading Tests:</h3>\n";

// Test case 1: Pass - Both CQ average > 22 and MCQ > 9
$cq_marks = [25, 24]; // Average = 24.5
$mcq_marks = 12;
$total_marks = 100;
$pass_marks = 33;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 1 - CQ: [25, 24], MCQ: 12 => Grade: $result (Expected: Pass)<br>\n";

// Test case 2: Fail - CQ average <= 22
$cq_marks = [20, 24]; // Average = 22
$mcq_marks = 12;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 2 - CQ: [20, 24], MCQ: 12 => Grade: $result (Expected: F)<br>\n";

// Test case 3: Fail - MCQ <= 9
$cq_marks = [25, 24]; // Average = 24.5
$mcq_marks = 9;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 3 - CQ: [25, 24], MCQ: 9 => Grade: $result (Expected: F)<br>\n";

// Test case 4: Pass with good grade
$cq_marks = [40, 38]; // Average = 39
$mcq_marks = 25;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 4 - CQ: [40, 38], MCQ: 25 => Grade: $result (Expected: B or better)<br>\n";

echo "<br>\n";

// Test English grading function
echo "<h3>English Grading Tests:</h3>\n";

// Test case 1: Pass - CQ average > 32
$cq_marks = [35, 31]; // Average = 33
$total_marks = 100;
$pass_marks = 33;
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 1 - CQ: [35, 31] => Grade: $result (Expected: Pass)<br>\n";

// Test case 2: Fail - CQ average <= 32
$cq_marks = [30, 32]; // Average = 31
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 2 - CQ: [30, 32] => Grade: $result (Expected: F)<br>\n";

// Test case 3: Pass with good grade
$cq_marks = [40, 36]; // Average = 38
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 3 - CQ: [40, 36] => Grade: $result (Expected: C or better)<br>\n";

// Test case 4: Single CQ mark > 32
$cq_marks = [35]; // Average = 35
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 4 - CQ: [35] => Grade: $result (Expected: Pass)<br>\n";

// Test case 5: Single CQ mark <= 32
$cq_marks = [32]; // Average = 32
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 5 - CQ: [32] => Grade: $result (Expected: F)<br>\n";
?>