我编写了以下逻辑来识别可用插槽,以便在我使用yii框架开发的系统中启动会议请求。
// checks for availabilty of teacher
public function isTeacherAvailable($subjectId, $startAt, $endAt) {
$success = true;
$meetings = Meeting::model()->findAll("subjectId=:subjectId AND endAt>=:endAt AND type=:type", array(':subjectId' => $subjectId, ':endAt' => date('Y-m-d H:i:s'), ':type' => '0')); // get all the meetings for subject that is not ended and active.
foreach ($meetings as $meeting) {
if (($startAt >= $meeting->startAt) && ($startAt <= $meeting->endAt)) {
$success = false; // trying to create inside a existing slot.
break;
} else if (($endAt >= $meeting->startAt) && ($endAt <= $meeting->endAt)) {
$success = false; // trying to create inside a existing slot.
break;
}
}
return $success;
}
这是输入的示例。 StartAt:2014-02-10 08:45:00 endAt:2014-02-10 08:49:00 ..你可以跳过subjectId
有了这个我能够阻止在已经为给定的StartAt和endAt安排会议时创建会议请求。
我遇到的一个问题是,当我插入这样的东西时它会失败。
StartAt:2014-02-10 08:40:00 endAt:2014-02-10 08:55:00 (新请求在现有广告位之外)
所以我们的想法是在会议安排从上午8点到上午9点。它不应该允许从早上7点到10点创建一个会议,就像它停在上午7:30到8.30 上午和上午8:30到9:00 。
让我知道我做错了什么。
答案 0 :(得分:0)
我认为错误是在两个条件下进行的唯一评估:
if CONDITION1 then FALSE else if CONDITION2 then FALSE
这两个条件应在OR中测试:
if CONDITION1 or CONDITION2 then FALSE
答案 1 :(得分:0)
试试这个
// checks for availabilty of teacher
public function isTeacherAvailable($subjectId, $startAt, $endAt) {
$success = true;
$meetings = Meeting::model()->findAll("subjectId=:subjectId AND endAt>=:endAt AND type=:type", array(':subjectId' => $subjectId, ':endAt' => date('Y-m-d H:i:s'), ':type' => '0')); // get all the meetings for subject that is not ended and active.
foreach ($meetings as $meeting) {
if (($startAt >= $meeting->startAt) && ($startAt <= $meeting->endAt)) {
$success = false; // trying to create inside a existing slot.
break;
} else if (($endAt >= $meeting->startAt) && ($endAt <= $meeting->endAt)) {
$success = false; // trying to create inside a existing slot.
break;
} else if (($startAt <= $meeting->startAt && $endAt >= $meeting->startAt) || ($startAt >= $meeting->startAt && $startAt <= $meeting->endAt)) {
$success = false; // trying to create inside a existing slot.
break;
}
}
return $success;
}
答案 2 :(得分:0)
我按照以下方式编写了函数来实现解决方案。
// checks for availabilty of teacher
public function isTeacherAvailable($subjectId, $startAt, $endAt) {
$success = true;
$meetings = Meeting::model()->findAll("subjectId=:subjectId AND endAt>=:endAt AND type=:type", array(':subjectId' => $subjectId, ':endAt' => date('Y-m-d H:i:s'), ':type' => '0')); // get all the meetings for subject that is not ended and active.
foreach ($meetings as $meeting) {
if (($startAt >= $meeting->startAt) && ($startAt <= $meeting->endAt)) {
$success = false; // trying to create inside a existing slot.
break;
} else if (($endAt >= $meeting->startAt) && ($endAt <= $meeting->endAt)) {
$success = false; // trying to create inside a existing slot.
break;
} else if (date('Y-m-d', strtotime($startAt)) == date('Y-m-d', strtotime($meeting->startAt)) && date('Y-m-d', strtotime($endAt)) == date('Y-m-d', strtotime($meeting->endAt))) { //if same date
if ($startAt <= $meeting->startAt && $endAt >= $meeting->endAt) {
$success = false; // trying to create outside a existing slot when there is one in middle on same day.
break;
}
}
}
return $success;
}