<?php    
    include '../database.php';

    function checkWonRace($db, $userRow, $inTaskID, $timeStartedUpdate){
        // Set to winning the race until proven otherwise
        $result = "";
        
        try {
            // Check to see if the passed in variable is a number
            if (is_numeric($inTaskID) && $inTaskID>0){
                // Set and initialize variables
                $taskID = $inTaskID;
                
                
                // Create a query to see if they won
                $sql = 'SELECT IF(TIMEDIFF(LastUpdated, "' . $timeStartedUpdate . '") < 0,1,0) AS WonRace ' . 
                       'FROM tasks WHERE TaskID="' . $taskID . '";';
                
                // run the query
                $hasWonQuery = $db->queryDB($sql, $userRow, "check_race_condition Form occurance 1");

                // Determine if we have a row.
                $hasWonAssocRow=null;
                if (mysqli_num_rows($hasWonQuery)>0){
                    // Get the Row returned
                    $hasWonAssocRow = $hasWonQuery->fetch_assoc();
                    
                    // Determine if we have a row.
                    if ($hasWonAssocRow["WonRace"]==0){
                        // Let them know that we lost the race
                        $result='ABORT RACE CHECK (check_race_condition)::This task has been updated since you opened it. Will not update now. Aborting...';
                    }
                    else {
                        // All I need to do now is set the result to nothing
                        $result="";
                    }
                }
            }
            else {
                // Return an error code
                $result='ABORT RACE CHECK (check_race_condition)::Error determine who won the race. No Task ID.  Aborting.';
            }
        } catch (Exception $ex) {
            // Set up an error message for the exception
            $result='ABORT RACE CHECK (check_race_condition)::There was ' . 
                    'an error checking for race condition.\n\n The error is: ' . 
                    $ex->getMessage();
        }

        // Return whatever we got
        return $result;
    }
    
    $db = new Database();
    $db->setupTmsDB();

    // Set defaults for the passed in variables
    $taskID=-1;
    $timeStartedUpdate=date('m/d/Y h:i:s a', time());
    
    // check the passed in task ID
    if (isset($_GET['taskID']) ){
        // get the passed in task ID
        $taskID = $_GET['taskID'];
    }

    // check the passed in time they started their update
    if (isset($_GET['updateStartTime']) ){
        // get the passed in time they started their update
        $timeStartedUpdate = $_GET['updateStartTime'];
    }

    // Set the user row to nothing because we are not getting the current user
    $userRow = null;

    // Determine if they actually won the race
    $return = checkWonRace($db, $userRow, $taskID, $timeStartedUpdate);
    
    echo $return;
?>   


