PHP、いくつかの JavaScript、および SQL Server 2008 を使用してスケジューリング プロジェクトに取り組んでいます。ユーザーはジョブ フォームを使用して、スケジュールされたジョブを更新できます。変更できるフィールドは、ジョブ番号、開始時刻、終了時刻、説明、およびジョブのステータスです。SQL は、更新前にこれらすべてをテストする必要があります。
- 私が抱えている問題のいくつかは次のとおりです。
- ユーザーが終了時刻を (後の時刻に) 増やした場合、スケジュールのオーバーラップが可能になります。
- ユーザーが開始時刻を戻す (以前の時刻に) 場合、オーバーラップが許可されます。
- ユーザーが過去の予定日を選択すると、将来予定されている項目との予定の競合が発生します。
私はこれに2日間立ち往生しており、これらの問題を解決するために複数の方向に進んでいます. これは私が現在持っているものです:
<?php
include_once("../php/functions.php");
$conn = connect();
$params = array
(
$jobNum = $_POST['jobNum'],
$asset = $_POST['drp_asset'],
$jobStatus = $_POST['drp_status'],
$sDate = $_POST['startTime'],
$eDate = $_POST['endTime'],
$department = $_POST['drp_department'],
$descrip = $_POST['txtarea_description'],
$job = $_POST['jobid'],
$asset2 = $_POST['drp_asset']
);
/********************************************
Check to see if the delete button was pressed
And if the pre-check warning was confirmed
delete the record.
********************************************/
if (isset($_POST['updateDelete']))
{
$tsql = "Delete from Calendar_view
where JobID = '$job'";
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt)
{
checkRows($stmt);
returnTo($conn);
}
else
{
errMsg();
}
}
/****************************************
If the times and/or asset have not changed
update the job status
****************************************/
else
{
$tsql_check ="select StartTime, EndTime, Asset
from Calendar_View
where '$sDate' = StartTime and '$eDate' = EndTime and '$asset' = Asset";
$stmt = sqlsrv_query($conn, $tsql_check);
$rows = sqlsrv_has_rows($stmt);
if($stmt)
{
if ($rows === true)
{
updateJobStatus($conn, $params);
}
else
{
timeChanges($conn, $params);
}
}
}
function checkOverlaps($conn, $params)
{
$sDate = $params[3];
//$sDate = new DateTime($params[4]);
//$sDate = date_format($sDate,'Y-m-d G:i:s');
$eDate = $params[4];
//$eDate = new DateTime($params[5]);
//$eDate = date_format($eDate,'Y-m-d G:i:s');
$asset = $params[1];
$tsql_check ="select StartTime, EndTime
from Calendar_View
where (('$sDate' < EndTime and '$sDate' >= StartTime) or ('$eDate' < EndTime and '$eDate' > StartTime)) and '$asset' = Asset";
$stmt = sqlsrv_query($conn, $tsql_check);
if ($stmt)
{
// If there is a match, there will be an overlap
$rows = sqlsrv_has_rows($stmt);
if ($rows === true)
{
checkRows($stmt);
}
//If there is no match then job is being moved
//to an open spot
else
{
updateJob($conn, $params);
}
}
}
/************************************
If the start time or end time have changed
/***********************************/
function timeChanges($conn, $params)
{
$sDate = $params[3];
$eDate = $params[4];
$asset = $params[1];
$tsql_timeCheck ="select StartTime, EndTime
from Calendar_View
where (('$sDate' <= StartTime) or ('$eDate' <= EndTime)) and '$asset' = Asset";
$stmt2 = sqlsrv_query($conn, $tsql_timeCheck);
if ($stmt2 == true)
{
$rows = sqlsrv_has_rows($stmt2);
if ($rows === true)
{
updateJobStatus($conn, $params);
//updateJobStatus($conn, $params);
}
else
{
checkOverlaps($conn, $params);
}
}
}
function updateJob($conn, $params)
{
$tsql = "UPDATE Calendar_View
SET JobNum = ?,
Asset = ?,
JobStatus =?,
StartTime =?,
EndTime =?,
Department = ?,
Description = ?
WHERE JobID = ?";
$stmt = sqlsrv_query($conn, $tsql, $params);
if ($stmt)
{
checkRows($stmt);
returnTo($conn);
}
else
{
errMsg();
}
}
/***************************************
Update job status
****************************************/
function updateJobStatus($conn, $params)
{
$tsql = "UPDATE Calendar_View
SET JobNum = ?,
Asset = ?,
JobStatus =?,
StartTime =?,
EndTime =?,
Department = ?,
Description = ?
WHERE JobID = ? and Asset = ?";
$stmt = sqlsrv_query($conn, $tsql, $params);
if ($stmt)
{
checkRows($stmt);
returnTo($conn);
}
else
{
errMsg();
}
}
/**********************************
Return user to scheduling page they
were working on.
***********************************/
function returnTo($conn)
{
sqlsrv_close($conn);
echo "<meta http-equiv='refresh' content='1;URL=../pages/schedule2.php' />";
}
function errMsg()
{
die( print_r(sqlsrv_errors(), true));
exit;
}
/*************************************
Check if any rows were altered by the
query. If so alert success, else alert
that there was a conflict (nothing done)
**************************************/
function checkRows($stmt)
{
$rows_affected = sqlsrv_rows_affected($stmt);
if ($rows_affected > 0)
{
echo "Job updated successfully!<br>";
}
else
echo "There was a scheduling conflict.";
echo "<meta http-equiv='refresh' content='2;URL=../pages/schedule2.php' />";
}
?>
どんな助けでも大歓迎です!