<?php
session_start();
if(isset($_SESSION['admin']))
{
    $admin_id=$_SESSION['admin']['id'];
    include '../classes/classroom_class.php';
    include '../classes/subjects_class.php';
    $classroom=new ClassRoom("../manage/connection.php");
    $subject=new Subject("../manage/connection.php");
    include "../manage/connection.php";

    if(isset($_POST['class_id']))
    {
        $class_id=htmlentities($_POST['class_id']);
        $time=time();
        $added_count=0;
        $existed_subject=0;
        foreach($subject->getAllSubjects() as $subj)
        {
            // echo $subj['name'].'<br>';
            if(isset($_POST[$subj['id']]))
            {
                $fetched_id=$_POST[$subj['id']];
                $get=$connect->query("SELECT * FROM class_subject WHERE class_id='$class_id' AND subject_id='$fetched_id'");
                if($get->num_rows!=0)
                {
                    $existed_subject++;
                }
                else
                {
                    $add_subject=$connect->query("INSERT INTO class_subject (class_id, subject_id, added_by, time) 
                    VALUES('$class_id', '$fetched_id', '$admin_id', '$time')");
                    if($add_subject)
                    {
                        $added_count++;
                    }
                }
            }
        }

        if($existed_subject>0)
        {
            echo "$added_count Subject(s) added, $existed_subject subject(s) already Exist";
        }
        else
        {
            echo "$added_count Subjects added";
        }
    } 
}
?> 