<?php
session_start();
include '../manage/function.php';
include '../classes/assessment_class.php';
$assessment=new Assessment("../manage/connection.php");
include '../classes/subjects_class.php';
$subject=new Subject("../manage/connection.php");
$teacher_id=$_SESSION['teacher']['id'];
$time=time();

if(isset($_POST['student_id']) && isset($_POST['teacher_id']) && isset($_POST['class_id']) && isset($_POST['session_id']))
{
    $session_id=htmlentities($_POST['session_id']);
    $student_id=htmlentities($_POST['student_id']);
    $teacher_id=htmlentities($_POST['teacher_id']);
    $class_id=htmlentities($_POST['class_id']);
 
$access_count=0;
$update_count=0;
    // loop and get subjects
    foreach($subject->getAllSubjects() as $subj)
    {
        if(isset($_POST['subject_'.$subj['id']]))
        {
            $subject_id=$subj['id'];
            $marks=$_POST['subject_'.$subj['id']];
            
            // echo "<br> Marks is $marks of $subject_id and name ".$subj['name'];
            if(!$assessment->findCA2($student_id, $session_id, $class_id, $subject_id))
            {
                // Not Found Add 
                if($assessment->addCA2($student_id, $session_id, $class_id, $subject_id, $teacher_id, $marks, $time))
                {
                    $access_count++;
                }
            }
            else
            {
                // Update Marks
                if($assessment->updateCA2($student_id, $session_id, $class_id, $subject_id, $marks))
                {
                    $update_count++;
                }
            }
        }
    } 

    echo ($access_count+$update_count)!=0?" Accessement Stored":" Could not access student";
}
?>