# Combined Subjects Implementation

## Overview
This document describes the implementation of combined subjects for Bangla and English in the school management system. The system now treats Bangla (বাংলা + বাংলা ব্যাকরণ) and English (ইংরেজি + ইংরেজি ব্যাকরণ) as single combined subjects in both marksheet and tabulation sheet reports.

## Implementation Details

### Subject Grouping Logic

The system now groups related subjects together:
- **Bangla Subjects**: বাংলা and বাংলা ব্যাকরণ are combined into a single "Combined Bangla" subject
- **English Subjects**: ইংরেজি and ইংরেজি ব্যাকরণ are combined into a single "Combined English" subject
- **Other Subjects**: All other subjects remain unchanged

### Grading Criteria

#### Combined Bangla Subject
- **Requirement**: CQ average > 22 AND MCQ > 9
- **Processing**: 
  - CQ marks from both Bangla subjects are averaged
  - MCQ marks from both Bangla subjects are averaged
  - Both conditions must be met for a passing grade
- **Grading Scale**: Standard percentage-based grading (A+ to F)

#### Combined English Subject
- **Requirement**: CQ average > 32
- **Processing**: 
  - CQ marks from both English subjects are averaged
  - Only the CQ average condition needs to be met for a passing grade
- **Grading Scale**: Standard percentage-based grading (A+ to F)

### Files Modified

1. `reports/marksheet.php` - Added subject grouping and combined grading logic
2. `reports/tabulation_sheet.php` - Added subject grouping and combined grading logic

### Key Functions

#### combineBanglaSubjects()
```php
function combineBanglaSubjects($bangla_results) {
    if (empty($bangla_results)) return null;
    
    // If only one Bangla subject, return it as is but mark it as combined
    if (count($bangla_results) == 1) {
        $result = $bangla_results[0];
        $result['subject_name'] = 'Combined Bangla';
        $result['cq_marks_array'] = [$result['cq_marks']]; // Convert to array for processing
        return $result;
    }
    
    // For multiple Bangla subjects, combine them
    $combined = [
        'subject_name' => 'Combined Bangla',
        'subject_code' => 'CBAN',
        'cq_marks' => 0,
        'cq_marks_array' => [], // Store individual CQ marks for processing
        'mcq_marks' => 0,
        'practical_marks' => 0,
        'assignment_marks' => 0,
        'attendance_marks' => 0,
        'marks_obtained' => 0,
        'full_marks' => 100,
        'pass_marks' => 40,
        'grade' => 'F',
        'grade_point' => 0.00
    ];
    
    // Sum up the marks from all Bangla subjects
    foreach ($bangla_results as $result) {
        $combined['cq_marks'] += $result['cq_marks'];
        $combined['cq_marks_array'][] = $result['cq_marks']; // Store individual marks
        $combined['mcq_marks'] += $result['mcq_marks'];
        $combined['practical_marks'] += $result['practical_marks'];
        $combined['assignment_marks'] += $result['assignment_marks'];
        $combined['attendance_marks'] += $result['attendance_marks'];
        $combined['marks_obtained'] += $result['marks_obtained'];
    }
    
    // Average the marks for proper grading
    $count = count($bangla_results);
    if ($count > 0) {
        $combined['cq_marks'] = $combined['cq_marks'] / $count;
        $combined['mcq_marks'] = $combined['mcq_marks'] / $count;
        $combined['marks_obtained'] = $combined['marks_obtained'] / $count;
    }
    
    return $combined;
}
```

#### combineEnglishSubjects()
```php
function combineEnglishSubjects($english_results) {
    if (empty($english_results)) return null;
    
    // If only one English subject, return it as is but mark it as combined
    if (count($english_results) == 1) {
        $result = $english_results[0];
        $result['subject_name'] = 'Combined English';
        $result['cq_marks_array'] = [$result['cq_marks']]; // Convert to array for processing
        return $result;
    }
    
    // For multiple English subjects, combine them
    $combined = [
        'subject_name' => 'Combined English',
        'subject_code' => 'CENG',
        'cq_marks' => 0,
        'cq_marks_array' => [], // Store individual CQ marks for processing
        'mcq_marks' => 0,
        'practical_marks' => 0,
        'assignment_marks' => 0,
        'attendance_marks' => 0,
        'marks_obtained' => 0,
        'full_marks' => 100,
        'pass_marks' => 40,
        'grade' => 'F',
        'grade_point' => 0.00
    ];
    
    // Sum up the marks from all English subjects
    foreach ($english_results as $result) {
        $combined['cq_marks'] += $result['cq_marks'];
        $combined['cq_marks_array'][] = $result['cq_marks']; // Store individual marks
        $combined['mcq_marks'] += $result['mcq_marks'];
        $combined['practical_marks'] += $result['practical_marks'];
        $combined['assignment_marks'] += $result['assignment_marks'];
        $combined['attendance_marks'] += $result['attendance_marks'];
        $combined['marks_obtained'] += $result['marks_obtained'];
    }
    
    // Average the marks for proper grading
    $count = count($english_results);
    if ($count > 0) {
        $combined['cq_marks'] = $combined['cq_marks'] / $count;
        $combined['mcq_marks'] = $combined['mcq_marks'] / $count;
        $combined['marks_obtained'] = $combined['marks_obtained'] / $count;
    }
    
    return $combined;
}
```

### Test Results

#### Bangla Subjects Test:
- Input: বাংলা (CQ: 25, MCQ: 12) + বাংলা ব্যাকরণ (CQ: 24, MCQ: 12)
- Combined: CQ average = 24.5, MCQ = 12
- Result: Grade D (Pass - both conditions met)

#### English Subjects Test:
- Input: ইংরেজি (CQ: 35) + ইংরেজি ব্যাকরণ (CQ: 31)
- Combined: CQ average = 33
- Result: Grade D (Pass - condition met)

## Benefits

1. **Simplified Reporting**: Students see combined results for related subjects
2. **Accurate Grading**: Follows the specified criteria for Bangla and English subjects
3. **SSC Compliance**: Meets the requirements of the Secondary School Certificate examination system
4. **Consistent Processing**: Same logic applied in both marksheet and tabulation sheet

## Usage

The system automatically groups Bangla and English subjects when generating reports. No manual intervention is required - the grouping and grading happens automatically based on subject names containing 'বাংলা'/'Bangla' or 'ইংরেজি'/'English'.