<?php    
    include '../database.php';
                        
    function clockOutAllClockedIn($db, $userRow, $inClockOutAllTime){
        // Init variables
        $returnMessage = '';
        $clockOutAllTime = $inClockOutAllTime;
        
        try {
            // Determine if anything is passed in
            if ($clockOutAllTime==null || $clockOutAllTime = ""){
                // Set it to the current clock in time
                $now = new Datetime("now");
                $clockOutAllTime=$now->modify('-1 hours');
                $clockOutAllTimeString = $clockOutAllTime->format('Y-m-d H:i:s');
                
                // Set the return message to let them know we set it as the currewnt time
                $returnMessage="There was no time set. All employees have been set to clock out as of '" . $clockOutAllTimeString . "'";
            }
            else {
                // Set the return message to let them know we set it as the currewnt time
                $returnMessage="All employees have been set to clock out as of '" . $inClockOutAllTime . "' successfully.";
                
                // Set the clock out time to what was passed in
                $clockOutAllTimeString=$inClockOutAllTime;
            }
            
            // create an sql to clock them out as clocked out
            $clockedStateSQL = ', CurrentLaborCategoryID=(SELECT LaborCategoryID FROM laborcategories WHERE Name="Clocked Out") ';

            // Let's create the update query off of what we have
            $sql='UPDATE laborclock SET ';
            $sql = $sql . 'EndLocation="Auto Clock Out"';
            $sql = $sql . ', EndTime="' . $clockOutAllTimeString . '"';
            $sql = $sql . $clockedStateSQL;
            $sql = $sql . ', CurrentLaborCategoryStartTime=CURRENT_TIMESTAMP '; 
            $sql=$sql .' WHERE EndTime IS NULL AND' .
                       ' UserID NOT IN (SELECT UserID FROM users WHERE UserLevel="SU");';
            
            // Attempt to run the query and evaluate the response
            if (!$db->queryDB($sql, $userRow, "clock_out_all Form occurance 1")){
                // Let them know that the query was not successful
                $returnMessage = "The database was NOT updated.  The query failed.  Please let the system admin know.";
            }
        } 
        catch (Exception $ex) {
            // Let them know that the query was not successful
            $returnMessage = "The database was NOT updated.  The query failed.  Please let the system admin know.";
        }

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

    $sessionID=-1;
    $clockOutAllTime="";

    // check the passed in session ID
    if (isset($_GET['sessionID']) ){
        // get the passed in session ID
        $sessionID = $_GET['sessionID'];
    }

    // Attempt to log in with this user
    $currentUser = $db->getLoggedInSessionUser($sessionID);
    
    // Set the return to nothing as of now
    $return="";

    // If there is only one result for a username/password combo, then they are good to go.
    if ($currentUser != null){
        // check the passed in session ID
        if (isset($_GET['clockAllOutTime']) ){
            // get the passed in time to clock all out at
            $clockOutAllTime = $_GET['clockAllOutTime'];
        }

        // Attempt to get the user row
        $userRow = $currentUser->fetch_assoc();

        // Go and get the machines and all of their tasks
        $return = clockOutAllClockedIn($db, $userRow, $clockOutAllTime);
    }

    echo $return;
?>   
