ajax status php,jquery - Live progress status reporting using AJAX and PHP - Stack Overflow

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

gwoQC.png

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

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

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("

[Auto Course Scheduler]:

Initiating to schedule courses that have conflict...

");

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("

[Auto Course Scheduler]:

Not Enough Timeslot To Equally Spread Each Course: Finding Best Timeslot for ".$row['Course_ID']."

");

}

}

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("

[Auto Course Scheduler]:

Cant Schedule Course ID: ".$row['Course_ID']." due to many conflicts

");

}

$inValues = array();

//$commaSeparatedInValues = "";

$coursePrioritization = true;

$scheduledTimeslot = array();

$selectWhereNoConflict = "";

}

storeMessageToSession("

[Auto Course Scheduler]:

All courses are successfully scheduled

");

}else{

$coursePrioritization = true;

storeMessageToSession("

[Auto Course Scheduler]:

All courses that have conflict are already scheduled.

");

}

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("

[Auto Course Scheduler]:

All courses that have no conflict are already scheduled.

");

}else{

storeMessageToSession("

[Auto Course Scheduler]:

Initiating to schedule courses that have no conflict...

");

$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("

[Auto Course Scheduler]:

All courses that have no conflict are now all scheduled.

");

}

}

}else{

storeMessageToSession("

[Auto Course Scheduler]:

The system can't initiate the automatic course scheduler due to some rooms are scheduled

");

}

function storeMessageToSession($message){

session_start();

$_SESSION["acs_progress"] = $message;

session_write_close();

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值