# SSC-like Grading System Implementation

## Overview
This document describes the implementation of an SSC (Secondary School Certificate) like grading system for Bangla and English subjects in the school management system.

## Requirements Implemented

### Bangla Subjects (বাংলা ও বাংলা ব্যাকরণ)
- Both Creative Question (CQ) average of 2 marks AND MCQ marks must be >= 33 individually
- If either CQ average < 33 OR MCQ < 33, the subject grade is 'F'
- When both are >= 33, the overall grade is calculated based on the sum of CQ average and MCQ marks

### English Subjects (ইংরেজি ও ইংরেজি ব্যাকরণ)
- The average of 2 CQ marks must be >= 33
- If CQ average < 33, the subject grade is 'F'
- When CQ average >= 33, the overall grade is calculated based on the CQ average

## Implementation Details

### Files Modified
1. `reports/marksheet.php` - Added custom grading functions
2. `reports/tabulation_sheet.php` - Added custom grading functions

### Functions Added

#### calculateBanglaGrade()
```php
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];
    }
    
    // Calculate overall marks (average of CQ + MCQ)
    $overall_marks = $cq_average + $mcq_marks;
    
    // Check if both CQ average and MCQ pass individually
    // For SSC-like system, both CQ average and MCQ must be >= 33
    $cq_pass_marks = 33;
    $mcq_pass_marks = 33;
    
    // For SSC-like system, both CQ average and MCQ must pass
    if ($cq_average < $cq_pass_marks || $mcq_marks < $mcq_pass_marks) {
        return 'F';
    }
    
    // Also check overall marks
    if ($overall_marks < $pass_marks) {
        return 'F';
    }
    
    // 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';
}
```

#### calculateEnglishGrade()
```php
function calculateEnglishGrade($cq_marks_array, $total_marks, $pass_marks) {
    // Calculate average of CQ marks (requirement is average of 2 CQ >= 33)
    $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 subjects, both need average of 2 CQ marks >= 33
    $cq_pass_marks = 33; // Fixed requirement: average >= 33
    
    if ($cq_average < $cq_pass_marks) {
        return 'F';
    }
    
    // Also check overall marks
    if ($cq_average < $pass_marks) {
        return 'F';
    }
    
    // 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';
}
```

## Testing

A test script was created to verify the implementation:
- `test_grading_functions.php` - Standalone test for the grading functions

## Usage

The grading functions are automatically applied in both marksheet and tabulation sheet reports when:
- Subject name contains 'বাংলা' or 'Bangla' for Bangla subjects
- Subject name contains 'ইংরেজি' or 'English' for English subjects

## Verification Examples

### Bangla Subject Tests:
1. CQ: [40, 35] (avg=37.5), MCQ: 25 → Grade: F (MCQ < 33)
2. CQ: [45, 40] (avg=42.5), MCQ: 40 → Grade: A+ (Both >= 33)

### English Subject Tests:
1. CQ: [35, 31] (avg=33) → Grade: D (Average = 33)
2. CQ: [30, 32] (avg=31) → Grade: F (Average < 33)