lundi 28 juin 2021

Live progress status reporting using AJAX and PHP

Good day everyone, I'm trying to create a live progress status report for my automatic course scheduler. Currently, I tried using session variables to achieve my expected functionality but still no luck. Also, some progress report messages aren't displaying and the session variable doesn't seem to update. I would like to seek for help or advice to the stated problems.

For further information, I included below my code and expected and actual results. Thank you

Expected Result for Progress Status Reporting When The System is Automatically Scheduling Courses:

  • [Automatic Course Scheduler]: Initiating to schedule courses that have conflict...
  • [Automatic Course Scheduler]: All courses are successfully scheduled
  • [Automatic Course Scheduler]: Initiating to schedule courses that have no conflict...
  • [Automatic Course Scheduler]: All courses that have no conflict are now all scheduled.

Actual Result for Progress Status Reporting When The System is Automatically Scheduling Courses
Image of Actual Result

CODE:

Javascript (JQuery Lib)

$('.left-section').on('click', '#sp-auto-courses', function(){

            
            $.ajax({
                url: 'application_layer/automated_course_scheduler.php',
                type: 'post',
                success: function(response,status){

                }  

            });
            getProgress();
            function getProgress(){

                $.ajax({
                url: 'application_layer/schedulingLogRealTime.php',
                type: 'post',
                success: function(response,status){
                    if(response.length > 0){
                            
                                $('.schedulinglogs').append(response);
                                getProgress();
                        }else{
                                $("#sw-select-date").val("0").change();
                                    $('#table-responsive').empty();
                                    $('#table-responsive').load('application_layer/course-pagination.php');
                        }

                                

                    }  

                });



            }
        
    


    });

PHP

schedulingLogRealTime.php

<?php
session_start();

if (isset($_SESSION["acs_progress"]) && strlen($_SESSION["acs_progress"]) > 0) {
    sleep(1);
    echo $_SESSION["acs_progress"];
    unset($_SESSION["acs_progress"]);


}
?>

automated_course_scheduler.php

<?php 

require "dbcaller.php";

$timeslotpos = 1;
$inValues = array();
$scheduledTimeslot = array();
//$commaSeparatedInValues = "";
$coursePrioritization = false;
$processResult = array();
$selectWhereNoConflict = array();

$messageLog = array();
//SPREAD CONFLICT CONSTRAINTS DETECT
$spreadConflict = "true";
$selectAllCourseThatHaveConflict = $db->query("Select DISTINCT courses.Course_ID from courses INNER JOIN course_conflict ON course_conflict.Course_ID = courses.Course_ID WHERE CDataset_ID = 1 AND NOT EXISTS (SELECT Course_ID from examination_schedule WHERE course_conflict.Course_ID = examination_schedule.Course_ID)");

//CHECK If There Are Room Scheduled
$queryIfThereAreScheduledRooms = $db->query( "Select RoomID from examination_schedule Where ExamEvent_ID=1 AND RoomID !=0");

if($queryIfThereAreScheduledRooms->numRows() == 0){

    if($selectAllCourseThatHaveConflict->numRows() > 0){
        $coursesThatHaveConflict = $selectAllCourseThatHaveConflict->fetchAll();    
         storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]: <p style='color: #8B8000;display: inline'>Initiating to schedule courses that have conflict... </p></p></div>");
             
         
                foreach($coursesThatHaveConflict as $row){

                    //CHECK IF THERE ARE SCHEDULED
                    $get_dataIfThereAreScheduled = $db->query("Select TimeslotPos, ExamScheduleID, Timeslot_ID from examination_schedule WHERE ExamEvent_ID=1 AND Course_ID=0")->fetchArray();
                    foreach($get_dataIfThereAreScheduled as $data){

                            $scheduledTimeslot[] = $data;   
                    }

                            
                    //NEED TO KNOW THE COURSE CONFLICT OF THE COURSES
                    $queryConflict = $db->query("SELECT course_conflict.ConflictCourse FROM course_conflict INNER JOIN courses ON course_conflict.Course_ID = courses.Course_ID WHERE courses.CDataset_ID = 1 AND course_conflict.Course_ID= ?", $row['Course_ID'])->fetchAll();

                    foreach($queryConflict as $row2) {

                            $inValues[] = $row2['ConflictCourse'];
                        
                
                    }
            
                    if($spreadConflict == "false"){
                         $selectWhereNoConflict = $db->query("Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE ExamEvent_ID=1 AND NOT EXISTS (SELECT Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.Course_ID IN (".rtrim(str_repeat("?,", count($inValues)), ",").") AND examination_schedule.ExamEvent_ID=1) ORDER BY STR_TO_DATE (timeslot.Date, '%M %e, %Y'),  STR_TO_DATE (CONCAT(timeslot.Start_Time,' - ',timeslot.End_Time), '%l %p - %l %p') LIMIT 1;", $inValues);

                    }else if($spreadConflict=="true"){


                         $selectWhereNoConflict = $db->query("Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE ExamEvent_ID=1 AND NOT EXISTS (SELECT Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.Course_ID IN (".rtrim(str_repeat("?,", count($inValues)), ",").") AND examination_schedule.ExamEvent_ID=1) AND NOT EXISTS (Select examination_schedule.Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.ExamEvent_ID=1) ORDER BY STR_TO_DATE (CONCAT(timeslot.Start_Time,' - ',timeslot.End_Time), '%l %p - %l %p') LIMIT 1;",  $inValues);   

                        if($selectWhereNoConflict->numRows() == 0){
                            $selectWhereNoConflict = $db->query("Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE ExamEvent_ID=1 AND NOT EXISTS (SELECT Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.Course_ID IN (".rtrim(str_repeat("?,", count($inValues)), ",").") AND examination_schedule.ExamEvent_ID=1) ORDER BY STR_TO_DATE (CONCAT(timeslot.Start_Time,' - ',timeslot.End_Time), '%l %p - %l %p') LIMIT 1;", $inValues);  

                            storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]:  <p style='color: red;display: inline'>Not Enough Timeslot To Equally Spread Each Course: Finding Best Timeslot for ".$row['Course_ID']."</p></p></div>");      
                             

                    

                        }   
                    

                    }

                     if($selectWhereNoConflict->numRows() > 0){
                            $newTimeslotPos = 0;
                            $timeslotId = $selectWhereNoConflict->fetchArray();
                        
                            if(in_array($timeslotId['Timeslot_ID'], array_column($scheduledTimeslot, 'Timeslot_ID')) AND !empty($scheduledTimeslot)){
                                
                                    foreach($scheduledTimeslot as $data){

                                        $updateRowWithCourse = $db->query("UPDATE `examination_schedule` SET `Course_ID`= ? WHERE ExamScheduleID= ?", $row['Course_ID'], $data['ExamScheduleID']);
                                        break;
                                    
                                    }
                                    //$commaSeparatedInValues = "";         
                            }else{

                        
                                $selectCurrentTimeslotPos = $db->query("SELECT COALESCE(MAX(TimeslotPos), 0) as CurrentTimeslotPos FROM examination_schedule WHERE ExamEvent_ID=1 AND Timeslot_ID= ?", $timeslotId['Timeslot_ID']);

                                if($selectCurrentTimeslotPos->numRows() > 0){
                                    $currTimeslotPos = $selectCurrentTimeslotPos->fetchArray();
                                    $newTimeslotPos = $currTimeslotPos['CurrentTimeslotPos'] + 1;
                                }else{
                                    $newTimeslotPos = 1;
                                }

                                $insertCourse = $db->query("INSERT INTO `examination_schedule`(`Course_ID`, `Timeslot_ID`, `ExamEvent_ID`, `TimeslotPos`) VALUES (?,?,'1',?)", $row['Course_ID'], $timeslotId['Timeslot_ID'], $newTimeslotPos);
                                //      $commaSeparatedInValues = "";
                            }
                                
                     }else{
                         storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]: <p style='color: red;display: inline'>Cant Schedule Course ID: ".$row['Course_ID']." due to many conflicts</p></p></div>");
                         
                

                     }  

                    $inValues = array();
                    //$commaSeparatedInValues = "";
                    $coursePrioritization = true;
                    $scheduledTimeslot = array();
                    $selectWhereNoConflict = "";

            }

            
                storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]: <p style='color: green;display: inline'>All courses are successfully scheduled</p></p></div>");
                 
    
    }else{
            $coursePrioritization = true;
            
             storeMessageToSession("<div ><p style='display: inline'>[Auto Course Scheduler]:  <p style='color: green;display: inline'>All courses that have conflict are already scheduled.</p></p></div>");
             


    }
        

    if($coursePrioritization == true){
            
            $selectAllUnscheduledCourse = $db->query("Select DISTINCT courses.Course_ID from courses WHERE CDataset_ID = 1 AND NOT EXISTS (SELECT Course_ID from examination_schedule WHERE courses.Course_ID = examination_schedule.Course_ID)");

                if($selectAllUnscheduledCourse->numRows() == 0){

                     storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]: <p style='color: green;display: inline'>All courses that have no conflict are already scheduled.</p></p></div>");
                     


                }else{

                         storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]: <p style='color: #8B8000;display: inline'>Initiating to schedule courses that have no conflict... </p></p></div>");
                         
                        $unscheduledCourses = $selectAllUnscheduledCourse->fetchAll();
                        foreach($unscheduledCourses as $row){

                                //CHECK IF THERE ARE SCHEDULED (REFRESH)
                                $get_dataIfThereAreScheduled = $db->query("Select TimeslotPos, ExamScheduleID, Timeslot_ID from examination_schedule WHERE ExamEvent_ID=1 AND Course_ID=0")->fetchArray();
                                foreach($get_dataIfThereAreScheduled as $data){

                                        $scheduledTimeslot[] = $data;   
                                }

                                $newTimeslotPos = 0;
                                $timeslotId = array();


                                    
                                $timeslotId = $db->query("Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE timeslot.ExamEvent_ID=1 ORDER BY RAND() LIMIT 1;")->fetchArray();                    

                                if(in_array($timeslotId['Timeslot_ID'], array_column($scheduledTimeslot, 'Timeslot_ID')) AND !empty($scheduledTimeslot)){
                                            
                                    foreach($scheduledTimeslot as $data){

                                        $updateRowWithCourse = $db->query("UPDATE `examination_schedule` SET `Course_ID`='".$row['Course_ID']."' WHERE ExamScheduleID=?", $data['ExamScheduleID']);
                                        break;
                                
                                    }

                                                    
                                }else{

                                        $selectCurrentTimeslotPos = $db->query("SELECT COALESCE(MAX(TimeslotPos), 0) as CurrentTimeslotPos FROM examination_schedule WHERE ExamEvent_ID=1 AND Timeslot_ID= ?", $timeslotId['Timeslot_ID']);
                                        if($selectCurrentTimeslotPos->numRows() > 0){
                                            $currTimeslotPos = $selectCurrentTimeslotPos->fetchArray();
                                            $newTimeslotPos = $currTimeslotPos['CurrentTimeslotPos'] + 1;
                                        }else{
                                            $newTimeslotPos = 1;
                                        }

                                        $insertCourse = $db->query("INSERT INTO `examination_schedule`(`Course_ID`, `Timeslot_ID`, `ExamEvent_ID`, `TimeslotPos`) VALUES (?,?,'1',?)", $row['Course_ID'], $timeslotId['Timeslot_ID'], $newTimeslotPos);


                                }

                            $scheduledTimeslot = array();
                        }


    

                     storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]: <p style='color: green;display: inline'>All courses that have no conflict are now all scheduled. </p></p></div>");
                     
                            

                }
                    
    }

}else{

    storeMessageToSession("<div><p style='display: inline'>[Auto Course Scheduler]: <p style='color: red;display: inline'>The system can't initiate the automatic course scheduler due to some rooms are scheduled </p></p></div>");
    


}


function storeMessageToSession($message){


    session_start();
    $_SESSION["acs_progress"] = $message;
    session_write_close();

}

?>



Aucun commentaire:

Enregistrer un commentaire