diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php index 6f70dfb622aa..a26ed66e8536 100644 --- a/app/Http/Requests/Request.php +++ b/app/Http/Requests/Request.php @@ -195,4 +195,56 @@ class Request extends FormRequest public function prepareForValidation() { } + + public function checkTimeLog(array $log): bool + { + if(count($log) == 0) + return true; + + /*Get first value of all arrays*/ + $result = array_column($log, 0); + + /*Sort the array in ascending order*/ + asort($result); + + $new_array = []; + + /*Rebuild the array in order*/ + foreach($result as $key => $value) + $new_array[] = $log[$key]; + + /*Iterate through the array and perform checks*/ + foreach($new_array as $key => $array) + { + /*Flag which helps us know if there is a NEXT timelog*/ + $next = false; + /* If there are more than 1 time log in the array, ensure the last timestamp is not zero*/ + if(count($new_array) >1 && $array[1] == 0) + return false; + + /* Check if the start time is greater than the end time */ + /* Ignore the last value for now, we'll do a separate check for this */ + if($array[0] > $array[1] && $array[1] != 0) + return false; + + /* Find the next time log value - if it exists */ + if(array_key_exists($key+1, $new_array)) + $next = $new_array[$key+1]; + + /* check the next time log and ensure the start time is GREATER than the end time of the previous record */ + if($next && $next[0] < $array[1]) + return false; + + /* Get the last row of the timelog*/ + $last_row = end($new_array); + + /*If the last value is NOT zero, ensure start time is not GREATER than the endtime */ + if($last_row[1] != 0 && $last_row[0] > $last_row[1]) + return false; + + return true; + } + + } + } diff --git a/app/Http/Requests/Task/StoreTaskRequest.php b/app/Http/Requests/Task/StoreTaskRequest.php index 3b4b4171cfc6..0d0247902050 100644 --- a/app/Http/Requests/Task/StoreTaskRequest.php +++ b/app/Http/Requests/Task/StoreTaskRequest.php @@ -53,6 +53,9 @@ class StoreTaskRequest extends Request $fail('The '.$attribute.' - '.print_r($k,1).' is invalid. Unix timestamps only.'); } + if(!$this->checkTimeLog($values)) + $fail('Please correct overlapping values'); + }]; diff --git a/app/Http/Requests/Task/UpdateTaskRequest.php b/app/Http/Requests/Task/UpdateTaskRequest.php index 5a15cb7fd730..501861c48ed9 100644 --- a/app/Http/Requests/Task/UpdateTaskRequest.php +++ b/app/Http/Requests/Task/UpdateTaskRequest.php @@ -59,6 +59,9 @@ class UpdateTaskRequest extends Request $fail('The '.$attribute.' - '.print_r($k,1).' is invalid. Unix timestamps only.'); } + if(!$this->checkTimeLog($values)) + $fail('Please correct overlapping values'); + }]; return $this->globalRules($rules); diff --git a/tests/Feature/TaskApiTest.php b/tests/Feature/TaskApiTest.php index 80e1827f5e92..80c2b3e77097 100644 --- a/tests/Feature/TaskApiTest.php +++ b/tests/Feature/TaskApiTest.php @@ -55,29 +55,25 @@ class TaskApiTest extends TestCase foreach($result as $key => $value) $new_array[] = $log[$key]; - foreach($new_array as $key => $array) { $next = false; if(count($new_array) >1 && $array[1] == 0) return false; - + /* First test is to check if the start time is greater than the end time */ /* Ignore the last value for now, we'll do a separate check for this */ if($array[0] > $array[1] && $array[1] != 0){ - nlog("1"); return false; } if(array_key_exists($key+1, $new_array)){ - nlog("2"); $next = $new_array[$key+1]; } /* check the next time log and ensure the start time is GREATER than the end time of the previous record */ if($next && $next[0] < $array[1]){ - nlog("3"); return false; } @@ -85,14 +81,12 @@ class TaskApiTest extends TestCase if($last_row[1] != 0 && $last_row[0] > $last_row[1]){ nlog($last_row[0]. " ".$last_row[1]); - nlog("4"); return false; } return true; } - } public function testTimeLogChecker1() @@ -186,7 +180,7 @@ class TaskApiTest extends TestCase } - public function testTimeLogChecker8() + public function testTimeLogChecker8() { $log = [ @@ -198,6 +192,45 @@ class TaskApiTest extends TestCase } + public function testTimeLogChecker9() + { + + $log = [ + [4,5,'bb'], + [50,0,'aa'], + ]; + + $this->assertTrue($this->checkTimeLog($log)); + + } + + + + public function testTimeLogChecker10() + { + + $log = [ + [4,5,'5'], + [50,0,'3'], + ]; + + $this->assertTrue($this->checkTimeLog($log)); + + } + + + public function testTimeLogChecker11() + { + + $log = [ + [1,2,'a'], + [3,4,'d'], + ]; + + $this->assertTrue($this->checkTimeLog($log)); + + } + public function testTaskListClientStatus() {