<?php
include '../manage/connection.php';
include '../manage/function.php';

// $class_id=1;
// $session_id=1;

// $allStudents=array();
// $getStud=$connect->query("SELECT DISTINCT(student_id) FROM ca_2 WHERE session_id='$session_id' AND class_id='$class_id'
//  UNION SELECT DISTINCT(student_id) FROM ca_1 WHERE session_id='$session_id' AND class_id='$class_id'
//  UNION SELECT DISTINCT(student_id) FROM exam WHERE session_id='$session_id' AND class_id='$class_id'");
// while($stud=$getStud->fetch_assoc())
// {
//     array_push($allStudents, $stud);
// }
// echo $allStudents[0]['student_id'];


// session_start();
// if(isset($_POST['class_id']) && isset($_POST['class_id']))
// {
//     $teacher_id=$_SESSION['teacher']['id'];
//     $session_id=htmlentities($_POST['session_id']);
//     $class_id=htmlentities($_POST['class_id']);
//     include '../classes/classroom_class.php';
//     include '../classes/assessment_class.php';
//     include '../classes/students_class.php';
//     include '../classes/subjects_class.php';
//     include '../classes/class_subjects_class.php';
//     include '../classes/result_class.php';

//     $assessment=new Assessment("../manage/connection.php");
//     $classroom=new ClassRoom("../manage/connection.php");
//     $dstudent=new Student("../manage/connection.php");
//     $subject=new Subject("../manage/connection.php");
//     $csubject=new ClassSubject("../manage/connection.php");
//     $result=new Result("../manage/connection.php");

//     $time=time();
//     $added_count=0;
//     $update_count=0;

//     foreach($csubject->getAllClassSubjects($class_id) as $allSub)
//     {
//         // echo ' Subj '.$allSub['subject_id'].'<br>';
//         $subject_id=$allSub['subject_id'];
//         $subres=array();
//         // Get Student oc a class
//         foreach($dstudent->studBySession($session_id, $class_id) as $studentInfo)
//         {
//             $studInfo=$dstudent->getStudent($studentInfo['id']);
//             $stud=$studInfo['firstname'].' '.$studInfo['lastname'];
//             $student_id=$studInfo['id'];
    
//             $ca1=$connect->query("SELECT * FROM ca_1 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'")->fetch_assoc();
//             $ca2=$connect->query("SELECT * FROM ca_2 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'")->fetch_assoc();
//             $exam=$connect->query("SELECT * FROM exam WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'")->fetch_assoc();
//             $total_mark=($exam['marks']+$ca1['marks']+$ca2['marks']);

//             array_push($subres, array("student_id"=>$student_id, "student_name"=>$stud, "total_marks"=>$total_mark));

//         }
        
//         usort($subres, mySort('total_marks'));
//         echo '<br><br><div>Subject '.$subject->getSubject($subject_id)['name'].'</div>';
        
//         $position=0;
//         $previousMark=0;
//         foreach($subres as $sub_res)
//         {
//             // Add to subject result table 
            
//             if($previousMark!=$sub_res['total_marks'])
//             {
//                 $position++;
//             }
//             $previousMark=$sub_res['total_marks'];

//             $student_id=$sub_res['student_id'];
//             $total_mark=$sub_res['total_marks'];


//             // echo '<div>'.$sub_res['student_id'].' '.$sub_res['student_name'].' '.$sub_res['total_marks'].' '.$position.'</div>';
//             // Check if result exist on database, update, else add new
//             // Add to storage
//             $resultInfo=$connect->query("SELECT * FROM subject_result WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
//             if($resultInfo->num_rows!=0)
//             {
//                 // Update
//                 if($connect->query("UPDATE subject_result SET marks='$total_mark', position='$position' WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'"))
//                 {
//                     // Updated
//                     $update_count++;
//                 }
//             }
//             else
//             {
//                 // add new
//                 if($connect->query("INSERT INTO subject_result (student_id, session_id, class_id, subject_id, added_by, added_uid, marks, position, time) 
//                 VALUES('$student_id', '$session_id', '$class_id', '$subject_id', 'teacher', '$teacher_id', '$total_mark', '$position', '$time')"))
//                 {
//                     // Added
//                     $added_count++;
//                 }
//             }
//         }
//     }

//     if($added_count!=0 || $update_count!=0)
//     {
//         echo 'Result Generated';
//     }
//     else
//     {
//         echo 'Could Not Generate Result';
//     }
// }
// // SORTING FUNCTION
// function mySort($key)
// {
//     return function($a,$b) use ($key)
//     {
//         return strnatcmp($b[$key], $a[$key]);
//     };
// }

// Getting days based on session
// echo daysInSchool('2021-03-01','2021-04-15').' days in school';

// Testing getting next sessionbased on term end and new term begins
$end='2021-05-12';
$next_term="";
$getSessions=$connect->query("SELECT * FROM session ORDER BY begin ASC");
while($sess=$getSessions->fetch_assoc())
{ 
    if(strtotime($sess['begin'])>strtotime($end))
    {
        // echo $sess['begin'].'<br>';
        $next_term=$sess['begin'];
        break;
    }
}
echo 'Next Term: '.$next_term;
?>
 