================================================================================
  PRIMARY SCHOOL SUBJECTS - SQL FIXED & READY
================================================================================

ERROR: Unknown column 'description' in 'field list'

CAUSE: The subjects table only has (id, name, code) columns
       No 'description' column exists

FIX: Removed 'description' from all INSERT statements

================================================================================
WHAT WAS CHANGED:
================================================================================

BEFORE (BROKEN):
----------------
INSERT IGNORE INTO subjects (name, code, description) VALUES
('English Language (Grade 4)', 'G4-ENG-001', 'English reading, writing...'),
('Mathematics (Grade 4)', 'G4-MAT-003', 'Arithmetic, basic geometry...');


AFTER (FIXED):
--------------
INSERT IGNORE INTO subjects (name, code) VALUES
('English Language (Grade 4)', 'G4-ENG-001'),
('Mathematics (Grade 4)', 'G4-MAT-003');

================================================================================
ALL GRADES FIXED:
================================================================================

✓ Grade 4: 11 subjects (INSERT statement fixed)
✓ Grade 5: 12 subjects (INSERT statement fixed)
✓ Grade 6: 13 subjects (INSERT statement fixed)
✓ Grade 7: 12 subjects (INSERT statement fixed)
✓ Verification queries (removed description column)

Total: 48 subjects ready to insert

================================================================================
HOW TO RUN:
================================================================================

METHOD 1: phpMyAdmin (Recommended)
-----------------------------------

1. OPEN phpMyAdmin
   https://yoursite.com:2083/phpMyAdmin
   OR
   http://localhost/phpmyadmin (XAMPP)

2. SELECT YOUR DATABASE
   Click on: melane_multi_tenant_skoloi (or your database name)

3. CLICK "SQL" TAB
   At the top of the page

4. COPY SQL SCRIPT
   Open: C:\xampp\htdocs\Multi-Tanent\database\seeds\primary_school_subjects_g4_g7.sql
   Select all (Ctrl+A)
   Copy (Ctrl+C)

5. PASTE INTO SQL BOX
   Click in the SQL text area
   Paste (Ctrl+V)

6. CLICK "GO" BUTTON
   At the bottom right

7. VERIFY SUCCESS
   You should see messages like:
   - 11 rows inserted (Grade 4)
   - 12 rows inserted (Grade 5)  
   - 13 rows inserted (Grade 6)
   - 12 rows inserted (Grade 7)
   - Grade-subject links created


METHOD 2: Command Line
-----------------------

cd C:\xampp\htdocs\Multi-Tanent
mysql -u root -p melane_multi_tenant_skoloi < database/seeds/primary_school_subjects_g4_g7.sql

Enter password when prompted
Watch for success messages

================================================================================
EXPECTED RESULT:
================================================================================

SUBJECTS TABLE:
---------------
48 new subjects added:

Grade 4 Subjects (11):
  G4-ENG-001: English Language (Grade 4)
  G4-SIS-002: SiSwati Language (Grade 4)
  G4-MAT-003: Mathematics (Grade 4)
  G4-SCI-004: Science (Grade 4)
  G4-SOC-005: Social Studies (Grade 4)
  G4-REL-006: Religious Education (Grade 4)
  G4-HE-007: Home Economics (Grade 4)
  G4-AGR-008: Agriculture (Grade 4)
  G4-ART-009: Art (Grade 4)
  G4-MUS-010: Music (Grade 4)
  G4-PE-011: Physical Education (Grade 4)

Grade 5 Subjects (12):
  G5-ENG-001: English Language (Grade 5)
  G5-SIS-002: SiSwati Language (Grade 5)
  G5-MAT-003: Mathematics (Grade 5)
  G5-SCI-004: Science (Grade 5)
  G5-SOC-005: Social Studies (Grade 5)
  G5-REL-006: Religious Education (Grade 5)
  G5-HE-007: Home Economics (Grade 5)
  G5-BCS-008: Basic Consumer Science (Grade 5)
  G5-AGR-009: Agriculture (Grade 5)
  G5-CA-010: Creative Arts (Grade 5)
  G5-PE-011: Physical Education (Grade 5)
  G5-ICT-012: ICT (Grade 5)

Grade 6 Subjects (13):
  G6-ENG-001: English Language & Literature (Grade 6)
  G6-SIS-002: SiSwati Language & Literature (Grade 6)
  G6-MAT-003: Mathematics (Grade 6)
  G6-SCI-004: Science (Grade 6)
  G6-SOC-005: Social Studies (Grade 6)
  G6-GEO-006: Geography (Grade 6)
  G6-HIS-007: History (Grade 6)
  G6-CIV-008: Civics (Grade 6)
  G6-REL-009: Religious Education (Grade 6)
  G6-HE-010: Home Economics (Grade 6)
  G6-AGR-011: Agriculture (Grade 6)
  G6-CA-012: Creative & Performing Arts (Grade 6)
  G6-ICT-013: ICT / Digital Literacy (Grade 6)

Grade 7 Subjects (12):
  G7-ENG-001: English Language (Grade 7)
  G7-SIS-002: SiSwati Language (Grade 7)
  G7-MAT-003: Mathematics (Grade 7)
  G7-SCI-004: Integrated Science (Grade 7)
  G7-SOC-005: Social Studies (Grade 7)
  G7-REL-006: Religious Education (Grade 7)
  G7-HE-007: Home Economics (Grade 7)
  G7-AGR-008: Agriculture (Grade 7)
  G7-CA-009: Creative Arts (Grade 7)
  G7-MUS-010: Music (Grade 7)
  G7-DRA-011: Drama (Grade 7)
  G7-ICT-012: ICT / Computer Studies (Grade 7)

GRADE_SUBJECT TABLE:
--------------------
All subjects automatically linked to their respective grades

================================================================================
VERIFICATION:
================================================================================

After running, execute these queries to verify:

1. COUNT SUBJECTS PER GRADE:
   
   SELECT 
       g.name as grade_name,
       COUNT(gs.subject_id) as subject_count
   FROM grades g
   LEFT JOIN grade_subject gs ON g.id = gs.grade_id
   WHERE g.name IN ('Grade 4', 'Grade 5', 'Grade 6', 'Grade 7')
   GROUP BY g.id, g.name
   ORDER BY g.name;

   Expected:
   Grade 4: 11 subjects
   Grade 5: 12 subjects
   Grade 6: 13 subjects
   Grade 7: 12 subjects

2. VIEW GRADE 7 SUBJECTS:
   
   SELECT s.name, s.code
   FROM subjects s
   WHERE s.code LIKE 'G7-%'
   ORDER BY s.code;

   Expected: 12 rows

================================================================================
NEXT STEPS AFTER SQL:
================================================================================

1. MAP SUBJECTS TO MOODLE
   Go to: admin/moodle/configure_mapping.php
   Map each subject to its Moodle course

2. TEST MAPPINGS
   Go to: admin/moodle/test_mappings.php
   Verify all mappings show "Valid" status

3. TEST WITH STUDENT
   Assign subject to a student
   Check they're enrolled in Moodle

================================================================================
TROUBLESHOOTING:
================================================================================

ISSUE: "Duplicate entry for key 'name'"

SOLUTION: Subject name already exists
          This is OK - INSERT IGNORE will skip it
          The script continues with other subjects

ISSUE: Grade links not created

SOLUTION: Check grades exist with exact names:
          - 'Grade 4'
          - 'Grade 5'  
          - 'Grade 6'
          - 'Grade 7'
          
          If different, update the script @variables

ISSUE: Foreign key constraint fails

SOLUTION: Run just the INSERT statements first
          Then run the grade linking section separately

================================================================================
FILES READY:
================================================================================

1. database/seeds/primary_school_subjects_g4_g7.sql (FIXED)
2. admin/moodle/test_mappings.php (FIXED)
3. admin/moodle/configure_mapping.php (ready)

Windows Explorer showing the SQL file!

================================================================================
QUICK RUN:
================================================================================

phpMyAdmin:
1. SQL tab
2. Paste contents of: primary_school_subjects_g4_g7.sql
3. Click Go
4. Done!

Then go to: admin/moodle/test_mappings.php to verify

================================================================================

The script is now fixed and ready to run without errors! 🎉

================================================================================

