# Setup Guide: Automatic 8 PM Absence Marking

This guide will help you set up automatic absence marking that runs every day at 8 PM.

## What This Does

The `auto_mark_absent_8pm.php` script will:
- Automatically mark all students who haven't been scanned today as ABSENT
- Only run at 8 PM (20:00) or later each day
- Send SMS notifications to parents (if SMS is enabled)
- Check for 3+ day consecutive absences and send email alerts
- Log all activities to `logs/auto_mark_absent.log`

## Setup Options

### Option 1: Web-based Cron Service (Recommended for XAMPP/Windows)

This is the easiest option if you're using XAMPP on Windows:

1. **Sign up for a free cron service:**
   - Go to [cron-job.org](https://cron-job.org) (free)
   - Create an account

2. **Create a new cron job:**
   - **Title:** Daily Auto Mark Absent at 8 PM
   - **Address:** `http://localhost/attendance_system/auto_mark_absent_8pm.php`
     - Replace `localhost` with your actual domain if hosted online
   - **Schedule:** Daily at 20:00 (8:00 PM)
   - **Notification:** Enable email notifications if job fails

3. **Save the cron job**

### Option 2: Windows Task Scheduler (For Local XAMPP)

If you're running XAMPP locally on Windows:

1. **Open Task Scheduler:**
   - Press `Win + R`, type `taskschd.msc`, press Enter

2. **Create Basic Task:**
   - Click "Create Basic Task" in the right panel
   - Name: "Auto Mark Absent 8 PM"
   - Description: "Automatically mark students absent at 8 PM daily"

3. **Set Trigger:**
   - Trigger: Daily
   - Start time: 8:00:00 PM
   - Recur every: 1 day

4. **Set Action:**
   - Action: Start a program
   - Program/script: `C:\xampp\php\php.exe`
   - Add arguments: `C:\xampp\htdocs\attendance_system\auto_mark_absent_8pm.php`
   - Start in: `C:\xampp\htdocs\attendance_system`

5. **Finish the wizard**

### Option 3: Manual Testing

You can test the script manually:

1. **Via Browser:**
   - Open: `http://localhost/attendance_system/auto_mark_absent_8pm.php?force=1`
   - The `?force=1` parameter allows it to run even if it's not 8 PM yet

2. **Via Command Line:**
   ```bash
   cd C:\xampp\htdocs\attendance_system
   php auto_mark_absent_8pm.php force
   ```

## Verify It's Working

1. **Check the log file:**
   - Location: `logs/auto_mark_absent.log`
   - This file will be created automatically on first run
   - Check it after 8 PM to see if the script ran

2. **Check the database:**
   - Look at the `attendance` table
   - Students should have records with `status = 'absent'` and `auto_marked = 1`

3. **Check the output:**
   - If accessed via browser, you'll see JSON output with results
   - If run via command line, you'll see text output

## Troubleshooting

### Script says "can only run at 8 PM"
- This is normal if you're testing before 8 PM
- Use `?force=1` parameter to test: `auto_mark_absent_8pm.php?force=1`

### Script not running automatically
- Check if cron job/task scheduler is set up correctly
- Verify the URL/path is correct
- Check the log file for errors
- Make sure PHP has permission to write to the `logs/` directory

### No students marked absent
- Check if students already have attendance records for today
- The script only marks students who have NO attendance record
- Check the log file for details

### SMS not sending
- Verify SMS is enabled in `config.php` (`SMS_ENABLED = true`)
- Check SMS API key is configured
- Check log file for SMS errors

## Timezone Settings

The script uses `Asia/Manila` timezone by default. To change it:

1. Open `auto_mark_absent_8pm.php`
2. Find: `date_default_timezone_set('Asia/Manila');`
3. Change to your timezone, e.g.:
   - `date_default_timezone_set('America/New_York');`
   - `date_default_timezone_set('Europe/London');`
   - See [PHP timezones](https://www.php.net/manual/en/timezones.php) for full list

## Log File Location

Logs are saved to: `logs/auto_mark_absent.log`

Each run creates entries like:
```
[2024-01-15 20:00:01] === Auto Mark Absent Script Started ===
[2024-01-15 20:00:01] Current time: 2024-01-15 20:00:01
[2024-01-15 20:00:01] Students marked absent: 5
[2024-01-15 20:00:02] === Auto Mark Absent Script Completed ===
```

## Need Help?

- Check the log file first: `logs/auto_mark_absent.log`
- Verify your cron job/task scheduler is running
- Test manually with `?force=1` parameter
- Check database connection in `config.php`




