Summary:
I have an app with a slew of checkboxes on the page. The user selects whatever checkboxes they want, and hits submit.
I need to do some conflict checking.
My current state of the code is working great.. however I discovered a flaw in the logic it seems, that allows for certain 'selections' to pass through without being noted as a conflict. (but I'm not clear as to WHY they are slipping past, nor how to fix it)
Technical Overview:
I take an array of the submitted controls/elements, and loop through each one doing the following:
1.) checkStartFirst() = See what one starts first and arrange the order of the two 'times' as arguments sent to another function (isConflict())
2.) isConflict() = takes the arguments passed to it..and checks to see if the argumentX start time is greater than argumentY's end time. I then push these values into another array to be used later one.
3.) After the above loop is complete.. I have another function that clears out any dups in the array and passes it back where I need it to be (to be used in front end for highlighting said conflicts)
debugging output for 1 of the questionable selections that slips past and doesnt get marked as a conflict, (where other same date/time selection DO get flagged as a conflict)
Sending: hours_9_7_reg_session_101_945_1005 / hours_9_7_reg_session_102_945_1005
Received for checking: hours_9_7_reg_session_101_945_1005 / hours_9_7_reg_session_102_945_1005
B: CHECKING: hours_9_7_reg_session_102_945_1005 [end: 09/07/2015 10:05] > hours_9_7_reg_session_101_945_1005 [start: 09/07/2015 9:45]
Since it is in a loop, it gets checked again (other way)
Sending: hours_9_7_reg_session_102_925_945 / hours_9_7_reg_session_101_945_1005
Received for checking: hours_9_7_reg_session_102_925_945 / hours_9_7_reg_session_101_945_1005
A: CHECKING: hours_9_7_reg_session_102_925_945 [end: 09/07/2015 9:45] > hours_9_7_reg_session_101_945_1005 [start: 09/07/2015 9:45]
(again, slips past)
My PHP functions.. starts with calling setConflicts()
//new (proposed) session conflicts checker
function checkStartFirst($cf_presX, $cf_presY) {
//$cf_presX['fullStart'] < $cf_presY['fullStart'] ? $this->isConflict($cf_presX, $cf_presY) : $this->isConflict($cf_presY, $cf_presX);
echo 'Received for checking: '. $cf_presX['id'] . ' / ' . $cf_presY['id'] .'<br>';
if($cf_presX['fullStart'] < $cf_presY['fullStart']){
echo 'A: ';
$this->isConflict($cf_presX, $cf_presY);
}else{
echo 'B: ';
$this->isConflict($cf_presY, $cf_presX);
}
}
function isConflict ($cc_presX, $cc_presY) {
echo 'CHECKING: ' . $cc_presX['id'] .' [end: ' . $cc_presX['fullEnd'] . '] > ' . $cc_presY['id'] .' [start: ' . $cc_presY['fullStart'] . ']';
if ($cc_presX['fullEnd'] > $cc_presY['fullStart']) {
echo ' -- has conflict <br>';
array_push($this->conflict_output, $cc_presX['id']);
array_push($this->conflict_output, $cc_presY['id']);
//echo 'Found Conflict: ' . $cc_presX['id'] . ' / ' . $cc_presY['id'] . '<br>';
}else{
//only here for debugging readability
echo '<br>';
}
}
function setConflicts() {
$presentations = $this->conflict_list;
for ($i = 0; $i < count($presentations); $i++) {
for ($j = 0; $j < count($presentations); $j++) {
// if it is not the same event
if ($presentations[$i]['id'] != $presentations[$j]['id']) {
echo '<br><br>Sending: '.($presentations[$i]['id'] .' / '. $presentations[$j]['id']) .'<br>';
$this->checkStartFirst($presentations[$i], $presentations[$j]);
}else{
echo '<br><br><br>same session, do not check: ' . ($presentations[$i]['id'] .' / '. $presentations[$j]['id']) . '<br><br><br>';
}
}
}
$this->getConflicts();
}
function getConflicts(){
$presentations = $this->conflict_output;
//remove duplicates in array & re-key (sequentially)
$uniquePresentations = array_unique($presentations);
//re-key array using sequential index #'s
$uniquePresentations = array_values($uniquePresentations);
if(count($uniquePresentations) > 0){
//save conflict (names) to array
$this->conflict_return = $uniquePresentations;
$this->errmsg = 'Please review the form for errors, there are conflicts in the highlighted sessions below. (Possible duplicate or overlapping session times have been selected.) <br>You can not proceed until all conflicts are resolved.';
}
}
How can the code/time selections in the blockquote above be slipping past? Where as others -do- get flagged?
I'm not sure if I need to tighten up some conditionals or something?
Well the solution (error) was easier than I expected.
Basically anywhere I am checking the date/time stamps.. I need to make sure I am casting/converting them to a strtotme() value ... as pulling the value out of the object/array it is treated as a string.
Why every other time conflict checking worked without issue.. I dont know. LOL
But doing this fixed my problems: