AJAX disabledDates Fullcalendar Machines/Users

43 views Asked by At

i have multiple jquery fullcalendars on a page which is used for machine scheduling in our software. each calender will make a ajax call once a specific input for the workload changes. In my AJAX script i need to calculate the dates that will be disabled in the calendar based upon the already planned time of the machine and whether or not there are workers with enough time avail on that day.

so here is what i got so far:

$retval = Array();
$objectid = $_REQUEST["objectid"];
$type = $_REQUEST["type"];
$amount = $_REQUEST["amount"];
$date = $_REQUEST["date"];
$month = date("m",strtotime($date));
$year = date("Y",strtotime($date));
if ($month == date("m") && $year == date("Y"))
{
    $start = mktime(0,0,0,date("m"),date("d"),date("Y"));
} else {
    $mktime1 = mktime(0,0,0,$month,24,$year);
    $start = strtotime('-1 month',$mktime1);
}
$mktime2 = mktime(0,0,0,$month,6,$year);
$end = strtotime('+2 month',$mktime2);



$job_arr = Array();
$jobs = PlanningJob::getAllJobs(" AND start>".$start." AND end<".$end);
foreach ($jobs as $job)
{
    $date = mktime(0,0,0,date("m",$job->getDate()),date("d",$job->getDate()),date("Y",$job->getDate()));
    $job_arr[$date] = 0;
    if ($job->getTime()>0)
        $job_arr[$date] += $job->getTime();
    elseif ($job->getPlannedTime()>0)
        $job_arr[$date] += $job->getPlannedTime();
}

if ($type == "ME"){
    $me = new Machineentry($objectid);
    $machine = $me->getMachine();
    for ( $i = $start; $i <= $end; $i = $i + 86400 ) {
        $date = mktime(0,0,0,date("m",$i),date("d",$i),date("Y",$i));
        $total_seconds = $machine->getRunningtimeForDay($date);
        if (isset($job_arr[$date]))
        {
            $total_seconds -= ($job_arr[$date]*60*60);
        }
        if ($total_seconds < ($amount*60*60))
            $retval[] = date("d.m.Y",$date);
    }
} else if ($type == "OP")
{
    $op = new Orderposition($objectid);
    $jobart = new Article($op->getObjectid());
}

now i wonder if there is a better approach to calculating for so many dates (date range) at once - performance wise. as this one is quite heavy and takes a couple of seconds which really slows down our scheduling process.

i would appreciate any help/advice!

Kind Regards,

Alex

[EDIT] some more info's on what i need to output:

One Array containing: all dates within the range
and within this array per date:
1. all users having spare time on that day and the specific amount of time
2. total time scheduled on the machine that day

Second Array
Per date: if not enough 'free' time on the machine only the date so it can be disabled in the calendar

1

There are 1 answers

1
skrilled On

Here is a function I wrote a while back which will take two dates and return an array of the range between them. I use this function on a variety of things and it's very fast.

Dates provided for $dateFrom and $dateTo should be in YYYY-MM-DD format.

function createDateRangeArray($dateFrom,$dateTo)
{
    $arrayRange=array();

    $dateFrom=mktime(1,0,0,substr($dateFrom,5,2), substr($dateFrom,8,2),substr($dateFrom,0,4));
    $dateTo=mktime(1,0,0,substr($dateTo,5,2), substr($dateTo,8,2),substr($dateTo,0,4));

    if ($dateTo>=$dateFrom)
    {
        array_push($arrayRange,date('Y-m-d',$dateFrom));
        while ($dateFrom<$dateTo)
        {
            $dateFrom+=86400;
            array_push($arrayRange,date('Y-m-d',$dateFrom));
        }
    }
    return $arrayRange;
}