# Bulk Upload Fourth Subject Feature

## Overview
This feature allows administrators and teachers to upload student data via CSV file, where the last subject code in the subject_codes column is automatically treated as the fourth subject for that student.

## Files Created/Modified

### 1. `bulk_upload/students_with_fourth_subject_upload.php`
- Main upload page for students with fourth subject assignment
- Automatically treats the last subject code as the fourth subject
- Provides comprehensive error handling and validation
- Includes download template functionality

### 2. Modified `includes/sidebar_layout.php`
- Added link to the new upload functionality in the sidebar
- Available to both admin and teacher roles
- Properly integrated in the student management section

## Features

### CSV Upload Process
1. **Template Download**: Users can download a CSV template with all required fields
2. **Subject Assignment**: All subjects in the subject_codes column are assigned to the student
3. **Fourth Subject Marking**: The **last subject** in the subject_codes list is automatically marked as the fourth subject
4. **Validation**: Comprehensive validation to ensure data integrity
5. **Error Handling**: Detailed error reporting for failed imports

### CSV Format
The CSV file should contain the following columns:

| Column | Description | Required |
|--------|-------------|----------|
| first_name | Student's first name | Yes |
| last_name | Student's last name | No |
| father_name | Father's name | No |
| mother_name | Mother's name | No |
| email | Student's email | No |
| phone | Student's phone number | No |
| address | Student's address | No |
| date_of_birth | Date of birth (YYYY-MM-DD) | No |
| gender | male/female/other | No |
| class_name | Name of the class | Yes |
| session | Academic session (e.g., 2025) | Yes |
| group_name | Student group (e.g., Science) | No |
| roll_number | Student's roll number | Yes |
| admission_date | Admission date (YYYY-MM-DD) | No |
| guardian_name | Guardian's name | No |
| guardian_phone | Guardian's phone | No |
| guardian_email | Guardian's email | No |
| guardian_address | Guardian's address | No |
| subject_codes | Comma-separated subject codes (last one becomes fourth subject) | Yes |

### Example CSV Format
```
first_name,last_name,father_name,mother_name,email,phone,address,date_of_birth,gender,class_name,session,group_name,roll_number,admission_date,guardian_name,guardian_phone,guardian_email,guardian_address,subject_codes
John,Doe,John Sr.,Jane Doe,john@example.com,1234567890,123 Main St,2010-01-15,male,Ten,2025,Science,1,2025-01-10,John Sr.,1234567890,john.sr@example.com,123 Main St,101,102,107,108,109
```

In this example, subject code `109` will be treated as the fourth subject for this student.

## Implementation Details

### Subject Code Processing
- The system processes subject codes by splitting on commas
- Each code is validated against the subjects table
- Range notation (e.g., `101_102`) is supported and expands to individual codes
- The last code in the sequence is marked with `is_fourth_subject = 1`
- All other subjects are marked with `is_fourth_subject = 0`

### Database Integration
- Uses the existing `student_subjects` table
- Leverages the `is_fourth_subject` column to mark fourth subjects
- Maintains referential integrity with students and subjects tables
- Uses transactions to ensure data consistency

## Security
- Access restricted to admin and teacher roles
- Input validation to prevent injection attacks
- Proper error handling to avoid information disclosure
- File upload validation to accept only CSV files

## Usage
1. Navigate to the "ছাত্র-ছাত্রী ও চতুর্থ বিষয় আপলোড" link in the sidebar
2. Download the CSV template
3. Fill in student information and subject codes
4. Ensure the last subject code in the list should be the fourth subject
5. Upload the completed CSV file
6. Review the upload summary to confirm successful processing

## Notes
- The system automatically generates student IDs
- Duplicate roll numbers within the same class, session, and group are not allowed
- Subject codes must exist in the subjects table
- Class names must exist in the classes table
- The fourth subject designation is important for result calculations where failure in the fourth subject doesn't cause overall failure